Skip to content

Commit

Permalink
Introduce dex_aggregator_base_trades (#6653)
Browse files Browse the repository at this point in the history
* Introduce dex_aggregator_base_trades

* Fix typo

* Refactor lifi_trades

* Add lifi to base_trades

* Fix config blocks

* Fix typo

* Fix typo

* Apply suggestions from code review

Co-authored-by: jeff-dude <[email protected]>

* Draft enrich_dex_aggregator_trades macro

* Remove 5 columns

* Remove 5 columns

* Recover add_tx_columns

---------

Co-authored-by: jeff-dude <[email protected]>
  • Loading branch information
Hosuke and jeff-dude authored Sep 16, 2024
1 parent ad8c48e commit 9bf7433
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 224 deletions.
99 changes: 99 additions & 0 deletions dbt_subprojects/dex/macros/models/enrich_dex_aggregator_trades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{% macro enrich_dex_aggregator_trades(
base_trades = null
, filter = null
, tokens_erc20_model = null
)
%}

WITH base_trades as (
SELECT
*
FROM
{{ base_trades }}
WHERE
{{ filter }}
{% if is_incremental() %}
AND
{{ incremental_predicate('block_time') }}
{% endif %}
)

, enrichments AS (
SELECT
base_trades.blockchain
, base_trades.project
, base_trades.version
, base_trades.block_month
, base_trades.block_date
, base_trades.block_time
-- , base_trades.block_number -- temporary missing
, erc20_bought.symbol AS token_bought_symbol
, erc20_sold.symbol AS token_sold_symbol
, case
when lower(erc20_bought.symbol) > lower(erc20_sold.symbol) then concat(erc20_sold.symbol, '-', erc20_bought.symbol)
else concat(erc20_bought.symbol, '-', erc20_sold.symbol)
end AS token_pair
, base_trades.token_bought_amount_raw / power(10, erc20_bought.decimals) AS token_bought_amount
, base_trades.token_sold_amount_raw / power(10, erc20_sold.decimals) AS token_sold_amount
, base_trades.token_bought_amount_raw
, base_trades.token_sold_amount_raw
, base_trades.token_bought_address
, base_trades.token_sold_address
, coalesce(base_trades.taker, base_trades.tx_from) AS taker
, base_trades.maker
, base_trades.project_contract_address
, base_trades.tx_hash
, base_trades.tx_from
, base_trades.tx_to
, base_trades.trace_address
, base_trades.evt_index
FROM
base_trades
LEFT JOIN
{{ tokens_erc20_model }} as erc20_bought
ON erc20_bought.contract_address = base_trades.token_bought_address
AND erc20_bought.blockchain = base_trades.blockchain
LEFT JOIN
{{ tokens_erc20_model }} as erc20_sold
ON erc20_sold.contract_address = base_trades.token_sold_address
AND erc20_sold.blockchain = base_trades.blockchain
)

, enrichments_with_prices AS (
{{
add_amount_usd(
trades_cte = 'enrichments'
)
}}
)

SELECT
blockchain
, project
, version
, block_month
, block_date
, block_time
-- , block_number
, 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

{% endmacro %}
32 changes: 31 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,34 @@ 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_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
26 changes: 1 addition & 25 deletions dbt_subprojects/dex/models/_projects/lifi/fantom/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,12 @@ models:
- &block_time
name: block_time
description: "UTC event block time of each DEX trade"
- &token_bought_symbol
name: token_bought_symbol
description: "Token symbol for token bought in the transaction"
- &token_sold_symbol
name: token_sold_symbol
description: "Token symbol for token sold in the transaction"
- &token_pair
name: token_pair
description: "Token symbol pair for each token involved in the transaction"
- &token_bought_amount
name: token_bought_amount
description: "Value of the token bought at time of execution in the original currency"
- &token_sold_amount
name: token_sold_amount
description: "Value of the token sold at time of execution in the original currency"
- &token_bought_amount_raw
name: token_bought_amount_raw
description: "Raw value of the token bought at time of execution in the original currency"
- &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 +81,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 @@ -115,14 +97,8 @@ models:
- *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
- *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 @@ -20,14 +20,8 @@ FROM (
block_date,
block_month,
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,8 @@ SELECT
cast(date_trunc('DAY', dexs.block_time) as date) as block_date,
cast(date_trunc('MONTH', dexs.block_time) as date) as block_month,
dexs.block_time,
erc20a.symbol as token_bought_symbol,
erc20b.symbol as token_sold_symbol,
case
when lower(erc20a.symbol) > lower(erc20b.symbol) then concat(erc20b.symbol, '-', erc20a.symbol)
else concat(erc20a.symbol, '-', erc20b.symbol)
end as token_pair,
dexs.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount,
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 @@ -95,29 +82,3 @@ inner join {{ source('fantom', 'transactions') }} tx
{% if is_incremental() %}
and tx.block_time >= date_trunc('day', now() - interval '7' Day)
{% endif %}
left join {{ source('tokens', 'erc20') }} erc20a
on erc20a.contract_address = dexs.token_bought_address
and erc20a.blockchain = 'fantom'
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 %}
40 changes: 40 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,40 @@
{{ config(
schema = 'lifi',
alias = 'base_trades'
)
}}

{% 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_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'
Loading

0 comments on commit 9bf7433

Please sign in to comment.