Query editor
with params as (selectdate '2022-01-01' as start_date,0xE47c80e8c23f6B4A1aE41c34837a0599D5D16bb0 as migrator,0xcdf7028ceab81fa0c6971208e83fa7872994bee5 as token,0x727200b48f3c812bfb404b578574e1c03694edb122d80fa6dcb352a9e4f8a938 as topic0_wrap,0xf64ae1cc3e0e07da9c895b3225439175cab5838aca24c4e74852704858c96a7b as topic0_unwrap),transfers as (selecterc20.evt_block_time as block_time,erc20.evt_tx_hash as tx_hash,erc20."from" as from_addr,erc20."to" as to_addr,cast(erc20.value as double)/ cast(power(10, coalesce((select max(m.decimals) from tokens_ethereum.erc20 m where m.contract_address = params.token), 18)) as double) as amtfrom erc20_ethereum.evt_transfer erc20join params on erc20.contract_address = params.token and (erc20."from" = params.migrator or erc20."to" = params.migrator)where cast(erc20.evt_block_time as date) >= params.start_date),hourly as (selectdate_trunc('hour', t.block_time) as hour,sum(case when t.from_addr = params.migrator and exists (select 1 from ethereum.logs logswhere logs.tx_hash = t.tx_hash and logs.contract_address = params.migrator and logs.topic0 = params.topic0_wrap) then t.amt else 0 end) as wrapped,sum(case when t.to_addr = params.migrator and exists (select 1 from ethereum.logs logswhere logs.tx_hash = t.tx_hash and logs.contract_address = params.migrator and logs.topic0 = params.topic0_unwrap) then t.amt else 0 end) as unwrapped,sum(case when t.to_addr = params.migrator and not exists (select 1 from ethereum.logs logswhere logs.tx_hash = t.tx_hash and logs.contract_address = params.migrator and logs.topic0 in (params.topic0_wrap, params.topic0_unwrap)) then t.amt else 0 end) as inflow_wo_topics,sum(case when t.from_addr = params.migrator and not exists (select 1 from ethereum.logs logswhere logs.tx_hash = t.tx_hash and logs.contract_address = params.migrator and logs.topic0 in (params.topic0_wrap, params.topic0_unwrap)) then t.amt else 0 end) as outflow_wo_topicsfrom transfers tjoin params on truegroup by 1),series as (selecthour,date_trunc('day', hour) as day,date_diff('day', min(date_trunc('day', hour)) over (), date_trunc('day', hour)) as x_days,(wrapped - unwrapped) as hourly_t,sum(wrapped - unwrapped) over (order by hour) as cum_keep_tfrom hourly),daily as (selectday,min(x_days) as x_days,sum(hourly_t) as hourly_t,max_by(cum_keep_t, hour) as cum_keep_tfrom seriesgroup by 1