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+ )
0 commit comments