-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <[email protected]> Co-authored-by: Huang Geyang <[email protected]>
- Loading branch information
1 parent
cd1fd20
commit d907f7b
Showing
11 changed files
with
577 additions
and
17 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
models/hashflow/avalanche_c/hashflow_avalanche_c_crosschain_trades.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
{{ | ||
config( | ||
alias="cross_chain_trades" | ||
,partition_by = ['block_date'] | ||
,materialized='incremental' | ||
,incremental_strategy = 'merge' | ||
,unique_key = ['block_date', 'source_chain', 'tx_hash'] | ||
,post_hook='{{ expose_spells(\'["avalanche_c"]\', | ||
"project", | ||
"hashflow", | ||
\'["BroderickBonelli"]\') }}' | ||
) | ||
}} | ||
|
||
|
||
with cross_chain_trades AS ( | ||
SELECT | ||
evt_block_time AS block_time | ||
,trader | ||
,quoteTokenAmount AS token_bought_amount_raw | ||
,baseTokenAmount AS token_sold_amount_raw | ||
,cast(NULL AS double) AS amount_usd | ||
,quoteToken AS token_bought_address | ||
,baseToken AS token_sold_address | ||
,evt_tx_hash AS tx_hash | ||
,CASE WHEN dstChainId = 1 OR dstChainId = 101 THEN 'ethereum' | ||
WHEN dstChainId = 10 OR dstChainId = 110 THEN 'arbitrum' | ||
WHEN dstChainId = 11 OR dstChainId = 111 THEN 'optimism' | ||
WHEN dstChainId = 6 OR dstChainId = 106 THEN 'avalanche_c' | ||
WHEN dstChainId = 9 OR dstChainId = 109 THEN 'polygon' | ||
WHEN dstChainId = 2 OR dstChainId = 102 THEN 'bnb' END AS destination_chain | ||
,'avalanche_c' AS source_chain | ||
FROM | ||
{{ source('hashflow_avalanche_c', 'Pool_evt_LzTrade') }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
|
||
UNION | ||
|
||
SELECT | ||
evt_block_time AS block_time | ||
,trader | ||
,quoteTokenAmount AS token_bought_amount_raw | ||
,baseTokenAmount AS token_sold_amount_raw | ||
,cast(NULL AS double) AS amount_usd | ||
,quoteToken AS token_bought_address | ||
,baseToken AS token_sold_address | ||
,evt_tx_hash AS tx_hash | ||
,CASE WHEN dstChainId = 1 THEN 'ethereum' | ||
WHEN dstChainId = 2 THEN 'arbitrum' | ||
WHEN dstChainId = 3 THEN 'optimism' | ||
WHEN dstChainId = 4 THEN 'avalanche_c' | ||
WHEN dstChainId = 5 THEN 'polygon' | ||
WHEN dstChainId = 6 THEN 'bnb' END AS destination_chain | ||
,'avalanche_c' AS source_chain | ||
FROM | ||
{{ source('hashflow_avalanche_c', 'Pool_evt_XChainTrade') }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
) | ||
|
||
SELECT | ||
try_cast(date_trunc('DAY', cross_chain_trades.block_time) AS date) AS block_date | ||
,cross_chain_trades.block_time | ||
,erc20a.symbol AS token_bought_symbol | ||
,erc20b.symbol AS token_sold_symbol | ||
,cross_chain_trades.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount | ||
,cross_chain_trades.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount | ||
,CAST(cross_chain_trades.token_bought_amount_raw AS DECIMAL(38,0)) AS token_bought_amount_raw | ||
,CAST(cross_chain_trades.token_sold_amount_raw AS DECIMAL(38,0)) AS token_sold_amount_raw | ||
,coalesce( | ||
cross_chain_trades.amount_usd | ||
, (cross_chain_trades.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price | ||
, (cross_chain_trades.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price | ||
) AS amount_usd | ||
,cross_chain_trades.token_bought_address | ||
,cross_chain_trades.token_sold_address | ||
,cross_chain_trades.trader | ||
,cross_chain_trades.tx_hash | ||
,cross_chain_trades.source_chain | ||
,cross_chain_trades.destination_chain | ||
FROM cross_chain_trades | ||
LEFT JOIN {{ ref('tokens_erc20') }} erc20a | ||
ON erc20a.contract_address = cross_chain_trades.token_bought_address | ||
LEFT JOIN {{ ref('tokens_erc20') }} erc20b | ||
ON erc20b.contract_address = cross_chain_trades.token_sold_address | ||
AND erc20b.blockchain = cross_chain_trades.source_chain | ||
LEFT JOIN {{ source('prices', 'usd') }} p_bought | ||
ON p_bought.minute = date_trunc('minute', cross_chain_trades.block_time) | ||
AND p_bought.contract_address = cross_chain_trades.token_bought_address | ||
AND p_bought.blockchain = 'avalanche_c' | ||
{% if is_incremental() %} | ||
AND p_bought.minute >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
LEFT JOIN {{ source('prices', 'usd') }} p_sold | ||
ON p_sold.minute = date_trunc('minute', cross_chain_trades.block_time) | ||
AND p_sold.contract_address = cross_chain_trades.token_sold_address | ||
AND p_sold.blockchain = 'avalanche_c' | ||
{% if is_incremental() %} | ||
AND p_sold.minute >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
{{ | ||
config( | ||
alias="cross_chain_trades" | ||
,partition_by = ['block_date'] | ||
,materialized='incremental' | ||
,incremental_strategy = 'merge' | ||
,file_format = 'delta' | ||
,unique_key = ['block_date', 'source_chain', 'tx_hash'] | ||
,post_hook='{{ expose_spells(\'["bnb"]\', | ||
"project", | ||
"hashflow", | ||
\'["BroderickBonelli"]\') }}' | ||
) | ||
}} | ||
|
||
|
||
with cross_chain_trades AS ( | ||
SELECT | ||
evt_block_time AS block_time | ||
,trader | ||
,quoteTokenAmount AS token_bought_amount_raw | ||
,baseTokenAmount AS token_sold_amount_raw | ||
,cast(NULL AS double) AS amount_usd | ||
,quoteToken AS token_bought_address | ||
,baseToken AS token_sold_address | ||
,evt_tx_hash AS tx_hash | ||
,CASE WHEN dstChainId = 1 OR dstChainId = 101 THEN 'ethereum' | ||
WHEN dstChainId = 10 OR dstChainId = 110 THEN 'arbitrum' | ||
WHEN dstChainId = 11 OR dstChainId = 111 THEN 'optimism' | ||
WHEN dstChainId = 6 OR dstChainId = 106 THEN 'avalanche_c' | ||
WHEN dstChainId = 9 OR dstChainId = 109 THEN 'polygon' | ||
WHEN dstChainId = 2 OR dstChainId = 102 THEN 'bnb' END AS destination_chain | ||
,'bnb' AS source_chain | ||
FROM | ||
{{ source('hashflow_bnb', 'Pool_evt_LzTrade') }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= (SELECT MAX(block_time) FROM {{ this }}) | ||
{% endif %} | ||
|
||
UNION | ||
|
||
SELECT | ||
evt_block_time AS block_time | ||
,trader | ||
,quoteTokenAmount AS token_bought_amount_raw | ||
,baseTokenAmount AS token_sold_amount_raw | ||
,cast(NULL AS double) AS amount_usd | ||
,quoteToken AS token_bought_address | ||
,baseToken AS token_sold_address | ||
,evt_tx_hash AS tx_hash | ||
,CASE WHEN dstChainId = 1 THEN 'ethereum' | ||
WHEN dstChainId = 2 THEN 'arbitrum' | ||
WHEN dstChainId = 3 THEN 'optimism' | ||
WHEN dstChainId = 4 THEN 'avalanche_c' | ||
WHEN dstChainId = 5 THEN 'polygon' | ||
WHEN dstChainId = 6 THEN 'bnb' END AS destination_chain | ||
,'bnb' AS source_chain | ||
FROM | ||
{{ source('hashflow_bnb', 'Pool_evt_XChainTrade') }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
) | ||
|
||
SELECT | ||
try_cast(date_trunc('DAY', cross_chain_trades.block_time) AS date) AS block_date | ||
,cross_chain_trades.block_time | ||
,erc20a.symbol AS token_bought_symbol | ||
,erc20b.symbol AS token_sold_symbol | ||
,cross_chain_trades.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount | ||
,cross_chain_trades.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount | ||
,CAST(cross_chain_trades.token_bought_amount_raw AS DECIMAL(38,0)) AS token_bought_amount_raw | ||
,CAST(cross_chain_trades.token_sold_amount_raw AS DECIMAL(38,0)) AS token_sold_amount_raw | ||
,coalesce( | ||
cross_chain_trades.amount_usd | ||
, (cross_chain_trades.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price | ||
, (cross_chain_trades.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price | ||
) AS amount_usd | ||
,cross_chain_trades.token_bought_address | ||
,cross_chain_trades.token_sold_address | ||
,cross_chain_trades.trader | ||
,cross_chain_trades.tx_hash | ||
,cross_chain_trades.source_chain | ||
,cross_chain_trades.destination_chain | ||
FROM cross_chain_trades | ||
LEFT JOIN {{ ref('tokens_erc20') }} erc20a | ||
ON erc20a.contract_address = cross_chain_trades.token_bought_address | ||
LEFT JOIN {{ ref('tokens_erc20') }} erc20b | ||
ON erc20b.contract_address = cross_chain_trades.token_sold_address | ||
AND erc20b.blockchain = cross_chain_trades.source_chain | ||
LEFT JOIN {{ source('prices', 'usd') }} p_bought | ||
ON p_bought.minute = date_trunc('minute', cross_chain_trades.block_time) | ||
AND p_bought.contract_address = cross_chain_trades.token_bought_address | ||
AND p_bought.blockchain = 'bnb' | ||
{% if is_incremental() %} | ||
AND p_bought.minute >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
LEFT JOIN {{ source('prices', 'usd') }} p_sold | ||
ON p_sold.minute = date_trunc('minute', cross_chain_trades.block_time) | ||
AND p_sold.contract_address = cross_chain_trades.token_sold_address | ||
AND p_sold.blockchain = 'bnb' | ||
{% if is_incremental() %} | ||
AND p_sold.minute >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.