Skip to content

Commit

Permalink
[CoW Protocol] Base changes dependent on #7295
Browse files Browse the repository at this point in the history
  • Loading branch information
fleupold committed Dec 12, 2024
1 parent 52fe097 commit efcf2fb
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{{ config(
schema = 'cow_protocol_base',
alias = 'batches',
materialized='incremental',
partition_by = ['block_date'],
unique_key = ['tx_hash'],
on_schema_change='sync_all_columns',
file_format ='delta',
incremental_strategy='merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
post_hook='{{ expose_spells(blockchains = \'["base"]\',
spell_type = "project",
spell_name = "cow_protocol",
contributors = \'["felix"]\') }}'
)
}}

WITH
batch_counts as (
select try_cast(date_trunc('day', s.evt_block_time) as date) as block_date,
s.evt_block_number,
s.evt_block_time,
s.evt_tx_hash,
solver,
name,
sum(
case
when selector != 0x2e1a7d4d -- unwrap
and selector != 0x095ea7b3 -- approval
then 1
else 0
end) as dex_swaps,
sum(case when selector = 0x2e1a7d4d then 1 else 0 end) as unwraps,
sum(case when selector = 0x095ea7b3 then 1 else 0 end) as token_approvals
from {{ source('gnosis_protocol_v2_base', 'GPv2Settlement_evt_Settlement') }} s
left outer join {{ source('gnosis_protocol_v2_base', 'GPv2Settlement_evt_Interaction') }} i
on i.evt_tx_hash = s.evt_tx_hash
{% if is_incremental() %}
AND {{ incremental_predicate('i.evt_block_time') }}
{% endif %}
join {{ ref('cow_protocol_base_solvers') }}
on solver = address
{% if is_incremental() %}
WHERE {{ incremental_predicate('s.evt_block_time') }}
{% endif %}
group by s.evt_block_number, s.evt_block_time, s.evt_tx_hash, solver, name
),

batch_values as (
select
tx_hash,
count(*) as num_trades,
sum(usd_value) as batch_value,
sum(fee_usd) as fee_value,
price as eth_price
from {{ source('cow_protocol_base', 'trades') }}
left outer join {{ source('prices', 'usd') }} as p
on p.contract_address = 0x4200000000000000000000000000000000000006
{% if is_incremental() %}
and {{ incremental_predicate('minute') }}
{% endif %}
and p.minute = date_trunc('minute', block_time)
and blockchain = 'base'
{% if is_incremental() %}
WHERE {{ incremental_predicate('block_time') }}
{% endif %}
group by tx_hash, price
),

combined_batch_info as (
select
b.block_date,
evt_block_number as block_number,
evt_block_time as block_time,
num_trades,
dex_swaps,
batch_value,
solver as solver_address,
evt_tx_hash as tx_hash,
gas_price,
gas_used,
((gas_price / pow(10, 9)) * gas_used * eth_price) / pow(10, 9) as tx_cost_usd,
fee_value,
2 * bytearray_length(data) / 1024 as call_data_size,
unwraps,
token_approvals
from batch_counts b
join batch_values t
on b.evt_tx_hash = t.tx_hash
inner join {{ source('base', 'transactions') }} tx
on evt_tx_hash = hash
{% if is_incremental() %}
AND {{ incremental_predicate('block_time') }}
{% endif %}
where num_trades > 0 --! Exclude Withdraw Batches
)

select * from combined_batch_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2

models:
- name: cow_protocol_base_solvers
meta:
blockchain: base
project: cow_protocol
contributors: felix
config:
tags: ["base", "cow_protocol", "solver"]
description: >
CoW Protocol solvers list on Base Chain
- name: cow_protocol_base_batches
meta:
blockchain: base
project: cow_protocol
contributors: felix
config:
tags: ["base", "cow_protocol", "trades", "dex", "aggregator", "auction"]
description: >
CoW Protocol enriched batches table on Base Chain
data_tests:
- unique:
column_name: tx_hash
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{{ config(
schema = 'cow_protocol_base',
alias='solvers',
post_hook='{{ expose_spells(blockchains = \'["base"]\',
spell_type = "project",
spell_name = "cow_protocol",
contributors = \'["felix"]\') }}'
)}}

