> For the complete documentation index, see [llms.txt](https://docs.world.inc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.world.inc/details/matching-algorithm.md).

# Matching Algorithm

The order book uses a ladder matching algorithm that ensures the taker receives the best execution.\
This is different from single-price-per-trade matching algorithms that are employed elsewhere. Order\
matching is atomic (in the same blockchain transaction as the incoming taker order): the orderbook will\
never have overlapping orders from two sides. Orders in the book at the same price are sorted by time\
priority, the earliest orders at the same price are matched first.

For example: The book contains two buy orders, one at $100, quantity 2, another at $101, quantity 3. A sell order comes in that’s priced at $99, quantity 4. Two trades are created: one trade at $101, quantity 3, and another at $100, quantity 1.

If the exchange did not implement this algorithm, a secondary smart contract could break incoming orders atomically and automatically, as the order book is fully lit. To prevent flow re-routing and provide the same outcome for all users, the exchange must therefore implement this algorithm.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.world.inc/details/matching-algorithm.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
