Skip to content

Commit

Permalink
[READY FOR REVIEW] round of improvements to balancer spells (#5709)
Browse files Browse the repository at this point in the history
* convert protocol fee to table

* changes to labels and token whitelist
  • Loading branch information
viniabussafi authored Apr 7, 2024
1 parent 98269f7 commit 9a9cd58
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 51 deletions.
17 changes: 2 additions & 15 deletions macros/models/_project/balancer/balancer_protocol_fee_macro.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ WITH pool_labels AS (
AVG(price) AS price
FROM {{ source('prices', 'usd') }}
WHERE blockchain = '{{blockchain}}'
{% if is_incremental() %}
AND {{ incremental_predicate('minute') }}
{% endif %}
GROUP BY 1, 2, 3

),
Expand All @@ -39,9 +36,6 @@ WITH pool_labels AS (
approx_percentile(median_price, 0.5) AS price,
sum(sample_size) AS sample_size
FROM {{ ref('dex_prices') }}
{% if is_incremental() %}
WHERE {{ incremental_predicate('hour') }}
{% endif %}
GROUP BY 1, 2
HAVING sum(sample_size) > 3
),
Expand Down Expand Up @@ -96,10 +90,7 @@ WITH pool_labels AS (
token AS token_address,
SUM(protocol_fees) AS protocol_fee_amount_raw
FROM {{ source('balancer_v2_' + blockchain, 'Vault_evt_PoolBalanceChanged') }} b
CROSS JOIN unnest("protocolFeeAmounts", "tokens") AS t(protocol_fees, token)
{% if is_incremental() %}
WHERE {{ incremental_predicate('b.evt_block_time') }}
{% endif %}
CROSS JOIN unnest("protocolFeeAmounts", "tokens") AS t(protocol_fees, token)
GROUP BY 1, 2, 3

UNION ALL
Expand All @@ -113,11 +104,7 @@ WITH pool_labels AS (
INNER JOIN {{ source('erc20_' + blockchain, 'evt_transfer') }} t
ON t.contract_address = b.poolAddress
AND t."from" = 0x0000000000000000000000000000000000000000
AND t.to = 0xce88686553686DA562CE7Cea497CE749DA109f9F --ProtocolFeesCollector address, which is the same across all chains
{% if is_incremental() %}
WHERE {{ incremental_predicate('t.evt_block_time') }}
AND {{ incremental_predicate('b.evt_block_time') }}
{% endif %}
AND t.to = 0xce88686553686DA562CE7Cea497CE749DA109f9F --ProtocolFeesCollector address, which is the same across all chains
GROUP BY 1, 2, 3
),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
config(
schema = 'balancer_v2_arbitrum',
alias = 'protocol_fee',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'pool_id', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]

materialized = 'table',
file_format = 'delta'
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
config(
schema = 'balancer_v2_avalanche_c',
alias = 'protocol_fee',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'pool_id', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]
materialized = 'table',
file_format = 'delta'
)
}}

Expand Down
1 change: 1 addition & 0 deletions models/balancer/balancer_token_whitelist.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ WITH whitelist_token as (
(0x4ecaba5870353805a9f068101a40e0f32ed605c6, 'USDT', 'gnosis'),
(0xe91d153e0b41518a2ce8dd3d7944fa863463a97d, 'WXDAI', 'gnosis'),
(0x6a023ccd1ff6f2045c3309768ead9e68f978f6e1, 'WETH', 'gnosis'),
(0x6c76971f98945ae98dd7d4dfca8711ebea946ea6, 'WSTETH', 'gnosis'),
(0xdAC17F958D2ee523a2206206994597C13D831ec7, 'USDT', 'ethereum'),
(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48, 'USDC', 'ethereum'),
(0x6B175474E89094C44Da98b954EedeAC495271d0F, 'DAI', 'ethereum'),
Expand Down
7 changes: 2 additions & 5 deletions models/balancer/base/balancer_v2_base_protocol_fee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
config(
schema = 'balancer_v2_base',
alias = 'protocol_fee',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'pool_id', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]
materialized = 'table',
file_format = 'delta'
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
config(
schema = 'balancer_v2_ethereum',
alias = 'protocol_fee',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'pool_id', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]
materialized = 'table',
file_format = 'delta'
)
}}

