# Orderbook Checksum

Every orderbook message contains a 32-bit integer checksum (represented as a base-10 string). This checksum is used to compare a client's orderbook state versus the matching engine's orderbook state.

Should these checksums differ between client and server, the Websocket client is encouraged to re-subscribe to the channel to fetch the new orderbook snapshot in order to re-build the orderbook state.

* The preimage of the checksum is generated by concatenating each order's price and size, sorted by their price levels
* Only the **best 100 price levels** are included for purposes of checksumming.
* Format: `<best_bid_price>:<best_bid_size>:<best_ask_price>:<best_ask_size>:<second_best_bid_price>:<second_best_bid_size>:...`

**Example**:

* orderbook: `{ "bids": ["9", "2"]], "asks": [["10", "1"] }`
* resulting concatenation: `9:2:10:1`

```python
// Code Example

import zlib

def checksum(bids, asks):
    preimage = ""
    iterations = max(len(bids), len(asks))

    for index in range(min(iterations, 100)):
        if len(bids) > index:
            price, size = bids[index]
            preimage += price + ":" + size + ":"

        if len(asks) > index:
            price, size = asks[index]
            preimage += price + ":" + size + ":"
    
    preimage = preimage[:-1] # strip last colon
    crc = zlib.crc32(preimage.encode("utf8")) & 0xFFFFFFFF
    return crc

print(checksum([["9", "2"]], [["10", "1"]])) # 1226559413
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aevo.xyz/api-reference/orderbook-checksum.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
