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

enrich balancer_v3 trades on dex.trades #7350

Merged
merged 9 commits into from
Dec 20, 2024
Merged
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
122 changes: 122 additions & 0 deletions dbt_subprojects/dex/macros/models/enrich_balancer_v3_dex_trades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{% macro enrich_balancer_v3_dex_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
, 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.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'
)
}}
)

, erc4626_prices AS(
SELECT
minute,
blockchain,
wrapped_token,
decimals,
APPROX_PERCENTILE(median_price, 0.5) AS price,
LEAD(minute, 1, NOW()) OVER (PARTITION BY wrapped_token ORDER BY minute) AS time_of_next_change
FROM {{ source('balancer_v3', 'erc4626_token_prices') }}
GROUP BY 1, 2, 3, 4
)

SELECT
dexs.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
, COALESCE(
dexs.amount_usd,
dexs.token_bought_amount * erc4626a.price,
dexs.token_sold_amount * erc4626a.price
) AS amount_usd
, token_bought_address
, token_sold_address
, taker
, maker
, project_contract_address
, tx_hash
, tx_from
, tx_to
, evt_index
FROM
enrichments_with_prices dexs
LEFT JOIN erc4626_prices erc4626a
ON erc4626a.wrapped_token = dexs.token_bought_address
AND erc4626a.minute <= dexs.block_time
AND dexs.block_time < erc4626a.time_of_next_change
AND dexs.blockchain = erc4626a.blockchain
LEFT JOIN erc4626_prices erc4626b
ON erc4626b.wrapped_token = dexs.token_sold_address
AND erc4626b.minute <= dexs.block_time
AND dexs.block_time < erc4626b.time_of_next_change
AND dexs.blockchain = erc4626b.blockchain

{% endmacro %}
2 changes: 1 addition & 1 deletion dbt_subprojects/dex/models/trades/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ models:
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, kaia, optimism, polygon, scroll, zksync, linea, blast, sei, ronin
sector: dex
short_description: The `dex.trades` table captures detailed data on trades executed via decentralized exchanges (DEXs). This table contains a detailed breakdown of trade execution containing one or many trades per transaction.
contributors: 0xRob, hosuke, jeff-dude, tomfutago
contributors: 0xRob, hosuke, jeff-dude, tomfutago, viniabussafi
config:
tags: [ 'dex', 'trades']
description: '{{ doc("dex_trades_doc") }}'
Expand Down
17 changes: 14 additions & 3 deletions dbt_subprojects/dex/models/trades/dex_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
]\',
"sector",
"dex",
\'["hosuke", "0xrob", "jeff-dude", "tomfutago"]\') }}')
\'["hosuke", "0xrob", "jeff-dude", "tomfutago", "viniabussafi"]\') }}')
}}

-- keep existing dbt lineages for the following projects, as the team built themselves and use the spells throughout the entire lineage
Expand All @@ -46,18 +46,28 @@ WITH curve AS (
{{
enrich_curve_dex_trades(
base_trades = ref('dex_base_trades')
, filter = "project = 'curve'"
, filter = "project = 'curve' AND block_date = TIMESTAMP '2024-12-17'"
, curve_ethereum = ref('curve_ethereum_base_trades')
, curve_optimism = ref('curve_optimism_base_trades')
, tokens_erc20_model = source('tokens', 'erc20')
)
}}
)
, balancer_v3 AS (
-- due to Balancer V3 having trades between ERC4626 tokens, which won't be priced on prices.usd, enrich separately
{{
enrich_balancer_v3_dex_trades(
base_trades = ref('dex_base_trades')
, filter = "(project = 'balancer' AND version = '3') AND block_date = TIMESTAMP '2024-12-17'"
, tokens_erc20_model = source('tokens', 'erc20')
)
}}
)
, dexs AS (
{{
enrich_dex_trades(
base_trades = ref('dex_base_trades')
, filter = "project != 'curve'"
, filter = "project != 'curve' AND NOT (project = 'balancer' AND version = '3') AND block_date = TIMESTAMP '2024-12-17'"
, tokens_erc20_model = source('tokens', 'erc20')
)
}}
Expand Down Expand Up @@ -106,6 +116,7 @@ WITH curve AS (
'curve'
, 'as_is_dexs'
, 'dexs'
, 'balancer_v3'
]
%}

Expand Down
Loading