Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce dex_aggregator_base_trades #6653

Merged
merged 16 commits into from
Sep 16, 2024
37 changes: 36 additions & 1 deletion dbt_subprojects/dex/models/_projects/lifi/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,39 @@ models:
description: "the position of this event log within this transaction"
- &block_month
name: block_month
description: "UTC event block month of each DEX trade"
description: "UTC event block month of each DEX trade"

- name: lifi_base_trades
meta:
blockchain: fantom, optimism
sector: dex
project: lifi
contributors: Henrystats, hosuke
config:
tags: [ 'fantom', 'optimism', 'lifi','trades', 'dex', 'aggregator', 'Henrystats','cross-chain' ]
description: >
lifi aggregator base trades on all chains across all contracts and versions. This table will load dex_aggregator trades downstream.
columns:
- *blockchain
- *project
- *version
- *block_date
- *block_time
- *token_bought_symbol
- *token_sold_symbol
- *token_pair
- *token_bought_amount
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *token_bought_address
- *token_sold_address
- *taker
- *maker
- *project_contract_address
- *tx_hash
- *tx_from
- *tx_to
- *trace_address
- *evt_index
- *block_month
6 changes: 1 addition & 5 deletions dbt_subprojects/dex/models/_projects/lifi/fantom/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ models:
- &token_sold_amount_raw
name: token_sold_amount_raw
description: "Raw value of the token sold at time of execution in the original currency"
- &amount_usd
name: amount_usd
description: "USD value of the trade at time of execution"
- &token_bought_address
name: token_bought_address
description: "Contract address of the token bought"
Expand Down Expand Up @@ -99,7 +96,7 @@ models:
name: block_month
description: "UTC event block month of each DEX trade"

- name: lifi_fantom_trades
- name: lifi_fantom_base_trades
meta:
blockchain: fantom
sector: dex
Expand All @@ -122,7 +119,6 @@ models:
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *amount_usd
- *token_bought_address
- *token_sold_address
- *taker
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{{ config(
schema = 'lifi_fantom',
alias = 'trades'
alias = 'base_trades'
)
}}

