Query editor
withtotal_trader as (selectdistinct to as addrfrom erc20_ethereum.evt_Transferwhere evt_block_time >= date '2023-03-10'and evt_block_number >= 17228251and contract_address = 0x72e4f9f808c49a2a61de9c5896298920dc4eeea9 -- HarryPotterObamaSonic10Inu (BITCOIN)and to not in (0x2cc846fff0b08fb3bffad71f53a60b4b6e6d6482 -- V2 Pool, 0x0c30062368eefb96bf3ade1218e685306b8e89fa -- V3 Pool)), raw_transfer as (selectdate_trunc('hour', evt_block_time) as trade_block_time, "from", to, value / 1e8 as amount-- , evt_tx_hashfrom erc20_ethereum.evt_Transferwhere evt_block_time >= date '2023-03-10'and evt_block_number >= 17228251and contract_address = 0x72e4f9f808c49a2a61de9c5896298920dc4eeea9 -- HarryPotterObamaSonic10Inu (BITCOIN)), token_price as (selecthour, median_pricefrom dex.priceswhere block_month >= date '2023-05-01'-- and block_date >= date '2023-05-10'and blockchain = 'ethereum'and contract_address = 0x72e4f9f808c49a2a61de9c5896298920dc4eeea9 -- HarryPotterObamaSonic10Inu (BITCOIN)), buy_and_sell as (selecttrade_block_time, addr, if(addr = raw_transfer.to, 'Buy', 'Sell') as func, amount * median_price as amount_usdfrom total_traderleft join raw_transfer on (addr = raw_transfer."from" or addr = raw_transfer.to)left join token_price on trade_block_time = token_price.hour), raw_trader as (selectaddr, sum(amount_usd) filter (where func = 'Buy') as total_bought_amount, sum(amount_usd) filter (where func = 'Sell') as total_sold_amount, sum(if(func = 'Buy', -amount_usd, amount_usd)) as profitfrom buy_and_sellgroup by addr)select