Query editor
withPE as( SELECT price from prices.usd_latest where symbol = 'ETH' and blockchain is null),AE as( select date(block_time) as day, "to" as address ,"from" as bridge, call_type, 0-cast(value as double)/1e18 as amount from ethereum.traces where success = true union allselect date(block_time) as day, "from" as address ,"to" as bridge, call_type, cast(value as double)/1e18 as amount from ethereum.traces where success = true),BE as( select day, 0 as contract_address, sum(amount) as eth_amount, sum(amount * (select * from PE) ) as eth_valuefrom AEwhere bridge = 0x49048044D57e1C92A77f79988d21Fa8fAF74E97eand (call_type is null or call_type NOT IN ('delegatecall', 'callcode', 'staticcall') )and day > date('2023-03-01')group by 1,2order by day desc),P as( SELECT * from prices.usd_latest),A as( select date(evt_block_time) as day, "to" as address ,"from" as bridge, contract_address, 0-cast(value as double)/1e6 as amount from evms.erc20_transfers union allselect date(evt_block_time) as day, "from" as address ,"to" as bridge, contract_address, cast(value as double)/1e6 as amount from evms.erc20_transfers ),B as( select day, a.contract_address, b.symbol,case when b.symbol in ('USDC','USDT') then sum(b.price*amount) else sum(b.price*amount/1e12) end as valuefrom A as ajoin P as b on a.contract_address = b.contract_addresswhere bridge = 0x57891966931Eb4Bb6FB81430E6cE0A03AAbDe063group by 1,2,3order by day desc ),T as( SELECT distinct *FROM unnest(sequence(date('2023-03-01'), date(now()), interval '1' day)) AS t(day)cross join (select distinct contract_address, symbol from B) ),C as( select a.day, a.symbol, case when a.symbol in ('USDC','USDT') then a.symbol else 'altcoins' end as cate,case when b.day is null then 0 else value end as valuefrom T as aleft join B as b on a.day = b.day and a.contract_address = b.contract_address )select day, 'ETH' as cate, sum(eth_value) over (partition by contract_address order by day) as tvl from BE-- union all-- select day, cate, sum(value) over (partition by cate order by day) as tvl from C