diff --git a/models/lido/lido_liquidity.sql b/models/lido/lido_liquidity.sql index 9bbe73f1fd9..781f2e22b48 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,9 +44,14 @@ 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_linea_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'), + ref('lido_liquidity_scroll_zebra_pools') ] %} {% set project_start_date = '2020-12-15'%} 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..70b7a73af2c --- /dev/null +++ b/models/lido/liquidity/base/lido_liquidity_base_aerodrome_cl_pools.sql @@ -0,0 +1,320 @@ +{{ config( + schema='lido_liquidity_base', + alias = 'aerodrome_cl', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(blockchains = \'["base"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["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 {{ incremental_predicate('p.minute') }} +{% 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 {{ incremental_predicate('p.minute') }} + {% 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 {{ incremental_predicate('m.evt_block_time') }} + {% 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 {{ 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(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 {{ incremental_predicate('s.evt_block_time') }} + {% 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 {{ 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 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 {{ 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, + 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, + 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, + 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/base/lido_liquidity_base_uniswap_v3_pools.sql b/models/lido/liquidity/base/lido_liquidity_base_uniswap_v3_pools.sql index ff2bddb6449..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(\'["optimism"]\', - "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 new file mode 100644 index 00000000000..c1b4c093d87 --- /dev/null +++ b/models/lido/liquidity/optimism/lido_liquidity_optimism_velodrome_cl_pools.sql @@ -0,0 +1,319 @@ +{{ config( + schema='lido_liquidity_optimism', + alias = 'velodrome_cl', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(blockchains = \'["optimism"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["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 {{ incremental_predicate('p.minute') }} +{% 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 {{ incremental_predicate('p.minute') }} + {% 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 {{ incremental_predicate('m.evt_block_time') }} + {% 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 {{ 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(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 {{ incremental_predicate('s.evt_block_time') }} + {% 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 {{ 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 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 {{ 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, + 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 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, + 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/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..33a6f7d992e --- /dev/null +++ b/models/lido/liquidity/scroll/lido_liquidity_scroll_syncswap_pools.sql @@ -0,0 +1,285 @@ +{{ config( + schema='lido_liquidity_scroll', + alias = 'syncswap_pools', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['pool', 'time'], + post_hook='{{ expose_spells(blockchains = \'["scroll"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["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 {{ incremental_predicate('p.minute') }} +{% 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 {{ incremental_predicate('p.minute') }} + {% 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 {{ 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_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 {{ 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_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 {{ 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 = 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 {{ 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 = 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 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, + 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/scroll/lido_liquidity_scroll_zebra_pools.sql b/models/lido/liquidity/scroll/lido_liquidity_scroll_zebra_pools.sql new file mode 100644 index 00000000000..d97d696e628 --- /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(blockchains = \'["scroll"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["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 {{ incremental_predicate('p.minute') }} +{% 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 {{ incremental_predicate('p.minute') }} + {% 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 {{ 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('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 {{ 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(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 {{ 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 = 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 + + {% 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 = 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 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, + 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/zksync/lido_liquidity_zksync_maverick_pools.sql b/models/lido/liquidity/zksync/lido_liquidity_zksync_maverick_pools.sql new file mode 100644 index 00000000000..edd71fefe8c --- /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(blockchains = \'["zksync"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["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 {{ incremental_predicate('p.minute') }} + {% 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 {{ incremental_predicate('p.minute') }} + {% 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 {{ incremental_predicate('sw.evt_block_time') }} + {% 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 {{ incremental_predicate('a.call_block_time') }} + {% 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 {{ incremental_predicate('a.call_block_time') }} + {% 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 {{ incremental_predicate('sw.evt_block_time') }} + {% 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 blockchain||' '||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/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..06403042175 --- /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(blockchains = \'["zksync"]\', + spell_type = "project", + spell_name = "lido_liquidity", + contributors = \'["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/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 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..3f8563c0460 --- /dev/null +++ b/sources/lido/liquidity/scroll/lido_liquidity_scroll_sources.yml @@ -0,0 +1,24 @@ +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 + + - 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 diff --git a/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml b/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml index aa5b7074bc4..34a1f4ca019 100644 --- a/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml +++ b/sources/lido/liquidity/zksync/lido_liquidity_zksync_sources.yml @@ -11,4 +11,27 @@ sources: loaded_at_field: evt_block_time - 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 + 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 +