Matching Logic
Core price-time prioerity logic explained
Order Book Structure
Orderbook {
bids: BTreeMap<OrderbookKey, Order>, // Sorted high-to-low
asks: BTreeMap<OrderbookKey, Order>, // Sorted low-to-high
}
OrderbookKey {
price: i64, // Negated for bids
timestamp: u64,
order_id: OrderId,
}Matching Algorithm
while best_bid.price >= best_ask.price:
1. Prune any zero-quantity orders at top of book
2. Check crossing conditions (price, market_id, self-trade)
3. Determine maker (earlier timestamp wins)
4. Execute at maker's price
5. Update quantities; remove exhausted ordersMaker Determination
Partial Fills
Trade Output
Safety Checks
Last updated