Query editor
with params as (select{{wallet}} as wallet,date '{{start_date}}' as start_date),counterparty_events as (selectif(t."from" = p.wallet, t."to", t."from") as counterparty,t.block_time,t.hash as tx_hash,'native_tx' as sourcefrom bnb.transactions tcross join params pwhere t.block_date >= p.start_dateand t.success = trueand (t."from" = p.wallet or t."to" = p.wallet)and if(t."from" = p.wallet, t."to", t."from") is not nullunion allselectif(e."from" = p.wallet, e."to", e."from") as counterparty,e.evt_block_time as block_time,e.evt_tx_hash as tx_hash,'erc20_transfer' as sourcefrom erc20_bnb.evt_transfer ecross join params pwhere e.evt_block_date >= p.start_dateand (e."from" = p.wallet or e."to" = p.wallet)and if(e."from" = p.wallet, e."to", e."from") is not null),counterparties as (selectcounterparty,min(block_time) as first_seen_with_wallet,max(block_time) as last_seen_with_wallet,count(distinct tx_hash) as wallet_interaction_txs,array_join(array_sort(array_distinct(array_agg(source))), ', ') as counterparty_sourcesfrom counterparty_eventsgroup by 1),oneinch_addresses as (selectaddress,block_time,tx_hash,role,amount_usdfrom dex.trades dcross join unnest(array['tx_from', 'taker', 'maker'],array[d.tx_from, d.taker, d.maker]) as u(role, address)cross join params pwhere d.blockchain = 'bnb'and d.project = '1inch'and d.block_month >= date_trunc('month', p.start_date)and d.block_date >= p.start_dateand address is not null),