Skip to content

Commit 686b99a

Browse files
Add Odos Avalanche aggregator trades
1 parent e3a7dc9 commit 686b99a

5 files changed

Lines changed: 200 additions & 131 deletions

File tree

dbt_subprojects/dex/models/_projects/odos/avalanche_c/_schema.yml

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
version: 2
22

33
models:
4-
- name: odos_avalanche_c_trades
4+
- name: odos_v2_avalanche_c_trades
55
meta:
66
blockchain: avalanche_c
77
sector: dex
88
project: odos
99
contributors: Henrystats
1010
config:
11-
tags: ['avalanche_c','trades', 'dfx','dex']
11+
tags: ['avalanche_c','trades', 'odos','dex']
1212
description: >
13-
odos aggregator trades on avalanche_c
13+
A table containing all trades of odos v2 on avalanche_c
1414
data_tests:
1515
- dbt_utils.unique_combination_of_columns:
1616
arguments:
@@ -26,17 +26,20 @@ models:
2626
arguments:
2727
blockchain: avalanche_c
2828
project: odos
29-
version: 1
29+
version: 2
3030
columns:
3131
- &blockchain
3232
name: blockchain
3333
description: "Blockchain which the DEX is deployed"
34-
- &project
34+
- &project
3535
name: project
36-
description: "Project name of the DEX"
36+
description: "Project name of the DEX"
3737
- &version
3838
name: version
3939
description: "Version of the contract built and deployed by the DEX project"
40+
- &block_month
41+
name: block_month
42+
description: "UTC event block month of each DEX trade"
4043
- &block_date
4144
name: block_date
4245
description: "UTC event block date of each DEX trade"
@@ -91,9 +94,45 @@ models:
9194
- &tx_to
9295
name: tx_to
9396
description: "Address which received the transaction"
94-
- &trace_address
95-
name: trace_address
96-
description: ""
9797
- &evt_index
9898
name: evt_index
9999
description: ""
100+
- &trace_address
101+
name: trace_address
102+
description: ""
103+
104+
- name: odos_avalanche_c_trades
105+
meta:
106+
blockchain: avalanche_c
107+
sector: dex
108+
project: odos
109+
contributors: Henrystats
110+
config:
111+
tags: ['avalanche_c','trades', 'odos','dex']
112+
description: >
113+
A table containing all trades of odos on avalanche_c.
114+
columns:
115+
- *blockchain
116+
- *project
117+
- *version
118+
- *block_month
119+
- *block_date
120+
- *block_time
121+
- *token_bought_symbol
122+
- *token_sold_symbol
123+
- *token_pair
124+
- *token_bought_amount
125+
- *token_sold_amount
126+
- *token_bought_amount_raw
127+
- *token_sold_amount_raw
128+
- *amount_usd
129+
- *token_bought_address
130+
- *token_sold_address
131+
- *taker
132+
- *maker
133+
- *project_contract_address
134+
- *tx_hash
135+
- *tx_from
136+
- *tx_to
137+
- *evt_index
138+
- *trace_address
Lines changed: 40 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,45 @@
11
{{ config(
2-
tags=['prod_exclude'],
3-
schema = 'odos_avalanche_c',
4-
alias = 'trades',
5-
partition_by = ['block_date'],
6-
materialized = 'incremental',
7-
file_format = 'delta',
8-
incremental_strategy = 'merge',
9-
unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index', 'trace_address']
10-
, post_hook='{{ hide_spells() }}'
11-
)
2+
schema = 'odos_avalanche_c',
3+
alias = 'trades'
4+
, post_hook='{{ hide_spells() }}'
5+
)
126
}}
137

14-
/*
15-
note: this spell has not been migrated to dunesql, as there are duplicates issues and issue is not resolved
16-
please migrate to dunesql & fix duplicates to ensure up-to-date logic & data
17-
*/
8+
{% set odos_models = [
9+
ref('odos_v2_avalanche_c_trades')
10+
] %}
1811

