Index PancakeSwap V2 prices from pair reserve events
Track V2 spot prices by discovering pairs from the factory and replaying reserve updates, while accounting for token decimals, liquidity and chain reorgs.
The Chainvane Desk5 min read

To index PancakeSwap V2 spot prices on BNB Chain, discover each pair from the V2 factory and track its reserve updates in block order. A pair holds two tokens, and its reserves define the pool’s current marginal price; a swap event alone does not give you that price. This approach gives you a reproducible view of V2 pools, but it does not cover PancakeSwap’s other pool types or establish a token’s value in dollars.
How do you find every PancakeSwap V2 pair?
Start with the V2 factory contract on the BNB Chain network you intend to index. Listen for its PairCreated event and save the two token addresses, the pair address and the event’s block position. The PancakeSwap V2 subgraph configuration identifies the BNB Chain factory as 0xBCfCcbde45cE874adCB698cC183deBcF17952812. Treat that address as part of the data source definition, and verify it against the deployment you mean to cover.
For a new index, scan factory logs from an appropriate historical starting point through the current chain head, then continue with new blocks. For each discovered pair, read token0, token1 and getReserves from the pair contract to establish its token ordering and initial state. Store the chain, pair address and token addresses together: a token symbol is neither unique nor a reliable identifier.
Pair discovery is more complete than taking a list from a market API, which may show only selected or ranked pools. It also means an index can encounter empty or suspicious pairs. PancakeSwap’s swap FAQ describes cases where a pair was created without initial liquidity and later manipulated with a token transfer and sync. Check that a pair came from the intended factory, and distinguish a deployed pair from a pool with meaningful, usable liquidity. For a practical walkthrough of token checks, see this fuller guide to checking BNB Chain tokens with Poocoin.
Which events give you the current price?
Use the pair’s Sync event to update its reserves. The PancakeSwap V2 pair contract emits Sync when it writes new reserve values, including after swaps, liquidity additions or removals, and a direct call to sync. The Swap event is useful for trade amounts and volume, but reserve state is the better basis for the pool’s spot quote.
For a pair with raw reserves reserve0 and reserve1, and token decimals dec0 and dec1, calculate token1 per token0 as:
(reserve1 / 10^dec1) / (reserve0 / 10^dec0)
Reverse the ratio for token0 per token1. Read decimals from each token contract and store them as metadata; the reserve values are integer units, not human-readable token quantities. Keep full precision through the calculation, and define what your index returns when either reserve is zero. A ticker label cannot tell you the decimal count or which asset is token0.
Apply each Sync in canonical block, transaction and log order. Persist the block hash as well as the height so the index can detect a reorganization and roll back events from blocks that are no longer canonical. On restart, replay logs from the last accepted checkpoint, then reconcile with the latest pair reserves. This costs more than polling only a single recent reserve snapshot, but preserves historical state and makes gaps easier to repair.
What does a V2 spot price tell you?
The reserve ratio is the pool’s spot price before considering how a particular trade changes the reserves. A larger order moves the ratio as it consumes liquidity, so its execution price can differ from the spot quote even if both use the same pair. Token transfer taxes and other token-specific behavior can create further differences between the quoted reserve ratio and the amount a trader receives.
That makes reserve-based V2 prices useful for tracking pool state, comparing pairs and building historical series. They are not automatically a fair-value feed. A thin pool can show a sharp price move from a small trade, and one pool may disagree with deeper pools or prices elsewhere. To express a token in a common unit such as dollars, you need a separate, explicit conversion path and a rule for choosing among available markets.
Keep the index’s scope clear. PancakeSwap’s current interface can route trades across V2, V3 and other liquidity sources, while a V2 factory index sees only V2 pairs. A V2-only series is easier to interpret and reconstruct, but it cannot stand in for the price or execution route shown by the broader interface. Name the pool and quote asset alongside each price.
What should an indexer store and check?
A useful record joins pair identity, reserve state and the event that produced it. At minimum, retain:
- Chain identifier, factory and pair addresses, plus
token0andtoken1. - Token decimals and any metadata source or retrieval status.
- Raw reserves, derived price direction, block number and block hash.
- Transaction and log positions, so updates can be replayed in their original order.
Use the raw reserves as the authoritative indexed values, and keep the derived price reproducible from those values and the recorded decimals. The next signals to watch are new factory pair events, reserve changes, zero or unusually small liquidity, metadata failures and block reorganizations. Together, they show whether a price change reflects pool activity, a fragile market or a correction to the chain history.