Skip to main content
The orderbook channel gives you a snapshot on subscribe, then deltas. You keep two sorted maps (bids, asks) and apply deltas as upserts, treating a size of "0" as a delete. sequenceId lets you detect and recover from gaps.

Step 1 - subscribe

Step 2 - seed from the snapshot

Bids arrive sorted descending by price, asks ascending. Each level is [price, size] (strings).

Step 3 - apply deltas

Each delta entry is [price, newSize]. Upsert the level; if newSize === "0", delete it. Levels not mentioned are unchanged.

Step 4 - detect gaps.

sequenceId is monotonic per market. If an incoming delta’s sequenceId is greater than lastSeq + 1, you missed something — issue a resync and rebuild from the fresh snapshot.
Precision: prices and sizes are decimal strings to preserve on-chain precision. Use them as map keys verbatim; only parse to float for display / sorting, never for equality or storage.