Query editor
with time_series as (with series as (SELECT date_add('hour', step, day) AS timeFROM unnest(SEQUENCE(CAST('2023-10-24 00:00' AS TIMESTAMP), current_date, INTERVAL '1' day)) AS t(day)CROSS JOIN (SELECT * FROM unnest(SEQUENCE(1, 1440, 1)) AS t(step)))select time from seriesWHERE time between CAST('2023-10-24 00:00' as TIMESTAMP) AND NOW()),stETH_holdings as (SELECT DATE_TRUNC('hour', time) as time, stETHBalancefrom (select ROW_NUMBER() OVER (PARTITION BY DATE_TRUNC('hour', time) ORDER BY time DESC) as _rn, time, balance as stETHBalancefrom (select ROW_NUMBER() OVER (PARTITION BY call_block_number ORDER BY element_at(call_trace_address, cardinality(call_trace_address)) DESC) as rn,call_block_number, call_block_time as time, output_0/1e18 as balance from lido_ethereum.steth_call_balanceOfwhere _account = 0x325a0e5c84b4d961b19161956f57ae8ba5bb3c26AND call_success = TRUE)WHERE rn = 1)WHERE _rn = 1order by time DESC),lidoWithdrawalsRequested as (select DATE_TRUNC('hour', block_time) as time, SUM(varbinary_to_decimal(varbinary_ltrim(varbinary_substring(data,1,32)))/1e18) as stETH_withdrawal_reqfrom ethereum.logswhere contract_address = 0x889edc2edab5f40e902b864ad4d7ade8e412f9b1 -- withdrawal Request Unstake LIDOAND block_time > CAST('2023-10-20 00:00' as TIMESTAMP)AND topic0 = 0xf0cb471f23fb74ea44b8252eb1881a2dca546288d9f6e90d1a0e82fe0ed342abAND varbinary_ltrim(topic2) = 0xa00ecfda4C0272c2e8Ac777C4F520d75B3fc2A76 -- Vault Managergroup by 1),balance as (select time, stETHBalance, sign, ROW_NUMBER() OVER (Partition BY sign ORDER BY sign, time DESC) as rn--CASE WHEN stETHBalance < 0 then SUM(stETHBalance) OVER (ORDER BY time RANGE BETWEEN 'first positive preceding row' PRECEDING AND CURRENT ROW)FROM (select time, stETHBalance, 'P' as sign from stETH_holdingsUNION ALLselect time, -(stETH_withdrawal_req) as stETHBalance, 'N' as sign from lidoWithdrawalsRequested))select * from balance--joins as (select t.time, s.stETHBalance, 3 as auxfrom time_series tLEFT JOIN stETH_holdings s on s.time = t.timegroup by 1,2,3),