# How the AMM + Order Book Model Works

### Hybrid AMM + Order Book Model

Monday Trade employs a hybrid model that seamlessly integrates an **Automated Market Maker (AMM)** with an **onchain order book**, delivering both **instant liquidity** and **precise trade execution** on a single platform.

#### Core Architecture

At the heart of the system is a **tick-based architecture**, where price levels are discretized into fixed intervals (“ticks”). This framework enables structured liquidity and granular order placement.

The protocol is composed of three main components:

* Liquidity Handler (AMM Management)
* Order Handler (Limit Order Management)
* Swap Handler (Trade Execution)

***

#### AMM Mechanics

The AMM serves as a **fallback liquidity source** when limit orders are unavailable or insufficient. Traders can always execute trades at the current AMM price.

* Inspired by **Uniswap V3**, liquidity is concentrated into **custom price ranges**.
* The **liquidity handler** calculates **active liquidity**, i.e., liquidity available in the current price range.
* Swaps are routed through a **swap router or custom aggregator**, ensuring optimal execution even for less liquid tokens.

***

#### Onchain Order Book

The **order book** is managed by the order handler and allows users to place **limit orders at specific ticks** (e.g., "buy ETH at $2000").

* Orders must be placed at ticks **above or below the current price**, depending on the token pair (e.g., token0 = ETH, token1 = USDC). This reduces arbitrage risks.
* **Orders on the same tick are merged** for efficiency and clarity.
* **Filled orders are automatically withdrawn** before new placements can occur at the same tick.

***

#### Swap Execution & Lazy Crossing

The **swap handler** introduces a novel **lazy crossing mechanism**, optimizing gas costs while maintaining order book precision.

* **Fine-grained ticks** are used for order placement, while **coarser ticks** are used for swap settlement.
* **Lazy crossing** updates only the necessary **swap ticks during trade execution** and delays order tick updates until the next relevant order placement.
* A **nonce system** ensures correctness and prevents desynchronization.

> ✅ This mechanism delivers up to 98% gas savings — for example, crossing 101 order ticks may only update 2 swap ticks.


---

# 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.monday.trade/spot-trading/spot-protocol-mechanics/how-the-amm-+-order-book-model-works.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.
