From 0e6019b437dca82a080ba682d95e859f447c49fc Mon Sep 17 00:00:00 2001 From: jason Date: Wed, 8 Jan 2025 12:27:07 +0530 Subject: [PATCH 1/5] added optimism: uniswap pools dex balances --- dbt_macros/shared/balances_subset_daily.sql | 25 ++++++++++ .../dex/pools/optimism/uniswap/_schema.yml | 0 .../uniswap_pools_optimism_balances.sql | 46 +++++++++++++++++++ sources/_subprojects_outputs/dex/_sources.yml | 1 + .../_subprojects_outputs/tokens/balances.yml | 1 + 5 files changed, 73 insertions(+) create mode 100644 dbt_macros/shared/balances_subset_daily.sql create mode 100644 dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/_schema.yml create mode 100644 dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap_pools_optimism_balances.sql 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..e69de29bb2d diff --git a/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap_pools_optimism_balances.sql b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap_pools_optimism_balances.sql new file mode 100644 index 00000000000..c6d08f1f2c9 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/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 = '0x4200000000000000000000000000000000000042' + or token1 = '0x4200000000000000000000000000000000000042' +) + +, filtered_balances as ( + {{ balances_subset_daily( + blockchain='optimism', + token_address="'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/_subprojects_outputs/dex/_sources.yml b/sources/_subprojects_outputs/dex/_sources.yml index 88751103645..3a58a1afe25 100644 --- a/sources/_subprojects_outputs/dex/_sources.yml +++ b/sources/_subprojects_outputs/dex/_sources.yml @@ -60,6 +60,7 @@ sources: - name: uniswap_v3_optimism tables: - name: ovm1_pool_mapping + - name: pools - name: cow_protocol_ethereum tables: - name: trades 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 From 8386b9cfde4b684303d7800179d0f486f1766491 Mon Sep 17 00:00:00 2001 From: jason Date: Wed, 8 Jan 2025 14:00:28 +0530 Subject: [PATCH 2/5] update sql file --- .../dex/pools/optimism/uniswap_pools_optimism_balances.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap_pools_optimism_balances.sql b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap_pools_optimism_balances.sql index c6d08f1f2c9..d6bb096e3c7 100644 --- a/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap_pools_optimism_balances.sql +++ b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap_pools_optimism_balances.sql @@ -20,14 +20,14 @@ with op_pools as ( from {{ source('uniswap_v3_optimism', 'pools') }} where - token0 = '0x4200000000000000000000000000000000000042' - or token1 = '0x4200000000000000000000000000000000000042' + token0 = from_hex('0x4200000000000000000000000000000000000042') + or token1 = from_hex('0x4200000000000000000000000000000000000042') ) , filtered_balances as ( {{ balances_subset_daily( blockchain='optimism', - token_address="'0x4200000000000000000000000000000000000042'", + token_address="from_hex('0x4200000000000000000000000000000000000042')", start_date='2021-11-11' ) }} ) From c09f3f50047297f8fdc40f3c10a3c8b8bd817b10 Mon Sep 17 00:00:00 2001 From: Jason4276 <155130636+Jason4276@users.noreply.github.com> Date: Wed, 8 Jan 2025 15:25:44 +0530 Subject: [PATCH 3/5] Update _schema.yml --- .../dex/pools/optimism/uniswap/_schema.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 index e69de29bb2d..a99ada06b78 100644 --- 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 @@ -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'] + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - pool_address + - snapshot_day From 9eade12ec8cdad20a29b74a17874ca7d2979fdb9 Mon Sep 17 00:00:00 2001 From: jason Date: Thu, 9 Jan 2025 12:14:30 +0530 Subject: [PATCH 4/5] update files --- .../dex/pools/optimism/uniswap/_schema.yml | 17 +++++++++++++++++ .../uniswap_pools_optimism_balances.sql | 0 .../_sector/dex/trades/optimism/_sources.yml | 2 ++ sources/_subprojects_outputs/dex/_sources.yml | 1 - 4 files changed, 19 insertions(+), 1 deletion(-) rename dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/{ => uniswap}/uniswap_pools_optimism_balances.sql (100%) 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 index e69de29bb2d..66894032eaf 100644 --- 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 @@ -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'] + 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_pools_optimism_balances.sql b/dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/uniswap_pools_optimism_balances.sql similarity index 100% rename from dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap_pools_optimism_balances.sql rename to dbt_subprojects/daily_spellbook/models/_sector/dex/pools/optimism/uniswap/uniswap_pools_optimism_balances.sql diff --git a/sources/_sector/dex/trades/optimism/_sources.yml b/sources/_sector/dex/trades/optimism/_sources.yml index e13ce79639f..8d6534054f5 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/dex/_sources.yml b/sources/_subprojects_outputs/dex/_sources.yml index 3a58a1afe25..88751103645 100644 --- a/sources/_subprojects_outputs/dex/_sources.yml +++ b/sources/_subprojects_outputs/dex/_sources.yml @@ -60,7 +60,6 @@ sources: - name: uniswap_v3_optimism tables: - name: ovm1_pool_mapping - - name: pools - name: cow_protocol_ethereum tables: - name: trades From 232ac8fbd5af84efda591b72bfe6206bf7a8721a Mon Sep 17 00:00:00 2001 From: jason Date: Thu, 9 Jan 2025 14:33:44 +0530 Subject: [PATCH 5/5] Resolved merge conflicts in _schema.yml --- .../models/_sector/dex/pools/optimism/uniswap/_schema.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 6a6c1b7af21..c6eecee085d 100644 --- 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 @@ -9,7 +9,7 @@ models: project: uniswap contributors: jason config: - tags: ['optimism', 'op_token', 'balances', 'uniswap'] + tags: ['optimism', 'op_token', 'balances', 'uniswap','pools'] data_tests: - dbt_utils.unique_combination_of_columns: combination_of_columns: