Query editor
with token_balances as (select -- tokens sold-sum(cast(value as double) / pow(10, b.decimals)) as amount, "from" as addressfrom erc20_ethereum.evt_Transfer ajoin tokens.erc20 b on a.contract_address = b.contract_addresswhere a.contract_address = 0xd6203889C22D9fe5e938a9200f50FDFFE9dD8e02group by 2union allselect -- tokens boughtsum(cast(value as double) / pow(10, b.decimals)) as amount, a.to as addressfrom erc20_ethereum.evt_Transfer ajoin tokens.erc20 b on a.contract_address = b.contract_addresswhere a.contract_address = 0xd6203889C22D9fe5e938a9200f50FDFFE9dD8e02group by 2),token_holders as (selectaddress, sum(amount) as balancefrom token_balancesgroup by 1),token_trades as (selectt.tx_from, t.token_bought_address, t.token_sold_address, t.amount_usd, casewhen t.token_bought_address = 0xd6203889C22D9fe5e938a9200f50FDFFE9dD8e02 then t.token_bought_amountelse 0end as token_bought_amount, casewhen t.token_sold_address = 0xd6203889C22D9fe5e938a9200f50FDFFE9dD8e02 then t.token_sold_amountelse 0end as token_sold_amount, t.block_time, h.balancefrom dex.trades tinner join token_holders h on t.tx_from = h.addresswhere (t.token_sold_address = 0xd6203889C22D9fe5e938a9200f50FDFFE9dD8e02 or t.token_bought_address = 0xd6203889C22D9fe5e938a9200f50FDFFE9dD8e02)union allselect -- adding cowswap agg trades for now; needs to use dex_aggregator in the futurec.trader as tx_from, c.buy_token_address as token_bought_address, c.sell_token_address as token_sold_address, c.usd_value as amount_usd, casewhen c.buy_token_address = 0xd6203889C22D9fe5e938a9200f50FDFFE9dD8e02 then c.units_boughtelse 0end as token_bought_amount, case