Overview
Drift Common provides two primary client implementations for interacting with the Drift protocol:- AuthorityDrift - For user-facing applications with wallet integration
- CentralServerDrift - For server-side applications and APIs
DriftClient from @drift-labs/sdk and provide specialized functionality for different use cases.
AuthorityDrift Client
TheAuthorityDrift client is designed for applications that need to subscribe to all user accounts for a given authority (wallet). This is ideal for:
- Trading UIs and web applications
- Wallet applications with Drift integration
- Real-time user account monitoring
Key Features
Real-time Subscriptions- Subscribes to all user accounts for a wallet authority
- Continuous market data updates via polling and websockets
- Oracle price and mark price caching
- L2 orderbook management via websocket
- Deposits and withdrawals
- Perpetual market orders (market and limit)
- Spot market swaps
- Order management (edit, cancel)
- PnL settlement and funding payments
- Mark price cache with websocket and polling sources
- Oracle price cache from DriftClient subscriptions
- Priority fee tracking for optimized transaction costs
Basic Usage
Data Caching
TheAuthorityDrift client maintains three key data caches:
Mark Price Cache
- Sources: Websocket DLOB subscriber (active market), Polling DLOB server (other markets)
- Updated based on market activity and user positions
- Accessible via
authorityDrift.markPriceCache
- Sources: DriftClient oracle subscriptions, Polling DLOB server
- Real-time oracle price updates
- Accessible via
authorityDrift.oraclePriceCache
- Stores fetched user account data for all sub-accounts
- Enhanced with oracle and mark price data
- Accessible via
authorityDrift.userAccountCache
Subscription Management
The client optimizes polling based on user activity:CentralServerDrift Client
TheCentralServerDrift client is designed for server-side applications that need to fetch user data on-demand while maintaining continuous market data subscriptions. Perfect for:
- API servers
- Transaction generation services
- Backend trading systems
Key Features
On-Demand User Data- Fetches user account data only when needed
- No persistent user subscriptions
- Uses
OneShotUserAccountSubscriberfor efficient one-time reads
- Continuous subscription to specified markets
- Efficient account loader with polling
- Priority fee tracking
- Creates unsigned transactions for client signing
- Context wrapper for proper authority management
- Support for all Drift operations
Basic Usage
Transaction Generation
TheCentralServerDrift client provides methods to generate transactions for various operations:
Account Management
Context Wrapper Pattern
TheCentralServerDrift client uses a context wrapper pattern to manage DriftClient state:
- User creation and subscription
- Authority management
- Wallet replacement for correct transaction signing
- Cleanup and state restoration
Fetching User Data
Market Access
Both clients provide access to market data through the DriftClient:CentralServerDrift Market Queries
TheCentralServerDrift client includes a markets property for convenient market queries:
Priority Fees
Both clients include priority fee management:- Current network conditions via
PriorityFeeSubscriber - High activity market accounts
- Configurable priority fee methods
Choosing the Right Client
Use AuthorityDrift when:- Building a trading UI or wallet application
- Need real-time user account updates
- Want continuous price feed subscriptions
- Require orderbook data via websocket
- Building an API server or backend service
- Need to generate transactions for multiple users
- Want to minimize memory and connection overhead
- Only need on-demand user data access
Next Steps
- Learn about Configuration options
- Explore Environment Constants for different networks
- Check the API Reference for detailed method signatures