DriftClient is the main entry point for interacting with Drift Protocol. It manages connections to the Solana blockchain, handles account subscriptions, and provides methods for trading and account management.
Prerequisites
Before initializing the DriftClient, ensure you have:- A Solana RPC connection
- A wallet/keypair
- The Drift program ID
Basic Initialization
1
Import Required Dependencies
2
Set Up Connection and Wallet
3
Create Account Loader
4
Initialize DriftClient
Configuration Options
DriftClientConfig
Connection
required
Solana web3.js Connection instance
IWallet
required
Wallet instance implementing the IWallet interface
PublicKey
required
Drift program ID (use
initialize() helper to get correct ID)object
required
Account subscription configuration
DriftEnv
Environment: ‘mainnet-beta’ or ‘devnet’ (default: ‘mainnet-beta’)
TxSender
Custom transaction sender (default: RetryTxSender)
ConfirmOptions
Solana transaction confirmation options
Subscription Types
Polling Subscription
Best for most use cases. Efficiently batches account fetches.WebSocket Subscription
Real-time updates with lower latency. Higher RPC load.gRPC Subscription
Lowest latency, highest throughput. Requires gRPC infrastructure.Environment Configuration
Use theinitialize() helper to get correct addresses for each environment:
The
initialize() function returns configuration including:DRIFT_PROGRAM_ID: Main Drift program addressUSDC_MINT_ADDRESS: USDC token mint- Market configuration and oracles
Complete Example
Best Practices
Choose the right subscription type
Choose the right subscription type
- Polling: Default choice, good balance of performance and simplicity
- WebSocket: When you need real-time updates and can handle reconnection logic
- gRPC: For professional market makers and high-frequency trading
Handle subscription lifecycle
Handle subscription lifecycle
Error handling
Error handling
Next Steps
Create User Account
Set up your trading account
Place Orders
Start trading on Drift
Account Subscriptions
Learn about subscription mechanisms
API Reference
Explore DriftClient methods