Query editor
-- Sandwich rules v0.1.0-draft, stages S3b to S5, single-block instances, window START to END (UTC, end exclusive). The rows-- of stages S1 to S3a come from s3_rows.sql, materialized per window; this query only reads them (token ROWS).-- Its rows carry proposed changes 1 (collapse a transaction's swaps on a pool by net token flow) and 3 (public routers-- derived by usage: entry contracts with at least N_ROUTER distinct signers in a six-hour slice of block time, flagged-- to_is_router); this query applies 5 (actor linkage also when the recipient of one leg is the signer of the other).-- Approved on 2026-09-04 for pipeline v1. Proposed change 10 (2026-09-17): the back-run is also sought among later-- opposite-direction rows of the pool in the block: the first one that shares an actor with a run row is evaluated as a-- second candidate, with the rows in between foreign (E4 false). It is kept when the first opposite row, as back-run,-- leaves no victim, else only when it gives Tier 1 or 2 and the first opposite row does not, or does only through a-- change 15 close without E1 (below), so no instance of the earlier rule is lost. f_selection says 'linked, back-run after-- foreign rows' for those.-- Proposed change 12 (2026-09-18): the closed position (E2) holds on either reading of the legs, their net flows per-- transaction or their flows kept apart by direction. The two differ when a leg sells the position back and re-enters in-- the same transaction, which the net reading hides, or oversells and buys back, which the direction reading hides. The-- re-entry is a new position, counted as the residue by the profit valuation. Amounts on the rows stay net, so profit-- stays the net position change.-- Change 15 (2026-10-09): a split back-run. When B and its linked unwind legs leave E2 false, the rows after B in its run are-- read again without linkage, and the prefix that brings B_sold closest to F_bought (net reading, the shorter on a tie) is-- attached if E2 then holds. unwind_selection says which rule attached the legs: linked, closest close, or none. A candidate-- closed this way whose back-run shares no actor with its front-run (E1 false) ranks below every other Tier 1 or 2 candidate-- of the bracket, so a coincidental close by unrelated sellers cannot displace a back-run linked to the front-run (change 10).-- A pool is (venue key, token pair) so multi-token pools do not mix pairs. Non-pool venues (RFQ, PMM routers) are excluded-- in s3_rows.sql, but for dex.trades' 0x API and 1inch-LOP.-- Cost containment: the swaps are read once, by s3_rows.sql, whose rows are materialized per window as-- sw_rows_<YYYYMMDD>_p<N> and read by the self-joins below; this query is materialized as sw_chunk_<YYYYMMDD>_p<N> (one-- window a day since 2026-10-11, p1; four of six hours before, p1 to p4). The rows after each back-run (R4, change 15) are-- read for the candidates alone, and bnb.transactions is joined only for the member transactions of instances.-- Tokens: START, END, ROWS.WITH runs AS (SELECT * FROM dune.blockspace_forensics_consortium.result_sw_rows_20261130_p1),runs_agg AS (SELECT block_number, venue_key, token_lo, token_hi, run_id, min(pos) AS first_pos, max(pos) AS last_pos, count(*) AS nFROM runs GROUP BY 1, 2, 3, 4, 5),brackets AS (SELECT a.block_number, a.venue_key, a.token_lo, a.token_hi, a.run_id, a.first_pos, a.last_pos, a.n, nb.first_pos AS b_posFROM runs_agg aJOIN runs_agg nb ON nb.block_number = a.block_number AND nb.venue_key = a.venue_key AND nb.token_lo = a.token_lo AND nb.token_hi = a.token_hi AND nb.run_id = a.run_id + 1WHERE a.n >= 2),cand_all AS ( -- run rows paired with every later opposite-direction row of the pool in the block (runs alternate direction)SELECT br.block_number, br.venue_key, br.token_lo, br.token_hi, br.run_id, br.first_pos, br.last_pos, br.b_pos AS first_opp_pos, b.pos AS b_pos,r.pos, r.tx_hash, r.tx_from, r.tx_to, r.taker, r.to_is_router, r.sold_raw, r.bought_raw, r.sold_gross, r.bought_gross, r.amount_usd, r.project, r.version, r.sym_lo, r.sym_hi, r.dir,b.tx_hash AS b_hash, b.tx_from AS b_from, b.tx_to AS b_to, b.taker AS b_taker, b.to_is_router AS b_to_is_router, b.sold_raw AS b_sold1, b.bought_raw AS b_bought1,b.sold_gross AS b_sold_gross1, b.bought_gross AS b_bought_gross1,(r.tx_from = b.tx_from OR (r.taker IS NOT NULL AND r.taker = b.taker) OR (r.tx_to = b.tx_to AND NOT b.to_is_router) OR (r.taker IS NOT NULL AND r.taker = b.tx_from) OR (b.taker IS NOT NULL AND r.tx_from = b.taker)) AS linked_b,((CASE WHEN r.bought_gross >= b.sold_gross THEN r.bought_gross - b.sold_gross ELSE b.sold_gross - r.bought_gross END) * uint256 '100' <= r.bought_grossOR (CASE WHEN r.sold_gross >= b.bought_gross THEN r.sold_gross - b.bought_gross ELSE b.bought_gross - r.sold_gross END) * uint256 '100' <= r.sold_gross) AS flow_bFROM brackets brJOIN runs r ON r.block_number = br.block_number AND r.venue_key = br.venue_key AND r.token_lo = br.token_lo AND r.token_hi = br.token_hi AND r.run_id = br.run_idJOIN runs b ON b.block_number = br.block_number AND b.venue_key = br.venue_key AND b.token_lo = br.token_lo AND b.token_hi = br.token_hiAND b.run_id > br.run_id AND (b.run_id - br.run_id) % 2 = 1),cand_pick AS ( -- change 10: the first later opposite row linked to a run row is a second back-run candidateSELECT c.*, min(CASE WHEN linked_b THEN b_pos END) OVER (PARTITION BY block_number, venue_key, token_lo, token_hi, run_id) AS first_linked_b_posFROM cand_all c),cand AS ( -- one or two candidates per bracket, told apart by b_pos in every later stageSELECT c.*, (b_pos <> first_opp_pos) AS interleaved