Skip to content

Commit

Permalink
Init beets daily spells (duneanalytics#7430)
Browse files Browse the repository at this point in the history
* init beets dex models

* fix

* fix schema

* again

* fix typo

* fix copypasta

* add sonic base trades

* adjust macro

* fix schema

* Update beets_sonic_base_trades_seed.csv

* add version column

* lighter rerun

* fix comment

* again

* update dex_info

* init beets daily_spellbook spells

* Update beets_sonic_base_trades_seed.csv

* fix source

* update sources and token whitelist for pricing

* again

* again

* again

* update macros to allow cros subproject spells

* broken ref

* fix column

* Update beets_sonic_base_trades_seed.csv

* merge source changes on single file

* again
  • Loading branch information
viniabussafi authored Jan 13, 2025
1 parent 3ff237d commit a2c8d3a
Show file tree
Hide file tree
Showing 19 changed files with 896 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dbt_macros/shared/balancer/balancer_bpt_prices_macro.sql
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ WITH pool_labels AS (
decimals,
APPROX_PERCENTILE(median_price, 0.5) AS price,
DATE_TRUNC ('day', next_change) AS next_change
FROM {{ ref('balancer_v3_erc4626_token_prices') }}
FROM {{ source('balancer_v3' , 'erc4626_token_prices') }}
WHERE blockchain = '{{blockchain}}'
GROUP BY 1, 2, 3, 5
),
Expand Down
2 changes: 1 addition & 1 deletion dbt_macros/shared/balancer/balancer_liquidity_macro.sql
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ WITH pool_labels AS (
decimals,
APPROX_PERCENTILE(median_price, 0.5) AS price,
DATE_TRUNC ('day', next_change) AS next_change
FROM {{ ref('balancer_v3_erc4626_token_prices') }}
FROM {{ source('balancer_v3' , 'erc4626_token_prices') }}
WHERE blockchain = '{{blockchain}}'
GROUP BY 1, 2, 3, 5
),
Expand Down
2 changes: 1 addition & 1 deletion dbt_macros/shared/balancer/balancer_protocol_fee_macro.sql
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ WITH pool_labels AS (
decimals,
APPROX_PERCENTILE(median_price, 0.5) AS price,
DATE_TRUNC ('day', next_change) AS next_change
FROM {{ ref('balancer_v3_erc4626_token_prices') }}
FROM {{ source('balancer_v3' , 'erc4626_token_prices') }}
WHERE blockchain = '{{blockchain}}'
GROUP BY 1, 2, 3, 5
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ WITH
decimals,
APPROX_PERCENTILE(median_price, 0.5) AS price,
DATE_TRUNC ('day', next_change) AS next_change
FROM {{ ref('balancer_v3_erc4626_token_prices') }}
FROM {{ source('balancer_v3' , 'erc4626_token_prices') }}
WHERE blockchain = '{{blockchain}}'
GROUP BY 1, 2, 3, 5
),
Expand Down
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
Loading

0 comments on commit a2c8d3a

Please sign in to comment.