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

DuneSQL Migration: balancer_trades #4097

Merged
merged 134 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 132 commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
1281424
Update balancer_arbitrum_schema.yml
thetroyharris Aug 9, 2023
5daed9d
Update balancer_v2_arbitrum_transfers_bpt.sql
thetroyharris Aug 9, 2023
68e4504
Update balancer_v2_arbitrum_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
662d5fa
Create balancer_v2_avalanche_c_transfers_bpt.sql
thetroyharris Aug 9, 2023
e55a48c
Create balancer_v2_avalanche_c_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
8c0f175
Update balancer_avalanche_c_schema.yml
thetroyharris Aug 9, 2023
8a39942
Update balancer_schema.yml
thetroyharris Aug 9, 2023
50f594b
Create balancer_transfers_bpt.sql
thetroyharris Aug 9, 2023
c985d22
Create balancer_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
41c1ac4
Create balancer_base_schema.yml
thetroyharris Aug 9, 2023
a3fd2b2
Create balancer_base_sources.yml
thetroyharris Aug 9, 2023
8c2c811
Create balancer_v2_base_transfers_bpt.sql
thetroyharris Aug 9, 2023
a4c247f
Create balancer_v2_base_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
bea7a4f
Update balancer_ethereum_schema.yml
thetroyharris Aug 9, 2023
88c1d6a
Update balancer_v2_ethereum_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
b5571f3
Update balancer_v2_ethereum_transfers_bpt.sql
thetroyharris Aug 9, 2023
8ff5397
Update balancer_gnosis_schema.yml
thetroyharris Aug 9, 2023
8eccb02
Create balancer_v2_gnosis_transfers_bpt.sql
thetroyharris Aug 9, 2023
2de2c5c
Create balancer_v2_gnosis_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
41e701b
Update balancer_v2_gnosis_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
93c2ff8
Update balancer_optimism_schema.yml
thetroyharris Aug 9, 2023
08bf440
Update balancer_v2_optimism_transfers_bpt.sql
thetroyharris Aug 9, 2023
b623467
Update balancer_v2_optimism_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
4483600
Update balancer_polygon_schema.yml
thetroyharris Aug 9, 2023
f284a5f
Update balancer_v2_polygon_transfers_bpt.sql
thetroyharris Aug 9, 2023
07fae70
Update balancer_v2_polygon_transfers_bpt_legacy.sql
thetroyharris Aug 9, 2023
7d3a3bb
Merge branch 'main' into tt
thetroyharris Aug 9, 2023
f99cc8c
Merge branch 'main' into tt
thetroyharris Aug 10, 2023
fbf8458
Merge branch 'main' into tt
thetroyharris Aug 11, 2023
2a1ccce
Merge branch 'main' into tt
thetroyharris Aug 14, 2023
98bec8f
Merge branch 'duneanalytics:main' into tt
thetroyharris Aug 14, 2023
b30905c
Merge branch 'main' into tt
thetroyharris Aug 14, 2023
f233764
Migrate `pools_fees` to DuneSQL
thetroyharris Aug 14, 2023
8e82d44
Add migrated `pools_fees` model to Avalanche, Base, and create an agg…
thetroyharris Aug 14, 2023
83ca534
Migrate `balancer_v2_trades` to DuneSQL
thetroyharris Aug 15, 2023
b1e3362
Update balancer_v2_arbitrum_trades.sql
thetroyharris Aug 15, 2023
d6ff85a
Update balancer_v2_arbitrum_trades.sql
thetroyharris Aug 15, 2023
f41a607
Update balancer_ethereum_trades.sql
thetroyharris Aug 15, 2023
aae6872
Update balancer_v2_optimism_trades.sql
thetroyharris Aug 15, 2023
d037954
Update balancer_ethereum_trades.sql
thetroyharris Aug 15, 2023
c03026c
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 15, 2023
c8fea08
Revert "Update balancer_v2_ethereum_trades.sql"
thetroyharris Aug 15, 2023
0b2c7af
Revert "Update balancer_ethereum_trades.sql"
thetroyharris Aug 15, 2023
6403e40
Revert "Update balancer_v2_optimism_trades.sql"
thetroyharris Aug 15, 2023
ab0b52e
Revert "Update balancer_ethereum_trades.sql"
thetroyharris Aug 15, 2023
387971a
Revert "Update balancer_v2_arbitrum_trades.sql"
thetroyharris Aug 15, 2023
b8bd7de
Revert "Update balancer_v2_arbitrum_trades.sql"
thetroyharris Aug 15, 2023
7e0b7b1
Revert "Migrate `balancer_v2_trades` to DuneSQL"
thetroyharris Aug 15, 2023
e9a806b
Merge branch 'main' into tt
thetroyharris Aug 15, 2023
70e2317
Merge branch 'main' into tt
thetroyharris Aug 15, 2023
a07115f
Update balancer_v2_ethereum_bpt_prices.sql
thetroyharris Aug 15, 2023
d196ffe
Merge branch 'main' into tt
thetroyharris Aug 15, 2023
e48d380
Merge branch 'main' into tt
thetroyharris Aug 15, 2023
ee80419
Update balancer_v2_ethereum_bpt_prices.sql
thetroyharris Aug 15, 2023
79677b5
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 15, 2023
7190833
Merge branch 'main' into tt
thetroyharris Aug 15, 2023
cefb17f
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 15, 2023
6d6dd08
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 15, 2023
05425e4
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 15, 2023
dca07f5
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 15, 2023
d48961f
Requested changes.
thetroyharris Aug 15, 2023
9fab3a7
Merge branch 'tt' into xx
thetroyharris Aug 15, 2023
7c0ec0e
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 15, 2023
ff198fe
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 15, 2023
1ae4878
Update balancer_ethereum_schema.yml
thetroyharris Aug 15, 2023
671647a
Update balancer_v2_ethereum_trades_legacy.sql
thetroyharris Aug 15, 2023
525e935
Update balancer_v2_ethereum_trades_legacy.sql
thetroyharris Aug 15, 2023
40b2402
Removing the _legacy part
thetroyharris Aug 15, 2023
9ed3411
Revert "Removing the _legacy part"
thetroyharris Aug 15, 2023
8e47fab
Update balancer_ethereum_schema.yml
thetroyharris Aug 15, 2023
f8e4073
Merge branch 'main' into xx
thetroyharris Aug 16, 2023
b71200f
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 16, 2023
16597a0
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 16, 2023
3f56a76
Add migrated bpt_prices to arb, gno, poly, opt
thetroyharris Aug 16, 2023
b2c7659
Migrate `bpt_prices` on Avalanche and deploy on Base.
thetroyharris Aug 16, 2023
dc370b4
Update balancer_base_schema.yml
thetroyharris Aug 16, 2023
ca488dd
Update balancer_base_sources.yml
thetroyharris Aug 16, 2023
ac17e35
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 16, 2023
13e6aa6
Migrate balancer_v2_trades and deploy on avalanche_c and base.
thetroyharris Aug 16, 2023
00ca48a
Add in casting to keep the same data types as old lang.
thetroyharris Aug 16, 2023
ce9166e
Revert "Add in casting to keep the same data types as old lang."
thetroyharris Aug 16, 2023
4646957
Migrate Balancer Trades v1 to DuneSQL
thetroyharris Aug 16, 2023
9acb7d8
Update balancer_v1_ethereum_trades.sql
thetroyharris Aug 16, 2023
00df47c
Update balancer_v1_ethereum_trades.sql
thetroyharris Aug 16, 2023
87ae31e
Add DuneSQL tag to balancer_ethereum_trades aggregation spell.
thetroyharris Aug 16, 2023
5d1c14a
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 17, 2023
7672089
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 17, 2023
e7c7df5
Migrate balancer trades agg spells to DuneSQL and deploy on base and …
thetroyharris Aug 17, 2023
c1b9869
Update balancer_avalanche_c_schema.yml
thetroyharris Aug 17, 2023
7a4422d
Update balancer_base_schema.yml
thetroyharris Aug 17, 2023
fe5aa40
Update balancer_base_schema.yml
thetroyharris Aug 17, 2023
7b600dc
Update balancer_avalanche_c_trades.sql
thetroyharris Aug 17, 2023
165e652
Update balancer_avalanche_c_trades_legacy.sql
thetroyharris Aug 17, 2023
5cf076d
Add in `schema = 'balancer_base',`
thetroyharris Aug 17, 2023
eaca2af
Remove `_legacy` in schemas for each chains agg trades spell.
thetroyharris Aug 17, 2023
66cbd8d
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 17, 2023
2f55197
Update balancer_base_schema.yml
thetroyharris Aug 17, 2023
4b96d74
Add in `pool_id` and swap_fee` to balancer trades agg spell and schemas.
thetroyharris Aug 17, 2023
afc3d86
Update balancer_schema.yml
thetroyharris Aug 17, 2023
20ee18a
Update balancer_arbitrum_schema.yml
thetroyharris Aug 17, 2023
a6d9ac4
Update balancer_avalanche_c_schema.yml
thetroyharris Aug 17, 2023
91876ea
Update balancer_ethereum_schema.yml
thetroyharris Aug 17, 2023
55e185e
Update schemas
thetroyharris Aug 17, 2023
c4e47a6
Update balancer_base_schema.yml
thetroyharris Aug 17, 2023
d034822
Cast NULL values to appropriate data types.
thetroyharris Aug 17, 2023
a3eb8a2
Fix name.
thetroyharris Aug 17, 2023
1c7afc8
Update balancer_ethereum_schema.yml
thetroyharris Aug 17, 2023
abf625a
Hot fix
thetroyharris Aug 17, 2023
6f98721
Spark fixes
thetroyharris Aug 18, 2023
921cc7c
Update balancer_v1_ethereum_trades.sql
thetroyharris Aug 18, 2023
240c462
Update balancer_v1_ethereum_trades_legacy.sql
thetroyharris Aug 18, 2023
3ed1453
Update balancer_v1_ethereum_trades.sql
thetroyharris Aug 18, 2023
5c3f2a2
Update balancer_v2_ethereum_trades.sql
thetroyharris Aug 18, 2023
0e52383
Update balancer_v2_ethereum_trades_legacy.sql
thetroyharris Aug 18, 2023
b1ea103
FIx type issues.
thetroyharris Aug 18, 2023
1409e57
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 21, 2023
0cfdc01
Remove duplicate partition by.
thetroyharris Aug 21, 2023
1cd76cc
Update balancer_ethereum_schema.yml
thetroyharris Aug 21, 2023
6622d1e
Revert "Update balancer_ethereum_schema.yml"
thetroyharris Aug 21, 2023
3486413
Check the combination of unique columns.
thetroyharris Aug 21, 2023
ec340a6
Fix casting issue.
thetroyharris Aug 21, 2023
3ccef3a
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 21, 2023
3942235
Change casting.
thetroyharris Aug 21, 2023
5d54292
Merge branch 'migration-balancer-trades' of https://github.com/thetro…
thetroyharris Aug 21, 2023
ad350cd
Update balancer_v1_ethereum_trades_legacy.sql
thetroyharris Aug 21, 2023
3509be4
Update balancer_v1_ethereum_trades_legacy.sql
thetroyharris Aug 21, 2023
b1075f9
Update balancer_v1_ethereum_trades_legacy.sql
thetroyharris Aug 21, 2023
9e89f47
Fix mistake.
thetroyharris Aug 21, 2023
a97e0dc
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 22, 2023
b0b2c97
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 22, 2023
8e96a32
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 22, 2023
d5363df
Requested changes.
thetroyharris Aug 22, 2023
b0db0a4
Update balancer_v1_ethereum_trades.sql
thetroyharris Aug 22, 2023
d5f6a5b
Merge branch 'main' into migration-balancer-trades
thetroyharris Aug 22, 2023
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
11 changes: 8 additions & 3 deletions models/balancer/arbitrum/balancer_arbitrum_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ models:
- tx_hash
- evt_index
- trace_address
- check_dex_seed_legacy:
- check_dex_seed:
blockchain: arbitrum
project: balancer
version: 2
Expand Down Expand Up @@ -212,6 +212,10 @@ models:
- &maker
name: maker
description: "Address of trader who sold a token"
- *pool_id
- &swap_fee
name: swap_fee
description: 'Swap fee'
- &project_contract_address
name: project_contract_address
description: "Pool address"
Expand Down Expand Up @@ -337,7 +341,7 @@ models:
- tx_hash
- evt_index
- trace_address
- check_dex_seed_legacy:
- check_dex_seed:
blockchain: arbitrum
project: balancer
version: 2
Expand All @@ -346,6 +350,7 @@ models:
- name: project
- name: version
- name: block_date
- name: block_month
- name: block_time
- name: token_bought_symbol
- name: token_sold_symbol
Expand All @@ -359,9 +364,9 @@ models:
- name: token_sold_address
- name: taker
- name: maker
- name: project_contract_address
- name: pool_id
- name: swap_fee
- name: project_contract_address
- name: tx_hash
- name: tx_from
- name: tx_to
Expand Down
3 changes: 3 additions & 0 deletions models/balancer/arbitrum/balancer_arbitrum_trades.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{ config(
tags = ['dunesql'],
alias = alias('trades'),
post_hook='{{ expose_spells(\'["arbitrum"]\',
"project",
Expand Down Expand Up @@ -32,6 +33,8 @@ FROM (
token_sold_address,
taker,
maker,
pool_id,
swap_fee,
project_contract_address,
tx_hash,
tx_from,
Expand Down
2 changes: 2 additions & 0 deletions models/balancer/arbitrum/balancer_arbitrum_trades_legacy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ FROM (
token_sold_address,
taker,
maker,
pool_id,
swap_fee,
project_contract_address,
tx_hash,
tx_from,
Expand Down
56 changes: 29 additions & 27 deletions models/balancer/arbitrum/balancer_v2_arbitrum_bpt_prices.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ WITH
WHERE tokenIn = bytearray_substring(poolId, 1, 20) OR tokenOut = bytearray_substring(poolId, 1, 20)
{% if is_incremental() %}
AND v.evt_block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}
),
{% endif %}
),

all_trades_info AS (
SELECT
SELECT
a.evt_tx_hash AS tx_hash,
a.evt_block_time AS block_time,
a.evt_block_number AS block_number,
Expand All @@ -41,23 +41,25 @@ WITH
COALESCE(p2.symbol, t2.symbol) AS token_out_sym,
COALESCE(p2.decimals, t2.decimals) AS token_out_decimals
FROM bpt_trades a
LEFT JOIN {{ source ('prices', 'usd') }} p1 ON p1.contract_address = a.tokenIn AND p1.blockchain = 'arbitrum'
LEFT JOIN {{ source ('prices', 'usd') }} p1 ON p1.contract_address = a.tokenIn AND p1.blockchain = 'arbitrum'
AND p1.minute = date_trunc('minute', a.evt_block_time)
{% if is_incremental() %}
AND p1.minute >= date_trunc('day', now() - interval '7' day)
{% endif %}
{% endif %}

LEFT JOIN {{ source ('prices', 'usd') }} p2 ON p2.contract_address = a.tokenOut AND p2.blockchain = 'arbitrum'
AND p2.minute = date_trunc('minute', a.evt_block_time)
{% if is_incremental() %}
AND p2.minute >= date_trunc('day', now() - interval '7' day)
{% endif %}
{% endif %}

LEFT JOIN {{ ref ('tokens_erc20') }} t1 ON t1.contract_address = a.tokenIn AND t1.blockchain = 'arbitrum'
LEFT JOIN {{ ref ('tokens_erc20') }} t2 ON t2.contract_address = a.tokenOut AND t2.blockchain = 'arbitrum'
ORDER BY a.evt_block_number DESC, a.evt_index DESC
),
),

all_trades_calc_2 AS (
SELECT *,
SELECT *,
amount_in / POWER(10, COALESCE(token_in_decimals, 18)) AS amount_in_norm,
amount_out / POWER(10, COALESCE(token_out_decimals, 18)) AS amount_out_norm,
(amount_in / POWER(10, COALESCE(token_in_decimals, 18))) / (amount_out / POWER(10, COALESCE(token_out_decimals, 18))) AS in_out_norm_rate,
Expand All @@ -74,27 +76,27 @@ WITH
ELSE COALESCE(
token_out_p,
(amount_in / POWER(10, COALESCE(token_in_decimals, 18))) / (amount_out / POWER(10, COALESCE(token_out_decimals, 18))) * token_in_p
)
)
END AS token_out_price
FROM all_trades_info
),
),

unique_tx_token_price AS (
SELECT
distinct
tx_hash,
token,
AVG(token_price) OVER(PARTITION BY tx_hash, token) AS avg_price
SELECT
distinct
tx_hash,
token,
AVG(token_price) OVER(PARTITION BY tx_hash, token) AS avg_price
FROM (
SELECT tx_hash, token_in AS token, token_in_price AS token_price
FROM all_trades_calc_2
UNION ALL
SELECT tx_hash, token_out AS token, token_out_price AS token_price
FROM all_trades_calc_2
)
)
ORDER BY 1,2
),

backfill_pricing_1 AS (
SELECT
c2.block_time,
Expand All @@ -109,8 +111,8 @@ WITH
FROM all_trades_calc_2 c2
LEFT JOIN unique_tx_token_price u1 ON u1.tx_hash = c2.tx_hash AND u1.token = c2.token_in
LEFT JOIN unique_tx_token_price u2 ON u2.tx_hash = c2.tx_hash AND u2.token = c2.token_out
),
),

backfill_pricing_2 AS (
SELECT
block_time,
Expand All @@ -132,10 +134,10 @@ WITH
contract_address,
approx_percentile(price, 0.5) FILTER (WHERE is_finite(price)) AS median_price
FROM (
SELECT block_time, contract_address, token_in_price AS price
SELECT block_time, contract_address, token_in_price AS price
FROM backfill_pricing_2 b2 WHERE b2.contract_address = b2.token_in
UNION
SELECT block_time, contract_address, token_out_price AS price
SELECT block_time, contract_address, token_out_price AS price
FROM backfill_pricing_2 b2 WHERE b2.contract_address = b2.token_out
)
GROUP BY 1, 2, 3
Expand All @@ -146,11 +148,11 @@ SELECT
blockchain,
hour,
contract_address,
CASE
CASE
WHEN median_price IS NOT NULL THEN median_price
WHEN LEAD(median_price) OVER(PARTITION BY contract_address ORDER BY hour DESC) IS NOT NULL
WHEN LEAD(median_price) OVER(PARTITION BY contract_address ORDER BY hour DESC) IS NOT NULL
THEN LEAD(median_price) OVER(PARTITION BY contract_address ORDER BY hour DESC)
WHEN LAG(median_price) OVER(PARTITION BY contract_address ORDER BY hour DESC) IS NOT NULL
WHEN LAG(median_price) OVER(PARTITION BY contract_address ORDER BY hour DESC) IS NOT NULL
THEN LAG(median_price) OVER(PARTITION BY contract_address ORDER BY hour DESC)
ELSE approx_percentile(median_price, 0.5) OVER(
PARTITION BY contract_address ORDER BY hour
Expand Down
3 changes: 0 additions & 3 deletions models/balancer/arbitrum/balancer_v2_arbitrum_pools_fees.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ WITH registered_pools AS (
DISTINCT poolAddress AS pool_address
FROM
{{ source ('balancer_v2_arbitrum', 'Vault_evt_PoolRegistered') }}
{% if is_incremental() %}
WHERE evt_block_time >= DATE_TRUNC('day', NOW() - interval '7' day)
{% endif %}
)
SELECT
'arbitrum' AS blockchain,
Expand Down
Loading