Skip to content

Commit

Permalink
Merge branch 'main' into daily-spellbook-op
Browse files Browse the repository at this point in the history
  • Loading branch information
aalan3 authored Jun 4, 2024
2 parents 1f545d0 + d108e3d commit d8de49b
Show file tree
Hide file tree
Showing 265 changed files with 8,104 additions and 4,751 deletions.
15 changes: 0 additions & 15 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ models:
optimism:
+schema: aave_optimism

balancer:
+schema: balancer
arbitrum:
+schema: balancer_arbitrum
ethereum:
+schema: balancer_ethereum
polygon:
+schema: balancer_polygon
optimism:
+schema: balancer_optimism
gnosis:
+schema: balancer_gnosis

ens:
+schema: ens
ethereum:
Expand Down Expand Up @@ -448,8 +435,6 @@ models:
+schema: transfers_arbitrum
polygon:
+schema: transfers_polygon
fantom:
+schema: transfers_fantom
base:
+schema: transfers_base
celo:
Expand Down
34 changes: 11 additions & 23 deletions macros/models/_project/balancer/balancer_bpt_supply_macro.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ WITH pool_labels AS (
joins AS (
SELECT
DATE_TRUNC('day', evt_block_time) AS block_date,
tokenOut AS token,
tokenOut,
pool_type,
CASE WHEN pool_type IN ('weighted')
THEN 0
ELSE SUM(amountOut / POWER(10, 18))
END AS amount
END AS ajoins
FROM {{ source('balancer_v2_' + blockchain, 'Vault_evt_Swap') }}
LEFT JOIN pool_labels ON BYTEARRAY_SUBSTRING(poolId, 1, 20) = address
WHERE tokenOut = BYTEARRAY_SUBSTRING(poolId, 1, 20)
Expand All @@ -94,37 +94,25 @@ WITH pool_labels AS (
exits AS (
SELECT
DATE_TRUNC('day', evt_block_time) AS block_date,
tokenIn AS token,
tokenIn,
pool_type,
CASE WHEN pool_type IN ('weighted')
THEN 0
ELSE SUM( -amountIn / POWER(10, 18))
END AS amount
ELSE SUM(amountIn / POWER(10, 18))
END AS aexits
FROM {{ source('balancer_v2_' + blockchain, 'Vault_evt_Swap') }}
LEFT JOIN pool_labels ON BYTEARRAY_SUBSTRING(poolId, 1, 20) = address
WHERE tokenIn = BYTEARRAY_SUBSTRING(poolId, 1, 20)
GROUP BY 1, 2, 3
),

joins_and_exits_1 AS (
SELECT
*
FROM joins

UNION ALL

SELECT
*
FROM exits
),

joins_and_exits AS (
SELECT
block_date,
token AS bpt,
LEAD(block_date, 1, NOW()) OVER (PARTITION BY token ORDER BY block_date) AS day_of_next_change,
SUM(amount) OVER (PARTITION BY token ORDER BY block_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS adelta
FROM joins_and_exits_1
j.block_date,
j.tokenOut AS bpt,
SUM(COALESCE(ajoins, 0) - COALESCE(aexits, 0)) OVER (PARTITION BY j.tokenOut ORDER BY j.block_date ASC) AS adelta
FROM joins j
FULL OUTER JOIN exits e ON j.block_date = e.block_date AND e.tokenIn = j.tokenOut
),

calendar AS (
Expand All @@ -142,7 +130,7 @@ WITH pool_labels AS (
COALESCE(SUM(b.supply - COALESCE(preminted_bpts, 0) + COALESCE(adelta, 0)),0) AS supply
FROM calendar c
LEFT JOIN balances b ON b.day <= c.day AND c.day < b.day_of_next_change
LEFT JOIN joins_and_exits j ON j.block_date <= c.day AND c.day < j.day_of_next_change AND b.token = j.bpt
LEFT JOIN joins_and_exits j ON c.day = j.block_date AND b.token = j.bpt
LEFT JOIN premints p ON b.token = p.bpt
LEFT JOIN pool_labels l ON b.token = l.address
WHERE l.pool_type IN ('weighted', 'LBP', 'investment', 'stable', 'linear', 'ECLP', 'managed', 'FX')
Expand Down
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 %}
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 %}
Loading

0 comments on commit d8de49b

Please sign in to comment.