Query editor
with date_col as (SELECT dtFROMUNNEST(sequence(cast('2023-06-19' as date), current_date, interval '1' day)) t (dt)), pool_inflow as (select date_trunc('day', evt_block_time) as dt, sum( value / pow(10, 18) ) as inflow_volumefrom erc20_bnb.evt_Transferwhere contract_address = 0x55d398326f99059fF775485246999027B3197955 and "to" = 0x33aCA8fD6f40104804794F96Af850406112df381group by date_trunc('day', evt_block_time)), pool_outflow as (select date_trunc('day', evt_block_time) as dt, sum( value / pow(10, 18) ) as outflow_volumefrom erc20_bnb.evt_Transferwhere contract_address = 0x55d398326f99059fF775485246999027B3197955 and "from" = 0x33aCA8fD6f40104804794F96Af850406112df381group by date_trunc('day', evt_block_time)), pool_net_change as (select coalesce(i.dt, o.dt) as dt,coalesce(i.inflow_volume, 0) as inflow_volume, coalesce(o.outflow_volume, 0) as outflow_volume,coalesce(i.inflow_volume, 0) - coalesce(o.outflow_volume, 0) as net_changefrom pool_inflow i full join pool_outflow o on i.dt = o.dt), daily_tvl as (select c.dt, a.inflow_volume, a.outflow_volume, a.net_change,case when a.tvl is null then lag(a.tvl) over(order by c.dt) else a.tvl end as tvlfrom date_col c left join (select *, sum(net_change) over(order by dt rows between unbounded preceding and current row) as tvlfrom pool_net_change) a on c.dt = a.dt), daily_bflp_mint as (select date_trunc('day', evt_block_time) as dt, sum( value / pow(10, 18) ) as mint_cntfrom erc20_bnb.evt_Transferwhere contract_address = 0x33aCA8fD6f40104804794F96Af850406112df381and "from" = 0x0000000000000000000000000000000000000000group by date_trunc('day', evt_block_time)), daily_bflp_burn as (select date_trunc('day', evt_block_time) as dt, sum( value / pow(10, 18) ) as burn_cntfrom erc20_bnb.evt_Transferwhere contract_address = 0x33aCA8fD6f40104804794F96Af850406112df381and "to" = 0x0000000000000000000000000000000000000000group by date_trunc('day', evt_block_time)), daily_existing_bflp as (select *, sum(net_change) over(order by dt rows between unbounded preceding and current row ) as existing_cntfrom (select coalesce(m.dt, b.dt) as dt,coalesce(m.mint_cnt, 0) - coalesce(b.burn_cnt, 0) as net_changefrom daily_bflp_mint m full join daily_bflp_burn b on m.dt = b.dt) a), daily_existing_bflp_filled as (select c.dt, case when b.existing_cnt is null then lag(b.existing_cnt) over(order by c.dt) else b.existing_cnt end as existing_cntfrom date_col c left join daily_existing_bflp b on c.dt = b.dt), lp_token_price as (select t.dt, t.tvl, e.existing_cnt,t.tvl / e.existing_cnt as pricefrom daily_tvl t left join daily_existing_bflp_filled e on t.dt = e.dt), daily_est_apr as (select dt, price, lag(price, 1) over(order by dt) as p,case when dt > date '2023-06-19' and dt < date '2023-07-19' then (price - 1) / (1 * date_diff('day', date '2023-06-19', dt)) * 365else (price - lag(price, 30) over(order by dt)) / (lag(price, 30) over(order by dt) * 30) * 365 end as est_aprfrom lp_token_price)select c.dt,case when t.tvl is null then lag(t.tvl) over(order by c.dt) else t.tvl end as "TVL",