Skip to content

Commit

Permalink
add erc4626 token pricing to balancer.trades (#7301)
Browse files Browse the repository at this point in the history
* add erc4626 token pricing to balancer.trades

* fix copypasta

* add missing comma

* remove extra comma

* remove extra comma

* fix erc4626_token_prices

* fix group by

* fix wrong reference
  • Loading branch information
viniabussafi authored Dec 12, 2024
1 parent fbbdd8c commit ddb63e1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ WITH
AND bpt_prices.day <= DATE_TRUNC('day', dexs.block_time)
AND bpt_prices.blockchain = 'ethereum'
GROUP BY 1, 2, 3, 4, 5
),

erc4626_prices AS(
SELECT
minute,
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') }}
WHERE blockchain = 'ethereum'
GROUP BY 1, 2, 3
)

SELECT
Expand All @@ -110,8 +122,8 @@ SELECT
dexs.token_sold_amount_raw,
COALESCE(
dexs.amount_usd,
dexs.token_bought_amount_raw / POWER(10, COALESCE(erc20a.decimals, 18)) * bpa_bpt_prices.bpt_price,
dexs.token_sold_amount_raw / POWER(10, COALESCE(erc20b.decimals, 18)) * bpb_bpt_prices.bpt_price
dexs.token_bought_amount_raw / POWER(10, COALESCE(erc20a.decimals, erc4626a.decimals, 18)) * COALESCE(bpa_bpt_prices.bpt_price, erc4626a.price),
dexs.token_sold_amount_raw / POWER(10, COALESCE(erc20b.decimals, erc4626b.decimals, 18)) * COALESCE(bpb_bpt_prices.bpt_price, erc4626b.price)
) AS amount_usd,
dexs.token_bought_address,
dexs.token_sold_address,
Expand Down Expand Up @@ -148,4 +160,12 @@ FROM dexs
LEFT JOIN {{ source('balancer', 'bpt_prices') }} bpb_bpt_prices
ON bpb_bpt_prices.contract_address = bpb.contract_address
AND bpb_bpt_prices.day = bpb.bpb_max_block_date
AND bpb_bpt_prices.blockchain = 'ethereum'
AND bpb_bpt_prices.blockchain = 'ethereum'
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ WITH
AND bpt_prices.day <= DATE_TRUNC('day', dexs.block_time)
AND bpt_prices.blockchain = 'gnosis'
GROUP BY 1, 2, 3, 4, 5
),

erc4626_prices AS(
SELECT
minute,
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') }}
WHERE blockchain = 'gnosis'
GROUP BY 1, 2, 3
)

SELECT
Expand All @@ -110,8 +122,8 @@ SELECT
dexs.token_sold_amount_raw,
COALESCE(
dexs.amount_usd,
dexs.token_bought_amount_raw / POWER(10, COALESCE(erc20a.decimals, 18)) * bpa_bpt_prices.bpt_price,
dexs.token_sold_amount_raw / POWER(10, COALESCE(erc20b.decimals, 18)) * bpb_bpt_prices.bpt_price
dexs.token_bought_amount_raw / POWER(10, COALESCE(erc20a.decimals, erc4626a.decimals, 18)) * COALESCE(bpa_bpt_prices.bpt_price, erc4626a.price),
dexs.token_sold_amount_raw / POWER(10, COALESCE(erc20b.decimals, erc4626b.decimals, 18)) * COALESCE(bpb_bpt_prices.bpt_price, erc4626b.price)
) AS amount_usd,
dexs.token_bought_address,
dexs.token_sold_address,
Expand Down Expand Up @@ -148,4 +160,12 @@ FROM dexs
LEFT JOIN {{ source('balancer', 'bpt_prices') }} bpb_bpt_prices
ON bpb_bpt_prices.contract_address = bpb.contract_address
AND bpb_bpt_prices.day = bpb.bpb_max_block_date
AND bpb_bpt_prices.blockchain = 'gnosis'
AND bpb_bpt_prices.blockchain = 'gnosis'
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
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
3 changes: 3 additions & 0 deletions sources/_subprojects_outputs/spellbook/_sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ sources:
- name: balancer_v3_gnosis
tables:
- name: bpt_prices
- name: balancer_v3
tables:
- name: erc4626_token_prices

- name: addresses_ethereum
tables:
Expand Down

0 comments on commit ddb63e1

Please sign in to comment.