// the find
nahrek/polyledger
Resumable Polymarket indexer: CLOB market metadata plus on-chain trades from Polygon, in one DuckDB file you can query with SQL
PolyLedger is a Python CLI that pulls Polymarket market metadata from the CLOB API and every OrderFilled event from Polygon through Envio HyperSync, then writes both into one DuckDB file. It is for people who want to analyse Polymarket volume, prices, or trader behaviour in SQL without running a node or writing their own log decoder.
Crash safety is handled properly. Rows and the block cursor commit in one transaction, and order_fills is keyed on (transaction_hash, log_index) with ON CONFLICT DO NOTHING, so an interrupted run or a re-run of a range can't double-write. The trades table records maker_side and taker_side explicitly and documents how price, shares, and usd_size are derived from the raw amounts, so you never have to guess whose perspective a row is written from. It decodes both the V1 and V2 OrderFilled layouts, and every API response passes through Pydantic, so an upstream format change fails loudly instead of writing nulls. The 41 offline tests include one that recomputes topic0 from the event signatures, which catches a typo in either the constant or the signature.
It only sees filled trades. Order book state, quotes, and cancellations never touch the chain, and FeeCharged events aren't indexed, so the fee column is just what the fill event carries. Reorg handling is avoidance rather than reconciliation: it stops 64 blocks behind the head (a couple of minutes on Polygon) and never rolls back, which is fine for historical work and wrong for anything near-live. The first backfill on the free HyperSync tier can take days, trades_mat is a snapshot that goes stale until you rerun polyledger trades, and the README's own example shows 392 unmatched fills, so every query that needs question text has to handle unmatched_market = true.