From 7f829ba0c275f638630366abf5a2c3ebb884f0cd Mon Sep 17 00:00:00 2001 From: Henrystats <65398020+henrystats@users.noreply.github.com> Date: Wed, 13 Mar 2024 01:36:14 +0100 Subject: [PATCH 1/3] Create dex liquidity spell --- macros/models/_sector/dex/dex_liquidity.sql | 43 +++++++++++++++++++ models/_sector/dex/liquidity/_schema.yml | 33 ++++++++++++++ .../_sector/dex/liquidity/chains/_schema.yml | 33 ++++++++++++++ .../chains/dex_ethereum_liquidity.sql | 18 ++++++++ .../dex/liquidity/dex_liquidity_beta.sql | 42 ++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 macros/models/_sector/dex/dex_liquidity.sql create mode 100644 models/_sector/dex/liquidity/_schema.yml create mode 100644 models/_sector/dex/liquidity/chains/_schema.yml create mode 100644 models/_sector/dex/liquidity/chains/dex_ethereum_liquidity.sql create mode 100644 models/_sector/dex/liquidity/dex_liquidity_beta.sql diff --git a/macros/models/_sector/dex/dex_liquidity.sql b/macros/models/_sector/dex/dex_liquidity.sql new file mode 100644 index 00000000000..601ec8dc8d1 --- /dev/null +++ b/macros/models/_sector/dex/dex_liquidity.sql @@ -0,0 +1,43 @@ +{% macro dex_liquidity( + blockchain = null + , pools_model = null + , balances_model = null + ) +%} + +WITH + +unnest_pool as ( + SELECT + p.blockchain, + p.project, + p.version, + p.pool, + t.token as token_address + FROM + {{ pools_model }} p + CROSS JOIN UNNEST(p.tokens) AS t(token) +) + +SELECT + b.day, + up.blockchain, + up.project, + up.version, + up.pool, + up.token_address, + b.balance, + b.balance_raw, + b.balance_usd, + b.type +FROM +unnest_pool up +LEFT JOIN +{{ balances_model }} b + ON up.pool = b.wallet_address + AND up.token_address = b.token_address +{% if is_incremental() %} +WHERE {{ incremental_predicate('b.day') }} +{% endif %} + +{% endmacro %} \ No newline at end of file diff --git a/models/_sector/dex/liquidity/_schema.yml b/models/_sector/dex/liquidity/_schema.yml new file mode 100644 index 00000000000..8d1fe9a6c33 --- /dev/null +++ b/models/_sector/dex/liquidity/_schema.yml @@ -0,0 +1,33 @@ +version: 2 + +models: + - name: dex_liquidity_beta + meta: + blockchain: ethereum + sector: dex + contributors: Henrystats + config: + tags: [ 'dex', 'liquidity', 'beta' ] + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - day + - pool + - token_address + columns: + - &blockchain + name: blockchain + description: "Blockchain which the DEX is deployed" + - &project + name: project + description: "Project name of the DEX" + - &version + name: version + description: "Version of the contract built and deployed by the DEX project" + - name: pool + - name: token_address + - name: day + - name: balance + - name: balance_raw + - name: balance_usd + - name: type \ No newline at end of file diff --git a/models/_sector/dex/liquidity/chains/_schema.yml b/models/_sector/dex/liquidity/chains/_schema.yml new file mode 100644 index 00000000000..8dc54374aea --- /dev/null +++ b/models/_sector/dex/liquidity/chains/_schema.yml @@ -0,0 +1,33 @@ +version: 2 + +models: + - name: dex_ethereum_liquidity + meta: + blockchain: ethereum + sector: dex + contributors: Henrystats + config: + tags: [ 'dex', 'liquidity', 'beta' ] + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - day + - pool + - token_address + columns: + - &blockchain + name: blockchain + description: "Blockchain which the DEX is deployed" + - &project + name: project + description: "Project name of the DEX" + - &version + name: version + description: "Version of the contract built and deployed by the DEX project" + - name: pool + - name: token_address + - name: day + - name: balance + - name: balance_raw + - name: balance_usd + - name: type \ No newline at end of file diff --git a/models/_sector/dex/liquidity/chains/dex_ethereum_liquidity.sql b/models/_sector/dex/liquidity/chains/dex_ethereum_liquidity.sql new file mode 100644 index 00000000000..010fbd647eb --- /dev/null +++ b/models/_sector/dex/liquidity/chains/dex_ethereum_liquidity.sql @@ -0,0 +1,18 @@ +{{ config( + schema = 'dex_ethereum', + alias = 'liquidity', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'day', 'token_address'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')] + ) +}} + +{{ + dex_liquidity( + blockchain = 'ethereum' + , pools_model = ref('dex_ethereum_pools') + , balances_model = ref('tokens_ethereum_balances_daily') + ) +}} \ No newline at end of file diff --git a/models/_sector/dex/liquidity/dex_liquidity_beta.sql b/models/_sector/dex/liquidity/dex_liquidity_beta.sql new file mode 100644 index 00000000000..070d7a65862 --- /dev/null +++ b/models/_sector/dex/liquidity/dex_liquidity_beta.sql @@ -0,0 +1,42 @@ +{{ config( + schema = 'dex' + , alias = 'liquidity_beta' + , materialized = 'view' + , unique_key = ['pool', 'token_address', 'day'] + , incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')] + ) +}} + +{% set base_models = [ + ref('dex_ethereum_liquidity') +] %} + +WITH base_union AS ( + SELECT * + FROM ( + {% for base_model in base_models %} + SELECT + blockchain + , project + , version + , pool + , token_address + , balance + , balance_raw + , balance_usd + , type + , fee + FROM + {{ base_model }} + {% if is_incremental() %} + WHERE + {{ incremental_predicate('day') }} + {% endif %} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} + ) +) + +SELECT * FROM base_union \ No newline at end of file From 33ea72fb726c8c06e9936991df57d4e14d365754 Mon Sep 17 00:00:00 2001 From: Henrystats <65398020+henrystats@users.noreply.github.com> Date: Tue, 19 Mar 2024 02:06:07 +0100 Subject: [PATCH 2/3] smol change --- macros/models/_sector/dex/dex_liquidity.sql | 34 ++++++------------- models/_sector/dex/liquidity/_schema.yml | 4 +-- .../_sector/dex/liquidity/chains/_schema.yml | 4 +-- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/macros/models/_sector/dex/dex_liquidity.sql b/macros/models/_sector/dex/dex_liquidity.sql index 601ec8dc8d1..2cc55c90e04 100644 --- a/macros/models/_sector/dex/dex_liquidity.sql +++ b/macros/models/_sector/dex/dex_liquidity.sql @@ -5,37 +5,23 @@ ) %} -WITH - -unnest_pool as ( - SELECT - p.blockchain, - p.project, - p.version, - p.pool, - t.token as token_address - FROM - {{ pools_model }} p - CROSS JOIN UNNEST(p.tokens) AS t(token) -) - SELECT b.day, - up.blockchain, - up.project, - up.version, - up.pool, - up.token_address, + p.blockchain, + p.project, + p.version, + p.pool, + b.token_address, + b.token_symbol, b.balance, b.balance_raw, - b.balance_usd, - b.type + b.balance_usd FROM -unnest_pool up +{{ pools_model }} p LEFT JOIN {{ balances_model }} b - ON up.pool = b.wallet_address - AND up.token_address = b.token_address + ON p.pool = b.address + AND b.token_standard = 'erc20' {% if is_incremental() %} WHERE {{ incremental_predicate('b.day') }} {% endif %} diff --git a/models/_sector/dex/liquidity/_schema.yml b/models/_sector/dex/liquidity/_schema.yml index 8d1fe9a6c33..3eee16eec98 100644 --- a/models/_sector/dex/liquidity/_schema.yml +++ b/models/_sector/dex/liquidity/_schema.yml @@ -26,8 +26,8 @@ models: description: "Version of the contract built and deployed by the DEX project" - name: pool - name: token_address + - name: token_symbol - name: day - name: balance - name: balance_raw - - name: balance_usd - - name: type \ No newline at end of file + - name: balance_usd \ No newline at end of file diff --git a/models/_sector/dex/liquidity/chains/_schema.yml b/models/_sector/dex/liquidity/chains/_schema.yml index 8dc54374aea..e511ec7be7b 100644 --- a/models/_sector/dex/liquidity/chains/_schema.yml +++ b/models/_sector/dex/liquidity/chains/_schema.yml @@ -26,8 +26,8 @@ models: description: "Version of the contract built and deployed by the DEX project" - name: pool - name: token_address + - name: token_symbol - name: day - name: balance - name: balance_raw - - name: balance_usd - - name: type \ No newline at end of file + - name: balance_usd \ No newline at end of file From b233c43f5a35cf5dd038a2a952bee0681d43f871 Mon Sep 17 00:00:00 2001 From: Henrystats <65398020+henrystats@users.noreply.github.com> Date: Tue, 19 Mar 2024 02:10:43 +0100 Subject: [PATCH 3/3] fix --- macros/models/_sector/dex/dex_liquidity.sql | 1 - models/_sector/dex/liquidity/_schema.yml | 1 - models/_sector/dex/liquidity/chains/_schema.yml | 1 - models/_sector/dex/liquidity/dex_liquidity_beta.sql | 11 +++++------ models/_sector/dex/pools/dex_pools_beta.sql | 2 +- 5 files changed, 6 insertions(+), 10 deletions(-) diff --git a/macros/models/_sector/dex/dex_liquidity.sql b/macros/models/_sector/dex/dex_liquidity.sql index 2cc55c90e04..052b70cdc40 100644 --- a/macros/models/_sector/dex/dex_liquidity.sql +++ b/macros/models/_sector/dex/dex_liquidity.sql @@ -14,7 +14,6 @@ SELECT b.token_address, b.token_symbol, b.balance, - b.balance_raw, b.balance_usd FROM {{ pools_model }} p diff --git a/models/_sector/dex/liquidity/_schema.yml b/models/_sector/dex/liquidity/_schema.yml index 3eee16eec98..724198e10d2 100644 --- a/models/_sector/dex/liquidity/_schema.yml +++ b/models/_sector/dex/liquidity/_schema.yml @@ -29,5 +29,4 @@ models: - name: token_symbol - name: day - name: balance - - name: balance_raw - name: balance_usd \ No newline at end of file diff --git a/models/_sector/dex/liquidity/chains/_schema.yml b/models/_sector/dex/liquidity/chains/_schema.yml index e511ec7be7b..e21cb16fc4f 100644 --- a/models/_sector/dex/liquidity/chains/_schema.yml +++ b/models/_sector/dex/liquidity/chains/_schema.yml @@ -29,5 +29,4 @@ models: - name: token_symbol - name: day - name: balance - - name: balance_raw - name: balance_usd \ No newline at end of file diff --git a/models/_sector/dex/liquidity/dex_liquidity_beta.sql b/models/_sector/dex/liquidity/dex_liquidity_beta.sql index 070d7a65862..8bbe14b39c2 100644 --- a/models/_sector/dex/liquidity/dex_liquidity_beta.sql +++ b/models/_sector/dex/liquidity/dex_liquidity_beta.sql @@ -1,7 +1,7 @@ {{ config( schema = 'dex' , alias = 'liquidity_beta' - , materialized = 'view' + , materialized = 'incremental' , unique_key = ['pool', 'token_address', 'day'] , incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')] ) @@ -16,16 +16,15 @@ WITH base_union AS ( FROM ( {% for base_model in base_models %} SELECT - blockchain + day + , blockchain , project , version , pool , token_address - , balance - , balance_raw + , token_symbol + , balance , balance_usd - , type - , fee FROM {{ base_model }} {% if is_incremental() %} diff --git a/models/_sector/dex/pools/dex_pools_beta.sql b/models/_sector/dex/pools/dex_pools_beta.sql index faca48cbe86..87f20977a7c 100644 --- a/models/_sector/dex/pools/dex_pools_beta.sql +++ b/models/_sector/dex/pools/dex_pools_beta.sql @@ -1,7 +1,7 @@ {{ config( schema = 'dex' , alias = 'pools_beta' - , materialized = 'view' + , materialized = 'incremental' , unique_key = ['pool'] , incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.creation_block_time')] )