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

Init beets daily spells #7430

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{
config(
schema = 'beets',
alias = 'bpt_prices',
materialized = 'table',
file_format = 'delta'
)
}}


WITH v2 AS(
{{
balancer_v2_compatible_bpt_prices_macro(
blockchain = 'sonic',
version = '2',
project_decoded_as = 'beethoven_x_v2',
base_spells_namespace = 'beets',
pool_labels_spell = ref('labels_beets_pools_sonic')
)
}}),

v3 AS(
{{
balancer_v3_compatible_bpt_prices_macro(
blockchain = 'sonic',
version = '3',
project_decoded_as = 'beethoven_x_v3',
base_spells_namespace = 'beets',
pool_labels_spell = ref('labels_beets_pools_sonic')
)
}}
)

SELECT * FROM v2

UNION

SELECT * FROM v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{ config(
schema = 'beets',
alias = 'bpt_supply',
materialized = 'table',
file_format = 'delta'
)
}}

WITH v2 AS(
{{
balancer_v2_compatible_bpt_supply_macro(
blockchain = 'sonic',
version = '2',
project_decoded_as = 'beethoven_x_v2',
base_spells_namespace = 'beets',
pool_labels_spell = ref('labels_beets_pools_sonic')
)
}}),

v3 AS({{
balancer_v3_compatible_bpt_supply_macro(
blockchain = 'sonic',
version = '3',
project_decoded_as = 'beethoven_x_v3',
base_spells_namespace = 'beets',
pool_labels_spell = ref('labels_beets_pools_sonic')
)
}})

SELECT * FROM v2

UNION

SELECT * FROM v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{
config(
schema = 'beets_sonic',
alias = 'bpt_supply_changes',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_date', 'evt_tx_hash', 'evt_index', 'label'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.evt_block_time')]
)
}}

WITH v2 AS(
{{
balancer_v2_compatible_bpt_supply_changes_macro(
blockchain = 'sonic',
version = '2',
project_decoded_as = 'beethoven_x_v2',
base_spells_namespace = 'beets',
pool_labels_spell = ref('labels_beets_pools_sonic')
)
}}),

v3 AS(
{{
balancer_v3_compatible_bpt_supply_changes_macro(
blockchain = 'sonic',
version = '3',
project_decoded_as = 'beethoven_x_v3',
base_spells_namespace = 'beets',
pool_labels_spell = ref('labels_beets_pools_sonic')
)
}})

SELECT * FROM v2

UNION

SELECT * FROM v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{
config(
schema = 'beets_sonic',
alias = 'bpt_supply_changes_daily',
materialized = 'table',
file_format = 'delta'
)
}}

WITH v2 AS(
{{
balancer_v2_compatible_bpt_supply_changes_daily_agg_macro(
blockchain = 'sonic',
version = '2',
project_decoded_as = 'beethoven_x_v2',
base_spells_namespace = 'beets'
)
}}),

v3 AS(
{{
balancer_v3_compatible_bpt_supply_changes_daily_agg_macro(
blockchain = 'sonic',
version = '3',
project_decoded_as = 'beethoven_x_v3',
base_spells_namespace = 'beets'
)
}})

SELECT * FROM v2

UNION

SELECT * FROM v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% set blockchain = 'sonic' %}

{{
config(
schema = 'beets',
alias = 'liquidity',
materialized = 'table',
file_format = 'delta'
)
}}

WITH v2 AS(
{{
balancer_v2_compatible_liquidity_macro(
blockchain = blockchain,
version = '2',
project_decoded_as = 'beethoven_x_v2',
base_spells_namespace = 'beets',
pool_labels_spell = ref('labels_beets_pools_sonic')
)
}}),

v3 AS(
{{
balancer_v3_compatible_liquidity_macro(
blockchain = blockchain,
version = '3',
project_decoded_as = 'beethoven_x_v3',
base_spells_namespace = 'beets',
pool_labels_spell = ref('labels_beets_pools_sonic')
)
}})

SELECT * FROM v2

UNION

SELECT * FROM v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{{ config(
schema = 'beets',
alias = 'pools_metrics_daily',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_date', 'blockchain', 'project', 'version', 'project_contract_address'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_date')],
post_hook='{{ expose_spells(blockchains = \'["sonic"]\',
spell_type = "project",
spell_name = "beets",
contributors = \'["viniabussafi"]\') }}'
)
}}


WITH
trades AS(
SELECT
block_date,
version,
blockchain,
project_contract_address,
sum(amount_usd) AS swap_amount_usd
FROM {{ source('beets', 'trades') }}
{% if is_incremental() %}
WHERE {{incremental_predicate('block_date')}}
{% endif %}
GROUP BY 1, 2, 3, 4
),

