# How the Exchange Works

Meridian combines offchain order matching with onchain custody and settlement.

## Main Components

| Component       | Purpose                                                                                       |
| --------------- | --------------------------------------------------------------------------------------------- |
| Trading API     | Accepts signed requests and provides account and market data.                                 |
| Matching system | Orders, matches, cancels, and triggers orders.                                                |
| Sequencer       | Orders exchange actions and sends them for onchain processing.                                |
| Smart contracts | Hold collateral and record balances, positions, funding, fees, liquidations, and withdrawals. |
| Price oracle    | Supplies reference prices for marks and onchain checks.                                       |

## Trade Flow

1. A trader signs an order with the owner wallet or a linked signer.
2. The Trading API validates the request and sends it to the matching system.
3. WebSocket streams publish order and fill updates.
4. The sequencer orders fills and related actions for settlement.
5. The smart contracts verify the signed orders and settlement data before updating onchain state.

An accepted API request confirms receipt, not execution. Use WebSocket updates for order results and contract state for settlement.

## State Ordering

Oracle marks drive PnL, margin, stop triggers, and liquidations. The smart contracts also validate oracle data submitted with liquidations.

Funding, position fees, fills, and liquidations are processed in sequence. An earlier action can change the validity of a later action.

## Onchain Validation

Before a trade is settled, the smart contracts verify:

* Maker and taker EIP-712 signatures and order expiry.
* Account, subaccount, and linked-signer authorization.
* Order nonces, product status, side, price, quantity, tick size, lot size, and reduce-only constraints.
* Fill quantity, fill notional, and realized PnL against onchain calculations and tolerances.

The contracts also validate oracle freshness and maintenance margin for liquidations, account for funding and position fees, and enforce collateral and withdrawal rules.

The owner configures products, tokens, fees, operator roles, and upgrades. Authorized sequencers submit action batches. See [Contracts](/protocol-reference/contracts) and [Audits](/protocol-reference/audits).
