TradingOrder Book

Order Book

GX Exchange operates a central limit order book (CLOB) for each market with price-time priority matching and semantic transaction ordering to ensure fair execution.

Matching Algorithm

Price-Time Priority

Orders are matched using strict price-time priority:

  1. Price priority: The best-priced order is matched first. For buy orders, the highest price has priority. For sell orders, the lowest price has priority.
  2. Time priority: Among orders at the same price level, the order that arrived first is matched first (FIFO).

This is the same matching algorithm used by traditional exchanges and provides a transparent, deterministic execution model.

Matching Process

When a new order enters the book:

  1. The order is checked against resting orders on the opposite side
  2. If the incoming order’s price crosses the best resting price, a match occurs
  3. The match executes at the resting order’s price (the price of the order already in the book)
  4. If the incoming order is not fully filled, the remainder either rests in the book (GTC, Limit) or is cancelled (IOC, FOK)

Semantic Mempool Ordering

Within each block, transactions are sorted into three priority tiers before matching:

PriorityAction TypeRationale
1 (highest)Transfers, withdrawals, leverage changesMust settle before any trading to prevent margin manipulation
2CancellationsAllows traders to remove stale orders before new matches
3 (lowest)GTC and IOC ordersActual trading happens last, on a clean book

Within each tier, the block proposer’s original ordering is preserved. This semantic ordering prevents several attack vectors:

  • Sandwich attacks: An attacker cannot insert a trade before a victim’s order because order placement is in the lowest priority tier
  • Margin manipulation: Withdrawals and leverage changes are processed before new orders, preventing users from placing orders with margin they intend to withdraw
  • Stale order exploitation: Cancellations are processed before new orders, so market makers can update their quotes before new aggressive orders execute

Order Book Depth

Each market maintains separate bid and ask sides. Key properties:

  • Tick-aligned prices: All price levels are multiples of the market’s tick size
  • Lot-aligned sizes: All order sizes are multiples of the market’s lot size
  • No hidden orders: All resting orders are fully visible in the order book
  • Reduce-only flag: Orders marked as reduce-only can only decrease an existing position, never increase it

Order Book Data

The order book state is available through:

  • WebSocket subscriptions: Real-time order book updates (diffs) pushed to connected clients
  • REST API snapshots: Full order book depth at a point in time
  • Node data output: Raw book diffs written to node_raw_book_diffs/hourly/{date}/{hour} for historical analysis

Performance

The order book implementation is optimized for the GXCore matching engine’s industry-leading throughput:

  • Price levels are stored in sorted data structures for O(log n) insertion and O(1) best-price access
  • Order matching is lock-free within a single block’s execution
  • Book state is checkpointed every 10,000 blocks for fast node recovery