Query editor
WITH blocks AS(SELECT time, number, hash, miner as builder, (gas_used * base_fee_per_gas)/1e18 as burned_feesFROM ethereum.blocksWHERE time > DATE('2024-02-05'))-- traces for all general txns. this includes both single hash and different hash within the same block, traces as (SELECT tr.block_time, tr.block_number, tr.block_hash, tr.tx_index,tx_hash, tr."from", tr."to", bl.burned_fees, tr.value as builder_paymentFROM ethereum.traces trLEFT JOIN blocks bl ON tr.block_number = bl.numberAND tr.to = bl.builder -- payment to builderWHERE tr.block_time > DATE('2024-02-05')AND bl.time > DATE('2024-02-05')AND tx_success = trueAND value > CAST(0 as uint256)ORDER BY bl.time)-- Event Transfer-- , swaps_2 as (-- SELECT logs.tx_hash-- , CASE WHEN cast(peswap.amount0Out as double) = 0 THEN pes.reserve1 ELSE pes.reserve0 END AS token_bought_reserve_raw-- , CASE WHEN cast(peswap.amount0In as double) = 0 OR cast(peswap.amount1Out as double)= 0 THEN pes.reserve1 ELSE pes.reserve0 END AS token_sold_reserve_raw-- , COUNT(*) as count_swaps -- count number of "Transfer" events-- FROM traces tr-- LEFT JOIN ethereum.logs logs ON tr.tx_hash = logs.tx_hash-- LEFT JOIN uniswap_v2_ethereum.Pair_evt_Sync pes ON pes.evt_tx_hash = tr.tx_hash-- LEFT JOIN uniswap_v2_ethereum.Pair_evt_Swap peswap ON peswap.evt_tx_hash = tr.tx_hash-- WHERE logs.topic0 = 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef -- event signature Transfer(address,address,uint256)-- AND logs.block_time > DATE('2024-02-05')-- GROUP BY logs.tx_hash, pes.reserve0, pes.reserve1, peswap.amount0Out, peswap.amount0In, peswap.amount1Out-- )-- -- Traces with only 2 swaps-- , swaps_with_builder_payments as (-- SELECT DISTINCT tr.block_time, tr.block_number, tr.block_hash, tr.tx_hash, tr.tx_index,-- dex.project, dex.version, dex.token_pair,-- sw.token_bought_reserve_raw / power(10, erc20a.decimals) AS token_bought_reserve_raw,-- sw.token_sold_reserve_raw / power(10, erc20b.decimals) AS token_sold_reserve_raw,-- tr."to" as block_builder, tr."from" as swap_from, tr.burned_fees, tr.builder_payment,-- dex.tx_from, dex.tx_to, dex.token_bought_amount, dex.token_bought_symbol, dex.token_bought_address, dex.token_sold_amount, dex.token_sold_symbol, dex.token_sold_address, dex.amount_usd,-- sw.count_swaps-- FROM traces tr-- LEFT JOIN dex.trades dex on tr.tx_hash = dex.tx_hash-- LEFT JOIN swaps_2 sw on tr.tx_hash = sw.tx_hash-- LEFT JOIN tokens.erc20 erc20a ON erc20a.contract_address = dex.token_bought_address AND erc20a.blockchain = 'ethereum'-- LEFT JOIN tokens.erc20 erc20b ON erc20b.contract_address = dex.token_sold_address AND erc20b.blockchain = 'ethereum'-- where sw.count_swaps = 2 -- filter to one swap + one builder payment-- ), sandwich_txns as (select tx_hash as txnfrom dex_ethereum.sandwicheswhere block_time > DATE('2024-02-05'))--get the dex trades excluding sandwiches and their blocks, for joining later, dex_trades_blocks AS (select DISTINCT dt.tx_hash as dex_hash, t.block_number, t.block_hash, dt.tx_from, dt.block_time, t.indexfrom dex.trades dtjoin ethereum.transactions t on dt.tx_hash = t.hashwhere dt.blockchain = 'ethereum'and dt.block_time > DATE('2024-02-05')and t.block_time > DATE('2024-02-05')