Query editor
-- Reconcile pump-token swap quantities with movements of tokens actually owned-- by the reported trader. Include non-trade transfers to flag incomplete cost basis.-- This owner-based screen allows fee sponsorship; it does not substitute fee payer-- or passive recipient for the original reported trader. Whole wallets with any-- unmatched qualifying trade are excluded from profit classification.WITH legs AS (SELECT t.trader_id AS wallet, t.tx_id, t.block_time, t.tx_index,l.token_id, l.quantity, l.is_buy, t.amount_usdFROM dex_solana.trades tJOIN dune.defiance_cr.dataset_memecoin_expanded_cohort_jan2025_20260905 cON t.trader_id = c.walletCROSS JOIN UNNEST(ARRAY[ROW(t.token_bought_mint_address,t.token_bought_amount,true),ROW(t.token_sold_mint_address,t.token_sold_amount,false)]) AS l(token_id, quantity, is_buy)WHERE t.block_month >= DATE '2025-05-01'AND t.block_month < DATE '2025-05-08'AND t.block_date >= DATE '2025-05-01' AND t.block_date < DATE '2025-05-08'AND t.block_time >= TIMESTAMP '2025-05-01' AND t.block_time < TIMESTAMP '2025-05-08'AND varbinary_substring(xxhash64(to_utf8(t.trader_id)),1,1) < 0x02AND l.token_id LIKE '%pump'), transaction_tokens AS (SELECT wallet,tx_id,token_id,min(block_time) AS block_time,max(tx_index) AS tx_index,sum(IF(is_buy,quantity,-quantity)) AS net_tokens,sum(quantity) AS gross_tokens,sum(IF(is_buy,quantity,0)) AS bought_tokens,sum(IF(is_buy,amount_usd,0)) AS buy_usd,sum(IF(NOT is_buy,amount_usd,0)) AS sell_usd,count(*) AS trade_legs,count_if(quantity IS NULL OR amount_usd IS NULL OR quantity <= 0 OR amount_usd < 0) AS invalid_rowsFROM legs GROUP BY 1,2,3), owner_transfers AS (SELECT o.wallet,x.tx_id,x.token_mint_address AS token_id,min(x.block_time) AS block_time,max(x.tx_index) AS tx_index,sum(o.direction*x.amount_display) AS actual_net_tokens,sum(abs(x.amount_display)) AS actual_gross_tokens,count(*) AS transfer_rows,count_if(x.amount_display IS NULL OR x.amount_display < 0) AS invalid_transfer_rowsFROM tokens_solana.transfers xCROSS JOIN UNNEST(ARRAY[ROW(x.from_owner,-1),ROW(x.to_owner,1)]) AS o(wallet,direction)JOIN dune.defiance_cr.dataset_memecoin_expanded_cohort_jan2025_20260905 c ON o.wallet=c.walletWHERE x.block_date >= DATE '2025-05-01' AND x.block_date < DATE '2025-05-08'AND x.block_time >= TIMESTAMP '2025-05-01' AND x.block_time < TIMESTAMP '2025-05-08'AND x.token_mint_address LIKE '%pump'AND varbinary_substring(xxhash64(to_utf8(o.wallet)),1,1) < 0x02GROUP BY 1,2,3), reconciled AS (SELECT coalesce(t.wallet,x.wallet) AS wallet,coalesce(t.token_id,x.token_id) AS token_id,coalesce(t.tx_id,x.tx_id) AS tx_id,coalesce(t.block_time,x.block_time) AS block_time,coalesce(t.tx_index,x.tx_index) AS tx_index,coalesce(t.net_tokens,0) AS net_tokens,coalesce(t.bought_tokens,0) AS bought_tokens,coalesce(t.buy_usd,0) AS buy_usd,coalesce(t.sell_usd,0) AS sell_usd,coalesce(t.trade_legs,0) AS trade_legs,coalesce(t.invalid_rows,0) AS invalid_rows,coalesce(x.invalid_transfer_rows,0) AS invalid_transfer_rows,coalesce(x.actual_net_tokens,0) AS actual_net_tokens,t.tx_id IS NOT NULL AS is_trade,coalesce(t.tx_id IS NOT NULL AND x.tx_id IS NOT NULL AND x.invalid_transfer_rows=0AND abs(t.net_tokens-x.actual_net_tokens) <= greatest(1e-8,t.gross_tokens*1e-10),false) AS owner_match,t.tx_id IS NULL AND abs(coalesce(x.actual_net_tokens,0)) > greatest(1e-8,coalesce(x.actual_gross_tokens,0)*1e-10) AS nontrade_flowFROM transaction_tokens t FULL OUTER JOIN owner_transfers x