Query editor
-- Sandwich pipeline, stage S6 for one UTC day: Tier 1 and 2 instances enriched with the gas of the front-run and back-run-- transactions. Builder and validator are not set here. profit_usd, net_usd and materiality here use Dune's daily price of-- the profit token whatever its source; they are kept for audit only. The reported dollar profit is valued in the rolling-- view with outside prices only (recent_rolling.sql).-- Reads the day's chunk tables (sw_chunk_<YYYYMMDD>_p<N>, stages S3b to S5, one per detection window).-- Tokens: START, END, DAY, CHUNKS.WITH chunks AS (SELECT * FROM dune.blockspace_forensics_consortium.result_sw_chunk_20261130_p1),i AS (SELECT c.*,CASE WHEN (profit_token = 'sold token of F') = f_sells_lo THEN token_lo ELSE token_hi END AS profit_addr,CASE WHEN (profit_token = 'sold token of F') = f_sells_lo THEN sym_lo ELSE sym_hi END AS profit_sym,cast(profit_raw AS double) AS profit_raw_dFROM chunks cWHERE tier <= 2),px AS (SELECT contract_address, decimals, price FROM prices.dayWHERE blockchain = 'bnb' AND timestamp = timestamp '2026-11-30'),bnb AS (SELECT price AS bnb_usd FROM px WHERE contract_address = 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c),valued AS (SELECT i.*, px.price AS profit_token_price,CASE WHEN px.price IS NOT NULL THEN i.profit_raw_d / power(10, px.decimals) * px.price END AS profit_usdFROM iLEFT JOIN px ON px.contract_address = from_hex(substr(i.profit_addr, 3))),gas AS (SELECT v.instance_id, sum(x.gas_used * x.gas_price) AS gas_weiFROM valued vCROSS JOIN UNNEST(array[v.f_hash, v.b_hash]) AS t(h)JOIN bnb.transactions x ON x.block_number = v.block_number AND x.hash = from_hex(substr(t.h, 3))AND x.block_time >= timestamp '2026-11-30 00:00:00' AND x.block_time < timestamp '2026-12-01 00:00:00'GROUP BY 1)SELECT date '2026-11-30' AS day, v.*,cast(g.gas_wei AS double) / 1e18 * bnb.bnb_usd AS gas_usd,v.profit_usd - cast(g.gas_wei AS double) / 1e18 * bnb.bnb_usd AS net_usd,CASE WHEN v.profit_usd IS NULL THEN 'unpriced' WHEN v.profit_usd >= 1 THEN 'material' ELSE 'micro' END AS materiality,-- builder and validator come from the day's attribution table (shared/dune/builders_day.sql), joined in the rolling-- view, so attribution can be corrected without recomputing detection; the columns stay for a stable schemacast(NULL AS varchar) AS builder, cast(NULL AS varchar) AS bid_address, cast(NULL AS varchar) AS validator, cast(NULL AS varchar) AS minerFROM valued vLEFT JOIN gas g ON g.instance_id = v.instance_idCROSS JOIN bnb