Query editor
/* Dune: @gasweighing & Twitter/X: @Selectrong */with date_parameter as (select casewhen '{{timeframe}}' = 'show all time' then date '2024-12-01'when '{{timeframe}}' = 'past one year' then now() - interval '1' yearwhen '{{timeframe}}' = 'past six months' then now() - interval '6' monthwhen '{{timeframe}}' = 'past three months' then now() - interval '3' monthwhen '{{timeframe}}' = 'past one month' then now() - interval '1' monthwhen '{{timeframe}}' = 'past two weeks' then now() - interval '14' daywhen '{{timeframe}}' = 'past one week' then now() - interval '7' dayend as start_date), aave_pool_txn as (select date_trunc('day', block_time) as txn_date, hash as txn_hash, bytearray_substring("data", 1, 4) as method_idfrom sonic.transactionsjoin date_parameter dp on block_time >= dp.start_datewhere ("from" = 0x5362dbb1e601abf3a4c14c22ffeda64042e5eaa3or "to" = 0x5362dbb1e601abf3a4c14c22ffeda64042e5eaa3)and bytearray_substring("data", 1, 4) in (0x617ba037, 0x69328dec)and success = trueand casewhen '{{current_date}}' = 'exclude' then date_trunc('day', block_time) < date_trunc('day', current_date)when '{{current_date}}' = 'include' then date_trunc('day', block_time) <= date_trunc('day', current_date)else trueend), market_prices as (select date_trunc('day', "minute") as mkt_date, symbol, contract_address, decimals, avg(price) as mkt_pricefrom prices.usdwhere contract_address in (0x29219dd400f2bf60e5a23d13be72b486d4038894,0x039e2fb66102314ce7b64ce5ce3e5183bc94ad38,0x50c42deacd8fc9773493ed674b675be577f2634b)and casewhen '{{current_date}}' = 'exclude' then date_trunc('day', "minute") < date_trunc('day', current_date)when '{{current_date}}' = 'include' then date_trunc('day', "minute") <= date_trunc('day', current_date)else trueendgroup by 1, 2, 3, 4), tokens_pooled as (select apt.txn_date, mkp.symbol, casewhen apt.method_id = 0x617ba037 then 'Deposited'when apt.method_id = 0x69328dec then 'Withdrawn'end as method, round(cast(tkn."value" as double) / pow(10, mkp.decimals) * mkp.mkt_price, 6) as amount_usd, apt.txn_hashfrom erc20_sonic.evt_transfer tknjoin aave_pool_txn apt on tkn.evt_tx_hash = apt.txn_hashjoin market_prices mkp on tkn.contract_address = mkp.contract_addressand apt.txn_date = mkp.mkt_date