BulkAccountLoader
TheBulkAccountLoader is the core mechanism for polling-based subscriptions. It efficiently batches multiple account requests into single RPC calls.
Constructor
Connection
required
Solana connection instance
Commitment
required
Commitment level for account fetches (e.g., ‘confirmed’, ‘finalized’)
number
required
Polling interval in milliseconds (e.g., 1000 for 1 second)
Methods
addAccount
Add an account to the polling set.PublicKey
required
Public key of the account to poll
(buffer: Buffer, slot: number) => void
required
Callback function invoked when account data updates
removeAccount
Remove an account from the polling set.load
Manually trigger a load of all accounts.updatePollingFrequency
Change the polling frequency.number
required
New polling interval in milliseconds
Example Usage
PollingUserAccountSubscriber
Poll-based subscriber for user accounts.Constructor
Connection
required
Solana connection instance
PublicKey
required
Public key of the user account to subscribe to
BulkAccountLoader
required
Shared bulk account loader instance
(name: string, buffer: Buffer) => UserAccount
required
Function to decode account buffer into UserAccount
Methods
subscribe
Start polling the user account.UserAccount
Optional initial user account data
getUserAccountAndSlot
Get the current user account data.DataAndSlot<UserAccount>
Throws: NotSubscribedError if account hasn’t been fetched
updateData
Manually update the user account data.Example Usage
PollingDriftClientAccountSubscriber
Poll-based subscriber for Drift protocol accounts (state, markets, oracles).Constructor
Program
required
Anchor program instance for the Drift protocol
BulkAccountLoader
required
Shared bulk account loader instance
number[]
required
Array of perpetual market indexes to poll
number[]
required
Array of spot market indexes to poll
OracleInfo[]
required
Array of oracle information to poll
boolean
required
Whether to automatically discover and poll all markets
DelistedMarketSetting
required
How to handle delisted markets:
Unsubscribe, Subscribe, or DiscardMethods
subscribe
Start polling all configured accounts.addPerpMarket
Dynamically add a perpetual market to poll.addSpotMarket
Dynamically add a spot market to poll.addOracle
Dynamically add an oracle to poll.updateAccountLoaderPollingFrequency
Update the polling frequency.number
required
New polling interval in milliseconds
Example Usage
Other Polling Subscribers
The SDK provides polling subscribers for other account types:PollingTokenAccountSubscriber
Poll SPL token account updates:PollingOracleAccountSubscriber
Poll oracle account updates:Performance Considerations
Polling Frequency
Choose polling frequency based on your needs:- High frequency (500-1000ms): Real-time trading, liquidations
- Medium frequency (2000-5000ms): Portfolio monitoring, dashboards
- Low frequency (10000+ms): Background tasks, analytics
Batch Efficiency
TheBulkAccountLoader automatically batches requests:
- Uses
getMultipleAccountsfor efficiency - Chunks requests to avoid RPC limits (100 accounts per request)
- Processes chunks in parallel (10 chunks at a time)
- Shares a single loader across multiple subscribers
Rate Limiting
To avoid rate limits:Best Practices
- Share account loaders - Use a single
BulkAccountLoaderinstance across multiple subscribers - Choose appropriate polling frequency - Balance between latency and RPC usage
- Handle errors - Listen to error events and implement retry logic
- Monitor slot numbers - Check slot numbers to detect stale data
- Clean up properly - Unsubscribe to stop polling and free resources
Advantages Over WebSocket
- More reliable - Less affected by network issues
- Rate limit friendly - Batches multiple accounts in single requests
- Predictable resource usage - Fixed polling interval
- Easier to debug - Simple request/response model
- Better for batch operations - Efficient when monitoring many accounts
Disadvantages
- Higher latency - Updates only arrive at polling intervals
- More RPC calls - Regular polling even when data unchanged
- Resource usage - Continuous polling consumes RPC quota