Expand Down
7 changes: 2 additions & 5 deletions models/balancer/gnosis/balancer_v2_gnosis_protocol_fee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
config(
schema='balancer_v2_gnosis',
alias = 'protocol_fee',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'pool_id', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]
materialized = 'table',
file_format = 'delta'
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
config(
schema = 'balancer_v2_optimism',
alias = 'protocol_fee',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'pool_id', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]
materialized = 'table',
file_format = 'delta'
)
}}

Expand Down
7 changes: 2 additions & 5 deletions models/balancer/polygon/balancer_v2_polygon_protocol_fee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
config(
schema = 'balancer_v2_polygon',
alias = 'protocol_fee',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['day', 'pool_id', 'token_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')]
materialized = 'table',
file_format = 'delta'
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@ WITH pools AS (
ON c.evt_tx_hash = cc.call_tx_hash
AND bytearray_substring(c.poolId, 1, 20) = cc.output_0
CROSS JOIN UNNEST(cc.tokens) AS t(tokens)

UNION ALL

SELECT
c.poolId AS pool_id,
t.tokens AS token_address,
0 AS normalized_weight,
cc.symbol,
'ECLP' AS pool_type
FROM {{ source('balancer_v2_arbitrum', 'Vault_evt_PoolRegistered') }} c
INNER JOIN {{ source('gyroscope_arbitrum', 'Gyro2CLPPoolFactory_call_create') }} cc
ON c.evt_tx_hash = cc.call_tx_hash
AND bytearray_substring(c.poolId, 1, 20) = cc.output_0
CROSS JOIN UNNEST(cc.tokens) AS t(tokens)
),

settings AS (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ WITH pools AS (
WHERE t.pos = w.pos
) zip

UNION ALL

SELECT
pool_id,
zip.tokens AS token_address,
zip.weights / pow(10, 18) AS normalized_weight,
symbol,
pool_type
FROM (
SELECT
c.poolId AS pool_id,
t.tokens,
w.weights,
cc.symbol,
'weighted' AS pool_type
FROM {{ source('balancer_v2_optimism', 'Vault_evt_PoolRegistered') }} c
INNER JOIN {{ source('balancer_v2_optimism', 'WeightedPoolFactory_call_create') }} cc
ON c.evt_tx_hash = cc.call_tx_hash
AND bytearray_substring(c.poolId, 1, 20) = cc.output_0
CROSS JOIN UNNEST(cc.tokens) WITH ORDINALITY t(tokens, pos)
CROSS JOIN UNNEST(cc.normalizedWeights) WITH ORDINALITY w(weights, pos)
WHERE t.pos = w.pos
) zip

UNION ALL

SELECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,32 @@ sources:
- *symbol
- *tokens

- name: Gyro2CLPPoolFactory_call_create
description: >
Decoded table of registered pools on the Gyroscope ECLP contract.
loaded_at_field: call_block_time
columns:
- *call_block_number
- *call_block_time
- *call_success
- *call_trace_address
- *call_tx_hash
- name: capManager
- name: capParams
- name: contract_address
description: "Address of the ComposableStablePoolFactory contract"
- name: derivedECLPParams
- name: eclpParams
- name: name
- *output_0
- *owner
- name: pauseManager
- name: pauseParams
- *swapFeePercentage
- *rateProviders
- *symbol
- *tokens

- name: gyroscope_ethereum
tables:
- name: GyroECLPPoolFactory_call_create
Expand Down

0 comments on commit 9a9cd58

Please sign in to comment.