Skip to content
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ logs/
.envrc

# Compiled files
*.pyc

*.pyc
67 changes: 67 additions & 0 deletions macros/shared/balancer/balancer_lbps_macro.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{% macro
balancer_v3_compatible_lbps_macro(
blockchain, project_decoded_as
)
%}

WITH lbps_list AS (
SELECT
e.pool AS pool_address,
c.symbol AS pool_symbol,
e.projectToken AS project_token,
e.reserveToken AS reserve_token
FROM {{ source(project_decoded_as + '_' + blockchain, 'LBPoolFactory_evt_LBPoolCreated') }} e
JOIN {{ source(project_decoded_as + '_' + blockchain, 'LBPoolFactory_call_create') }} c ON e.pool = c.output_pool
),

lbps_weight_update AS (
SELECT
contract_address AS pool_address,
evt_block_time,
startTime,
endTime,
element_at(startWeights, 1) / POWER(10,18) AS project_token_start_weight,
element_at(startWeights, 2) / POWER(10,18) AS reserve_token_start_weight,
element_at(endWeights, 1) / POWER(10,18) AS project_token_end_weight,
element_at(endWeights, 2) / POWER(10,18) AS reserve_token_end_weight
FROM {{ source(project_decoded_as + '_' + blockchain, 'LBPool_evt_GradualWeightUpdateScheduled') }}
),

last_weight_update AS (
SELECT *
FROM (
SELECT
pool_address,
from_unixtime(startTime) AS start_time,
from_unixtime(endTime) AS end_time,
project_token_start_weight,
reserve_token_start_weight,
project_token_end_weight,
reserve_token_end_weight,
ROW_NUMBER() OVER (PARTITION BY pool_address ORDER BY evt_block_time DESC) AS ranking
FROM lbps_weight_update c
) w
WHERE ranking = 1
)

SELECT
'{{blockchain}}' AS blockchain,
l.pool_symbol,
l.pool_address,
w.start_time,
w.end_time,
l.project_token,
t1.symbol AS project_token_symbol,
l.reserve_token,
t2.symbol AS reserve_token_symbol,
project_token_start_weight,
reserve_token_start_weight,
project_token_end_weight,
reserve_token_end_weight
FROM lbps_list l
JOIN last_weight_update w
ON w.pool_address = l.pool_address
LEFT JOIN {{ source('tokens', 'erc20') }} t1 ON l.project_token = t1.contract_address AND t1.blockchain = '{{blockchain}}'
LEFT JOIN {{ source('tokens', 'erc20') }} t2 ON l.reserve_token = t2.contract_address AND t2.blockchain = '{{blockchain}}'

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{% macro
balancer_v2_compatible_bpt_supply_changes_daily_agg_macro(
blockchain, version, project_decoded_as, base_spells_namespace
)
%}
WITH
daily_balance AS (
SELECT
block_date,
blockchain,
pool_type,
pool_symbol,
token_address,
LEAD(block_date, 1, NOW()) OVER (PARTITION BY token_address ORDER BY block_date) AS day_of_next_change,
SUM(delta_amount) AS daily_amount
FROM {{ ref(base_spells_namespace + '_bpt_supply_changes') }}
WHERE blockchain = '{{blockchain}}'
AND version = '{{version}}'
GROUP BY 1, 2, 3, 4, 5
),

calendar AS (
SELECT date_sequence AS day
FROM unnest(sequence(date('2021-04-21'), date(now()), interval '1' day)) as t(date_sequence)
)

SELECT
c.day AS block_date,
'{{blockchain}}' as blockchain,
'{{version}}' AS version,
b.pool_type,
b.pool_symbol,
b.token_address,
b.daily_amount AS daily_delta
FROM calendar c
LEFT JOIN daily_balance b ON b.block_date = c.day
WHERE b.token_address IS NOT NULL
AND b.pool_type IS NOT NULL
{% endmacro %}

{# ######################################################################### #}

{% macro
balancer_v3_compatible_bpt_supply_changes_daily_agg_macro(
blockchain, version, project_decoded_as, base_spells_namespace
)
%}
WITH
daily_balance AS (
SELECT
block_date,
blockchain,
pool_type,
pool_symbol,
token_address,
LEAD(block_date, 1, NOW()) OVER (PARTITION BY token_address ORDER BY block_date) AS day_of_next_change,
SUM(delta_amount) AS daily_amount
FROM {{ ref(base_spells_namespace + '_bpt_supply_changes') }}
WHERE blockchain = '{{blockchain}}'
AND version = '{{version}}'
GROUP BY 1, 2, 3, 4, 5
),

calendar AS (
SELECT date_sequence AS day
FROM unnest(sequence(date('2024-12-01'), date(now()), interval '1' day)) as t(date_sequence)
)

SELECT DISTINCT
c.day AS block_date,
'{{blockchain}}' as blockchain,
'{{version}}' AS version,
b.pool_type,
b.pool_symbol,
b.token_address,
b.daily_amount AS daily_delta
FROM calendar c
LEFT JOIN daily_balance b ON b.block_date = c.day
WHERE b.token_address IS NOT NULL
AND b.pool_type IS NOT NULL

{% endmacro %}
Loading