Query editor
WITH blocks AS (SELECT time, number, hash, miner as builder, (gas_used * base_fee_per_gas)/1e18 as burned_feesFROM polygon.blocksWHERE time > NOW() - interval '1' month)-- 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 polygon.traces trLEFT JOIN blocks bl ON tr.block_number = bl.number--AND tr.to = bl.builder -- filter to txs with a payment to builderWHERE tr.block_time > NOW() - interval '1' monthAND bl.time > NOW() - interval '1' monthAND 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" eventsFROM traces trLEFT JOIN polygon.logs logs ON tr.tx_hash = logs.tx_hashLEFT JOIN uniswap_v2_polygon.UniswapV2Pair_evt_Sync pes ON pes.evt_tx_hash = tr.tx_hashLEFT JOIN uniswap_v2_polygon.UniswapV2Pair_evt_Swap peswap ON peswap.evt_tx_hash = tr.tx_hashWHERE logs.topic0 = 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef -- event signature Transfer(address,address,uint256)AND logs.block_time > NOW() - interval '1' monthGROUP BY logs.tx_hash, pes.reserve0, pes.reserve1, peswap.amount0Out, peswap.amount0In, peswap.amount1Out)-- -- Traces with only 2 swaps (nothing is 2 swaps for past 1 month!!), 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_swapsFROM traces trLEFT JOIN dex.trades dex on tr.tx_hash = dex.tx_hashLEFT JOIN swaps_2 sw on tr.tx_hash = sw.tx_hashLEFT JOIN tokens.erc20 erc20a ON erc20a.contract_address = dex.token_bought_address AND erc20a.blockchain = 'polygon'LEFT JOIN tokens.erc20 erc20b ON erc20b.contract_address = dex.token_sold_address AND erc20b.blockchain = 'polygon'where sw.count_swaps = 2 -- filter to one swap + one builder payment), sandwich_txns as (select tx_hash as txnfrom dex_polygon.sandwicheswhere block_time > NOW() - interval '1' month)--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 polygon.transactions t on dt.tx_hash = t.hashwhere dt.blockchain = 'polygon'and dt.block_time > NOW() - interval '1' monthand t.block_time > NOW() - interval '1' month