Skip to content

Commit

Permalink
Hashflow cross chain trades (#3397)
Browse files Browse the repository at this point in the history
* 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
3 people authored Jun 21, 2023
1 parent cd1fd20 commit d907f7b
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 17 deletions.
104 changes: 104 additions & 0 deletions models/hashflow/avalanche_c/hashflow_avalanche_c_crosschain_trades.sql
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 %}

41 changes: 40 additions & 1 deletion models/hashflow/avalanche_c/hashflow_avalanche_c_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,43 @@ models:
description: ""
- &evt_index
name: evt_index
description: ""
description: ""

- name: hashflow_avalanche_c_crosschain_trades
meta:
blockchain: avalanche_c
sector: dex
project: hashflow
contributors: BroderickBonelli
config:
tags: ['avalanche_c','hashflow','trades','dex','BroderickBonelli']
description: >
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.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- source_chain
- tx_hash
columns:
- *block_date
- *block_time
- *token_bought_symbol
- *token_sold_symbol
- *token_bought_amount
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *amount_usd
- *token_bought_address
- *token_sold_address
- &trader
name: trader
description: "Address of trader who executed the trade."
- *tx_hash
- &source_chain
name: source_chain
description: "Chain where the trade originated."
- &destination_chain
name: destination_chain
description: "Ending chain where swapped token balance will be received."
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sources:

- name: Pool_evt_LzTrade
loaded_at_field: evt_block_time
description: "Pool lazy trades events table"
description: "Pool layer zero trades events table"
columns:
- *evt_block_time
- *evt_index
Expand Down
105 changes: 105 additions & 0 deletions models/hashflow/bnb/hashflow_bnb_crosschain_trades.sql
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 %}

41 changes: 40 additions & 1 deletion models/hashflow/bnb/hashflow_bnb_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,43 @@ models:
description: ""
- &evt_index
name: evt_index
description: ""
description: ""

- name: hashflow_bnb_crosschain_trades
meta:
blockchain: bnb
sector: dex
project: hashflow
contributors: BroderickBonelli
config:
tags: ['bnb','hashflow','trades','dex','BroderickBonelli']
description: >
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.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- source_chain
- tx_hash
columns:
- *block_date
- *block_time
- *token_bought_symbol
- *token_sold_symbol
- *token_bought_amount
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *amount_usd
- *token_bought_address
- *token_sold_address
- &trader
name: trader
description: "Address of trader who executed the trade."
- *tx_hash
- &source_chain
name: source_chain
description: "Chain where the trade originated."
- &destination_chain
name: destination_chain
description: "Ending chain where swapped token balance will be received."
2 changes: 1 addition & 1 deletion models/hashflow/bnb/hashflow_bnb_sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ sources:

- name: Pool_evt_LzTrade
loaded_at_field: evt_block_time
description: "Pool lazy trades events table"
description: "Pool layer zero trade events table"
columns:
- *evt_block_time
- *evt_index
Expand Down
Loading

0 comments on commit d907f7b

Please sign in to comment.