Skip to content

Commit d907f7b

Browse files
BroderickBonellijeff-dudeHosuke
authored
Hashflow cross chain trades (#3397)
* add cross chain trade model for hashflow bnb * add crosschain models for ethereum, bnb, and avalanche_c. Update sources.yml files for each, correcting description on bnb and avalanche for Pool_evt_LzTrade tables, and adding to hashflow_ethereum_sources.yml. Add aggregated Hashflow crosschain models encompassing all chains in parent directory, update hashflow_trades_schema.yml. * update if is_incremental() filter per Dune's requirements * fix incremental * fix ambigious reference * Fix alias * list out columns on hashflow_crosschain_trades.sql model * lowercase blockchain names * fix source_chain name for avalanche, add join on blockchain to erc20b and source chain --------- Co-authored-by: jeff-dude <102681548+jeff-dude@users.noreply.github.com> Co-authored-by: Huang Geyang <Sukebeta@outlook.com>
1 parent cd1fd20 commit d907f7b

11 files changed

Lines changed: 577 additions & 17 deletions
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{{
2+
config(
3+
alias="cross_chain_trades"
4+
,partition_by = ['block_date']
5+
,materialized='incremental'
6+
,incremental_strategy = 'merge'
7+
,unique_key = ['block_date', 'source_chain', 'tx_hash']
8+
,post_hook='{{ expose_spells(\'["avalanche_c"]\',
9+
"project",
10+
"hashflow",
11+
\'["BroderickBonelli"]\') }}'
12+
)
13+
}}
14+
15+
16+
with cross_chain_trades AS (
17+
SELECT
18+
evt_block_time AS block_time
19+
,trader
20+
,quoteTokenAmount AS token_bought_amount_raw
21+
,baseTokenAmount AS token_sold_amount_raw
22+
,cast(NULL AS double) AS amount_usd
23+
,quoteToken AS token_bought_address
24+
,baseToken AS token_sold_address
25+
,evt_tx_hash AS tx_hash
26+
,CASE WHEN dstChainId = 1 OR dstChainId = 101 THEN 'ethereum'
27+
WHEN dstChainId = 10 OR dstChainId = 110 THEN 'arbitrum'
28+
WHEN dstChainId = 11 OR dstChainId = 111 THEN 'optimism'
29+
WHEN dstChainId = 6 OR dstChainId = 106 THEN 'avalanche_c'
30+
WHEN dstChainId = 9 OR dstChainId = 109 THEN 'polygon'
31+
WHEN dstChainId = 2 OR dstChainId = 102 THEN 'bnb' END AS destination_chain
32+
,'avalanche_c' AS source_chain
33+
FROM
34+
{{ source('hashflow_avalanche_c', 'Pool_evt_LzTrade') }}
35+
{% if is_incremental() %}
36+
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
37+
{% endif %}
38+
39+
UNION
40+
41+
SELECT
42+
evt_block_time AS block_time
43+
,trader
44+
,quoteTokenAmount AS token_bought_amount_raw
45+
,baseTokenAmount AS token_sold_amount_raw
46+
,cast(NULL AS double) AS amount_usd
47+
,quoteToken AS token_bought_address
48+
,baseToken AS token_sold_address
49+
,evt_tx_hash AS tx_hash
50+
,CASE WHEN dstChainId = 1 THEN 'ethereum'
51+
WHEN dstChainId = 2 THEN 'arbitrum'
52+
WHEN dstChainId = 3 THEN 'optimism'
53+
WHEN dstChainId = 4 THEN 'avalanche_c'
54+
WHEN dstChainId = 5 THEN 'polygon'
55+
WHEN dstChainId = 6 THEN 'bnb' END AS destination_chain
56+
,'avalanche_c' AS source_chain
57+
FROM
58+
{{ source('hashflow_avalanche_c', 'Pool_evt_XChainTrade') }}
59+
{% if is_incremental() %}
60+
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
61+
{% endif %}
62+
)
63+
64+
SELECT
65+
try_cast(date_trunc('DAY', cross_chain_trades.block_time) AS date) AS block_date
66+
,cross_chain_trades.block_time
67+
,erc20a.symbol AS token_bought_symbol
68+
,erc20b.symbol AS token_sold_symbol
69+
,cross_chain_trades.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount
70+
,cross_chain_trades.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount
71+
,CAST(cross_chain_trades.token_bought_amount_raw AS DECIMAL(38,0)) AS token_bought_amount_raw
72+
,CAST(cross_chain_trades.token_sold_amount_raw AS DECIMAL(38,0)) AS token_sold_amount_raw
73+
,coalesce(
74+
cross_chain_trades.amount_usd
75+
, (cross_chain_trades.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price
76+
, (cross_chain_trades.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price
77+
) AS amount_usd
78+
,cross_chain_trades.token_bought_address
79+
,cross_chain_trades.token_sold_address
80+
,cross_chain_trades.trader
81+
,cross_chain_trades.tx_hash
82+
,cross_chain_trades.source_chain
83+
,cross_chain_trades.destination_chain
84+
FROM cross_chain_trades
85+
LEFT JOIN {{ ref('tokens_erc20') }} erc20a
86+
ON erc20a.contract_address = cross_chain_trades.token_bought_address
87+
LEFT JOIN {{ ref('tokens_erc20') }} erc20b
88+
ON erc20b.contract_address = cross_chain_trades.token_sold_address
89+
AND erc20b.blockchain = cross_chain_trades.source_chain
90+
LEFT JOIN {{ source('prices', 'usd') }} p_bought
91+
ON p_bought.minute = date_trunc('minute', cross_chain_trades.block_time)
92+
AND p_bought.contract_address = cross_chain_trades.token_bought_address
93+
AND p_bought.blockchain = 'avalanche_c'
94+
{% if is_incremental() %}
95+
AND p_bought.minute >= date_trunc("day", now() - interval '1 week')
96+
{% endif %}
97+
LEFT JOIN {{ source('prices', 'usd') }} p_sold
98+
ON p_sold.minute = date_trunc('minute', cross_chain_trades.block_time)
99+
AND p_sold.contract_address = cross_chain_trades.token_sold_address
100+
AND p_sold.blockchain = 'avalanche_c'
101+
{% if is_incremental() %}
102+
AND p_sold.minute >= date_trunc("day", now() - interval '1 week')
103+
{% endif %}
104+

models/hashflow/avalanche_c/hashflow_avalanche_c_schema.yml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,43 @@ models:
9494
description: ""
9595
- &evt_index
9696
name: evt_index
97-
description: ""
97+
description: ""
98+
99+
- name: hashflow_avalanche_c_crosschain_trades
100+
meta:
101+
blockchain: avalanche_c
102+
sector: dex
103+
project: hashflow
104+
contributors: BroderickBonelli
105+
config:
106+
tags: ['avalanche_c','hashflow','trades','dex','BroderickBonelli']
107+
description: >
108+
Table of Hashflow cross chain trades on avalanche_c. Includes unique data for cross chain trades such as source and destination chains for further analysis of Hashflow's cross chain activity.
109+
tests:
110+
- dbt_utils.unique_combination_of_columns:
111+
combination_of_columns:
112+
- block_date
113+
- source_chain
114+
- tx_hash
115+
columns:
116+
- *block_date
117+
- *block_time
118+
- *token_bought_symbol
119+
- *token_sold_symbol
120+
- *token_bought_amount
121+
- *token_sold_amount
122+
- *token_bought_amount_raw
123+
- *token_sold_amount_raw
124+
- *amount_usd
125+
- *token_bought_address
126+
- *token_sold_address
127+
- &trader
128+
name: trader
129+
description: "Address of trader who executed the trade."
130+
- *tx_hash
131+
- &source_chain
132+
name: source_chain
133+
description: "Chain where the trade originated."
134+
- &destination_chain
135+
name: destination_chain
136+
description: "Ending chain where swapped token balance will be received."

models/hashflow/avalanche_c/hashflow_avalanche_c_sources.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sources:
4545

4646
- name: Pool_evt_LzTrade
4747
loaded_at_field: evt_block_time
48-
description: "Pool lazy trades events table"
48+
description: "Pool layer zero trades events table"
4949
columns:
5050
- *evt_block_time
5151
- *evt_index
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{{
2+
config(
3+
alias="cross_chain_trades"
4+
,partition_by = ['block_date']
5+
,materialized='incremental'
6+
,incremental_strategy = 'merge'
7+
,file_format = 'delta'
8+
,unique_key = ['block_date', 'source_chain', 'tx_hash']
9+
,post_hook='{{ expose_spells(\'["bnb"]\',
10+
"project",
11+
"hashflow",
12+
\'["BroderickBonelli"]\') }}'
13+
)
14+
}}
15+
16+
17+
with cross_chain_trades AS (
18+
SELECT
19+
evt_block_time AS block_time
20+
,trader
21+
,quoteTokenAmount AS token_bought_amount_raw
22+
,baseTokenAmount AS token_sold_amount_raw
23+
,cast(NULL AS double) AS amount_usd
24+
,quoteToken AS token_bought_address
25+
,baseToken AS token_sold_address
26+
,evt_tx_hash AS tx_hash
27+
,CASE WHEN dstChainId = 1 OR dstChainId = 101 THEN 'ethereum'
28+
WHEN dstChainId = 10 OR dstChainId = 110 THEN 'arbitrum'
29+
WHEN dstChainId = 11 OR dstChainId = 111 THEN 'optimism'
30+
WHEN dstChainId = 6 OR dstChainId = 106 THEN 'avalanche_c'
31+
WHEN dstChainId = 9 OR dstChainId = 109 THEN 'polygon'
32+
WHEN dstChainId = 2 OR dstChainId = 102 THEN 'bnb' END AS destination_chain
33+
,'bnb' AS source_chain
34+
FROM
35+
{{ source('hashflow_bnb', 'Pool_evt_LzTrade') }}
36+
{% if is_incremental() %}
37+
WHERE evt_block_time >= (SELECT MAX(block_time) FROM {{ this }})
38+
{% endif %}
39+
40+
UNION
41+
42+
SELECT
43+
evt_block_time AS block_time
44+
,trader
45+
,quoteTokenAmount AS token_bought_amount_raw
46+
,baseTokenAmount AS token_sold_amount_raw
47+
,cast(NULL AS double) AS amount_usd
48+
,quoteToken AS token_bought_address
49+
,baseToken AS token_sold_address
50+
,evt_tx_hash AS tx_hash
51+
,CASE WHEN dstChainId = 1 THEN 'ethereum'
52+
WHEN dstChainId = 2 THEN 'arbitrum'
53+
WHEN dstChainId = 3 THEN 'optimism'
54+
WHEN dstChainId = 4 THEN 'avalanche_c'
55+
WHEN dstChainId = 5 THEN 'polygon'
56+
WHEN dstChainId = 6 THEN 'bnb' END AS destination_chain
57+
,'bnb' AS source_chain
58+
FROM
59+
{{ source('hashflow_bnb', 'Pool_evt_XChainTrade') }}
60+
{% if is_incremental() %}
61+
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
62+
{% endif %}
63+
)
64+
65+
SELECT
66+
try_cast(date_trunc('DAY', cross_chain_trades.block_time) AS date) AS block_date
67+
,cross_chain_trades.block_time
68+
,erc20a.symbol AS token_bought_symbol
69+
,erc20b.symbol AS token_sold_symbol
70+
,cross_chain_trades.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount
71+
,cross_chain_trades.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount
72+
,CAST(cross_chain_trades.token_bought_amount_raw AS DECIMAL(38,0)) AS token_bought_amount_raw
73+
,CAST(cross_chain_trades.token_sold_amount_raw AS DECIMAL(38,0)) AS token_sold_amount_raw
74+
,coalesce(
75+
cross_chain_trades.amount_usd
76+
, (cross_chain_trades.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price
77+
, (cross_chain_trades.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price
78+
) AS amount_usd
79+
,cross_chain_trades.token_bought_address
80+
,cross_chain_trades.token_sold_address
81+
,cross_chain_trades.trader
82+
,cross_chain_trades.tx_hash
83+
,cross_chain_trades.source_chain
84+
,cross_chain_trades.destination_chain
85+
FROM cross_chain_trades
86+
LEFT JOIN {{ ref('tokens_erc20') }} erc20a
87+
ON erc20a.contract_address = cross_chain_trades.token_bought_address
88+
LEFT JOIN {{ ref('tokens_erc20') }} erc20b
89+
ON erc20b.contract_address = cross_chain_trades.token_sold_address
90+
AND erc20b.blockchain = cross_chain_trades.source_chain
91+
LEFT JOIN {{ source('prices', 'usd') }} p_bought
92+
ON p_bought.minute = date_trunc('minute', cross_chain_trades.block_time)
93+
AND p_bought.contract_address = cross_chain_trades.token_bought_address
94+
AND p_bought.blockchain = 'bnb'
95+
{% if is_incremental() %}
96+
AND p_bought.minute >= date_trunc("day", now() - interval '1 week')
97+
{% endif %}
98+
LEFT JOIN {{ source('prices', 'usd') }} p_sold
99+
ON p_sold.minute = date_trunc('minute', cross_chain_trades.block_time)
100+
AND p_sold.contract_address = cross_chain_trades.token_sold_address
101+
AND p_sold.blockchain = 'bnb'
102+
{% if is_incremental() %}
103+
AND p_sold.minute >= date_trunc("day", now() - interval '1 week')
104+
{% endif %}
105+

models/hashflow/bnb/hashflow_bnb_schema.yml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,43 @@ models:
9494
description: ""
9595
- &evt_index
9696
name: evt_index
97-
description: ""
97+
description: ""
98+
99+
- name: hashflow_bnb_crosschain_trades
100+
meta:
101+
blockchain: bnb
102+
sector: dex
103+
project: hashflow
104+
contributors: BroderickBonelli
105+
config:
106+
tags: ['bnb','hashflow','trades','dex','BroderickBonelli']
107+
description: >
108+
Table of chain trades on Hashflow. Includes unique data for cross chain trades such as source and destination chains for further analysis of Hashflow's cross chain activity.
109+
tests:
110+
- dbt_utils.unique_combination_of_columns:
111+
combination_of_columns:
112+
- block_date
113+
- source_chain
114+
- tx_hash
115+
columns:
116+
- *block_date
117+
- *block_time
118+
- *token_bought_symbol
119+
- *token_sold_symbol
120+
- *token_bought_amount
121+
- *token_sold_amount
122+
- *token_bought_amount_raw
123+
- *token_sold_amount_raw
124+
- *amount_usd
125+
- *token_bought_address
126+
- *token_sold_address
127+
- &trader
128+
name: trader
129+
description: "Address of trader who executed the trade."
130+
- *tx_hash
131+
- &source_chain
132+
name: source_chain
133+
description: "Chain where the trade originated."
134+
- &destination_chain
135+
name: destination_chain
136+
description: "Ending chain where swapped token balance will be received."

models/hashflow/bnb/hashflow_bnb_sources.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sources:
4545

4646
- name: Pool_evt_LzTrade
4747
loaded_at_field: evt_block_time
48-
description: "Pool lazy trades events table"
48+
description: "Pool layer zero trade events table"
4949
columns:
5050
- *evt_block_time
5151
- *evt_index

0 commit comments

Comments
 (0)