Query editor
-- FOMO Solana per-wallet PnL ledger — REHEARSAL v2 (3 days).-- Fixes vs v1: (a) sign-guarded cost attribution, (b) split computed on the attributed-- subset rather than against net_qty, (c) single (wallet,tx,mint) base to cut cost.with fee as (select tx_id, from_owner as walletfrom tokens_solana.transferswhere block_date >= date '2026-08-21' and block_date < date '2026-08-24'and to_owner = 'R4rNJHaffSUotNmqSKNEfDcJE8A7zJUkaoM5Jkd7cYX'and token_mint_address = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),cohort as (select distinct wallet from fee),fomo_tx as (select distinct tx_id from fee),flows as (select t.tx_id, t.action, t.token_mint_address as mint,t.from_owner, t.to_owner, t.amount_display as qty, t.amount_usdfrom tokens_solana.transfers tjoin fomo_tx f on f.tx_id = t.tx_idwhere t.block_date >= date '2026-08-21' and t.block_date < date '2026-08-24'and t.to_owner <> 'R4rNJHaffSUotNmqSKNEfDcJE8A7zJUkaoM5Jkd7cYX'),-- single pass: unnest both sides, keep only legs where a FOMO wallet is a counterpartylegs as (select fl.tx_id, c.wallet, fl.mint, fl.action,case when c.wallet = fl.to_owner then fl.qty else -fl.qty end as qty,case when c.wallet = fl.to_owner then coalesce(fl.amount_usd,0)else -coalesce(fl.amount_usd,0) end as usd,fl.mint in ('So11111111111111111111111111111111111111112','So11111111111111111111111111111111111111111','EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v','Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB') as is_quotefrom flows flcross join unnest(array[fl.to_owner, fl.from_owner]) as t(owner)join cohort c on c.wallet = t.owner),-- ONE aggregation base; net and tx_wallet both derive from it (cost optimisation)wtm as (select wallet, tx_id, mint, is_quote,sum(qty) as qty,sum(usd) as usd,sum(case when action = 'create_account' then usd else 0 end) as rentfrom legs group by 1,2,3,4),txw as (select wallet, tx_id,sum(case when is_quote then usd else 0 end) as quote_usd,count_if(not is_quote) as nq_mints, -- already distinctmax(case when not is_quote then mint end) as nq_mint,sum(case when not is_quote then qty else 0 end) as nq_qtyfrom wtm group by 1,2),net as (select wallet, mint, is_quote,sum(qty) as net_qty, sum(usd) as net_usd, sum(rent) as rent_usdfrom wtm group by 1,2,3),-- Cost basis from the QUOTE side. Sign guards: an acquire must SPEND quote,-- a disposal must RECEIVE quote. Anything else is counted but excluded.attrib as (select wallet, nq_mint as mint,sum(case when nq_qty > 0 and quote_usd < 0 then nq_qty else 0 end) as buy_qty,