Query editor
with volume_n_fees as (selectlp_addr, token_pair, fee_tier,count(token_pair) as num_trades,sum(amount_usd) as total_volume_usd,sum(fees_usd) as total_fees_usd,max(block_time) - min(block_time) as duration,date_diff('minute', min(block_time), max(block_time)) / 60.0000 / 24.0000 as duration_daysfrom query_2279996 -- ethereum uniswap v3 tradeswhere block_date >= date_trunc('day', current_timestamp) - interval '{{last n days}}' day -- subtract by n instead of (n-1) days cuz the current day may not be a full day and may have null amount_usdand (token_bought_symbol = upper('{{token}}') or token_sold_symbol = upper('{{token}}'))group by 1,2,3),-- get daily prices for those tokens on volume_n_feestokens_daily_prices as (select *from query_1965145 -- tokens daily median prices by chainwhere blockchain = 'ethereum' andsymbol in (select split_part(token_pair, '-', 1) as token from volume_n_feesunionselect split_part(token_pair, '-', 2) as token from volume_n_fees)),-- join lp daily stock and their daily median prices for these tokensdaily_lp_stock_n_prices as (selects.*,p0.symbol as symbol0, p0.decimals as decimals0, p0.price as price0,p1.symbol as symbol1, p1.decimals as decimals1, p1.price as price1from query_1985566 s -- LP daily stockleft join tokens_daily_prices p0on p0.contract_address = s.token0 and p0.day = s.dayleft join tokens_daily_prices p1on p1.contract_address = s.token1 and p1.day = s.daywhere p0.contract_address is not nulland p1.contract_address is not null),-- calculate daily TVLdaily_tvl as (selectlp_addr, token0, token1,concat(symbol0, '-', symbol1) as token_pair,day,cumu_amount0/power(10, decimals0) as cumu_amount0,cumu_amount1/power(10, decimals1) as cumu_amount1,price0, price1,(cumu_amount0/power(10, decimals0)*price0 + cumu_amount1/power(10, decimals1)*price1) as tvl_usdfrom daily_lp_stock_n_priceswhere (cumu_amount0/power(10, decimals0)*price0 + cumu_amount1/power(10, decimals1)*price1) is not null -- TODO: rescue rows with NULL TVL),-- extract TVL x days ago and TVL todaytvl_xdays_ago as (select *from daily_tvlwhere day = date_trunc('day', current_timestamp) - interval '{{last n days}}' day),tvl_today as (