diff --git a/dbt_macros/shared/balances_subset_daily.sql b/dbt_macros/shared/balances_subset_daily.sql new file mode 100644 index 00000000000..c55db341cd2 --- /dev/null +++ b/dbt_macros/shared/balances_subset_daily.sql @@ -0,0 +1,25 @@ +{%- macro balances_subset_daily( + blockchain, + token_address, + start_date + ) +%} + +with +filtered_balances as ( + select + address as pool_address, + balance as token_balance, + day as snapshot_day + from {{ source('tokens_' ~ blockchain, 'balances_daily') }} + where + token_address = {{ token_address }} + {% if is_incremental() %} + and {{ incremental_predicate('day') }} + {% else %} + and day >= cast('{{ start_date }}' as date) + {% endif %} +) + +select * from filtered_balances +{% endmacro %} \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/_schema.yml b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/_schema.yml new file mode 100644 index 00000000000..c6eecee085d --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/_schema.yml @@ -0,0 +1,17 @@ +version: 2 + +models: + - name: uniswap_pools_optimism_balances + description: "Tracks OP token balances in Uniswap pools on Optimism." + meta: + blockchain: optimism + sector: DEX + project: uniswap + contributors: jason + config: + tags: ['optimism', 'op_token', 'balances', 'uniswap','pools'] + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - pool_address + - snapshot_day \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/uniswap_pools_optimism_balances.sql b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/uniswap_pools_optimism_balances.sql new file mode 100644 index 00000000000..d6bb096e3c7 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/uniswap_pools_optimism_balances.sql @@ -0,0 +1,46 @@ +{{ + config( + schema = 'uniswap_pools_optimism', + alias = 'balances', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool_address', 'snapshot_day'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.snapshot_day')] + ) +}} + +with op_pools as ( + select + pool as pool_address, + token0, + token1, + fee as fee_tier, + creation_block_time as creation_time + from + {{ source('uniswap_v3_optimism', 'pools') }} + where + token0 = from_hex('0x4200000000000000000000000000000000000042') + or token1 = from_hex('0x4200000000000000000000000000000000000042') +) + +, filtered_balances as ( + {{ balances_subset_daily( + blockchain='optimism', + token_address="from_hex('0x4200000000000000000000000000000000000042')", + start_date='2021-11-11' + ) }} +) + +select + p.pool_address, + p.token0, + p.token1, + p.fee_tier, + p.creation_time, + coalesce(b.token_balance, 0) as op_balance, + coalesce(b.snapshot_day, current_date) as snapshot_day +from + filtered_balances b +right join + op_pools p on p.pool_address = b.pool_address \ No newline at end of file diff --git a/sources/_sector/dex/trades/optimism/_sources.yml b/sources/_sector/dex/trades/optimism/_sources.yml index 4d723797c87..c54e6a688bd 100644 --- a/sources/_sector/dex/trades/optimism/_sources.yml +++ b/sources/_sector/dex/trades/optimism/_sources.yml @@ -2,6 +2,8 @@ version: 2 sources: - name: uniswap_v3_optimism + tables: + - name: pools - name: sushi_optimism tables: - name: ConstantProductPool_evt_Swap diff --git a/sources/_subprojects_outputs/tokens/balances.yml b/sources/_subprojects_outputs/tokens/balances.yml index dc994a80daa..3c8f8f46e79 100644 --- a/sources/_subprojects_outputs/tokens/balances.yml +++ b/sources/_subprojects_outputs/tokens/balances.yml @@ -8,6 +8,7 @@ sources: tables: - name: balances_daily_agg - name: balances_daily_agg_base + - name: balances_daily - name: tokens_polygon tables: - name: balances_daily_agg