Matching Engine
The matching engine is responsible for executing trades when compatible orders meet.Order Book Structure
The order book maintains two sides:| Side | Contains | Best Price |
|---|---|---|
| Bids (Buy) | Buy orders | Highest price |
| Asks (Sell) | Sell orders | Lowest price |
Price-Time Priority
All matching follows strict price-time priority:Price Priority
- Buy orders: Highest prices have priority
- Sell orders: Lowest prices have priority
- A trade occurs when bid price >= ask price
Time Priority
At the same price level, orders are filled in the sequence they were placed (first-in, first-out).Execution Behavior
When Orders Match
A new order that crosses the spread executes immediately:| New Order | Matches Against | Execution Price |
|---|---|---|
| Buy at 101 | Resting ask at 100 | 100 (maker’s price) |
| Sell at 99 | Resting bid at 100 | 100 (maker’s price) |
Partial Fills
Orders may be partially filled:- The filled portion settles immediately
- Remaining quantity behavior depends on TimeInForce:
- GTC: Rests in book
- IOC: Cancelled
- FOK: Entire order cancelled (no partial execution)
Gas Considerations
To ensure predictable gas costs, matching operations have configurable limits. If an order cannot be fully processed within gas limits:- IOC/FOK orders: Remaining quantity is cancelled
- GTC orders: Remaining quantity rests in the book
Best Price Queries
Query current market state:Hooks Integration
The matching engine supports hooks at key points:| Hook Point | Use Cases |
|---|---|
| Pre-order | Custom validation, trading restrictions |
| Post-match | Logging, external integrations |