-
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.
Merge branch 'main' into daily-spellbook-op
- Loading branch information
Showing
265 changed files
with
8,104 additions
and
4,751 deletions.
There are no files selected for viewing
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
37 changes: 37 additions & 0 deletions
37
macros/models/_project/balancer/balancer_pool_token_supply_changes_daily_agg_macro.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,37 @@ | ||
{% macro | ||
bpt_supply_changes_daily_agg_macro( | ||
blockchain, version | ||
) | ||
%} | ||
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('balancer_bpt_supply_changes') }} | ||
WHERE blockchain = '{{blockchain}}' | ||
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 | ||
{% endmacro %} |
130 changes: 130 additions & 0 deletions
130
macros/models/_project/balancer/balancer_pool_token_supply_changes_macro.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,130 @@ | ||
{% macro | ||
bpt_supply_changes_macro( | ||
blockchain, version | ||
) | ||
%} | ||
WITH pool_labels AS ( | ||
SELECT | ||
address, | ||
name, | ||
pool_type | ||
FROM {{ ref('labels_balancer_v2_pools') }} | ||
WHERE blockchain = '{{blockchain}}' | ||
), | ||
|
||
-- Extract mints and burns from transfers | ||
transfers AS ( | ||
SELECT | ||
t.evt_block_time, | ||
t.evt_block_number, | ||
t.evt_tx_hash, | ||
t.evt_index, | ||
t.contract_address AS token, | ||
CASE | ||
WHEN t."from" = 0x0000000000000000000000000000000000000000 | ||
THEN 'mint' | ||
WHEN t.to = 0x0000000000000000000000000000000000000000 | ||
THEN 'burn' | ||
END AS label, | ||
l.pool_type, | ||
l.name, | ||
CASE | ||
WHEN t."from" = 0x0000000000000000000000000000000000000000 | ||
THEN value | ||
WHEN t.to = 0x0000000000000000000000000000000000000000 | ||
THEN - value | ||
ELSE 0 | ||
END AS amount | ||
FROM {{ ref('balancer_transfers_bpt') }} t | ||
LEFT JOIN pool_labels l ON t.contract_address = l.address | ||
WHERE t.blockchain = '{{blockchain}}' | ||
AND t.version = '{{version}}' | ||
{% if is_incremental() %} | ||
AND {{ incremental_predicate('t.evt_block_time') }} | ||
{% endif %} | ||
), | ||
|
||
-- Calculating Joins(mint) and Exits(burn) via Swap | ||
joins AS ( | ||
SELECT | ||
s.evt_block_time, | ||
s.evt_block_number, | ||
s.evt_tx_hash, | ||
s.evt_index, | ||
s.tokenOut AS token, | ||
'join' AS label, | ||
l.pool_type, | ||
l.name, | ||
CASE WHEN l.pool_type IN ('weighted') | ||
THEN 0 | ||
ELSE s.amountOut | ||
END AS amount | ||
FROM {{ source('balancer_v2_' + blockchain, 'Vault_evt_Swap') }} s | ||
LEFT JOIN pool_labels l ON BYTEARRAY_SUBSTRING(s.poolId, 1, 20) = l.address | ||
WHERE tokenOut = BYTEARRAY_SUBSTRING(s.poolId, 1, 20) | ||
{% if is_incremental() %} | ||
AND {{ incremental_predicate('s. evt_block_time') }} | ||
{% endif %} | ||
|
||
), | ||
|
||
exits AS ( | ||
SELECT | ||
s.evt_block_time, | ||
s.evt_block_number, | ||
s.evt_tx_hash, | ||
s.evt_index, | ||
s.tokenIn AS token, | ||
'exit' AS label, | ||
l.pool_type, | ||
l.name, | ||
CASE WHEN l.pool_type IN ('weighted') | ||
THEN 0 | ||
ELSE - s.amountIn | ||
END AS amount | ||
FROM {{ source('balancer_v2_' + blockchain, 'Vault_evt_Swap') }} s | ||
LEFT JOIN pool_labels l ON BYTEARRAY_SUBSTRING(s.poolId, 1, 20) = l.address | ||
WHERE tokenIn = BYTEARRAY_SUBSTRING(s.poolId, 1, 20) | ||
{% if is_incremental() %} | ||
AND {{ incremental_predicate('s. evt_block_time') }} | ||
{% endif %} | ||
) | ||
|
||
SELECT | ||
date_trunc('day', evt_block_time) AS block_date, | ||
evt_block_time, | ||
evt_block_number, | ||
'{{blockchain}}' AS blockchain, | ||
evt_tx_hash, | ||
evt_index, | ||
pool_type, | ||
name AS pool_symbol, | ||
'{{version}}' AS version, | ||
label, | ||
token AS token_address, | ||
amount AS delta_amount_raw, | ||
amount / POWER (10, 18) AS delta_amount --18 decimals standard for BPTs | ||
FROM | ||
( | ||
SELECT | ||
* | ||
FROM joins | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
* | ||
FROM exits | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
* | ||
FROM transfers | ||
WHERE label IS NOT NULL | ||
) | ||
{% if is_incremental() %} | ||
WHERE {{ incremental_predicate('evt_block_time') }} | ||
{% endif %} | ||
|
||
{% endmacro %} |
Oops, something went wrong.