Query editor
-- get deposit and withdraw amounts for token0 and token1with lp_deposits as (selectm.contract_address as lp_addr,f.tokenA as token0,f.tokenB as token1,evt_block_time,cast(amount0 as double) as amount0,cast(amount1 as double) as amount1from uniswap_v3_ethereum.Pair_evt_Mint mleft join uniswap_v3_ethereum.Factory_call_createPool f on f.output_pool = m.contract_address),lp_withdraws as (selectb.contract_address as lp_addr,f.tokenA as token0,f.tokenB as token1,evt_block_time,-cast(amount0 as double) as amount0,-cast(amount1 as double) as amount1from uniswap_v3_ethereum.Pair_evt_Burn bleft join uniswap_v3_ethereum.Factory_call_createPool f on f.output_pool = b.contract_address),lp_swaps as (selects.contract_address as lp_addr,f.tokenA as token0,f.tokenB as token1,evt_block_time,cast(amount0 as double) as amount0,cast(amount1 as double) as amount1from uniswap_v3_ethereum.Pair_evt_Swap sleft join uniswap_v3_ethereum.Factory_call_createPool f on f.output_pool = s.contract_address),-- calc daily flow, i.e., net (deposits - withdraws) amount of token0 and token1lp_daily_flow as (selectlp_addr, token0, token1, date_trunc('day', evt_block_time) as day,sum(amount0) as amount0,sum(amount1) as amount1from (select * from lp_depositsunion allselect * from lp_withdrawsunion allselect * from lp_swaps) tgroup by 1,2,3,4),-- calc daily stock, i.e., cumulative (deposits - withdraws) amount for token0 and token1lp_daily_stock_with_gaps as (selectlp_addr, token0, token1, day,-- amount0,sum(amount0) over(partition by lp_addr order by day) as cumu_amount0,-- amount1,sum(amount1) over(partition by lp_addr order by day) as cumu_amount1,lead(day, 1, current_date) over(partition by lp_addr order by day) as next_dayfrom lp_daily_flow),