Query editor
-- Sandwich pipeline, victim loss for one UTC day, tight constant-product case (rules section 6.2) with the fidelity gate.-- Reads the day's enriched table, PancakeSwap V2's decoded Sync events and the first victim's swap in dex.trades (Uniswap V2-- pairs are not covered yet). Tokens: ENRICHED, START, END, DAY.-- For Tier 1 and 2 instances on PancakeSwap V2 pools, backs the reserves before the front-run out of the front-run's own Sync-- event and recomputes the first victim's fill without the front-run. Exact when nothing sits between the front-run and that-- victim on the pool; flagged partial when an attacker leg does. A fidelity gate (the model must reproduce the victim's-- actual fill from the reserves right after the front-run, within 0.1%) marks pools that do not follow constant-product with-- the assumed fee.WITH i AS (SELECT block_number, tier, venue_key, project, version, sym_lo, sym_hi, f_sells_lo, f_tx_index, f_hash, victim_tx_indices, victim_hashes, leg_tx_indices,cast(f_sold AS uint256) AS f_sold, cast(f_bought AS uint256) AS f_bought,CASE WHEN project = 'pancakeswap' AND version = '2' THEN 25 WHEN project = 'uniswap' AND version = '2' THEN 30 END AS fee_bps,instance_idFROM dune.blockspace_forensics_consortium.result_sw_day_20261130WHERE tier <= 2 AND version = '2' AND project = 'pancakeswap'),sync AS ( -- reserves right after the front-run, from the decoded Sync table (a fraction of the raw logs' size)SELECT i.instance_id, max_by(l.reserve0, l.evt_index) AS reserve0, max_by(l.reserve1, l.evt_index) AS reserve1FROM iJOIN pancakeswap_v2_bnb.PancakePair_evt_Sync lON l.evt_block_number = i.block_number AND l.evt_tx_hash = from_hex(substr(i.f_hash, 3)) AND l.contract_address = from_hex(substr(i.venue_key, 3))AND l.evt_block_time >= timestamp '2026-11-30 00:00:00' AND l.evt_block_time < timestamp '2026-12-01 00:00:00'GROUP BY 1),v AS (SELECT i.instance_id, t.tx_hash AS v_hash, t.token_sold_amount_raw AS v_in, t.token_bought_amount_raw AS v_out_actual, t.token_sold_symbol, t.token_bought_symbol, t.amount_usdFROM iJOIN dex.trades t ON t.blockchain = 'bnb' AND t.block_number = i.block_number AND t.tx_hash = from_hex(substr(i.victim_hashes[1], 3))AND t.project_contract_address = from_hex(substr(i.venue_key, 3))AND t.block_time >= timestamp '2026-11-30 00:00:00' AND t.block_time < timestamp '2026-12-01 00:00:00'AND (t.token_sold_address = least(t.token_sold_address, t.token_bought_address)) = i.f_sells_lo),calc AS (SELECT i.*, v.v_hash, v.v_in, v.v_out_actual, v.token_sold_symbol AS v_sold_sym, v.token_bought_symbol AS v_bought_sym, v.amount_usd AS v_usd,s.reserve0 AS r0_after, s.reserve1 AS r1_after,CASE WHEN i.f_sells_lo THEN s.reserve0 - i.f_sold ELSE s.reserve0 + i.f_bought END AS r0_before,CASE WHEN i.f_sells_lo THEN s.reserve1 + i.f_bought ELSE s.reserve1 - i.f_sold END AS r1_before,(i.leg_tx_indices IS NULL OR cardinality(i.leg_tx_indices) = 0 OR i.leg_tx_indices[1] > i.victim_tx_indices[1]) AS tightFROM iJOIN sync s ON s.instance_id = i.instance_idJOIN v ON v.instance_id = i.instance_idWHERE i.fee_bps IS NOT NULL),fill AS (SELECT c.*, CASE WHEN f_sells_lo THEN r0_before ELSE r1_before END AS r_in_before, CASE WHEN f_sells_lo THEN r1_before ELSE r0_before END AS r_out_beforeFROM calc c),res AS (SELECT f.*,(r_out_before * v_in * cast(10000 - fee_bps AS uint256)) / (r_in_before * uint256 '10000' + v_in * cast(10000 - fee_bps AS uint256)) AS v_out_clean,-- fidelity gate: the same model applied to the reserves right after the front-run must reproduce the victim's on-chain fill(CASE WHEN f_sells_lo THEN r1_after ELSE r0_after END * v_in * cast(10000 - fee_bps AS uint256))/ (CASE WHEN f_sells_lo THEN r0_after ELSE r1_after END * uint256 '10000' + v_in * cast(10000 - fee_bps AS uint256)) AS v_out_predictedFROM fill f),gated AS (SELECT r.*,CASE WHEN v_out_predicted >= v_out_actual THEN v_out_predicted - v_out_actual ELSE v_out_actual - v_out_predicted END AS fidelity_diff,(CASE WHEN v_out_predicted >= v_out_actual THEN v_out_predicted - v_out_actual ELSE v_out_actual - v_out_predicted END) * uint256 '1000' <= v_out_actual AS fidelity_okFROM res r