Query editor
WITH p_ep AS (SELECT blockchain, token, p_wallet, p_handle, episode_id, p_timeFROM query_8576288),all_buys AS (SELECTr.s_addr AS wallet,s.blockchain,s.token_bought_address AS token,s.block_time,s.amount_usdFROM dex.trades sCROSS JOIN UNNEST(ARRAY['taker','tx_from'], ARRAY[s.taker, s.tx_from]) AS r(s_role, s_addr)WHERE s.blockchain IN ('robinhood', 'bnb')AND s.block_month >= date_trunc('month', timestamp '2026-06-03 00:00:00')AND s.block_month <= date_trunc('month', timestamp '2026-09-01 00:00:00')AND s.block_time >= timestamp '2026-06-03 00:00:00' - interval '30' minuteAND s.block_time < timestamp '2026-09-01 00:00:00'AND s.token_bought_amount > 0AND r.s_addr IS NOT NULLAND r.s_addr NOT IN (0x8f62a08537cede87d511aca6436274ab4ca080a3, 0xb8f305f27ccc406373de0082cc06cb1d065504ea,0x0a6ebed0155edb4b21d92ad02897a626cd90119e, 0x1890e719822bc704c4f117aa4109401c2bab6f79,0xcc0c581613dfd4ace7c8686668427236f8bd5cc5, 0x14aa2a71dbb5ef87b81f92205e2699aa4aa65794,0xb02208d1b27811480cf6171bccc2b7af9513cddb, 0x92bff72e6c943a1348aedf15d68e758e811dec64,0x696d1265c8fc4f14797abebfae3c43ebfa9d8e28, 0xefe9764acb00822338a80f907075c53f60a5781e)),-- the front-runs: earliest buy by this wallet inside the 30-min pre-window of a P episodeleads AS (SELECTb.wallet, p.p_wallet, p.p_handle, p.blockchain, p.token, p.episode_id, p.p_time,MIN(b.block_time) AS s_time,MAX(b.amount_usd) AS s_usdFROM all_buys bJOIN p_ep pON b.blockchain = p.blockchainAND b.token = p.tokenAND b.block_time < p.p_timeAND b.block_time >= p.p_time - interval '30' minuteGROUP BY 1,2,3,4,5,6,7),ranked AS (SELECT *,date_diff('second', s_time, p_time) AS lead_seconds,COUNT(*) OVER (PARTITION BY p_wallet, blockchain, token, episode_id) AS episode_crowd,RANK() OVER (PARTITION BY p_wallet, blockchain, token, episode_id ORDER BY s_time) AS lead_rankFROM leads),-- DENOMINATOR: every distinct token this wallet bought anywhere on these chains in the windowtotals AS (SELECT wallet, COUNT(DISTINCT blockchain || ':' || to_hex(token)) AS total_tokens_boughtFROM all_buysWHERE wallet IN (SELECT DISTINCT wallet FROM leads)GROUP BY wallet)SELECTr.wallet,COUNT(DISTINCT r.blockchain || ':' || to_hex(r.token)) AS tokens_frontrun,t.total_tokens_bought,ROUND(COUNT(DISTINCT r.blockchain || ':' || to_hex(r.token))