Query editor
-- Sandwich pipeline, stages S1 to S3a for one window: the collapsed rows of every pool with at least three transactions in-- a block, with their direction runs. It is the first half of the detection, materialized so that s3_s5_instances.sql-- reads these rows once per self-join instead of re-deriving them from the swaps each time: Dune inlines every reference-- to a named subquery, and the detection as one query read dex.trades 111 times per six-hour window (cost review of-- 2026-09-19). Within this stage, the check that a pool has at least three transactions in the block is a window over the-- collapsed rows rather than a second read of the swaps joined back.-- Its swaps come from the shared source analyses/sandwich/dune/swaps_bnb.sql (rules changes 16 and 17): Dune's dex.trades-- and the venues it lacks, rendered for the window into the token SWAPS. Non-pool venues (RFQ, PMM routers) are left out,-- but for dex.trades' 0x API and 1inch-LOP. That source is read once: the public routers of rules change 3 are counted on-- the collapsed rows, which keep each transaction's signer and target, since a second reader of the swaps would build them-- all again.-- Tokens: N_ROUTER, SWAPS.WITH s1 AS (SELECT block_number, evt_index, tx_hash, tx_from, tx_to, taker, project, version,cast(floor(to_unixtime(block_time) / 21600) AS bigint) AS router_slice,CASE WHEN version IN ('4', 'infinity_cl', 'infinity_lb') THEN maker ELSE project_contract_address END AS venue_key,least(token_sold_address, token_bought_address) AS token_lo,greatest(token_sold_address, token_bought_address) AS token_hi,token_sold_address = least(token_sold_address, token_bought_address) AS lo_for_hi,token_sold_amount_raw AS sold_raw, token_bought_amount_raw AS bought_raw, amount_usd,token_sold_symbol, token_bought_symbolFROM (-- Sandwich pipeline, the swaps of one window on BNB Smart Chain (rules changes 16 and 17, rules.md section 2.2): Dune's-- curated dex.trades, and the venues missing from it read from Dune's decoded event tables (TopazDEX and FstSwap, rules-- change 16; Lista DEX V3, change 17), each mapped as Dune's spellbook maps the same pool type-- (dbt_subprojects/dex/macros/models/_project/uniswap_compatible_trades.sql). Stage S1 (s3_rows.sql) and the orderflow-- stage (shared/dune/flow_day.sql) read it through the token SWAPS, so both see the same swaps.-- A decoded row is dropped when dex.trades holds the same event (block number and log index): the day Dune adds a venue,-- its rows come from dex.trades alone. Dollar value as Dune computes it (dbt_subprojects/dex/macros/add_amount_usd_dex_trades.sql):-- prices.usd_with_native at the minute; the bought amount if its token is trusted (prices_v2.trusted_tokens), else the sold-- amount if that one is, else the bought, else the sold.-- Each swap read stays in the partitions of its window: block_month for dex.trades, evt_block_date for the decoded tables;-- prices are read for the window's minutes; the pool-creation tables, tokens.erc20 and prices_v2.trusted_tokens are read whole.-- Inserted into other queries, where its comments are checked as code: no comment here may hold a token marker.-- Tokens: START, END.WITH curated AS (SELECT block_time, block_number, evt_index, tx_hash, tx_from, tx_to, taker, maker, project, version, project_contract_address,token_sold_address, token_bought_address, token_sold_amount_raw, token_bought_amount_raw, amount_usd,token_sold_symbol, token_bought_symbol, 'dex.trades' AS sourceFROM dex.tradesWHERE blockchain = 'bnb' AND block_time >= timestamp '2026-11-30 00:00:00' AND block_time < timestamp '2026-12-01 00:00:00'AND block_month >= date_trunc('month', date(timestamp '2026-11-30 00:00:00')) AND block_month <= date_trunc('month', date(timestamp '2026-12-01 00:00:00'))),-- TopazDEX concentrated liquidity: amount0 below zero means the trader bought token0 from the poolcl AS (SELECT s.evt_block_time AS block_time, s.evt_block_number AS block_number, s.evt_index, s.evt_tx_hash AS tx_hash,s.evt_tx_from AS tx_from, s.evt_tx_to AS tx_to, s.recipient AS taker, s.contract_address AS maker,'topazdex' AS project, 'cl' AS version, s.contract_address AS project_contract_address,CASE WHEN s.amount0 < int256 '0' THEN p.token1 ELSE p.token0 END AS token_sold_address,CASE WHEN s.amount0 < int256 '0' THEN p.token0 ELSE p.token1 END AS token_bought_address,cast(CASE WHEN s.amount0 < int256 '0' THEN abs(s.amount1) ELSE abs(s.amount0) END AS uint256) AS token_sold_amount_raw,cast(CASE WHEN s.amount0 < int256 '0' THEN abs(s.amount0) ELSE abs(s.amount1) END AS uint256) AS token_bought_amount_raw,'topazdex_bnb.clpool_evt_swap' AS sourceFROM topazdex_bnb.clpool_evt_swap sJOIN topazdex_bnb.clfactory_evt_poolcreated p ON p.pool = s.contract_addressWHERE s.evt_block_time >= timestamp '2026-11-30 00:00:00' AND s.evt_block_time < timestamp '2026-12-01 00:00:00'AND s.evt_block_date >= date(timestamp '2026-11-30 00:00:00') AND s.evt_block_date <= date(timestamp '2026-12-01 00:00:00')),-- TopazDEX two-token pools: bought token0 unless amount0Out is 0; sold token0 unless amount0In is 0 or amount1Out is 0vamm AS (