Introduction
The DLOB (Decentralized Limit Order Book) is a high-performance, event-driven orderbook aggregator for Drift Protocol. It maintains live orderbook state across multiple markets by consuming incremental updates from the blockchain.Key Features
- Event-Driven Architecture: Incrementally built from user account updates and slot changes
- Multi-Market Support: Aggregates orderbooks for multiple perpetual and spot markets
- L2 and L3 Snapshots: Provides both aggregated (L2) and order-level (L3) orderbook views
- Lock-Free Reads: Optimized for high-throughput concurrent access
- Order Matching: Built-in logic for finding crosses and matching orders
- Auction Support: Handles Drift’s unique auction mechanisms for limit and market orders
DLOB Structure
The DLOB struct aggregates orderbooks for multiple markets:Order Types
The DLOB organizes orders into different categories based on their semantics:Maker Orders (Resting)
- Limit Orders: Orders with fixed prices that rest on the book
- Floating Limit Orders: Orders with prices offset from the oracle price
Taker Orders (Auction)
- Market Orders: Auction orders with fixed price bounds that change by slot
- Oracle Orders: Auction orders with dynamic price bounds relative to oracle price
Conditional Orders
- Trigger Orders: Orders that activate when a trigger condition is met
Basic Usage
Initialization
Getting Orderbook Snapshots
Accessing Bids and Asks
Core Methods
Snapshot Methods
Get an L3 (order-level) snapshot of the orderbookParameters:
market_index: u16- Market indexmarket_type: MarketType- Perp or Spot market
Arc<L3Book>Panics: If orderbook doesn’t exist for the marketGet an L3 snapshot, returning None if orderbook doesn’t existReturns:
Option<Arc<L3Book>>Get an L2 (aggregated) snapshot of the orderbookNote: L2 snapshots must be enabled via
enable_l2_snapshot()Returns: Arc<L2Book>Order Matching Methods
Find all maker orders that would cross with a given taker orderParameters:
current_slot: u64- Current blockchain slotoracle_price: u64- Current oracle pricetaker_order: TakerOrder- The taker order to matchperp_market: Option<&PerpMarket>- Market info for vAMM calculationsdepth: Option<usize>- Max order depth to consider (default: 32)
MakerCrosses - Matched maker orders and fill infoFind all auction orders crossing resting limit orders at the current slotParameters:
market_index: u16market_type: MarketTypeslot: u64oracle_price: u64perp_market: Option<&PerpMarket>trigger_price: u64depth: Option<usize>
CrossesAndTopMakers - All crosses and top maker accountsFind the crossing region where best bid >= best askReturns:
Option<CrossingRegion> - Crossing orders if any existConfiguration Methods
Enable live L2 snapshots for all orderbooks (disabled by default)
Disable live L3 snapshots for all orderbooks (enabled by default)
Spawn a notifier channel for sending updates to the DLOBReturns:
DLOBNotifier - Channel for sending order and slot updatesL3Book Structure
The L3Book provides order-level detail with support for:- Fixed price orders
- Floating (oracle-offset) orders
- VAMM taker orders
- Trigger orders with dynamic post-trigger prices
L3Book Methods
L3Order Fields
L3Order Methods
is_long()- True if this is a long orderis_reduce_only()- True if this is a reduce-only orderis_trigger_above()- True if trigger condition is “above”is_post_only()- True if this is a post-only limit orderis_maker()- True if order is maker-onlyis_taker()- True if order is taker-onlypost_trigger_price(slot, oracle_price, perp_market)- Calculate post-trigger price for trigger orders
Order Kinds
Performance Considerations
Lock-Free Reads
The DLOB uses lock-free atomic operations for reading orderbook snapshots, enabling high-throughput concurrent access without contention.Memory Management
Orderbooks are lazily initialized on first write, and snapshots use double-buffering to avoid blocking readers during updates.Snapshot Trade-offs
- L3 snapshots (enabled by default): Higher memory usage, full order detail
- L2 snapshots (disabled by default): Lower memory usage, aggregated price levels
Related Types
DLOBNotifier- Channel for sending updates to DLOBDLOBBuilder- Convenience builder for constructing DLOB instancesOrderMetadata- Metadata about orders in the DLOBTakerOrder- Minimal taker order info for matchingMakerCrosses- Matched maker orders for a takerCrossesAndTopMakers- All auction crosses and top makersCrossingRegion- Orders in a crossed book region
See Also
- DLOB Builder - Builder pattern for DLOB construction
- Order Matching - Order matching algorithms and types