liquidity AS(
SELECT
day AS block_date,
blockchain,
version,
pool_address AS project_contract_address,
pool_type,
pool_symbol,
sum(pool_liquidity_usd) AS tvl_usd,
sum(pool_liquidity_eth) AS tvl_eth
FROM {{ ref('beets_liquidity') }}
{% if is_incremental() %}
WHERE {{incremental_predicate('day')}}
{% endif %}
GROUP BY 1, 2, 3, 4, 5, 6
),

fees AS(
SELECT
day,
version,
blockchain,
pool_address,
sum(protocol_fee_collected_usd) AS fee_amount_usd
FROM {{ ref('beets_protocol_fee') }}
{% if is_incremental() %}
WHERE {{incremental_predicate('day')}}
{% endif %}
GROUP BY 1, 2, 3, 4
)

SELECT
l.blockchain,
'beets' AS project,
l.version,
l.block_date,
l.project_contract_address,
l.pool_symbol,
l.pool_type,
t.swap_amount_usd,
l.tvl_usd,
l.tvl_eth,
f.fee_amount_usd
FROM liquidity l
LEFT JOIN trades t ON l.block_date = t.block_date
AND l.project_contract_address = t.project_contract_address
AND l.blockchain = t.blockchain
LEFT JOIN fees f ON l.block_date = f.day
AND l.project_contract_address = f.pool_address
AND l.blockchain = f.blockchain
ORDER BY 1 DESC, 7 DESC
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{{
config(
schema='beets',
alias = 'pools_tokens_weights',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['pool_id', 'token_address']
)
}}

WITH v2 AS(
WITH registered AS (
SELECT
poolID AS pool_id,
evt_block_time
FROM {{ source('beethoven_x_v2_', 'Vault_evt_PoolRegistered') }}
{% if is_incremental() %}
WHERE evt_block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}
),
weighted_pool_factory AS (
SELECT
call_create.output_0 AS pool_id,
t.pos AS pos,
t.token_address AS token_address,
t2.normalized_weight AS normalized_weight
FROM {{ source('beethoven_x_v2_sonic', 'WeightedPoolFactory_call_create') }} AS call_create
CROSS JOIN UNNEST(call_create.tokens) WITH ORDINALITY t(token_address, pos)
CROSS JOIN UNNEST(call_create.weights) WITH ORDINALITY t2(normalized_weight, pos)
WHERE t.pos = t2.pos
{% if is_incremental() %}
AND call_create.call_block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}
),

normalized_weights AS (
SELECT
pool_id,
token_address,
normalized_weight / POWER(10, 18) AS normalized_weight
FROM weighted_pool_factory
)

SELECT
'sonic' AS blockchain,
'2' AS version,
r.pool_id,
w.token_address,
w.normalized_weight
FROM normalized_weights w
LEFT JOIN registered r ON BYTEARRAY_SUBSTRING(r.pool_id,1,20) = w.pool_id
WHERE w.pool_id IS NOT NULL),

v3 AS(
WITH token_data AS (
SELECT
pool,
ARRAY_AGG(FROM_HEX(json_extract_scalar(token, '$.token')) ORDER BY token_index) AS tokens
FROM (
SELECT
pool,
tokenConfig,
SEQUENCE(1, CARDINALITY(tokenConfig)) AS token_index_array
FROM {{ source('beethoven_x_v3_sonic', 'Vault_evt_PoolRegistered') }}
) AS pool_data
CROSS JOIN UNNEST(tokenConfig, token_index_array) AS t(token, token_index)
GROUP BY 1
),

weighted_pool_factory AS (
SELECT
call_create.output_pool AS pool_id,
t.pos AS pos,
t.token_address AS token_address,
t2.normalized_weight AS normalized_weight
FROM {{ source('beethoven_x_v3_sonic', 'WeightedPoolFactory_call_create') }} AS call_create
JOIN token_data td ON td.pool = call_create.output_pool
CROSS JOIN UNNEST(td.tokens) WITH ORDINALITY t(token_address, pos)
CROSS JOIN UNNEST(call_create.normalizedWeights) WITH ORDINALITY t2(normalized_weight, pos)
WHERE t.pos = t2.pos
{% if is_incremental() %}
AND call_create.call_block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}
),

normalized_weights AS (
SELECT
pool_id,
token_address,
normalized_weight / POWER(10, 18) AS normalized_weight
FROM weighted_pool_factory
)

SELECT
'sonic' AS blockchain,
'3' AS version,
w.pool_id,
w.token_address,
w.normalized_weight
FROM normalized_weights w
WHERE w.pool_id IS NOT NULL
)

SELECT * FROM v2
UNION
SELECT * FROM v3
Loading
Loading