# Settlement - How trades become final

When you and your counterparty are matched off-chain, the sequencer creates an `IntentFillRecord` for each side of the match. It then packages all the fills from one match into a **batch**, identified by `batch_id`.

The batch is submitted on-chain via `BatchSettler.batchSettle(IntentFillRecord[] records)`. The contract iterates through every record and calls `Market.settleTrade` on the appropriate market contract. Each `settleTrade`:

1. Verifies the signed intent (as described above).
2. Verifies the margin: `fillSize ≤ MarginCalculator.maxSize(collateral, leverage, fillPrice)`.
3. Determines what kind of action this fill represents based on the trader's current position:
   * **Open** if they had no position.
   * **Increase** if same side as their position.
   * **Reduce** if opposite side, smaller than their position.
   * **Close** if opposite side, equal to their position.
4. Locks or releases collateral on the Hub accordingly.
5. Updates the position record.
6. Emits an event (`PositionOpened`, `PositionIncreased`, `PositionReduced`, `PositionClosed`).

#### Atomicity

The whole batch is atomic. If `settleTrade` for *any* fill in the batch reverts (invalid signature, exceeded `priceLimit`, intent expired, anything) **every fill in the batch reverts**.

This is critical. It means you can never end up with a one-sided settlement: if Alice opens a long, somebody must have opened the matching short, both recorded in the same transaction.

***


---

# 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.movex.market/architecture/settlement-how-trades-become-final.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.
