Order Flow
Order placement, matching, and cancellation lifecycle
Order Flow
This document describes the lifecycle of orders in the CLOB system from a user's perspective.
Order Placement
Limit Orders
Place an order at a specific price. The order rests in the book until filled or cancelled.
placeOrder(
PoolKey pool,
uint256 price,
uint256 quantity,
Side side,
address user
)What happens:
- Funds are verified and locked
- Order is checked against existing orders for potential matches
- Any matched portion executes immediately
- Remaining quantity (if any) rests in the order book
OrderPlacedevent is emitted with the order ID
Market Orders
Execute immediately at the best available prices.
placeMarketOrder(
PoolKey pool,
uint256 quantity,
Side side,
address user,
uint256 minAmountOut
)What happens:
- Funds are verified and locked
- Order executes against available liquidity at best prices
- Slippage protection ensures
minAmountOutis satisfied OrderMatchedevents are emitted for each fill
Order Types
| Type | Description |
|---|---|
| Limit | Rests at specified price until filled or cancelled |
| Market | Executes immediately at best available prices |
Time In Force Options
The TimeInForce parameter controls how long an order remains active:
| Type | Behavior |
|---|---|
| GTC (Good Till Cancel) | Order remains open until filled or explicitly cancelled |
| IOC (Immediate or Cancel) | Execute what's possible immediately, cancel the rest |
| FOK (Fill or Kill) | Execute entire quantity or cancel completely |
| PostOnly | Only add liquidity; cancel if would take liquidity |
Order Matching
Orders are matched following price-time priority:
-
Price Priority: Best prices match first
- Highest bid matches against lowest ask
-
Time Priority: At the same price level, earlier orders fill first
Trades execute at the maker's price (the resting order), providing price improvement for aggressive orders.
Order Cancellation
Single Order Cancellation
cancelOrder(PoolKey pool, uint256 orderId)What happens:
- Ownership is verified
- Order is removed from the book
- Locked funds are returned to available balance
OrderCancelledevent is emitted
Batch Cancellation
cancelBatchOrders(PoolKey pool, uint256[] orderIds)Cancels multiple orders in a single transaction for gas efficiency.
Self-Trade Prevention
The system prevents users from trading against their own orders using configurable STP modes:
| Mode | Behavior |
|---|---|
| None | Allow self-trades |
| CancelTaker | Cancel incoming order if it would match own order |
| CancelMaker | Cancel resting order if incoming order would match |
| CancelBoth | Cancel both orders |
Batch Operations
Batch Order Placement
placeBatchOrders(BatchOrder[] orders, bool atomic)| Mode | Behavior |
|---|---|
atomic = true | All orders succeed or all fail |
atomic = false | Each order processed independently |
Benefits:
- Gas savings through amortized transaction costs
- Atomic updates for market making strategies
- Place bid/ask pairs in single transaction
Error Handling
| Error | Cause | Resolution |
|---|---|---|
InsufficientBalance | Not enough funds for order | Deposit more funds |
InvalidPrice | Price is zero or exceeds limits | Use valid price |
InvalidPriceIncrement | Price not on valid tick | Round to valid tick |
OrderTooSmall | Below minimum order size | Increase quantity |
OrderTooLarge | Exceeds maximum order size | Reduce quantity |
PostOnlyWouldTake | Post-only order would cross spread | Use different price |
FillOrKillNotFulfilled | FOK order can't fully fill | Use IOC or adjust size |
SlippageExceeded | Execution worse than limit | Increase slippage tolerance |