19-
20-
{% set project_start_date = '2022-11-29' %}
21-
22-
WITH
23-
24-
dexs_raw as (
25-
SELECT
26-
evt_block_time as block_time,
27-
explode(outputs) as data_value,
28-
'' as maker,
29-
CAST(amountsIn[0] as double) as token_sold_amount_raw,
30-
CAST(amountsOut[0] as double) as token_bought_amount_raw,
31-
CAST(NULL as double) as amount_usd,
32-
CASE
33-
WHEN CAST(tokensIn[0] as string) IN ('0', 'O', '0x0000000000000000000000000000000000000000')
34-
THEN '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7' -- WAVAX
35-
ELSE CAST(tokensIn[0] as string)
36-
END as token_sold_address,
37-
contract_address as project_contract_address,
38-
evt_tx_hash as tx_hash,
39-
CAST(ARRAY() as array<bigint>) AS trace_address,
40-
evt_index
41-
FROM
42-
{{ source('odos_avalanche_c', 'OdosRouter_evt_Swapped') }}
43-
{% if is_incremental() %}
44-
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
45-
{% endif %}
46-
),
47-
48-
dexs as (
49-
SELECT
50-
*,
51-
CAST(data_value:receiver as string) as taker,
52-
CASE
53-
WHEN CAST(data_value:tokenAddress as string) IN ('0', 'O', '0x0000000000000000000000000000000000000000')
54-
THEN '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7' -- WAVAX
55-
ELSE CAST(data_value:tokenAddress as string)
56-
END as token_bought_address
57-
FROM
58-
dexs_raw
59-
)
60-
61-
SELECT
62-
'avalanche_c' as blockchain,
63-
'odos' as project,
64-
'1' as version,
65-
TRY_CAST(date_trunc('DAY', dexs.block_time) as date) as block_date,
66-
dexs.block_time,
67-
erc20a.symbol as token_bought_symbol,
68-
erc20b.symbol as token_sold_symbol,
69-
CASE
70-
WHEN lower(erc20a.symbol) > lower(erc20b.symbol) THEN concat(erc20b.symbol, '-', erc20a.symbol)
71-
ELSE concat(erc20a.symbol, '-', erc20b.symbol)
72-
END as token_pair,
73-
dexs.token_bought_amount_raw / power(10, erc20a.decimals) as token_bought_amount,
74-
dexs.token_sold_amount_raw / power(10, erc20b.decimals) as token_sold_amount,
75-
CAST(dexs.token_bought_amount_raw AS DECIMAL(38,0)) AS token_bought_amount_raw,
76-
CAST(dexs.token_sold_amount_raw AS DECIMAL(38,0)) AS token_sold_amount_raw,
77-
COALESCE(
78-
dexs.amount_usd,
79-
(dexs.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price,
80-
(dexs.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price
81-
) as amount_usd,
82-
dexs.token_bought_address,
83-
dexs.token_sold_address,
84-
COALESCE(dexs.taker, tx.from) as taker, -- subqueries rely on this COALESCE to avoid redundant joins with the transactions table
85-
dexs.maker,
86-
dexs.project_contract_address,
87-
dexs.tx_hash,
88-
tx.from as tx_from,
89-
tx.to AS tx_to,
90-
dexs.trace_address,
91-
dexs.evt_index
92-
FROM dexs
93-
INNER JOIN {{ source('avalanche_c', 'transactions') }} tx
94-
ON tx.hash = dexs.tx_hash
95-
{% if not is_incremental() %}
96-
AND tx.block_time >= '{{project_start_date}}'
12+
select *
13+
from (
14+
{% for aggregator_model in odos_models %}
15+
select
16+
blockchain
17+
, project
18+
, version
19+
, block_date
20+
, block_month
21+
, block_time
22+
, token_bought_symbol
23+
, token_sold_symbol
24+
, token_pair
25+
, token_bought_amount
26+
, token_sold_amount
27+
, token_bought_amount_raw
28+
, token_sold_amount_raw
29+
, amount_usd
30+
, token_bought_address
31+
, token_sold_address
32+
, taker
33+
, maker
34+
, project_contract_address
35+
, tx_hash
36+
, tx_from
37+
, tx_to
38+
, trace_address
39+
, evt_index
40+
from {{ aggregator_model }}
41+
{% if not loop.last %}
42+
union all
9743
{% endif %}
98-
{% if is_incremental() %}
99-
AND tx.block_time >= date_trunc("day", now() - interval '1 week')
100-
{% endif %}
101-
LEFT JOIN {{ source('tokens', 'erc20') }} erc20a
102-
ON erc20a.contract_address = dexs.token_bought_address
103-
AND erc20a.blockchain = 'avalanche_c'
104-
LEFT JOIN {{ source('tokens', 'erc20') }} erc20b
105-
ON erc20b.contract_address = dexs.token_sold_address
106-
AND erc20b.blockchain = 'avalanche_c'
107-
LEFT JOIN {{ source('prices', 'usd') }} p_bought
108-
ON p_bought.minute = date_trunc('minute', dexs.block_time)
109-
AND p_bought.contract_address = dexs.token_bought_address
110-
AND p_bought.blockchain = 'avalanche_c'
111-
{% if not is_incremental() %}
112-
AND p_bought.minute >= '{{project_start_date}}'
113-
{% endif %}
114-
{% if is_incremental() %}
115-
AND p_bought.minute >= date_trunc("day", now() - interval '1 week')
116-
{% endif %}
117-
LEFT JOIN {{ source('prices', 'usd') }} p_sold
118-
ON p_sold.minute = date_trunc('minute', dexs.block_time)
119-
AND p_sold.contract_address = dexs.token_sold_address
120-
AND p_sold.blockchain = 'avalanche_c'
121-
{% if not is_incremental() %}
122-
AND p_sold.minute >= '{{project_start_date}}'
123-
{% endif %}
124-
{% if is_incremental() %}
125-
AND p_sold.minute >= date_trunc("day", now() - interval '1 week')
126-
{% endif %}
127-
;
44+
{% endfor %}
45+
)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{{ config(
2+
alias = 'trades'
3+
,schema = 'odos_v2_avalanche_c'
4+
,materialized = 'incremental'
5+
,file_format = 'delta'
6+
,incremental_strategy = 'merge'
7+
,unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index', 'trace_address']
8+
,incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')]
9+
)
10+
}}
11+
12+
{% set project_start_date = '2023-07-13' %}
13+
14+
with event_data as (
15+
select
16+
evt_block_time as block_time
17+
, evt_block_number as block_number
18+
, sender as taker
19+
, contract_address as maker
20+
, inputAmount as token_sold_amount_raw
21+
, amountOut as token_bought_amount_raw
22+
, cast(null as double) as amount_usd
23+
, case
24+
when inputToken = 0x0000000000000000000000000000000000000000
25+
then 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7 -- WAVAX
26+
else inputToken
27+
end as token_sold_address
28+
, case
29+
when outputToken = 0x0000000000000000000000000000000000000000
30+
then 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7 -- WAVAX
31+
else outputToken
32+
end as token_bought_address
33+
, contract_address as project_contract_address
34+
, evt_tx_hash as tx_hash
35+
, evt_index
36+
, cast(array[-1] as array<bigint>) as trace_address
37+
from {{ source('odos_v2_avalanche_c', 'OdosRouterV2_evt_Swap') }}
38+
{% if is_incremental() %}
39+
where {{ incremental_predicate('evt_block_time') }}
40+
{% else %}
41+
where evt_block_time >= timestamp '{{project_start_date}}'
42+
{% endif %}
43+
)
44+
45+
select
46+
'avalanche_c' as blockchain
47+
, 'odos' as project
48+
, '2' as version
49+
, try_cast(date_trunc('day', e.block_time) as date) as block_date
50+
, try_cast(date_trunc('month', e.block_time) as date) as block_month
51+
, e.block_time
52+
, erc20a.symbol as token_bought_symbol
53+
, erc20b.symbol as token_sold_symbol
54+
, case
55+
when lower(erc20a.symbol) > lower(erc20b.symbol)
56+
then concat(erc20b.symbol, '-', erc20a.symbol)
57+
else concat(erc20a.symbol, '-', erc20b.symbol)
58+
end as token_pair
59+
, e.token_bought_amount_raw / power(10, erc20a.decimals) as token_bought_amount
60+
, e.token_sold_amount_raw / power(10, erc20b.decimals) as token_sold_amount
61+
, e.token_bought_amount_raw as token_bought_amount_raw
62+
, e.token_sold_amount_raw as token_sold_amount_raw
63+
, coalesce(
64+
e.amount_usd
65+
, (e.token_bought_amount_raw / power(10, erc20a.decimals)) * p_bought.price
66+
, (e.token_sold_amount_raw / power(10, erc20b.decimals)) * p_sold.price
67+
) as amount_usd
68+
, e.token_bought_address
69+
, e.token_sold_address
70+
, e.taker
71+
, e.maker
72+
, e.project_contract_address
73+
, e.tx_hash
74+
, tx."from" as tx_from
75+
, tx.to as tx_to
76+
, e.evt_index
77+
, e.trace_address
78+
from event_data e
79+
inner join {{ source('avalanche_c', 'transactions') }} tx
80+
on e.tx_hash = tx.hash
81+
{% if not is_incremental() %}
82+
and tx.block_time >= date '{{project_start_date}}'
83+
{% else %}
84+
and {{ incremental_predicate('tx.block_time') }}
85+
{% endif %}
86+
left join {{ source('tokens', 'erc20') }} erc20a
87+
on erc20a.contract_address = e.token_bought_address
88+
and erc20a.blockchain = 'avalanche_c'
89+
left join {{ source('tokens', 'erc20') }} erc20b
90+
on erc20b.contract_address = e.token_sold_address
91+
and erc20b.blockchain = 'avalanche_c'
92+
left join {{ source('prices', 'usd') }} p_bought
93+
on p_bought.minute = date_trunc('minute', e.block_time)
94+
and p_bought.contract_address = e.token_bought_address
95+
and p_bought.blockchain = 'avalanche_c'
96+
{% if not is_incremental() %}
97+
and p_bought.minute >= date '{{project_start_date}}'
98+
{% else %}
99+
and {{ incremental_predicate('p_bought.minute') }}
100+
{% endif %}
101+
left join {{ source('prices', 'usd') }} p_sold
102+
on p_sold.minute = date_trunc('minute', e.block_time)
103+
and p_sold.contract_address = e.token_sold_address
104+
and p_sold.blockchain = 'avalanche_c'
105+
{% if not is_incremental() %}
106+
and p_sold.minute >= date '{{project_start_date}}'
107+
{% else %}
108+
and {{ incremental_predicate('p_sold.minute') }}
109+
{% endif %}

dbt_subprojects/dex/models/_projects/odos/odos_trades.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ref('odos_ethereum_trades'),
1010
ref('odos_optimism_trades'),
1111
ref('odos_arbitrum_trades'),
12+
ref('odos_avalanche_c_trades'),
1213
ref('odos_base_trades')
1314
] %}
1415

