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

Refactor amount_usd calculation in lifi.trades #6229

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions dbt_subprojects/dex/models/_projects/lifi/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,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
4 changes: 0 additions & 4 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 @@ -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
Expand Up @@ -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 %}
113 changes: 73 additions & 40 deletions dbt_subprojects/dex/models/_projects/lifi/lifi_trades.sql
Original file line number Diff line number Diff line change
@@ -1,50 +1,83 @@
{{ 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"]\') }}'
)
}}

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

WITH basic_trades AS (
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 %}
)
)
, enrichments_with_prices AS (
{{
add_amount_usd(
trades_cte = 'basic_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 %}
)
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,
evt_index
FROM enrichments_with_prices
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 @@ -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
Expand Up @@ -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'
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 %}
Loading