> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kuru.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Building an Indexer

> Build a reorg-safe reducer for Kuru Spot accounts, markets, orders, trades, and liquidity.

Use this section when you operate your own event-derived state store. Applications using Kuru's hosted [market data](/api/market-data) and [account data](/api/account-activity) do not need to implement these reducers just to start an integration.

The goal is to reconstruct account balances, orders, executions, and liquidity positions from chain events, with enough provenance to undo and replay state after a reorg.

Index AccountCore as the custody ledger, each OrderBook as a market ledger, SpotRouter as the market registry, and each delegated trading-wallet EOA as its own intent store.

## Ordering and identity

Process logs in `(blockNumber, transactionIndex, logIndex)` order. Preserve block hash and parent hash so every derived write can be rolled back on a reorg.

Canonical keys include:

```text theme={null}
account:       (chainId, accountId)
active slot:   (chainId, market, userId, slotIdx)
order history: (chainId, market, userId, slotIdx, orderId)
fill:          (chainId, market, tradeId)
passive band:  (chainId, market, lowPrice)
position:      (chainId, market, positionId)
trigger:       (chainId, wallet, triggerId)
```

Slots are reused. Never identify historical orders without `orderId`.

## Bootstrap

1. Start from the component and market `fromBlock` values on [Testnet Deployments](/deployments/testnet).
2. Discover markets from SpotRouter registration events and confirm them against AccountCore registration.
3. Cache immutable market fields and current application-visible configuration.
4. Replay all logs in order.
5. Periodically compare balances, active slots, band state, and top-of-book views against the chain.

## Source precedence

* `TradesPacked` is required for execution-induced active and passive depth changes.
* `BookUpdatesPacked` covers newly resting state and non-trade removals.
* `MATCH_END` partitions fee and settlement groups inside one packed action.
* `SpotReserveUpdated` is an AccountCore post-state snapshot. Apply every snapshot in log order; do not also apply `Deposit`, `Withdrawal`, or transfer as a second balance mutation.
* `PassiveBandUpdated` is the passive inventory snapshot.
* `SpotSwap` is a taker summary, not a complete trade tape.

Unknown record lengths, flags, enum values, or implementation code hashes must fail closed into an unsupported-version path rather than being decoded heuristically.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.