Overview
TheAuthorityDrift client provides comprehensive access to Drift Protocol for trading applications. It maintains continuous subscriptions to all user accounts under a given authority (wallet) and optimizes market data fetching based on user positions and the selected trade market.
Constructor
Configuration
Parameters
- solanaRpcEndpoint: Solana RPC endpoint URL
- driftEnv: Environment (
'mainnet-beta'or'devnet') - wallet: User’s wallet (optional, uses placeholder if not provided)
- driftDlobServerHttpUrl: Custom DLOB server URL (optional)
- tradableMarkets: Markets to subscribe to (defaults to all markets)
- selectedTradeMarket: Market to prioritize for trading (optional)
- additionalDriftClientConfig: Additional DriftClient configuration
- priorityFeeSubscriberConfig: Priority fee configuration
- orderbookConfig: Orderbook subscription settings
Lifecycle Methods
subscribe()
Initializes all subscriptions and starts data fetching.- Subscribes to DriftClient (market accounts, oracle accounts)
- Filters out delisted markets
- Starts DLOB polling for mark prices
- Establishes orderbook websocket connection
- Subscribes to priority fee updates
- Checks geo-blocking status
- Sets up user account event listeners
unsubscribe()
Cleans up all subscriptions and releases resources.Properties
driftClient
src/drift/Drift/clients/AuthorityDrift/index.ts:302
authority
src/drift/Drift/clients/AuthorityDrift/index.ts:306
tradableMarkets
src/drift/Drift/clients/AuthorityDrift/index.ts:334
oraclePriceCache
src/drift/Drift/clients/AuthorityDrift/index.ts:314
markPriceCache
src/drift/Drift/clients/AuthorityDrift/index.ts:318
userAccountCache
src/drift/Drift/clients/AuthorityDrift/index.ts:322
orderbookCache
src/drift/Drift/clients/AuthorityDrift/index.ts:326
isGeoBlocked
src/drift/Drift/clients/AuthorityDrift/index.ts:343
Event Subscriptions
onUserAccountUpdate()
Subscribe to user account updates.src/drift/Drift/clients/AuthorityDrift/index.ts:713
onMarkPricesUpdate()
Subscribe to mark price updates.src/drift/Drift/clients/AuthorityDrift/index.ts:709
onOraclePricesUpdate()
Subscribe to oracle price updates.src/drift/Drift/clients/AuthorityDrift/index.ts:703
onOrderbookUpdate()
Subscribe to orderbook updates for the selected trade market.src/drift/Drift/clients/AuthorityDrift/index.ts:719
Trading Operations
createUserAndDeposit()
Creates a new user account and deposits initial collateral.src/drift/Drift/clients/AuthorityDrift/index.ts:764
deposit()
Deposits collateral into a user’s spot market position.src/drift/Drift/clients/AuthorityDrift/index.ts:791
withdraw()
Withdraws collateral from a user’s spot market position.src/drift/Drift/clients/AuthorityDrift/index.ts:801
openPerpOrder()
Opens a perpetual market order.src/drift/Drift/clients/AuthorityDrift/index.ts:811
Note: This method is geo-blocked and will throw GeoBlockError if the user is blocked.
swap()
Executes a swap between two spot markets using Jupiter.src/drift/Drift/clients/AuthorityDrift/index.ts:824
settleAccountPnl()
Settles profit and loss for perpetual positions.src/drift/Drift/clients/AuthorityDrift/index.ts:844
cancelOrders()
Cancels a list of open orders.src/drift/Drift/clients/AuthorityDrift/index.ts:866
deleteUser()
Deletes a user account.src/drift/Drift/clients/AuthorityDrift/index.ts:856
Market Management
updateSelectedTradeMarket()
Updates the selected trade market and optimizes subscriptions.- Updates orderbook websocket subscription to new market
- Adjusts polling cadences (old market downgraded, new market prioritized)
- Updates DLOB polling intervals
src/drift/Drift/clients/AuthorityDrift/index.ts:740
updateAuthority()
Updates the wallet authority and reestablishes subscriptions.- Unsubscribes from current user accounts
- Resets user account cache
- Updates DriftClient with new wallet
- Resubscribes to all markets
- Switches to specified sub-account
- Reestablishes event listeners
src/drift/Drift/clients/AuthorityDrift/index.ts:731
Priority Fees
getTxParams()
Gets transaction parameters with dynamic priority fees.src/drift/Drift/clients/AuthorityDrift/index.ts:872
Example: Complete Trading Flow
See Also
- CentralServerDrift - API server client
- SubscriptionManager - Subscription optimization details
- Orderbook Management - Orderbook websocket manager