Query editor
-- get base uniswap v3 tradeswith univ3_trades_base as (select u.*,concat(cast(f.fee/10000.00 as varchar), '%') as fee_tier, -- fee tier (i.e. 3000 -> 0.3%)u.amount_usd * (f.fee/1e6) as fees_usd -- volume in USD * %fee = fees earnedfrom query_1938945 u -- dune.coindataschool.result_univ_3_trades_all_chains uleft join uniswap_v3_base.UniswapV3Factory_call_createPool f on f.output_pool = u.lp_addrwhere u.blockchain = 'base'),-- calc volume and fees for pools composed of token X and over the last n days (X and n are user inputs)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 univ3_trades_basewhere block_date >= current_date - 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 = '{{token}}' or token_sold_symbol = '{{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 = 'base' 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 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_3675726 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