WITH
-- Aggregate the solver added and removed events into a single table
-- with true/false for adds/removes respectively
solver_activation_events as (
select solver, evt_block_number, evt_index, True as activated
from {{ source('gnosis_protocol_v2_base', 'GPv2AllowListAuthentication_evt_SolverAdded') }}
union
select solver, evt_block_number, evt_index, False as activated
from {{ source('gnosis_protocol_v2_base', 'GPv2AllowListAuthentication_evt_SolverRemoved') }}
),
-- Sorting by (evt_block_number, evt_index) allows us to pick the most recent activation status of each unique solver
ranked_solver_events as (
select
rank() over (partition by solver order by evt_block_number desc, evt_index desc) as rk,
solver,
evt_block_number,
evt_index,
activated as active
from solver_activation_events
),
registered_solvers as (
select solver, active
from ranked_solver_events
where rk = 1
),
-- Manually inserting environment and name for each "known" solver
known_solver_metadata (address, environment, name) as (
select *
from (
VALUES
(0x8d98057b8c3d6c7cB02f1C1BE7E37D416F2D3e96, 'barn', 'Baseline'),
(0x351DfD19DfA0e3BfD3E7D9B22658C09e66Fd14AD, 'barn', 'Seasolver'),
(0xcE55eD17ddD7ED6E7eC396eaC772959A6D7252EA, 'barn', 'Naive'),
(0x8982b03D56de1F6f173d04A940B20A69F6A59239, 'barn', 'Gnosis_1inch'),
(0x5951400dE8fA58DA26Ab9402D2603ec0bD788273, 'barn', 'Gnosis_ParaSwap'),
(0x147d05987f3008A6C9Ec3E93A4ead430907ac3E1, 'barn', 'Gnosis_0x'),
(0x9451D27C993f7a61096BFC33e0241644a7566F66, 'barn', 'Gnosis_BalancerSOR'),
(0x0AC9287C83C2386A6a0bb27F847Ce59a0034183C, 'barn', 'Laita'),
(0x69d7F96dFD091652f317D0734A5F2B492ACcbE07, 'prod', 'Baseline'),
(0x4cb862E4821fea2dabBD1f0A69c17d52da2A58f6, 'prod', 'Seasolver'),
(0xF401ceF222F1CA2fE84a8C7BFC75A636A4542A74, 'prod', 'Naive'),
(0x8F7f754300B1ccfa37eA25fD48FB059af0F19e12, 'prod', 'Gnosis_1inch'),
(0xe321609c56aD89711EfB69c248ebe94922902F81, 'prod', 'Gnosis_ParaSwap'),
(0xbBcCE072fb1Bd2C096667E257322f47693D3dc96, 'prod', 'Gnosis_0x'),
(0x983aC485620E265730e367B2C7BCBf6Eb9d62A21, 'prod', 'Gnosis_BalancerSOR'),
(0x1A422923290fd16C2ED00ED16B4203cF4bb35d82, 'prod', 'Laita')
) as _
)
-- Combining the metadata with current activation status for final table
select solver as address,
case when environment is not null then environment else 'new' end as environment,
case when name is not null then name else 'Uncatalogued' end as name,
active
from registered_solvers
left outer join known_solver_metadata on solver = address
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,26 @@ FROM
unwraps,
token_approvals
FROM {{ ref('cow_protocol_arbitrum_batches') }}

UNION ALL

SELECT
'base' AS blockchain,
'cow_protocol' AS project,
'1' AS version,
block_date,
block_time,
num_trades,
dex_swaps,
batch_value,
solver_address,
tx_hash,
gas_price,
gas_used,
tx_cost_usd,
fee_value,
call_data_size,
unwraps,
token_approvals
FROM {{ ref('cow_protocol_base_batches') }}
)

0 comments on commit efcf2fb

Please sign in to comment.