Overview
The Drift Protocol integration provides comprehensive access to Drift’s decentralized perpetual and spot trading platform on Solana. The integration offers two primary client types optimized for different use cases:- AuthorityDrift: Full-featured client for trading applications with real-time subscriptions
- CentralServerDrift: Lightweight client for API servers that fetch data on-demand
Key Features
Real-time Data Subscriptions
- User account updates with position and balance tracking
- Market data including mark prices and oracle prices
- Orderbook data via websocket connections
- Optimized polling based on user involvement
Trading Operations
- Perpetual market and limit orders
- Spot market deposits and withdrawals
- Token swaps via Jupiter integration
- Position management (settle PnL, funding)
- Order management (create, edit, cancel)
Smart Subscription Management
The integration automatically optimizes data fetching based on:- Selected market: Highest priority polling for active trading
- User-involved markets: Medium priority for markets with positions
- Other markets: Low priority background polling
Architecture
Data Sources
- Solana RPC: Direct on-chain account data via polling
- DLOB Server (HTTP): Mark prices and oracle data for background markets
- DLOB Server (WebSocket): Real-time orderbook data for selected market
- Swift Server: Fast order execution service
Core Components
- DriftClient: Core SDK client for Drift program interactions
- SubscriptionManager: Optimizes polling frequencies and market subscriptions
- PollingDlob: Manages DLOB server polling for price data
- DriftL2OrderbookManager: WebSocket orderbook subscriptions
- DriftOperations: Trading operation handlers
Client Comparison
| Feature | AuthorityDrift | CentralServerDrift |
|---|---|---|
| Use Case | Trading UIs, wallets | API servers |
| User Subscriptions | Continuous | On-demand |
| Market Subscriptions | Continuous | Continuous |
| Wallet Management | Full authority | Per-transaction |
| Orderbook WebSocket | Yes | No |
| Memory Footprint | Higher | Lower |
Quick Start
AuthorityDrift Example
CentralServerDrift Example
Environment Configuration
Next Steps
- AuthorityDrift Client - Full trading client
- CentralServerDrift Client - API server client
- SubscriptionManager - Subscription optimization
- Orderbook Management - Real-time orderbook data
Source Code
Location:~/workspace/source/common-ts/src/drift/Drift/
- Clients:
clients/ - Data management:
data/ - Stores and caches:
stores/ - Constants:
constants/