Query editor
with time as(SELECTgenerate_series('2021-08-04 00:00', NOW(), '1 day'::interval) as Date),treasury_weth as (SELECTdate_trunc('day', block_time) as Date,COALESCE(sum(-e.value/1e18), 0) as treasury_weth_supplyFROM erc20."ERC20_evt_Transfer" eLEFT JOIN ethereum."transactions" tx ON evt_tx_hash = tx.hashWHERE "contract_address" = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' -- weth contract addressand e."from" = '\x31F8Cc382c9898b273eff4e0b7626a6987C846E8'GROUP BY 1UNION ALLSELECTdate_trunc('day', block_time) as Date,COALESCE(sum(e.value/1e18), 0) as treasury_weth_supplyFROM erc20."ERC20_evt_Transfer" eLEFT JOIN ethereum."transactions" tx ON evt_tx_hash = tx.hashWHERE "contract_address" = '\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' -- weth contract addressand e."to" = '\x31F8Cc382c9898b273eff4e0b7626a6987C846E8'GROUP BY 1),final_treasury_weth as(SELECTDate,sum(sum(treasury_weth_supply)) over (order by Date) as treasury_wethFROM(SELECT Date, treasury_weth_supply as treasury_weth_supply FROM treasury_weth UNION ALLSELECT Date, 0 as treasury_weth_supply FROM time) tGROUP BY 1),weth_price AS (SELECT avg(price) weth_price, date_trunc('day', minute) AS "date"FROM prices.usdWHERE minute >= '2021-03-28'and contract_address ='\xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'group by 2),weth_price_ma as (select "date", AVG(weth_price) OVER(ORDER BY "date" ROWS BETWEEN 44 PRECEDING AND CURRENT ROW) AS weth_price_45d_mafrom weth_price)select tw."date", treasury_rfv, treasury_weth * weth_price_45d_ma as weth_mv, (treasury_weth * weth_price_45d_ma)/treasury_rfv as weth_pct_of_rfv,0.33*treasury_rfv as target,0.33*treasury_rfv - treasury_weth*weth_price_45d_ma as deltafrom final_treasury_weth twleft join dune_user_generated.treasury_rfv rfv on tw."date" = rfv."date"left join weth_price_ma ma on ma."date" = tw."date"order by 1 desc