Overview
TheSubscriptionManager handles all aspects of subscription management for the Drift protocol integration. It automatically optimizes polling frequencies and data fetching based on user positions and the selected trade market, ensuring efficient resource usage while maintaining accurate real-time data.
Key Responsibilities
- User account subscription and event handling
- Market account and oracle polling optimization
- DLOB server polling configuration
- Orderbook websocket subscription management
- Authority (wallet) management with full resubscription
Constructor
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:47
Note: This class is typically instantiated internally by AuthorityDrift and not used directly.
Market Categorization
categorizeMarketsByUserInvolvement()
Categorizes markets based on whether the user has active positions.- A market is “user-involved” if any user has an active perp or spot position in that market
- All other tradable markets are “user-not-involved”
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:97
Polling Optimization
Polling Cadences
The manager uses three tiers of polling frequencies:updateMarketAccountCadence()
Updates the polling frequency for a market and its oracle.- Updates the market account polling cadence
- Updates the associated oracle account polling cadence
- Only updates if cadence has changed (optimization)
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:138
updateAccountLoaderCadenceForMarkets()
Applies polling cadences to multiple markets based on categorization.- Selected trade market: 1s polling (highest priority)
- User-involved markets: 2s polling (medium priority)
- User-not-involved markets: 5s polling (lowest priority)
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:181
DLOB Polling Management
updatePollingDlobIntervals()
Configures DLOB server polling intervals for price data.- User-involved markets: Deep polling every 3 seconds
- User-not-involved markets: Shallow polling every 30 seconds
- Selected trade market: Real-time via websocket (not polled)
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:215
User Account Management
subscribeToAllUsersUpdates()
Sets up event listeners for all user accounts.- Gets all user accounts from DriftClient
- Initial hydration of user account cache
- Sets up ‘update’ event listeners on each user
- Triggers subscription optimization on each update
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:74
handleSubscriptionUpdatesOnUserUpdates()
Main orchestration method triggered on user account updates.- Categorizes markets by user involvement
- Filters out selected trade market from other categories
- Updates account loader cadences
- Updates DLOB polling intervals
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:242
subscribeToNonWhitelistedButUserInvolvedMarkets()
Ensures markets with user positions are subscribed even if not in tradable markets list.- Collects all markets with user positions
- Adds them to DriftClient subscription lists
- Resubscribes to account subscriber with updated market list
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:276
Selected Trade Market
updateSelectedTradeMarket()
Updates the selected trade market and optimizes all subscriptions.-
Unsubscribe from previous market orderbook
-
Downgrade previous market priority
- If user has positions: Move to USER_INVOLVED category
- If no positions: Move to USER_NOT_INVOLVED category
- Update polling cadence accordingly
-
Upgrade new market priority
- Remove from DLOB polling intervals (will use websocket)
- Set to SELECTED_MARKET polling cadence (1s)
- Subscribe orderbook websocket to new market
-
Subscribe to new market orderbook
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:357
Authority Management
updateAuthority()
Switches to a new wallet and reestablishes all subscriptions.-
Validate change
-
Cleanup old state
-
Update wallet
-
Resubscribe to markets
-
Switch sub-account (if specified)
-
Reestablish user subscriptions
src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:425
Configuration Updates
updateTradableMarkets()
Updates the list of tradable markets.src/drift/Drift/clients/AuthorityDrift/SubscriptionManager.ts:62
Polling Categories
The manager uses three polling categories:Category Characteristics
| Category | RPC Polling | DLOB Polling | Use Case |
|---|---|---|---|
| SELECTED_MARKET | 1s | Websocket | Active trading |
| USER_INVOLVED | 2s | 3s (deep) | Position monitoring |
| USER_NOT_INVOLVED | 5s | 30s (shallow) | Background data |
Example: Manual Optimization
While typically managed byAuthorityDrift, you can understand the optimization flow:
Performance Considerations
RPC Load
Polling frequency directly affects RPC requests:- Selected market: ~60 requests/minute (1s interval)
- User-involved markets: ~30 requests/minute each (2s interval)
- Other markets: ~12 requests/minute each (5s interval)
Optimization Tips
-
Limit tradable markets: Only include markets you need
-
Use selected market wisely: Only set for active trading
- Monitor user positions: The manager automatically optimizes based on actual usage
See Also
- AuthorityDrift - Main client using SubscriptionManager
- PollingDlob - DLOB polling system
- Orderbook Management - WebSocket orderbook manager