From a1c571b5ead0d6ef0ec56e69e12297f0ebd774b0 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Thu, 30 May 2024 10:59:21 +0200 Subject: [PATCH 01/13] lido liquidity updates --- .../lido_liquidity_base_uniswap_v3_pools.sql | 2 +- .../scroll/lido_liquidity_scroll_schema.yml | 64 ++++ .../lido_liquidity_scroll_syncswap_pools.sql | 286 ++++++++++++++++++ .../scroll/lido_liquidity_scroll_sources.yml | 14 + 4 files changed, 365 insertions(+), 1 deletion(-) create mode 100644 models/lido/liquidity/scroll/lido_liquidity_scroll_schema.yml create mode 100644 models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql create mode 100644 sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml diff --git a/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql b/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql index ff2bddb6449..6f3ccc17a14 100644 --- a/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql +++ b/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql @@ -6,7 +6,7 @@ file_format = 'delta', incremental_strategy = 'merge', unique_key = ['pool', 'time'], - post_hook='{{ expose_spells(\'["optimism"]\', + post_hook='{{ expose_spells(\'["base"]\', "project", "lido_liquidity", \'["ppclunghe"]\') }}' diff --git a/models/lido/liquidity/scroll/lido_liquidity_scroll_schema.yml b/models/lido/liquidity/scroll/lido_liquidity_scroll_schema.yml new file mode 100644 index 00000000000..0432acccca3 --- /dev/null +++ b/models/lido/liquidity/scroll/lido_liquidity_scroll_schema.yml @@ -0,0 +1,64 @@ +version: 2 + +models: + - name: lido_liquidity_scroll_syncswap_pools + meta: + blockchain: scroll + project: lido + contributors: pipistrella + config: + tags: ['zksync','lido','liquidity'] + description: + Lido wstETH liquidity pools on SyncSwap Scroll + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - pool + - blockchain + - time + columns: + - &pool_name + name: pool_name + description: "Liquidity pool's name consisting of the its blockchain, DEX project, symbols of tokens and fee value" + - &pool + name: pool + description: "Liquidity pool's address" + - &blockchain + name: blockchain + description: "Blockchain which the DEX is deployed" + - &project + name: project + description: "Project name of the DEX" + - &fee + name: fee + description: "Liquidity pool's trading fee" + - &time + name: time + description: "UTC event block date truncated to the day mark" + - &main_token + name: main_token + description: "Main token's address" + - &main_token_symbol + name: main_token_symbol + description: "Token symbol for main pool's token" + - &paired_token + name: paired_token + description: "Paired token's address" + - &paired_token_symbol + name: paired_token_symbol + description: "Token symbol for paired pool's token" + - &main_token_reserve + name: main_token_reserve + description: "Liquidity reserve of the main token in the pool" + - &paired_token_reserve + name: paired_token_reserve + description: "Liquidity reserve of the paired token in the pool" + - &main_token_usd_price + name: main_token_usd_price + description: "Price of the main token in the pool in USD" + - &paired_token_usd_price + name: paired_token_usd_price + description: "Price of the paired token in the pool in USD" + - &trading_volume + name: trading_volume + description: "USD value of the trade" \ No newline at end of file diff --git a/models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql b/models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql new file mode 100644 index 00000000000..f91f92782e3 --- /dev/null +++ b/models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql @@ -0,0 +1,286 @@ +{{ config( + schema='lido_liquidity_scroll', + alias = 'syncswap_pools', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(\'["scroll"]\', + "project", + "lido_liquidity", + \'["pipistrella"]\') }}' + ) +}} + +{% set project_start_date = '2023-10-15' %} + +with pools as ( +select pool AS address, + 'scroll' AS blockchain, + 'syncswap' AS project, + * +from {{source('syncswap_scroll','SyncSwapClassicPoolFactory_evt_PoolCreated')}} +where (token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + OR token1 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32) +) + +, tokens as ( + select distinct token + from ( + select token0 as token + from {{source('syncswap_scroll','SyncSwapClassicPoolFactory_evt_PoolCreated')}} + where token1 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + union all + select token1 + from {{source('syncswap_scroll','SyncSwapClassicPoolFactory_evt_PoolCreated')}} + where token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + union all + select 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + ) t + ) + + , tokens_prices_daily as ( +select distinct + DATE_TRUNC('day', minute) AS time, + contract_address AS token, + symbol, + decimals, + AVG(price) AS price +FROM {{source('prices','usd')}} p +{% if not is_incremental() %} +WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' +{% else %} +WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) +{% endif %} + + and date_trunc('day', minute) < current_date + and blockchain = 'scroll' + and contract_address IN (select token from tokens) +group by 1,2,3,4 +union all +select distinct + DATE_TRUNC('day', minute), + contract_address AS token, + symbol, + decimals, + LAST_VALUE(price) OVER (PARTITION BY DATE_TRUNC('day', minute),contract_address ORDER BY minute NULLS FIRST range BETWEEN UNBOUNDED preceding AND UNBOUNDED following) AS price + FROM + {{source('prices','usd')}} + WHERE + DATE_TRUNC('day', minute) = current_date + and blockchain = 'scroll' + and contract_address IN (select token from tokens) + ) + + , wsteth_prices_hourly AS ( + SELECT distinct + DATE_TRUNC('hour', minute) time, + contract_address as token, + symbol, + decimals, + last_value(price) over (partition by DATE_TRUNC('hour', minute), contract_address ORDER BY minute range between unbounded preceding AND unbounded following) AS price + FROM {{ source('prices', 'usd') }} p + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and blockchain = 'scroll' and contract_address = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + +) + +, wsteth_prices_hourly_with_lead AS ( +select time, + lead(time, 1, date_trunc('hour', now() + interval '1' hour)) over (order by time) as next_time, + price +from wsteth_prices_hourly +) + +, mint_events AS ( + select DATE_TRUNC('day', m.evt_block_time) AS time, + m.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0 AS DOUBLE)) AS amount0, + SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('syncswap_scroll','SyncSwapClassicPool_evt_Mint')}} m + left join {{source('syncswap_scroll','SyncSwapClassicPoolFactory_evt_PoolCreated')}} cr on m.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and m.contract_address in (select address from pools) + group by 1,2,3,4 + ) + + , burn_events as ( + select DATE_TRUNC('day', b.evt_block_time) AS time, + b.contract_address AS pool, + cr.token0, + cr.token1, + (-1)*SUM(CAST(amount0 AS DOUBLE)) AS amount0, + (-1)*SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('syncswap_scroll','SyncSwapClassicPool_evt_Burn')}} b + left join {{source('syncswap_scroll','SyncSwapClassicPoolFactory_evt_PoolCreated')}} cr on b.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and b.contract_address in (select address from pools) + group by 1,2,3,4 + + ) + +, swap_events as ( + select DATE_TRUNC('day', s.evt_block_time) AS time, + s.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0In AS DOUBLE) - CAST(amount0Out AS DOUBLE)) AS amount0, + SUM(CAST(amount1In AS DOUBLE) - CAST(amount1Out AS DOUBLE)) AS amount1 + from {{source('syncswap_scroll','SyncSwapClassicPool_evt_Swap')}} s + left join {{source('syncswap_scroll','SyncSwapClassicPoolFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and s.contract_address in (select address from pools) + group by 1,2,3,4 + +) + + , daily_delta_balance AS ( + +select time, pool, token0, token1, sum(amount0) as amount0, sum(amount1) as amount1 +from ( +select time, pool,token0, token1, amount0, amount1 +from mint_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from swap_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from burn_events + +) group by 1,2,3,4 +) + +, daily_delta_balance_with_lead AS ( +select time, pool, token0, token1, amount0, amount1, +lead(time, 1, now()) over (partition by pool order by time) as next_time +from daily_delta_balance +) + + +, pool_liquidity as ( +SELECT b.time, + b.pool, + token0, + token1, + SUM(amount0) AS amount0, + SUM(amount1) AS amount1 +FROM daily_delta_balance_with_lead b +GROUP BY 1,2,3,4 +) + +, swap_events_hourly as ( + select DATE_TRUNC('hour', s.evt_block_time) AS time, + s.contract_address AS pool, + sum(case when cr.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 then CAST(amount0In AS DOUBLE) + CAST(amount0Out AS DOUBLE) + else CAST(amount1In AS DOUBLE) + CAST(amount1Out AS DOUBLE) end) as wsteth_amount + from {{source('syncswap_scroll','SyncSwapClassicPool_evt_Swap')}} s + left join {{source('syncswap_scroll','SyncSwapClassicPoolFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and s.contract_address in (select address from pools) +group by 1,2 +) + +, trading_volume_hourly AS ( + SELECT + s.time, + pool, + wsteth_amount, + p.price, + COALESCE((p.price * wsteth_amount) / CAST(POWER(10, 18) AS DOUBLE), 0) AS volume + FROM + swap_events_hourly AS s + LEFT JOIN wsteth_prices_hourly_with_lead AS p ON s.time >= p.time + AND s.time < p.next_time + ) + +, trading_volume AS ( + SELECT DISTINCT + DATE_TRUNC('day', time) AS time, + pool, + SUM(volume) AS volume + FROM trading_volume_hourly + GROUP BY 1, 2 + ) + +, all_metrics AS ( + SELECT + l.pool, + pools.blockchain, + pools.project, + 0.0 as fee, + '' as pool_type, + cast(l.time as date) as time, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN l.token0 ELSE l.token1 END AS main_token, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN p0.symbol ELSE p1.symbol END AS main_token_symbol, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN l.token1 ELSE l.token0 END AS paired_token, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN p1.symbol ELSE p0.symbol END AS paired_token_symbol, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN amount0/CAST(POWER(10,p0.decimals) as double) ELSE amount1/CAST(POWER(10,p1.decimals) as double) END AS main_token_reserve, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN amount1/CAST(POWER(10, p1.decimals) as double) ELSE amount0/CAST(POWER(10, p0.decimals) as double) END AS paired_token_reserve, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN p0.price ELSE p1.price END AS main_token_usd_price, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN p1.price ELSE p0.price END AS paired_token_usd_price, + coalesce(volume,0) AS trading_volume + FROM + pool_liquidity AS l + LEFT JOIN pools ON l.pool = pools.address + LEFT JOIN tokens AS t0 ON l.token0 = t0.token + LEFT JOIN tokens AS t1 ON l.token1 = t1.token + LEFT JOIN tokens_prices_daily AS p0 ON l.time = p0.time + AND l.token0 = p0.token + LEFT JOIN tokens_prices_daily AS p1 ON l.time = p1.time + AND l.token1 = p1.token + LEFT JOIN trading_volume AS tv ON l.time = tv.time + AND l.pool = tv.pool + ) + + +select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, + pool, + blockchain, + project, + fee, + time, + main_token, + main_token_symbol, + paired_token, + paired_token_symbol, + main_token_reserve, + paired_token_reserve, + main_token_usd_price, + paired_token_usd_price, + trading_volume +from all_metrics \ No newline at end of file diff --git a/sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml b/sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml new file mode 100644 index 00000000000..880716bc8c4 --- /dev/null +++ b/sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml @@ -0,0 +1,14 @@ +version: 2 + +sources: + - name: syncswap_scroll + description: "Scroll decoded tables related to SyncSwap contract" + freshness: # default freshness + warn_after: { count: 12, period: hour } + error_after: { count: 24, period: hour } + tables: + - name: SyncSwapClassicPool_evt_Mint + loaded_at_field: evt_block_time + - name: SyncSwapClassicPool_evt_Burn + loaded_at_field: evt_block_time + From 0b76df0b65f1428b8d91e45b486ac07a132adde5 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Thu, 30 May 2024 11:15:30 +0200 Subject: [PATCH 02/13] liquidity spell --- models/lido/lido_liquidity.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/lido/lido_liquidity.sql b/models/lido/lido_liquidity.sql index 9bbe73f1fd9..2613354312a 100644 --- a/models/lido/lido_liquidity.sql +++ b/models/lido/lido_liquidity.sql @@ -45,7 +45,8 @@ ref('lido_liquidity_base_aerodrome_pools'), ref('lido_liquidity_base_uniswap_v3_pools'), ref('lido_liquidity_zksync_syncswap_pools'), - ref('lido_liquidity_linea_syncswap_pools') + ref('lido_liquidity_linea_syncswap_pools'), + ref('lido_liquidity_scroll_syncswap_pools') ] %} {% set project_start_date = '2020-12-15'%} From dde9ff29e88ef6cb1c9ae040142d4afddffd13b6 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Thu, 30 May 2024 14:42:53 +0200 Subject: [PATCH 03/13] zebra scroll pools --- .../lido_liquidity_scroll_zebra_pools.sql | 287 ++++++++++++++++++ .../scroll/lido_liquidity_scroll_sources.yml | 10 + 2 files changed, 297 insertions(+) create mode 100644 models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql diff --git a/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql b/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql new file mode 100644 index 00000000000..3a0c0234e80 --- /dev/null +++ b/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql @@ -0,0 +1,287 @@ +{{ config( + schema='lido_liquidity_scroll', + alias = 'zebra_pools', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(\'["scroll"]\', + "project", + "lido_liquidity", + \'["pipistrella"]\') }}' + ) +}} + +{% set project_start_date = '2023-11-20' %} + +with pools as ( +select pool AS address, + 'scroll' AS blockchain, + 'zebra' AS project, + 'v2' AS pool_type, + * +from {{source('zebra_scroll','ZebraV2Factory_evt_PoolCreated')}} +where (token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + OR token1 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32) +) + +, tokens as ( + select distinct token + from ( + select token0 as token + from {{source('zebra_scroll','ZebraV2Factory_evt_PoolCreated')}} + where token1 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + union all + select token1 + from {{source('zebra_scroll','ZebraV2Factory_evt_PoolCreated')}} + where token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + union all + select 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + ) t + ) + + , tokens_prices_daily as ( +select distinct + DATE_TRUNC('day', minute) AS time, + contract_address AS token, + symbol, + decimals, + AVG(price) AS price +FROM {{source('prices','usd')}} p +{% if not is_incremental() %} +WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' +{% else %} +WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) +{% endif %} + + and date_trunc('day', minute) < current_date + and blockchain = 'scroll' + and contract_address IN (select token from tokens) +group by 1,2,3,4 +union all +select distinct + DATE_TRUNC('day', minute), + contract_address AS token, + symbol, + decimals, + LAST_VALUE(price) OVER (PARTITION BY DATE_TRUNC('day', minute),contract_address ORDER BY minute NULLS FIRST range BETWEEN UNBOUNDED preceding AND UNBOUNDED following) AS price + FROM + {{source('prices','usd')}} + WHERE + DATE_TRUNC('day', minute) = current_date + and blockchain = 'scroll' + and contract_address IN (select token from tokens) + ) + + , wsteth_prices_hourly AS ( + SELECT distinct + DATE_TRUNC('hour', minute) time, + contract_address as token, + symbol, + decimals, + last_value(price) over (partition by DATE_TRUNC('hour', minute), contract_address ORDER BY minute range between unbounded preceding AND unbounded following) AS price + FROM {{ source('prices', 'usd') }} p + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and blockchain = 'scroll' and contract_address = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + +) + +, wsteth_prices_hourly_with_lead AS ( +select time, + lead(time, 1, date_trunc('hour', now() + interval '1' hour)) over (order by time) as next_time, + price +from wsteth_prices_hourly +) + +, mint_events AS ( + select DATE_TRUNC('day', m.evt_block_time) AS time, + m.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0 AS DOUBLE)) AS amount0, + SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('zebra_scroll','ZebraV2Pool_evt_Mint')}} m + left join {{source('zebra_scroll','ZebraV2Factory_evt_PoolCreated')}} cr on m.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and m.contract_address in (select address from pools) + group by 1,2,3,4 + ) + + , burn_events as ( + select DATE_TRUNC('day', b.evt_block_time) AS time, + b.contract_address AS pool, + cr.token0, + cr.token1, + (-1)*SUM(CAST(amount0 AS DOUBLE)) AS amount0, + (-1)*SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('zebra_scroll','ZebraV2Pool_evt_Burn')}} b + left join {{source('zebra_scroll','ZebraV2Factory_evt_PoolCreated')}} cr on b.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and b.contract_address in (select address from pools) + group by 1,2,3,4 + + ) + +, swap_events as ( + select DATE_TRUNC('day', s.evt_block_time) AS time, + s.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0 AS DOUBLE)) AS amount0, + SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('zebra_scroll','ZebraV2Pool_evt_Swap')}} s + left join {{source('zebra_scroll','ZebraV2Factory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and s.contract_address in (select address from pools) + group by 1,2,3,4 + +) + + , daily_delta_balance AS ( + +select time, pool, token0, token1, sum(amount0) as amount0, sum(amount1) as amount1 +from ( +select time, pool,token0, token1, amount0, amount1 +from mint_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from swap_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from burn_events + +) group by 1,2,3,4 +) + +, daily_delta_balance_with_lead AS ( +select time, pool, token0, token1, amount0, amount1, +lead(time, 1, now()) over (partition by pool order by time) as next_time +from daily_delta_balance +) + + +, pool_liquidity as ( +SELECT b.time, + b.pool, + token0, + token1, + SUM(amount0) AS amount0, + SUM(amount1) AS amount1 +FROM daily_delta_balance_with_lead b +GROUP BY 1,2,3,4 +) + +, swap_events_hourly as ( + select DATE_TRUNC('hour', s.evt_block_time) AS time, + s.contract_address AS pool, + sum(case when cr.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 then CAST(amount0 AS DOUBLE) + else CAST(amount1 AS DOUBLE) end) as wsteth_amount + from {{source('zebra_scroll','ZebraV2Pool_evt_Swap')}} s + left join {{source('zebra_scroll','ZebraV2Factory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and s.contract_address in (select address from pools) +group by 1,2 +) + +, trading_volume_hourly AS ( + SELECT + s.time, + pool, + wsteth_amount, + p.price, + COALESCE((p.price * wsteth_amount) / CAST(POWER(10, 18) AS DOUBLE), 0) AS volume + FROM + swap_events_hourly AS s + LEFT JOIN wsteth_prices_hourly_with_lead AS p ON s.time >= p.time + AND s.time < p.next_time + ) + +, trading_volume AS ( + SELECT DISTINCT + DATE_TRUNC('day', time) AS time, + pool, + SUM(volume) AS volume + FROM trading_volume_hourly + GROUP BY 1, 2 + ) + +, all_metrics AS ( + SELECT + l.pool, + pools.blockchain, + pools.project, + 0.0 as fee, + '' as pool_type, + cast(l.time as date) as time, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN l.token0 ELSE l.token1 END AS main_token, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN p0.symbol ELSE p1.symbol END AS main_token_symbol, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN l.token1 ELSE l.token0 END AS paired_token, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN p1.symbol ELSE p0.symbol END AS paired_token_symbol, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN amount0/CAST(POWER(10,p0.decimals) as double) ELSE amount1/CAST(POWER(10,p1.decimals) as double) END AS main_token_reserve, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN amount1/CAST(POWER(10, p1.decimals) as double) ELSE amount0/CAST(POWER(10, p0.decimals) as double) END AS paired_token_reserve, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN p0.price ELSE p1.price END AS main_token_usd_price, + CASE WHEN l.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 THEN p1.price ELSE p0.price END AS paired_token_usd_price, + coalesce(volume,0) AS trading_volume + FROM + pool_liquidity AS l + LEFT JOIN pools ON l.pool = pools.address + LEFT JOIN tokens AS t0 ON l.token0 = t0.token + LEFT JOIN tokens AS t1 ON l.token1 = t1.token + LEFT JOIN tokens_prices_daily AS p0 ON l.time = p0.time + AND l.token0 = p0.token + LEFT JOIN tokens_prices_daily AS p1 ON l.time = p1.time + AND l.token1 = p1.token + LEFT JOIN trading_volume AS tv ON l.time = tv.time + AND l.pool = tv.pool + ) + + +select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, + pool, + blockchain, + project, + fee, + time, + main_token, + main_token_symbol, + paired_token, + paired_token_symbol, + main_token_reserve, + paired_token_reserve, + main_token_usd_price, + paired_token_usd_price, + trading_volume +from all_metrics \ No newline at end of file diff --git a/sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml b/sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml index 880716bc8c4..3f8563c0460 100644 --- a/sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml +++ b/sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml @@ -12,3 +12,13 @@ sources: - name: SyncSwapClassicPool_evt_Burn loaded_at_field: evt_block_time + - name: zebra_scroll + description: "Scroll decoded tables related to Zebra DEX contract" + freshness: # default freshness + warn_after: { count: 12, period: hour } + error_after: { count: 24, period: hour } + tables: + - name: ZebraV2Pool_evt_Mint + loaded_at_field: evt_block_time + - name: ZebraV2Pool_evt_Burn + loaded_at_field: evt_block_time From 35853ccb0be0ce46e7a1d901152fbad2fae9e979 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Thu, 30 May 2024 16:09:26 +0200 Subject: [PATCH 04/13] liquidity spell upd --- models/lido/lido_liquidity.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/lido/lido_liquidity.sql b/models/lido/lido_liquidity.sql index 2613354312a..4c90a58c79d 100644 --- a/models/lido/lido_liquidity.sql +++ b/models/lido/lido_liquidity.sql @@ -46,7 +46,8 @@ ref('lido_liquidity_base_uniswap_v3_pools'), ref('lido_liquidity_zksync_syncswap_pools'), ref('lido_liquidity_linea_syncswap_pools'), - ref('lido_liquidity_scroll_syncswap_pools') + ref('lido_liquidity_scroll_syncswap_pools'), + ref('lido_liquidity_scroll_zebra_pools') ] %} {% set project_start_date = '2020-12-15'%} From c5ca58490d5498c37c74126bc1dbb3c86f316a72 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Thu, 30 May 2024 16:45:48 +0200 Subject: [PATCH 05/13] zebra volume fix --- .../liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql b/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql index 3a0c0234e80..f200c2aac44 100644 --- a/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql +++ b/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql @@ -201,8 +201,8 @@ GROUP BY 1,2,3,4 , swap_events_hourly as ( select DATE_TRUNC('hour', s.evt_block_time) AS time, s.contract_address AS pool, - sum(case when cr.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 then CAST(amount0 AS DOUBLE) - else CAST(amount1 AS DOUBLE) end) as wsteth_amount + sum(case when cr.token0 = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 then ABS(CAST(amount0 AS DOUBLE)) + else ABS(CAST(amount1 AS DOUBLE)) end) as wsteth_amount from {{source('zebra_scroll','ZebraV2Pool_evt_Swap')}} s left join {{source('zebra_scroll','ZebraV2Factory_evt_PoolCreated')}} cr on s.contract_address = cr.pool From 6484763f9d78f460f5caed7fae7b79cc3b31919e Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Thu, 30 May 2024 18:06:58 +0200 Subject: [PATCH 06/13] zksync maverick pools --- .../lido_liquidity_zksync_maverick_pools.sql | 253 ++++++++++++++++++ .../zksync/lido_liquidity_zksync_sources.yml | 12 + 2 files changed, 265 insertions(+) create mode 100644 models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql diff --git a/models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql b/models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql new file mode 100644 index 00000000000..f3b7620f4da --- /dev/null +++ b/models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql @@ -0,0 +1,253 @@ +{{ config( + alias = 'maverick_pools_zksync', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(\'["zksync"]\', + "project", + "lido_liquidity", + \'["pipistrella"]\') }}' + ) +}} + +{% set project_start_date = '2024-01-17' %} + +with + +pools as ( +select distinct poolAddress, tokenA, tokenB, cast(fee as double)/1e16 as fee +from {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} +where tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 + or tokenB = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 +) + + +, tokens as ( +select distinct token as address +from ( +select tokenA as token +from {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} +where tokenB = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 +union +select tokenB +from {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} +where tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 +union +select 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 +) t +) + +, tokens_prices_daily AS ( + SELECT distinct + DATE_TRUNC('day', minute) AS time, + contract_address as token, + symbol, + decimals, + avg(price) AS price + FROM {{source('prices','usd')}} p + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and date_trunc('day', minute) < current_date + and blockchain = 'zksync' + and contract_address in (select address from tokens) + group by 1,2,3,4 + union all + SELECT distinct + DATE_TRUNC('day', minute), + contract_address as token, + symbol, + decimals, + last_value(price) over (partition by DATE_TRUNC('day', minute), contract_address ORDER BY minute range between unbounded preceding AND unbounded following) AS price + FROM {{source('prices','usd')}} + WHERE date_trunc('day', minute) = current_date + and blockchain = 'zksync' + and contract_address in (select address from tokens) +) + +, wsteth_prices_hourly as ( + select time, lead(time,1, DATE_TRUNC('hour', now() + interval '1' hour)) over (order by time) as next_time, price + from ( + SELECT distinct + DATE_TRUNC('hour', minute) time, + last_value(price) over (partition by DATE_TRUNC('hour', minute), contract_address ORDER BY minute range between unbounded preceding AND unbounded following) AS price + FROM {{source('prices','usd')}} p + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and blockchain = 'zksync' + and contract_address = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 + ) p +) + +, swap_events as ( + select + date_trunc('day', sw.evt_block_time) as time, + sw.contract_address as pool, + cr.tokenA, cr.tokenB, + sum(case when tokenAIn then cast(amountIn as double) else (-1)*cast(amountOut as double) end) as amountA, + sum(case when tokenAIn then (-1)*cast(amountOut as double) else cast(amountIn as double) end) as amountB + from {{source('maverick_v1_zksync','pool_evt_Swap')}} sw + left join {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} cr on sw.contract_address = cr.poolAddress + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and sw.contract_address in (select poolAddress from pools) + group by 1,2,3,4 +) + +, addliquidity_events as ( + select date_trunc('day', call_block_time) as time, + a.contract_address as pool, + cr.tokenA, cr.tokenB, + sum(cast(output_tokenAAmount as double)) as amountA, + sum(cast(output_tokenBAmount as double)) as amountB +from {{source('maverick_v1_zksync','pool_call_addLiquidity')}} a +left join {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} cr on a.contract_address = cr.poolAddress + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', a.call_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', a.call_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and a.call_success + and a.contract_address in (select poolAddress from pools) +group by 1,2,3,4 +) + +, removeliquidity_events as ( +select date_trunc('day', call_block_time) as time, + a.contract_address as pool, + cr.tokenA, cr.tokenB, + (-1)*sum(cast(output_tokenAOut as double)) as amountA, + (-1)*sum(cast(output_tokenBOut as double)) as amountB +from {{source('maverick_v1_zksync','pool_call_removeLiquidity')}} a +left join {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} cr on a.contract_address = cr.poolAddress + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', a.call_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', a.call_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and a.call_success + and a.contract_address in (select poolAddress from pools) +group by 1,2,3,4 +) + +, daily_delta_balance AS ( + +select time, pool, tokenA, tokenB, sum(amountA) as amountA, sum(amountB) as amountB +from ( +select time, pool,tokenA, tokenB, amountA, amountB +from swap_events + +union all + +select time, pool,tokenA, tokenB, amountA, amountB +from addliquidity_events + +union all + +select time, pool,tokenA, tokenB, amountA, amountB +from removeliquidity_events + +) group by 1,2,3,4 +) + + +, pool_liquidity as ( +SELECT time, pools.fee, + pool, + b.tokenA, + b.tokenB, + coalesce((SUM(amountA)),0) AS amountA, + coalesce((SUM(amountB)),0) AS amountB +FROM daily_delta_balance b +left join pools on b.pool = pools.poolAddress +GROUP BY 1,2,3,4,5 +) + + + +, wsteth_traded_hourly as ( + select + date_trunc('hour', sw.evt_block_time) as time, + sw.contract_address as pool, + cr.tokenA, cr.tokenB, + sum(case when (cr.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 and tokenAIn = true) then amountIn + when (cr.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 and tokenAIn = false) then amountOut + when (cr.tokenA != 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 and tokenAIn = true) then amountOut + when (cr.tokenA != 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 and tokenAIn = false) then amountIn + end) as amount + from {{source('maverick_v1_zksync','pool_evt_Swap')}} sw + left join {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} cr on sw.contract_address = cr.poolAddress + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and sw.contract_address in (select poolAddress from pools) + group by 1,2,3,4 + +) + +, trading_volume_hourly as ( +select t.time, pool, t.amount*wp.price as volume_raw +from wsteth_traded_hourly t +left join wsteth_prices_hourly wp on date_trunc('hour',t.time) >= wp.time and date_trunc('hour',t.time) < wp.next_time +order by 1,2 +) + +, trading_volume as ( + select distinct date_trunc('day', time) as time + , pool + , sum(volume_raw)/1e18 as volume + from trading_volume_hourly + GROUP by 1,2 +) + +, all_metrics as ( +select + o.pool, + 'zksync' as blockchain, + 'maverick' as project, + format('%,.3f',round(coalesce(fee,0),4)) as fee, + cast(o.time as date) time, + case when o.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then o.tokenA else o.tokenB end as main_token, + case when o.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then pA.symbol else pB.symbol end as main_token_symbol, + case when o.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then o.tokenB else o.tokenA end as paired_token, + case when o.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then pB.symbol else pA.symbol end as paired_token_symbol, + case when o.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then amountA/power(10,pA.decimals) else amountB/power(10,pB.decimals) end as main_token_reserve, + case when o.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then amountB/power(10,pB.decimals) else amountA/power(10,pA.decimals) end as paired_token_reserve, + case when o.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then pA.price else pB.price end as main_token_usd_price, + case when o.tokenA = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then pB.price else pA.price end as paired_token_usd_price, + coalesce(t.volume,0) as trading_volume +from pool_liquidity o +left join tokens_prices_daily pA on o.time = pA.time and o.tokenA = pA.token +left join tokens_prices_daily pB on o.time = pB.time and o.tokenB = pB.token +left join trading_volume t on o.time = t.time and o.pool = t.pool +) + + +select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', fee, '(', cast(pool as varchar),')') as pool_name, + pool, + blockchain, + project, + fee, + time, + main_token, + main_token_symbol, + paired_token, + paired_token_symbol, + main_token_reserve, + paired_token_reserve, + main_token_usd_price, + paired_token_usd_price, + trading_volume +from all_metrics + diff --git a/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml b/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml index aa5b7074bc4..c338cae797e 100644 --- a/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml +++ b/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml @@ -11,4 +11,16 @@ sources: loaded_at_field: evt_block_time - name: SyncSwapClassicPool_evt_Burn loaded_at_field: evt_block_time + + - name: maverick_v1_zksync + description: "Zksync decoded tables related to Maverick DEX contracts" + freshness: # default freshness + warn_after: { count: 12, period: hour } + error_after: { count: 24, period: hour } + tables: + - name: pool_call_addLiquidity + loaded_at_field: call_block_time + - name: pool_call_removeLiquidity + loaded_at_field: call_block_time + From 91862c1c7dcab3cbab69f6c053d3a4f1ba9560a6 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Thu, 30 May 2024 18:34:00 +0200 Subject: [PATCH 07/13] liquidity spell maverick upd --- models/lido/lido_liquidity.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/models/lido/lido_liquidity.sql b/models/lido/lido_liquidity.sql index 4c90a58c79d..d599783b63d 100644 --- a/models/lido/lido_liquidity.sql +++ b/models/lido/lido_liquidity.sql @@ -45,6 +45,7 @@ ref('lido_liquidity_base_aerodrome_pools'), ref('lido_liquidity_base_uniswap_v3_pools'), ref('lido_liquidity_zksync_syncswap_pools'), + ref('lido_liquidity_zksync_maverick_pools'), ref('lido_liquidity_linea_syncswap_pools'), ref('lido_liquidity_scroll_syncswap_pools'), ref('lido_liquidity_scroll_zebra_pools') From 6f157eda6af892e6176453452cb19bb3c55f0d80 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Fri, 31 May 2024 11:25:40 +0200 Subject: [PATCH 08/13] velo/aero cl pools --- ...lido_liquidity_base_aerodrome_cl_pools.sql | 319 ++++++++++++++++++ ..._liquidity_optimism_aerodrome_cl_pools.sql | 319 ++++++++++++++++++ .../base/lido_liquidity_base_sources.yml | 12 + .../lido_liquidity_optimism_sources.yml | 18 +- 4 files changed, 667 insertions(+), 1 deletion(-) create mode 100644 models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql create mode 100644 models/lido/liquidity/optimism/lido_liquidity_optimism_aerodrome_cl_pools.sql diff --git a/models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql b/models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql new file mode 100644 index 00000000000..27911540778 --- /dev/null +++ b/models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql @@ -0,0 +1,319 @@ +{{ config( + schema='lido_liquidity_base', + alias = 'aerodrome_cl', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(\'["base"]\', + "project", + "lido_liquidity", + \'["pipistrella"]\') }}' + ) +}} + +{% set project_start_date = '2024-04-24' %} +with pools as ( +select pool AS address, + 'base' AS blockchain, + 'aerodrome' AS project, + 'CL' AS pool_type, * +from {{source('aerodrome_base', 'CLFactory_evt_PoolCreated')}} +where (token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 + OR token1 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452) +) + + , pool_fee as ( +select pool, max(cast(f1.output_0 as double)/10000) as fee +from {{source('aerodrome_base','CLFactory_call_getSwapFee')}} f1 +where f1.call_block_time = (select max(f2.call_block_time) + from {{source('aerodrome_base','CLFactory_call_getSwapFee')}} f2 where f2.pool = f1.pool) +and f1. pool in (select address from pools) +group by 1 +) + +, tokens as ( + select distinct token + from ( + select token0 as token + from {{source('aerodrome_base','CLFactory_evt_PoolCreated')}} + where token1 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 + union all + select token1 + from {{source('aerodrome_base','CLFactory_evt_PoolCreated')}} + where token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 + union all + select 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 + ) t + ) + +, tokens_prices_daily as ( +select distinct + DATE_TRUNC('day', minute) AS time, + contract_address AS token, + symbol, + decimals, + AVG(price) AS price +FROM {{source('prices','usd')}} p +{% if not is_incremental() %} +WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' +{% else %} +WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) --? +{% endif %} + + and date_trunc('day', minute) < current_date + and blockchain = 'base' + and contract_address IN (select token from tokens) +group by 1,2,3,4 + +union all + +select distinct + DATE_TRUNC('day', minute), + contract_address AS token, + symbol, + decimals, + LAST_VALUE(price) OVER (PARTITION BY DATE_TRUNC('day', minute),contract_address ORDER BY minute NULLS FIRST range BETWEEN UNBOUNDED preceding AND UNBOUNDED following) AS price + FROM + {{source('prices','usd')}} + WHERE + DATE_TRUNC('day', minute) = current_date + and blockchain = 'base' + and contract_address IN (select token from tokens) + ) + + , wsteth_prices_hourly AS ( + SELECT distinct + DATE_TRUNC('hour', minute) time, + contract_address as token, + symbol, + decimals, + last_value(price) over (partition by DATE_TRUNC('hour', minute), contract_address ORDER BY minute range between unbounded preceding AND unbounded following) AS price + FROM {{source('prices','usd')}} p + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and blockchain = 'base' and contract_address = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 + +) + +, wsteth_prices_hourly_with_lead AS ( +select time, + lead(time, 1, date_trunc('hour', now() + interval '1' hour)) over (order by time) as next_time, + price +from wsteth_prices_hourly +) + +, mint_events AS ( + select DATE_TRUNC('day', m.evt_block_time) AS time, + m.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0 AS DOUBLE)) AS amount0, + SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('aerodrome_base','CLPool_evt_Mint')}} m + left join {{source('aerodrome_base','CLFactory_evt_PoolCreated')}} cr on m.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and m.contract_address in (select address from pools) + group by 1,2,3,4 + ) + + , collect_events as ( + select DATE_TRUNC('day', b.evt_block_time) AS time, + b.contract_address AS pool, + cr.token0, + cr.token1, + (-1)*SUM(CAST(amount0 AS DOUBLE)) AS amount0, + (-1)*SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('aerodrome_base','CLPool_evt_Collect')}} b + left join {{source('aerodrome_base','CLFactory_evt_PoolCreated')}} cr on b.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and b.contract_address in (select address from pools) + group by 1,2,3,4 + + ) + +, swap_events as ( + select DATE_TRUNC('day', s.evt_block_time) AS time, + s.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0 AS DOUBLE)) AS amount0, + SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('aerodrome_base','CLPool_evt_Swap')}} s + left join {{source('aerodrome_base','CLFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and s.contract_address in (select address from pools) + group by 1,2,3,4 + +) +, fee_events as ( +select + DATE_TRUNC('day', s.evt_block_time) AS time, + s.contract_address AS pool, + cr.token0, + cr.token1, + (-1)*SUM(CAST(amount0 AS DOUBLE) ) AS amount0, + (-1)*SUM(CAST(amount1 AS DOUBLE) ) AS amount1 + from {{source('aerodrome_base','CLPool_evt_CollectFees')}} s + left join {{source('aerodrome_base','CLFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and s.contract_address in (select address from pools) + group by 1,2,3,4 + ) + + , daily_delta_balance AS ( + +select time, pool, token0, token1, sum(amount0) as amount0, sum(amount1) as amount1 +from ( +select time, pool,token0, token1, amount0, amount1 +from mint_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from swap_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from collect_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from fee_events +) group by 1,2,3,4 +) + +, daily_delta_balance_with_lead AS ( +select time, pool, token0, token1, amount0, amount1, +lead(time, 1, now()) over (partition by pool order by time) as next_time +from daily_delta_balance +) + + +, pool_liquidity as ( +SELECT b.time, + b.pool, + token0, + token1, + SUM(amount0) AS amount0, + SUM(amount1) AS amount1 +FROM daily_delta_balance_with_lead b +GROUP BY 1,2,3,4 +) + +, swap_events_hourly as ( + select DATE_TRUNC('hour', s.evt_block_time) AS time, + s.contract_address AS pool, + sum(case when cr.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 then abs(CAST(amount0 AS DOUBLE)) + else abs(CAST(amount1 AS DOUBLE)) end) as wsteth_amount + from {{source('aerodrome_base', 'CLPool_evt_Swap')}} s + left join {{source('aerodrome_base','CLFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and s.contract_address in (select address from pools) +group by 1,2 +) + +, trading_volume_hourly AS ( + SELECT + s.time, + pool, + wsteth_amount, + p.price, + COALESCE((p.price * wsteth_amount) / CAST(POWER(10, 18) AS DOUBLE), 0) AS volume + FROM + swap_events_hourly AS s + LEFT JOIN wsteth_prices_hourly_with_lead AS p ON s.time >= p.time + AND s.time < p.next_time + ) + +, trading_volume AS ( + SELECT DISTINCT + DATE_TRUNC('day', time) AS time, + pool, + SUM(volume) AS volume + FROM trading_volume_hourly + GROUP BY 1, 2 + ) + +, all_metrics AS ( + SELECT + l.pool, + pools.blockchain, + pools.project, + f.fee, + pools.pool_type, + cast(l.time as date) as time, + CASE WHEN l.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 THEN l.token0 ELSE l.token1 END AS main_token, + CASE WHEN l.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 THEN p0.symbol ELSE p1.symbol END AS main_token_symbol, + CASE WHEN l.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 THEN l.token1 ELSE l.token0 END AS paired_token, + CASE WHEN l.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 THEN p1.symbol ELSE p0.symbol END AS paired_token_symbol, + CASE WHEN l.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 THEN amount0/CAST(POWER(10,p0.decimals) as double) ELSE amount1/CAST(POWER(10,p1.decimals) as double) END AS main_token_reserve, + CASE WHEN l.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 THEN amount1/CAST(POWER(10, p1.decimals) as double) ELSE amount0/CAST(POWER(10, p0.decimals) as double) END AS paired_token_reserve, + CASE WHEN l.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 THEN p0.price ELSE p1.price END AS main_token_usd_price, + CASE WHEN l.token0 = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 THEN p1.price ELSE p0.price END AS paired_token_usd_price, + coalesce(volume,0) AS trading_volume + FROM + pool_liquidity AS l + LEFT JOIN pools ON l.pool = pools.address + LEFT JOIN pool_fee f on l.pool = f.pool + LEFT JOIN tokens AS t0 ON l.token0 = t0.token + LEFT JOIN tokens AS t1 ON l.token1 = t1.token + LEFT JOIN tokens_prices_daily AS p0 ON l.time = p0.time + AND l.token0 = p0.token + LEFT JOIN tokens_prices_daily AS p1 ON l.time = p1.time + AND l.token1 = p1.token + LEFT JOIN trading_volume AS tv ON l.time = tv.time + AND l.pool = tv.pool + ) + + +select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, + pool, + blockchain, + project, + fee, + time, + main_token, + main_token_symbol, + paired_token, + paired_token_symbol, + main_token_reserve, + paired_token_reserve, + main_token_usd_price, + paired_token_usd_price, + trading_volume +from all_metrics \ No newline at end of file diff --git a/models/lido/liquidity/optimism/lido_liquidity_optimism_aerodrome_cl_pools.sql b/models/lido/liquidity/optimism/lido_liquidity_optimism_aerodrome_cl_pools.sql new file mode 100644 index 00000000000..89f45ca1876 --- /dev/null +++ b/models/lido/liquidity/optimism/lido_liquidity_optimism_aerodrome_cl_pools.sql @@ -0,0 +1,319 @@ +{{ config( + schema='lido_liquidity_optimism', + alias = 'aerodrome_cl', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(\'["optimism"]\', + "project", + "lido_liquidity", + \'["pipistrella"]\') }}' + ) +}} + +{% set project_start_date = '2024-03-06' %} +with pools as ( +select pool AS address, + 'optimism' AS blockchain, + 'velodrome' AS project, + 'CL' AS pool_type, * +from {{source('velodrome_v2_optimism', 'CLFactory_evt_PoolCreated')}} +where (token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb + OR token1 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb) +) + + , pool_fee as ( +select pool, max(cast(f1.output_0 as double)/10000) as fee +from {{source('velodrome_v2_optimism','CLFactory_call_getSwapFee')}} f1 +where f1.call_block_time = (select max(f2.call_block_time) + from {{source('velodrome_v2_optimism','CLFactory_call_getSwapFee')}} f2 where f2.pool = f1.pool) +and f1. pool in (select address from pools) +group by 1 +) + +, tokens as ( + select distinct token + from ( + select token0 as token + from {{source('velodrome_v2_optimism','CLFactory_evt_PoolCreated')}} + where token1 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb + union all + select token1 + from {{source('velodrome_v2_optimism','CLFactory_evt_PoolCreated')}} + where token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb + union all + select 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb + ) t + ) + +, tokens_prices_daily as ( +select distinct + DATE_TRUNC('day', minute) AS time, + contract_address AS token, + symbol, + decimals, + AVG(price) AS price +FROM {{source('prices','usd')}} p +{% if not is_incremental() %} +WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' +{% else %} +WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) --? +{% endif %} + + and date_trunc('day', minute) < current_date + and blockchain = 'optimism' + and contract_address IN (select token from tokens) +group by 1,2,3,4 + +union all + +select distinct + DATE_TRUNC('day', minute), + contract_address AS token, + symbol, + decimals, + LAST_VALUE(price) OVER (PARTITION BY DATE_TRUNC('day', minute),contract_address ORDER BY minute NULLS FIRST range BETWEEN UNBOUNDED preceding AND UNBOUNDED following) AS price + FROM + {{source('prices','usd')}} + WHERE + DATE_TRUNC('day', minute) = current_date + and blockchain = 'optimism' + and contract_address IN (select token from tokens) + ) + + , wsteth_prices_hourly AS ( + SELECT distinct + DATE_TRUNC('hour', minute) time, + contract_address as token, + symbol, + decimals, + last_value(price) over (partition by DATE_TRUNC('hour', minute), contract_address ORDER BY minute range between unbounded preceding AND unbounded following) AS price + FROM {{source('prices','usd')}} p + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and blockchain = 'optimism' and contract_address = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb + +) + +, wsteth_prices_hourly_with_lead AS ( +select time, + lead(time, 1, date_trunc('hour', now() + interval '1' hour)) over (order by time) as next_time, + price +from wsteth_prices_hourly +) + +, mint_events AS ( + select DATE_TRUNC('day', m.evt_block_time) AS time, + m.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0 AS DOUBLE)) AS amount0, + SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('velodrome_v2_optimism','CLPool_evt_Mint')}} m + left join {{source('velodrome_v2_optimism','CLFactory_evt_PoolCreated')}} cr on m.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and m.contract_address in (select address from pools) + group by 1,2,3,4 + ) + + , collect_events as ( + select DATE_TRUNC('day', b.evt_block_time) AS time, + b.contract_address AS pool, + cr.token0, + cr.token1, + (-1)*SUM(CAST(amount0 AS DOUBLE)) AS amount0, + (-1)*SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('velodrome_v2_optimism','CLPool_evt_Collect')}} b + left join {{source('velodrome_v2_optimism','CLFactory_evt_PoolCreated')}} cr on b.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and b.contract_address in (select address from pools) + group by 1,2,3,4 + + ) + +, swap_events as ( + select DATE_TRUNC('day', s.evt_block_time) AS time, + s.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0 AS DOUBLE)) AS amount0, + SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('velodrome_v2_optimism','CLPool_evt_Swap')}} s + left join {{source('velodrome_v2_optimism','CLFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and s.contract_address in (select address from pools) + group by 1,2,3,4 + +) +, fee_events as ( +select + DATE_TRUNC('day', s.evt_block_time) AS time, + s.contract_address AS pool, + cr.token0, + cr.token1, + (-1)*SUM(CAST(amount0 AS DOUBLE) ) AS amount0, + (-1)*SUM(CAST(amount1 AS DOUBLE) ) AS amount1 + from {{source('velodrome_v2_optimism','CLPool_evt_CollectFees')}} s + left join {{source('velodrome_v2_optimism','CLFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + and s.contract_address in (select address from pools) + group by 1,2,3,4 + ) + + , daily_delta_balance AS ( + +select time, pool, token0, token1, sum(amount0) as amount0, sum(amount1) as amount1 +from ( +select time, pool,token0, token1, amount0, amount1 +from mint_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from swap_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from collect_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from fee_events +) group by 1,2,3,4 +) + +, daily_delta_balance_with_lead AS ( +select time, pool, token0, token1, amount0, amount1, +lead(time, 1, now()) over (partition by pool order by time) as next_time +from daily_delta_balance +) + + +, pool_liquidity as ( +SELECT b.time, + b.pool, + token0, + token1, + SUM(amount0) AS amount0, + SUM(amount1) AS amount1 +FROM daily_delta_balance_with_lead b +GROUP BY 1,2,3,4 +) + +, swap_events_hourly as ( + select DATE_TRUNC('hour', s.evt_block_time) AS time, + s.contract_address AS pool, + sum(case when cr.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb then abs(CAST(amount0 AS DOUBLE)) + else abs(CAST(amount1 AS DOUBLE)) end) as wsteth_amount + from {{source('velodrome_v2_optimism', 'CLPool_evt_Swap')}} s + left join {{source('velodrome_v2_optimism','CLFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + {% endif %} + + and s.contract_address in (select address from pools) +group by 1,2 +) + +, trading_volume_hourly AS ( + SELECT + s.time, + pool, + wsteth_amount, + p.price, + COALESCE((p.price * wsteth_amount) / CAST(POWER(10, 18) AS DOUBLE), 0) AS volume + FROM + swap_events_hourly AS s + LEFT JOIN wsteth_prices_hourly_with_lead AS p ON s.time >= p.time + AND s.time < p.next_time + ) + +, trading_volume AS ( + SELECT DISTINCT + DATE_TRUNC('day', time) AS time, + pool, + SUM(volume) AS volume + FROM trading_volume_hourly + GROUP BY 1, 2 + ) + +, all_metrics AS ( + SELECT + l.pool, + pools.blockchain, + pools.project, + f.fee, + pools.pool_type, + cast(l.time as date) as time, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN l.token0 ELSE l.token1 END AS main_token, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN p0.symbol ELSE p1.symbol END AS main_token_symbol, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN l.token1 ELSE l.token0 END AS paired_token, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN p1.symbol ELSE p0.symbol END AS paired_token_symbol, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN amount0/CAST(POWER(10,p0.decimals) as double) ELSE amount1/CAST(POWER(10,p1.decimals) as double) END AS main_token_reserve, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN amount1/CAST(POWER(10, p1.decimals) as double) ELSE amount0/CAST(POWER(10, p0.decimals) as double) END AS paired_token_reserve, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN p0.price ELSE p1.price END AS main_token_usd_price, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN p1.price ELSE p0.price END AS paired_token_usd_price, + coalesce(volume,0) AS trading_volume + FROM + pool_liquidity AS l + LEFT JOIN pools ON l.pool = pools.address + LEFT JOIN pool_fee f on l.pool = f.pool + LEFT JOIN tokens AS t0 ON l.token0 = t0.token + LEFT JOIN tokens AS t1 ON l.token1 = t1.token + LEFT JOIN tokens_prices_daily AS p0 ON l.time = p0.time + AND l.token0 = p0.token + LEFT JOIN tokens_prices_daily AS p1 ON l.time = p1.time + AND l.token1 = p1.token + LEFT JOIN trading_volume AS tv ON l.time = tv.time + AND l.pool = tv.pool + ) + + +select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, + pool, + blockchain, + project, + fee, + time, + main_token, + main_token_symbol, + paired_token, + paired_token_symbol, + main_token_reserve, + paired_token_reserve, + main_token_usd_price, + paired_token_usd_price, + trading_volume +from all_metrics \ No newline at end of file diff --git a/sources/lido/liquidity/base/lido_liquidity_base_sources.yml b/sources/lido/liquidity/base/lido_liquidity_base_sources.yml index a366167ad62..3eadd717998 100644 --- a/sources/lido/liquidity/base/lido_liquidity_base_sources.yml +++ b/sources/lido/liquidity/base/lido_liquidity_base_sources.yml @@ -33,6 +33,18 @@ sources: loaded_at_field: call_block_time - name: Pool_evt_Fees loaded_at_field: evt_block_time + - name: CLPool_evt_Mint + loaded_at_field: evt_block_time + - name: CLPool_evt_Collect + loaded_at_field: evt_block_time + - name: CLFactory_call_getSwapFee + loaded_at_field: call_block_time + - name: CLFactory_evt_PoolCreated + loaded_at_field: evt_block_time + - name: CLPool_evt_Swap + loaded_at_field: evt_block_time + - name: CLPool_evt_CollectFees + loaded_at_field: evt_block_time - name: uniswap_v3_base description: "Base decoded tables related to Uniswap v3 contract" diff --git a/sources/lido/liquidity/optimism/lido_liquidity_optimism_sources.yml b/sources/lido/liquidity/optimism/lido_liquidity_optimism_sources.yml index 0f059a19134..42db1b0d392 100644 --- a/sources/lido/liquidity/optimism/lido_liquidity_optimism_sources.yml +++ b/sources/lido/liquidity/optimism/lido_liquidity_optimism_sources.yml @@ -90,5 +90,21 @@ sources: - name: Pool_evt_Fees loaded_at_field: evt_block_time - name: PoolFactory_call_getFee - loaded_at_field: call_block_time + loaded_at_field: call_block_time + - name: CLFactory_evt_PoolCreated + loaded_at_field: evt_block_time + - name: CLPool_evt_Mint + loaded_at_field: evt_block_time + - name: CLPool_evt_Collect + loaded_at_field: evt_block_time + - name: CLPool_evt_Fees + loaded_at_field: evt_block_time + - name: CLPoolFactory_call_getFee + loaded_at_field: call_block_time + - name: CLFactory_call_getSwapFee + loaded_at_field: call_block_time + - name: CLPool_evt_Swap + loaded_at_field: evt_block_time + - name: CLPool_evt_CollectFees + loaded_at_field: evt_block_time \ No newline at end of file From 406380eddb98104915489fb9cca3d8d307e7db5f Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Fri, 31 May 2024 11:43:40 +0200 Subject: [PATCH 09/13] liquidity spell aero/velo upd --- models/lido/lido_liquidity.sql | 2 ++ ...pools.sql => lido_liquidity_optimism_velodrome_cl_pools.sql} | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) rename models/lido/liquidity/optimism/{lido_liquidity_optimism_aerodrome_cl_pools.sql => lido_liquidity_optimism_velodrome_cl_pools.sql} (99%) diff --git a/models/lido/lido_liquidity.sql b/models/lido/lido_liquidity.sql index d599783b63d..696200cf72a 100644 --- a/models/lido/lido_liquidity.sql +++ b/models/lido/lido_liquidity.sql @@ -25,6 +25,7 @@ ref('lido_liquidity_optimism_balancer_pools'), ref('lido_liquidity_optimism_velodrome_pools'), ref('lido_liquidity_optimism_velodrome_v2_pools'), + ref('lido_liquidity_optimism_velodrome_cl_pools'), ref('lido_liquidity_polygon_balancer_pools'), ref('lido_liquidity_polygon_uniswap_v3_pools'), ref('lido_liquidity_polygon_kyberswap_v2_pools'), @@ -43,6 +44,7 @@ ref('lido_liquidity_ethereum_solidly_pools'), ref('lido_liquidity_base_kyberswap_pools'), ref('lido_liquidity_base_aerodrome_pools'), + ref('lido_liquidity_base_aerodrome_cl_pools'), ref('lido_liquidity_base_uniswap_v3_pools'), ref('lido_liquidity_zksync_syncswap_pools'), ref('lido_liquidity_zksync_maverick_pools'), diff --git a/models/lido/liquidity/optimism/lido_liquidity_optimism_aerodrome_cl_pools.sql b/models/lido/liquidity/optimism/lido_liquidity_optimism_velodrome_cl_pools.sql similarity index 99% rename from models/lido/liquidity/optimism/lido_liquidity_optimism_aerodrome_cl_pools.sql rename to models/lido/liquidity/optimism/lido_liquidity_optimism_velodrome_cl_pools.sql index 89f45ca1876..46dd36d6365 100644 --- a/models/lido/liquidity/optimism/lido_liquidity_optimism_aerodrome_cl_pools.sql +++ b/models/lido/liquidity/optimism/lido_liquidity_optimism_velodrome_cl_pools.sql @@ -1,6 +1,6 @@ {{ config( schema='lido_liquidity_optimism', - alias = 'aerodrome_cl', + alias = 'velodrome_cl', materialized = 'incremental', file_format = 'delta', incremental_strategy = 'merge', From aa06d1d46177c4f057e065e0f53a506425915f21 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Wed, 5 Jun 2024 18:35:35 +0200 Subject: [PATCH 10/13] update according comments --- ...lido_liquidity_base_aerodrome_cl_pools.sql | 25 ++++----- .../lido_liquidity_base_uniswap_v3_pools.sql | 53 +++++-------------- ..._liquidity_optimism_velodrome_cl_pools.sql | 24 ++++----- .../lido_liquidity_scroll_syncswap_pools.sql | 31 ++++++----- .../lido_liquidity_scroll_zebra_pools.sql | 22 ++++---- .../lido_liquidity_zksync_maverick_pools.sql | 22 ++++---- 6 files changed, 74 insertions(+), 103 deletions(-) diff --git a/models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql b/models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql index 27911540778..70b7a73af2c 100644 --- a/models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql +++ b/models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql @@ -5,10 +5,10 @@ file_format = 'delta', incremental_strategy = 'merge', unique_key = ['pool', 'time'], - post_hook='{{ expose_spells(\'["base"]\', - "project", - "lido_liquidity", - \'["pipistrella"]\') }}' + post_hook='{{ expose_spells(blockchains = \'["base"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["pipistrella"]\') }}' ) }} @@ -58,7 +58,7 @@ FROM {{source('prices','usd')}} p {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} -WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) --? +WHERE {{ incremental_predicate('p.minute') }} {% endif %} and date_trunc('day', minute) < current_date @@ -93,7 +93,7 @@ select distinct {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} and blockchain = 'base' and contract_address = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452 @@ -119,7 +119,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('m.evt_block_time') }} {% endif %} and m.contract_address in (select address from pools) @@ -139,7 +139,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('b.evt_block_time') }} {% endif %} and b.contract_address in (select address from pools) @@ -160,7 +160,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -181,7 +181,7 @@ select {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) group by 1,2,3,4 @@ -240,7 +240,7 @@ GROUP BY 1,2,3,4 {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -301,7 +301,8 @@ group by 1,2 ) -select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, +select --CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, + blockchain||' '||project||' '||coalesce(paired_token_symbol,'unknown')||':'||main_token_symbol||' '||pool_type||' '||format('%,.3f',round(coalesce(fee,0),4)) as pool_name, pool, blockchain, project, diff --git a/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql b/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql index 6f3ccc17a14..3bb20d397c4 100644 --- a/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql +++ b/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql @@ -6,10 +6,10 @@ file_format = 'delta', incremental_strategy = 'merge', unique_key = ['pool', 'time'], - post_hook='{{ expose_spells(\'["base"]\', - "project", - "lido_liquidity", - \'["ppclunghe"]\') }}' + post_hook='{{ expose_spells(blockchains = \'["base"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["pipistrella"]\') }}' ) }} @@ -63,7 +63,7 @@ with {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} AND DATE_TRUNC('day', minute) < current_date AND blockchain = 'base' @@ -111,7 +111,7 @@ with {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} AND blockchain = 'base' AND contract_address IN (SELECT address FROM tokens) @@ -133,7 +133,7 @@ with {% if not is_incremental() %} WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('sw.evt_block_time') }} {% endif %} and sw.contract_address IN ( SELECT @@ -162,7 +162,7 @@ with {% if not is_incremental() %} WHERE DATE_TRUNC('day', mt.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', mt.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('mt.evt_block_time') }} {% endif %} and mt.contract_address IN ( SELECT @@ -193,7 +193,7 @@ with {% if not is_incremental() %} WHERE DATE_TRUNC('day', bn.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', bn.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('bn.evt_block_time') }} {% endif %} and bn.contract_address IN ( SELECT @@ -223,7 +223,7 @@ with {% if not is_incremental() %} WHERE DATE_TRUNC('day', bn.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', bn.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('bn.evt_block_time') }} {% endif %} and bn.contract_address IN ( SELECT @@ -320,7 +320,7 @@ with {% if not is_incremental() %} WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('sw.evt_block_time') }} {% endif %} GROUP BY 1, 2, 3, 4 @@ -389,36 +389,7 @@ with LEFT JOIN trading_volume AS tv ON l.time = tv.time AND l.pool = tv.pool ) SELECT - CONCAT( - CAST( - CONCAT( - CAST( - CONCAT( - CAST( - CONCAT( - CAST( - CONCAT( - CAST(blockchain AS VARCHAR), - CAST( - CONCAT(CAST(' ' AS VARCHAR), CAST(project AS VARCHAR)) AS VARCHAR - ) - ) AS VARCHAR - ), - CAST(' ' AS VARCHAR) - ) AS VARCHAR - ), - CAST( - COALESCE(paired_token_symbol, 'unknown') AS VARCHAR - ) - ) AS VARCHAR - ), - CAST(':' AS VARCHAR) - ) AS VARCHAR - ), - CAST(main_token_symbol AS VARCHAR), - CAST(' ' AS VARCHAR), - format('%,.3f',round(coalesce(fee,0),4)) - ) AS pool_name, + blockchain||' '||project||' '||COALESCE(paired_token_symbol, 'unknown')||':'||main_token_symbol||' '||format('%,.3f',round(coalesce(fee,0),4)) AS pool_name, * FROM all_metrics \ No newline at end of file diff --git a/models/lido/liquidity/optimism/lido_liquidity_optimism_velodrome_cl_pools.sql b/models/lido/liquidity/optimism/lido_liquidity_optimism_velodrome_cl_pools.sql index 46dd36d6365..c1b4c093d87 100644 --- a/models/lido/liquidity/optimism/lido_liquidity_optimism_velodrome_cl_pools.sql +++ b/models/lido/liquidity/optimism/lido_liquidity_optimism_velodrome_cl_pools.sql @@ -5,10 +5,10 @@ file_format = 'delta', incremental_strategy = 'merge', unique_key = ['pool', 'time'], - post_hook='{{ expose_spells(\'["optimism"]\', - "project", - "lido_liquidity", - \'["pipistrella"]\') }}' + post_hook='{{ expose_spells(blockchains = \'["optimism"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["pipistrella"]\') }}' ) }} @@ -58,7 +58,7 @@ FROM {{source('prices','usd')}} p {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} -WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) --? +WHERE {{ incremental_predicate('p.minute') }} {% endif %} and date_trunc('day', minute) < current_date @@ -93,7 +93,7 @@ select distinct {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} and blockchain = 'optimism' and contract_address = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb @@ -119,7 +119,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('m.evt_block_time') }} {% endif %} and m.contract_address in (select address from pools) @@ -139,7 +139,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('b.evt_block_time') }} {% endif %} and b.contract_address in (select address from pools) @@ -160,7 +160,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -181,7 +181,7 @@ select {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) group by 1,2,3,4 @@ -240,7 +240,7 @@ GROUP BY 1,2,3,4 {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -301,7 +301,7 @@ group by 1,2 ) -select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, +select blockchain||' '||project||' '||coalesce(paired_token_symbol,'unknown')||':'||main_token_symbol||' '||pool_type||' '||format('%,.3f',round(coalesce(fee,0),4)) as pool_name, pool, blockchain, project, diff --git a/models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql b/models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql index f91f92782e3..33a6f7d992e 100644 --- a/models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql +++ b/models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql @@ -5,10 +5,10 @@ file_format = 'delta', incremental_strategy = 'merge', unique_key = ['pool', 'time'], - post_hook='{{ expose_spells(\'["scroll"]\', - "project", - "lido_liquidity", - \'["pipistrella"]\') }}' + post_hook='{{ expose_spells(blockchains = \'["scroll"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["pipistrella"]\') }}' ) }} @@ -50,12 +50,12 @@ FROM {{source('prices','usd')}} p {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} -WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) +WHERE {{ incremental_predicate('p.minute') }} {% endif %} - and date_trunc('day', minute) < current_date - and blockchain = 'scroll' - and contract_address IN (select token from tokens) +and date_trunc('day', minute) < current_date +and blockchain = 'scroll' +and contract_address IN (select token from tokens) group by 1,2,3,4 union all select distinct @@ -83,10 +83,9 @@ select distinct {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} - - and blockchain = 'scroll' and contract_address = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 + and blockchain = 'scroll' and contract_address = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 ) @@ -110,7 +109,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('m.evt_block_time') }} {% endif %} and m.contract_address in (select address from pools) @@ -130,7 +129,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('b.evt_block_time') }} {% endif %} and b.contract_address in (select address from pools) @@ -151,7 +150,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -208,7 +207,7 @@ GROUP BY 1,2,3,4 {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -268,7 +267,7 @@ group by 1,2 ) -select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, +select blockchain||' '||project||' '||coalesce(paired_token_symbol,'unknown')||':'||main_token_symbol||' '||pool_type||' '||format('%,.3f',round(coalesce(fee,0),4)) as pool_name, pool, blockchain, project, diff --git a/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql b/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql index f200c2aac44..d97d696e628 100644 --- a/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql +++ b/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql @@ -5,10 +5,10 @@ file_format = 'delta', incremental_strategy = 'merge', unique_key = ['pool', 'time'], - post_hook='{{ expose_spells(\'["scroll"]\', - "project", - "lido_liquidity", - \'["pipistrella"]\') }}' + post_hook='{{ expose_spells(blockchains = \'["scroll"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["pipistrella"]\') }}' ) }} @@ -51,7 +51,7 @@ FROM {{source('prices','usd')}} p {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} -WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) +WHERE {{ incremental_predicate('p.minute') }} {% endif %} and date_trunc('day', minute) < current_date @@ -84,7 +84,7 @@ select distinct {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} and blockchain = 'scroll' and contract_address = 0xf610A9dfB7C89644979b4A0f27063E9e7d7Cda32 @@ -111,7 +111,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('m.evt_block_time') }} {% endif %} and m.contract_address in (select address from pools) @@ -131,7 +131,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('b.evt_block_time') }} {% endif %} and b.contract_address in (select address from pools) @@ -152,7 +152,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -209,7 +209,7 @@ GROUP BY 1,2,3,4 {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -269,7 +269,7 @@ group by 1,2 ) -select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, +select blockchain||' '||project||' '||coalesce(paired_token_symbol,'unknown')||':'||main_token_symbol||' '||pool_type||' '||format('%,.3f',round(coalesce(fee,0),4)) as pool_name, pool, blockchain, project, diff --git a/models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql b/models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql index f3b7620f4da..edd71fefe8c 100644 --- a/models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql +++ b/models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql @@ -4,10 +4,10 @@ file_format = 'delta', incremental_strategy = 'merge', unique_key = ['pool', 'time'], - post_hook='{{ expose_spells(\'["zksync"]\', - "project", - "lido_liquidity", - \'["pipistrella"]\') }}' + post_hook='{{ expose_spells(blockchains = \'["zksync"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["pipistrella"]\') }}' ) }} @@ -49,7 +49,7 @@ select 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} and date_trunc('day', minute) < current_date and blockchain = 'zksync' @@ -78,7 +78,7 @@ select 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} and blockchain = 'zksync' and contract_address = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 @@ -97,7 +97,7 @@ select 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 {% if not is_incremental() %} WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('sw.evt_block_time') }} {% endif %} and sw.contract_address in (select poolAddress from pools) group by 1,2,3,4 @@ -114,7 +114,7 @@ left join {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} cr on a.con {% if not is_incremental() %} WHERE DATE_TRUNC('day', a.call_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', a.call_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('a.call_block_time') }} {% endif %} and a.call_success and a.contract_address in (select poolAddress from pools) @@ -132,7 +132,7 @@ left join {{source('maverick_v1_zksync','factory_evt_PoolCreated')}} cr on a.con {% if not is_incremental() %} WHERE DATE_TRUNC('day', a.call_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', a.call_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('a.call_block_time') }} {% endif %} and a.call_success and a.contract_address in (select poolAddress from pools) @@ -189,7 +189,7 @@ GROUP BY 1,2,3,4,5 {% if not is_incremental() %} WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', sw.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('sw.evt_block_time') }} {% endif %} and sw.contract_address in (select poolAddress from pools) group by 1,2,3,4 @@ -234,7 +234,7 @@ left join trading_volume t on o.time = t.time and o.pool = t.pool ) -select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', fee, '(', cast(pool as varchar),')') as pool_name, +select blockchain||' '||project||' '||coalesce(paired_token_symbol,'unknown')||':'||main_token_symbol||' '||fee||'('||cast(pool as varchar)||')' as pool_name, pool, blockchain, project, From d0bbc834469945cd70d3bc411d7fbf844ea21d71 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Mon, 10 Jun 2024 13:36:19 +0200 Subject: [PATCH 11/13] zksync syncswap fixes --- .../lido_liquidity_zksync_syncswap_pools.sql | 14 +- ...ido_liquidity_zksync_syncswap_v2_pools.sql | 286 ++++++++++++++++++ .../zksync/lido_liquidity_zksync_sources.yml | 11 + 3 files changed, 304 insertions(+), 7 deletions(-) create mode 100644 models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_v2_pools.sql diff --git a/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_pools.sql b/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_pools.sql index 3114a6d43a9..6803b8e8f7f 100644 --- a/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_pools.sql +++ b/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_pools.sql @@ -12,7 +12,7 @@ ) }} -{% set project_start_date = '2024-02-10' %} +{% set project_start_date = '2024-01-01' %} with pools as ( select pool AS address, @@ -50,7 +50,7 @@ FROM {{source('prices','usd')}} p {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} -WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) +WHERE {{ incremental_predicate('p.minute') }} {% endif %} and date_trunc('day', minute) < current_date @@ -83,7 +83,7 @@ select distinct {% if not is_incremental() %} WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', p.minute) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('p.minute') }} {% endif %} and blockchain = 'zksync' and contract_address = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 @@ -110,7 +110,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('m.evt_block_time') }} {% endif %} and m.contract_address in (select address from pools) @@ -130,7 +130,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('b.evt_block_time') }} {% endif %} and b.contract_address in (select address from pools) @@ -151,7 +151,7 @@ from wsteth_prices_hourly {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) @@ -208,7 +208,7 @@ GROUP BY 1,2,3,4 {% if not is_incremental() %} WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' {% else %} - WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE_TRUNC('day', NOW() - INTERVAL '1' day) + WHERE {{ incremental_predicate('s.evt_block_time') }} {% endif %} and s.contract_address in (select address from pools) diff --git a/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_v2_pools.sql b/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_v2_pools.sql new file mode 100644 index 00000000000..a026c75f999 --- /dev/null +++ b/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_v2_pools.sql @@ -0,0 +1,286 @@ +{{ config( + schema='lido_liquidity_zksync', + alias = 'syncswap_v2_pools', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(\'["zksync"]\', + "project", + "lido_liquidity", + \'["pipistrella"]\') }}' + ) +}} + +{% set project_start_date = '2024-01-01' %} + +with pools as ( +select pool AS address, + 'zksync' AS blockchain, + 'syncswap' AS project, + * +from {{source('syncswap_v2_zksync','SyncSwapAquaPoolFactory_evt_PoolCreated')}} +where (token0 = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 + OR token1 = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867) +) + +, tokens as ( + select distinct token + from ( + select token0 as token + from {{source('syncswap_v2_zksync','SyncSwapAquaPoolFactory_evt_PoolCreated')}} + where token1 = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 + union all + select token1 + from {{source('syncswap_v2_zksync','SyncSwapAquaPoolFactory_evt_PoolCreated')}} + where token0 = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 + union all + select 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 + ) t + ) + + , tokens_prices_daily as ( +select distinct + DATE_TRUNC('day', minute) AS time, + contract_address AS token, + symbol, + decimals, + AVG(price) AS price +FROM {{source('prices','usd')}} p +{% if not is_incremental() %} +WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' +{% else %} +WHERE {{ incremental_predicate('p.minute') }} +{% endif %} + + and date_trunc('day', minute) < current_date + and blockchain = 'zksync' + and contract_address IN (select token from tokens) +group by 1,2,3,4 +union all +select distinct + DATE_TRUNC('day', minute), + contract_address AS token, + symbol, + decimals, + LAST_VALUE(price) OVER (PARTITION BY DATE_TRUNC('day', minute),contract_address ORDER BY minute NULLS FIRST range BETWEEN UNBOUNDED preceding AND UNBOUNDED following) AS price + FROM + {{source('prices','usd')}} + WHERE + DATE_TRUNC('day', minute) = current_date + and blockchain = 'zksync' + and contract_address IN (select token from tokens) + ) + + , wsteth_prices_hourly AS ( + SELECT distinct + DATE_TRUNC('hour', minute) time, + contract_address as token, + symbol, + decimals, + last_value(price) over (partition by DATE_TRUNC('hour', minute), contract_address ORDER BY minute range between unbounded preceding AND unbounded following) AS price + FROM {{ source('prices', 'usd') }} p + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', p.minute) >= DATE '{{ project_start_date }}' + {% else %} + WHERE {{ incremental_predicate('p.minute') }} + {% endif %} + + and blockchain = 'zksync' and contract_address = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 + +) + +, wsteth_prices_hourly_with_lead AS ( +select time, + lead(time, 1, date_trunc('hour', now() + interval '1' hour)) over (order by time) as next_time, + price +from wsteth_prices_hourly +) + +, mint_events AS ( + select DATE_TRUNC('day', m.evt_block_time) AS time, + m.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0 AS DOUBLE)) AS amount0, + SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('syncswap_v2_zksync','SyncSwapAquaPool_evt_Mint')}} m + left join {{source('syncswap_v2_zksync','SyncSwapAquaPoolFactory_evt_PoolCreated')}} cr on m.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', m.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE {{ incremental_predicate('m.evt_block_time') }} + {% endif %} + + and m.contract_address in (select address from pools) + group by 1,2,3,4 + ) + + , burn_events as ( + select DATE_TRUNC('day', b.evt_block_time) AS time, + b.contract_address AS pool, + cr.token0, + cr.token1, + (-1)*SUM(CAST(amount0 AS DOUBLE)) AS amount0, + (-1)*SUM(CAST(amount1 AS DOUBLE)) AS amount1 + from {{source('syncswap_v2_zksync','SyncSwapAquaPool_evt_Burn')}} b + left join {{source('syncswap_v2_zksync','SyncSwapAquaPoolFactory_evt_PoolCreated')}} cr on b.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', b.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE {{ incremental_predicate('b.evt_block_time') }} + {% endif %} + + and b.contract_address in (select address from pools) + group by 1,2,3,4 + + ) + +, swap_events as ( + select DATE_TRUNC('day', s.evt_block_time) AS time, + s.contract_address AS pool, + cr.token0, + cr.token1, + SUM(CAST(amount0In AS DOUBLE) - CAST(amount0Out AS DOUBLE)) AS amount0, + SUM(CAST(amount1In AS DOUBLE) - CAST(amount1Out AS DOUBLE)) AS amount1 + from {{source('syncswap_v2_zksync','SyncSwapAquaPool_evt_Swap')}} s + left join {{source('syncswap_v2_zksync','SyncSwapAquaPoolFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE {{ incremental_predicate('s.evt_block_time') }} + {% endif %} + + and s.contract_address in (select address from pools) + group by 1,2,3,4 + +) + + , daily_delta_balance AS ( + +select time, pool, token0, token1, sum(amount0) as amount0, sum(amount1) as amount1 +from ( +select time, pool,token0, token1, amount0, amount1 +from mint_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from swap_events + +union all + +select time, pool,token0, token1, amount0, amount1 +from burn_events + +) group by 1,2,3,4 +) + +, daily_delta_balance_with_lead AS ( +select time, pool, token0, token1, amount0, amount1, +lead(time, 1, now()) over (partition by pool order by time) as next_time +from daily_delta_balance +) + + +, pool_liquidity as ( +SELECT b.time, + b.pool, + token0, + token1, + SUM(amount0) AS amount0, + SUM(amount1) AS amount1 +FROM daily_delta_balance_with_lead b +GROUP BY 1,2,3,4 +) + +, swap_events_hourly as ( + select DATE_TRUNC('hour', s.evt_block_time) AS time, + s.contract_address AS pool, + sum(case when cr.token0 = 0x703b52F2b28fEbcB60E1372858AF5b18849FE867 then CAST(amount0In AS DOUBLE) + CAST(amount0Out AS DOUBLE) + else CAST(amount1In AS DOUBLE) + CAST(amount1Out AS DOUBLE) end) as wsteth_amount + from {{source('syncswap_v2_zksync','SyncSwapAquaPool_evt_Swap')}} s + left join {{source('syncswap_v2_zksync','SyncSwapAquaPoolFactory_evt_PoolCreated')}} cr on s.contract_address = cr.pool + + {% if not is_incremental() %} + WHERE DATE_TRUNC('day', s.evt_block_time) >= DATE '{{ project_start_date }}' + {% else %} + WHERE {{ incremental_predicate('s.evt_block_time') }} + {% endif %} + + and s.contract_address in (select address from pools) +group by 1,2 +) + +, trading_volume_hourly AS ( + SELECT + s.time, + pool, + wsteth_amount, + p.price, + COALESCE((p.price * wsteth_amount) / CAST(POWER(10, 18) AS DOUBLE), 0) AS volume + FROM + swap_events_hourly AS s + LEFT JOIN wsteth_prices_hourly_with_lead AS p ON s.time >= p.time + AND s.time < p.next_time + ) + +, trading_volume AS ( + SELECT DISTINCT + DATE_TRUNC('day', time) AS time, + pool, + SUM(volume) AS volume + FROM trading_volume_hourly + GROUP BY 1, 2 + ) + +, all_metrics AS ( + SELECT + l.pool, + pools.blockchain, + pools.project, + 0.0 as fee, + '' as pool_type, + cast(l.time as date) as time, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN l.token0 ELSE l.token1 END AS main_token, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN p0.symbol ELSE p1.symbol END AS main_token_symbol, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN l.token1 ELSE l.token0 END AS paired_token, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN p1.symbol ELSE p0.symbol END AS paired_token_symbol, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN amount0/CAST(POWER(10,p0.decimals) as double) ELSE amount1/CAST(POWER(10,p1.decimals) as double) END AS main_token_reserve, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN amount1/CAST(POWER(10, p1.decimals) as double) ELSE amount0/CAST(POWER(10, p0.decimals) as double) END AS paired_token_reserve, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN p0.price ELSE p1.price END AS main_token_usd_price, + CASE WHEN l.token0 = 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb THEN p1.price ELSE p0.price END AS paired_token_usd_price, + coalesce(volume,0) AS trading_volume + FROM + pool_liquidity AS l + LEFT JOIN pools ON l.pool = pools.address + LEFT JOIN tokens AS t0 ON l.token0 = t0.token + LEFT JOIN tokens AS t1 ON l.token1 = t1.token + LEFT JOIN tokens_prices_daily AS p0 ON l.time = p0.time + AND l.token0 = p0.token + LEFT JOIN tokens_prices_daily AS p1 ON l.time = p1.time + AND l.token1 = p1.token + LEFT JOIN trading_volume AS tv ON l.time = tv.time + AND l.pool = tv.pool + ) + + +select CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(blockchain,CONCAT(' ', project)) ,' '), coalesce(paired_token_symbol,'unknown')),':') , main_token_symbol, ' ', pool_type, ' ', format('%,.3f',round(coalesce(fee,0),4))) as pool_name, + pool, + blockchain, + project, + fee, + time, + main_token, + main_token_symbol, + paired_token, + paired_token_symbol, + main_token_reserve, + paired_token_reserve, + main_token_usd_price, + paired_token_usd_price, + trading_volume +from all_metrics \ No newline at end of file diff --git a/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml b/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml index c338cae797e..34a1f4ca019 100644 --- a/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml +++ b/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml @@ -12,6 +12,17 @@ sources: - name: SyncSwapClassicPool_evt_Burn loaded_at_field: evt_block_time + - name: syncswap_v2_zksync + description: "Zksync decoded tables related to SyncSwap V2 contract" + freshness: # default freshness + warn_after: { count: 12, period: hour } + error_after: { count: 24, period: hour } + tables: + - name: SyncSwapAquaPool_evt_Mint + loaded_at_field: evt_block_time + - name: SyncSwapAquaPool_evt_Burn + loaded_at_field: evt_block_time + - name: maverick_v1_zksync description: "Zksync decoded tables related to Maverick DEX contracts" freshness: # default freshness From eba8baecfaa8387b03c83242a1aa70f2665450a8 Mon Sep 17 00:00:00 2001 From: ppclunghe Date: Mon, 10 Jun 2024 14:00:56 +0200 Subject: [PATCH 12/13] liquidity spell fix with zksync pools --- models/lido/lido_liquidity.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/models/lido/lido_liquidity.sql b/models/lido/lido_liquidity.sql index 696200cf72a..781f2e22b48 100644 --- a/models/lido/lido_liquidity.sql +++ b/models/lido/lido_liquidity.sql @@ -47,6 +47,7 @@ ref('lido_liquidity_base_aerodrome_cl_pools'), ref('lido_liquidity_base_uniswap_v3_pools'), ref('lido_liquidity_zksync_syncswap_pools'), + ref('lido_liquidity_zksync_syncswap_v2_pools'), ref('lido_liquidity_zksync_maverick_pools'), ref('lido_liquidity_linea_syncswap_pools'), ref('lido_liquidity_scroll_syncswap_pools'), From 7efe8d43529d6cb07c3aaa6a2a6d7cab8fbd45ba Mon Sep 17 00:00:00 2001 From: Huang Geyang Date: Mon, 10 Jun 2024 22:12:55 +0800 Subject: [PATCH 13/13] Update post_hook --- .../zksync/lido_liquidity_zksync_syncswap_v2_pools.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_v2_pools.sql b/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_v2_pools.sql index a026c75f999..06403042175 100644 --- a/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_v2_pools.sql +++ b/models/lido/liquidity/zksync/lido_liquidity_zksync_syncswap_v2_pools.sql @@ -5,10 +5,10 @@ file_format = 'delta', incremental_strategy = 'merge', unique_key = ['pool', 'time'], - post_hook='{{ expose_spells(\'["zksync"]\', - "project", - "lido_liquidity", - \'["pipistrella"]\') }}' + post_hook='{{ expose_spells(blockchains = \'["zksync"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["pipistrella"]\') }}' ) }}