Query editor
-- get optimism uniswap v3 tradeswith univ3_trades_op 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 (select output_pool, feefrom uniswap_v3_optimism.Factory_call_createPoolunion allselect newAddress as output_pool, feefrom uniswap_v3_optimism.ovm1_pool_mapping) f on f.output_pool = u.lp_addrwhere u.blockchain = 'optimism'),-- 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_opwhere 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 = '{{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 = 'optimism' 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_2048641 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,