-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enrich balancer_v3 trades on dex.trades (#7350)
* enrich balancer_v3 trades * add missing ) * add missing quote * fix group by * again * alias blockchain * add date filter to decrease run time * add balancer trades to models * include bv2
- Loading branch information
1 parent
444fd27
commit c77a2bc
Showing
3 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
dbt_subprojects/dex/macros/models/enrich_balancer_v3_dex_trades.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters