Query editor
/* ============================================================L1 — TOP HOLDERS + IDENTITY + FUNDER FAN-OUT (IDF)Input : condition_id (market), top_n_per_side, start_dateOutput: 1 row / holder / outcome-token============================================================ */WITH md AS ( -- token_id <-> outcome mapping for the marketSELECT token_id, token_outcome, outcome_index, questionFROM polymarket_polygon.market_detailsWHERE condition_id = '{{condition_id}}'),fills AS ( -- every exchange fill touching those tokensSELECT f.maker AS wallet,f.tokenId AS token_id,f.evt_block_time,/* side = 0 -> maker BUYS shares (pays USDC) ; side = 1 -> maker SELLS */CASE WHEN f.side = 0 THEN CAST(f.takerAmountFilled AS double) / 1e6ELSE -CAST(f.makerAmountFilled AS double) / 1e6 END AS d_shares,CASE WHEN f.side = 0 THEN CAST(f.makerAmountFilled AS double) / 1e6ELSE -CAST(f.takerAmountFilled AS double) / 1e6 END AS d_usdcFROM polymarket_v2_polygon.ctfexchange_evt_orderfilled fWHERE f.evt_block_date >= DATE '{{start_date}}'AND f.tokenId IN (SELECT token_id FROM md)),pos AS (SELECT wallet, token_id,SUM(d_shares) AS shares,SUM(d_usdc) AS usdc_net,COUNT(*) AS n_fills,MIN(evt_block_time) AS first_trade,MAX(evt_block_time) AS last_trade,COUNT(DISTINCT date_trunc('day', evt_block_time)) AS active_daysFROM fillsGROUP BY 1, 2),ranked AS (SELECT p.*, m.token_outcome, m.outcome_index, m.question,ROW_NUMBER() OVER (PARTITION BY p.token_id ORDER BY p.shares DESC) AS holder_rankFROM pos pJOIN md m ON m.token_id = p.token_idWHERE p.shares > 0.5 -- ignore dust),top_holders AS (SELECT * FROM ranked WHERE holder_rank <= {{top_n_per_side}}),ident AS ( -- proxy wallet -> owner EOA + first funderSELECT u.polymarket_wallet, u.wallet_type, u.owner,u.first_funded_by, u.first_funded_time, u.created_timeFROM polymarket_polygon.users_address_lookup uWHERE u.polymarket_wallet IN (SELECT wallet FROM top_holders)),funder_degree AS ( -- how many Polymarket wallets did that funder ever fund?SELECT first_funded_by, COUNT(DISTINCT polymarket_wallet) AS wallets_fundedFROM polymarket_polygon.users_address_lookupWHERE first_funded_by IN (SELECT first_funded_by FROM ident WHERE first_funded_by IS NOT NULL)