Expand All @@ -27,7 +27,6 @@ FROM (
token_sold_amount,
token_bought_amount_raw,
token_sold_amount_raw,
amount_usd,
token_bought_address,
token_sold_address,
taker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ SELECT
dexs.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount,
dexs.token_bought_amount_raw,
dexs.token_sold_amount_raw,
coalesce(
dexs.amount_usd
,(dexs.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price
,(dexs.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price
) AS amount_usd,
dexs.token_bought_address,
dexs.token_sold_address,
tx."from" AS taker, -- no taker in swap event
Expand All @@ -101,23 +96,3 @@ left join {{ source('tokens', 'erc20') }} erc20a
left join {{ source('tokens', 'erc20') }} erc20b
on erc20b.contract_address = dexs.token_sold_address
and erc20b.blockchain = 'fantom'
left join {{ source('prices', 'usd') }} p_bought
on p_bought.minute = date_trunc('minute', dexs.block_time)
and p_bought.contract_address = dexs.token_bought_address
and p_bought.blockchain = 'fantom'
{% if not is_incremental() %}
and p_bought.minute >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
and p_bought.minute >= date_trunc('day', now() - interval '7' Day)
{% endif %}
left join {{ source('prices', 'usd') }} p_sold
on p_sold.minute = date_trunc('minute', dexs.block_time)
and p_sold.contract_address = dexs.token_sold_address
and p_sold.blockchain = 'fantom'
{% if not is_incremental() %}
and p_sold.minute >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
and p_sold.minute >= date_trunc('day', now() - interval '7' Day)
{% endif %}
49 changes: 49 additions & 0 deletions dbt_subprojects/dex/models/_projects/lifi/lifi_base_trades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{{ config(
schema = 'lifi',
alias = 'base_trades',
post_hook='{{ expose_spells(blockchains = \'["fantom", "optimism"]\',
spell_type = "project",
spell_name = "lifi",
contributors = \'["Henrystats", "hosuke"]\') }}'
Hosuke marked this conversation as resolved.
Show resolved Hide resolved
)
}}

{% set lifi_models = [
ref('lifi_fantom_base_trades')
, ref('lifi_optimism_base_trades')
] %}


SELECT *
FROM (
{% for dex_model in lifi_models %}
SELECT
blockchain,
project,
version,
block_month,
block_date,
block_time,
token_bought_symbol,
token_sold_symbol,
token_pair,
token_bought_amount,
token_sold_amount,
token_bought_amount_raw,
token_sold_amount_raw,
token_bought_address,
token_sold_address,
taker,
maker,
project_contract_address,
tx_hash,
tx_from,
tx_to,
trace_address, --ensure field is explicitly cast as array<bigint> in base models
evt_index
FROM {{ dex_model }}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
)
49 changes: 6 additions & 43 deletions dbt_subprojects/dex/models/_projects/lifi/lifi_trades.sql
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
{{ config(
schema = 'lifi',
alias = 'trades',
post_hook='{{ expose_spells(\'["fantom", "optimism"]\',
"project",
"lifi",
\'["Henrystats"]\') }}'
post_hook='{{ expose_spells(blockchains = \'["fantom", "optimism"]\',
spell_type = "project",
spell_name = "lifi",
contributors = \'["Henrystats", "hosuke"]\') }}'
)
}}

{% set lifi_models = [
ref('lifi_fantom_trades')
,ref('lifi_optimism_trades')
] %}


SELECT *
FROM (
{% for dex_model in lifi_models %}
SELECT
blockchain,
project,
version,
block_month,
block_date,
block_time,
token_bought_symbol,
token_sold_symbol,
token_pair,
token_bought_amount,
token_sold_amount,
token_bought_amount_raw,
token_sold_amount_raw,
amount_usd,
token_bought_address,
token_sold_address,
taker,
maker,
project_contract_address,
tx_hash,
tx_from,
tx_to,
trace_address, --ensure field is explicitly cast as array<bigint> in base models
evt_index
FROM {{ dex_model }}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
)
FROM {{ ref('dex_aggregator_trades') }}
WHERE project = 'lifi'
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ models:
- &token_sold_amount_raw
name: token_sold_amount_raw
description: "Raw value of the token sold at time of execution in the original currency"
- &amount_usd
name: amount_usd
description: "USD value of the trade at time of execution"
- &token_bought_address
name: token_bought_address
description: "Contract address of the token bought"
Expand Down Expand Up @@ -99,7 +96,7 @@ models:
name: block_month
description: "UTC event block month of each DEX trade"

- name: lifi_optimism_trades
- name: lifi_optimism_base_trades
meta:
blockchain: optimism
sector: dex
Expand All @@ -122,7 +119,6 @@ models:
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *amount_usd
- *token_bought_address
- *token_sold_address
- *taker
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{{ config(
schema = 'lifi_optimism',
alias = 'trades'
alias = 'base_trades'
)
}}

Expand All @@ -27,7 +27,6 @@ FROM (
token_sold_amount,
token_bought_amount_raw,
token_sold_amount_raw,
amount_usd,
token_bought_address,
token_sold_address,
taker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ SELECT
dexs.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount,
dexs.token_bought_amount_raw,
dexs.token_sold_amount_raw,
coalesce(
dexs.amount_usd
,(dexs.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price
,(dexs.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price
) AS amount_usd,
dexs.token_bought_address,
dexs.token_sold_address,
tx."from" AS taker, -- no taker in swap event
Expand All @@ -101,23 +96,3 @@ left join {{ source('tokens', 'erc20') }} erc20a
left join {{ source('tokens', 'erc20') }} erc20b
on erc20b.contract_address = dexs.token_sold_address
and erc20b.blockchain = 'optimism'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
left join {{ source('tokens', 'erc20') }} erc20b
on erc20b.contract_address = dexs.token_sold_address
and erc20b.blockchain = 'optimism'
left join {{ source('tokens', 'erc20') }} erc20b
on erc20b.contract_address = dexs.token_sold_address
and erc20b.blockchain = 'optimism'

while we're here, should we remove erc20 metadata too, and apply at the end with prices? similar to dex?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I drafted a similar enrichment macro in this PR.

left join {{ source('prices', 'usd') }} p_bought
on p_bought.minute = date_trunc('minute', dexs.block_time)
and p_bought.contract_address = dexs.token_bought_address
and p_bought.blockchain = 'optimism'
{% if not is_incremental() %}
and p_bought.minute >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
and p_bought.minute >= date_trunc('day', now() - interval '7' Day)
{% endif %}
left join {{ source('prices', 'usd') }} p_sold
on p_sold.minute = date_trunc('minute', dexs.block_time)
and p_sold.contract_address = dexs.token_sold_address
and p_sold.blockchain = 'optimism'
{% if not is_incremental() %}
and p_sold.minute >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
and p_sold.minute >= date_trunc('day', now() - interval '7' Day)
{% endif %}
45 changes: 44 additions & 1 deletion dbt_subprojects/dex/models/aggregator_trades/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,47 @@ models:
name: trace_address
- &evt_index
name: evt_index
description: "Index of the corresponding trade event"
description: "Index of the corresponding trade event"

- name: dex_aggregator_base_trades
meta:
blockchain: ethereum, gnosis, avalanche_c, fantom, optimism, arbitrum, bnb
sector: dex_aggregator
contributors: hosuke
config:
tags: ['ethereum', 'gnosis', 'avalanche_c', 'fantom', 'aggregator', 'dex', 'trades', 'cross-chain']
description: >
Base trades data without amount_usd column
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- blockchain
- project
- version
- tx_hash
- evt_index
- trace_address
columns:
- *blockchain
- *project
- *version
- *block_date
- *block_time
- *token_bought_symbol
- *token_sold_symbol
- *token_pair
- *token_bought_amount
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *token_bought_address
- *token_sold_address
- *taker
- *maker
- *project_contract_address
- *tx_hash
- *tx_from
- *tx_to
- *trace_address
- *evt_index
Loading
Loading