Overview
TheDLOBBuilder provides a convenient way to construct and manage an event-driven DLOB instance. It handles:
- Initializing the DLOB from existing user accounts
- Creating update handlers for gRPC subscriptions
- Managing the notifier channel for incremental updates
- Bootstrapping orderbook state
Structure
Construction
From AccountMap
The most common way to initialize a DLOB is from an existingAccountMap:
- Creates a new DLOB instance
- Spawns the notifier channel
- Iterates through all User accounts in the AccountMap
- Inserts all open orders into the DLOB
From User Iterator
Alternatively, you can initialize from an iterator of User accounts:gRPC Integration
The builder provides handlers that integrate seamlessly with Drift’s gRPC subscription system:Complete Example
Methods
new
Initialize a new DLOBBuilder from an AccountMapParameters:
account_map: &AccountMap- Account map with initial User accounts to bootstrap orderbook
DLOBBuilder<'a>This method:- Creates a new DLOB instance (leaked for ‘static lifetime)
- Spawns the notifier channel
- Iterates through all User accounts
- Inserts all open orders into the DLOB
new_with_users
Initialize a new DLOBBuilder from an iterator of User accountsParameters:
users: impl Iterator<Item = &'u User>- Iterator of User accountsslot: u64- Slot when users were retrieved
DLOBBuilder<'a>Useful for custom user account sources or when you want more control over initialization.dlob
Get a reference to the underlying DLOB instanceReturns:
&'a DLOBUse this to access all DLOB methods like get_l3_snapshot, find_crosses_for_taker_order, etc.account_update_handler
Create a handler for gRPC user account updatesParameters:
account_map: &'b AccountMap- Account map for comparing old/new states
impl Fn(&AccountUpdate) + Send + Sync + 'bThis handler:- Deserializes account updates from gRPC
- Compares old and new user states
- Generates order deltas (creates/removes)
- Sends deltas to the DLOB notifier
slot_update_handler
Create a handler for gRPC slot updatesParameters:
drift: DriftClient- Drift client for fetching oracle pricesmarkets: Vec<MarketId>- Markets to update on each slot
impl Fn(u64) + Send + Sync + 'staticThis handler:- Receives new slot numbers from gRPC
- Fetches current oracle prices for specified markets
- Sends slot and oracle updates to the DLOB notifier
- Triggers orderbook updates (expiring auctions, updating snapshots)
load_user
Manually load an individual user account to the DLOBParameters:
pubkey: Pubkey- User account public keyuser: &User- User account dataslot: u64- Current slot
- Adding users outside of the normal gRPC flow
- Testing and simulation
- Custom initialization scenarios
Implementation Details
User Account Updates
The account update handler uses theuser_update method of DLOBNotifier:
- Compares old and new user states (if old exists)
- Generates
OrderDeltaevents for changes:Create- New order or order became openRemove- Order cancelled, filled, or changed
- Sends deltas to the DLOB event channel
Slot and Oracle Updates
The slot update handler sendsSlotAndOracleUpdate events:
- Update the orderbook to the new slot
- Expire completed auctions
- Move auction orders to resting orders if applicable
- Update L2/L3 snapshots with new oracle price
DLOB Lifecycle
The DLOB instance is leaked (given a'static lifetime) because:
- It needs to live for the entire program duration
- Multiple threads need concurrent access
- The notifier thread holds a reference to it
Error Handling
The builder’s handlers will panic if:- The DLOB event channel is closed (indicates DLOB thread dropped)
- Oracle price cannot be fetched (in slot_update_handler)
Performance Considerations
Initialization
- Bootstrap time scales with number of user accounts and orders
- Consider using filters if you only need specific markets
- Large orderbooks (1000s of orders) initialize in milliseconds
Update Throughput
- Channel is bounded at 2048 events
- High update rates may block if DLOB processing falls behind
- Consider increasing channel size for high-frequency scenarios
Memory Usage
- Each order consumes ~200 bytes in the DLOB
- L3 snapshots double memory per orderbook
- Metadata and event tracking add ~100 bytes per order (if enabled)
Common Patterns
Filtered Markets
Only track specific markets to reduce memory and processing:Custom User Loading
Load users from custom sources:Manual Updates
Send updates manually without gRPC:Testing Example
See Also
- DLOB Overview - Core DLOB concepts and usage
- Order Matching - Order matching types and algorithms
DLOBNotifier- Low-level update channel (in mod.rs:220-294)