Query editor
WITH transfer_data AS (SELECT et.*,ROW_NUMBER() OVER (PARTITION BY et.tx_hash ORDER BY et.evt_index ASC) AS row_min,ROW_NUMBER() OVER (PARTITION BY et.tx_hash ORDER BY et.evt_index DESC) AS row_maxFROM dex.trades etleft join erc20_arbitrum.evt_Transfer ett on et.tx_hash = ett.evt_tx_hash and et.evt_index = ett.evt_indexWHERE "tx_from" <> 0x0000000000000000000000000000000000000000AND "tx_to" <> 0x0000000000000000000000000000000000000000and blockchain = 'arbitrum'),transfer_min AS (SELECT * FROM dex.trades tWHERE blockchain = 'arbitrum'and t.evt_index = (select min(evt_index) from erc20_arbitrum.evt_Transfer etwhere et.evt_tx_hash = t.tx_hash)),transfer_max AS (SELECT * FROM dex.trades tWHERE blockchain = 'arbitrum'and t.evt_index = (select max(evt_index) from erc20_arbitrum.evt_Transfer etwhere et.evt_tx_hash = t.tx_hash))SELECTt1.project_contract_address as contract_address,t1.tx_hash as evt_tx_hash,t1.block_time as evt_block_time,t1."tx_from" as arbitrager,t2.token_bought_amount - t1.token_sold_amount as profit_raw,coalesce((t2.token_bought_amount - t1.token_sold_amount) * p.price, 0) as profit_usd,tx.gas_price * tx.gas_used / 1e18 as fee_eth,tx.gas_price * tx.gas_used / 1e18 * p_eth.price as fee_usdFROM transfer_min t1JOIN transfer_max t2ON t1.tx_hash = t2.tx_hashAND t1.token_sold_address = t2.token_bought_addressAND t1.taker = t2.takerAND t1.evt_index < t2.evt_indexJOIN arbitrum.transactions tx on t1.tx_hash = tx.hashLEFT JOIN (SELECT * FROM prices.usd WHERE blockchain = 'arbitrum') pON t1.token_sold_address = p.contract_addressAND DATE_TRUNC('minute', t1.block_time) = p.minuteLEFT JOIN (SELECT * FROM prices.usd WHERE blockchain = 'arbitrum' and symbol = 'WETH') p_ethON DATE_TRUNC('minute', t1.block_time) = p_eth.minuteWHERE t2.token_bought_amount > t1.token_sold_amount