dbt_subprojects/dex/seeds/aggregator_trades/dex_aggregator_seed.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,5 @@ tempo,uniswap,4,2026-06-08,0x0f818a40385db44d6f4884ae4533139ecc03c48969688dee8a0
188188
tempo,uniswap,4,2026-06-08,0xa0dbdb0a59bf7aea37096edaad50ae80551e8d392bf64c7f2b908d3c7f672e26,7,"5,0,0,0,0",0x20c000000000000000000000b9537d11c60e8b50,3.193023,0x20c0000000000000000000000000000000000000,3.193343
189189
tempo,uniswap,4,2026-06-08,0x220921a8b3eb96976fa833fa1f078255e14869a2268f3d2a3782a5398e7e7926,6,"0,0,0",0x20c000000000000000000000b9537d11c60e8b50,37.9962,0x20c0000000000000000000000000000000000000,38
190190
arbitrum,1inch-LOP,4.0,2026-06-06,0x92c5f2295c67cb1def44ba86667a02c6cd4d96b64825dde9fc407d987ddb07cb,1,"3,0,1,2",0x82af49447d8a07e3bd95bd0d56f35241523fbab1,23.83804776884383,0xaf88d065e77c8cc2239327c5edb3a432268e5831,36981.05915
191+
avalanche_c,odos,2,2026-06-12,0xaa7786736844c5f4498e3b73adadb9ccefd1bda285e915ecf74b4dc038712f81,8,-1,0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e,3936.77744,0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7,600
192+
avalanche_c,odos,2,2026-06-12,0xd4d93edda2fdfe7f5ca4296fe102b3ea1fed3f19aeb698e24c8de76564301ada,6,-1,0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e,1501.807107,0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7,228.7881185486987

0 commit comments

Comments
 (0)