DriftClient
TheDriftClient class is the primary interface for interacting with Drift Protocol. It provides methods for managing user accounts, executing trades, handling deposits/withdrawals, and subscribing to protocol state.
Constructor
Configuration object for initializing the DriftClient
Subscription Methods
subscribe
Subscribes to Drift Protocol state and user accounts.Returns
true if subscription was successfulunsubscribe
Unsubscribes from all accounts and cleans up resources.fetchAccounts
Forces the account subscriber to fetch fresh account data from RPC.User Account Management
initializeUserAccount
Initializes a new user account with optional referrer information.Sub-account ID to initialize
Account name (defaults to “Main Account” for sub-account 0)
Referrer information for fee sharing
Transaction parameters (compute units, priority fees)
Returns a tuple of [transaction signature, user account public key]
deleteUser
Deletes a user account and reclaims rent.Sub-account ID to delete
Transaction parameters
Transaction signature
getUser
Returns a User instance for the specified sub-account.Sub-account ID (defaults to activeSubAccountId)
Authority public key (defaults to wallet public key)
User instance for the specified account
getUserAccount
Returns the UserAccount data for the specified sub-account.Sub-account ID (defaults to activeSubAccountId)
Authority public key
User account data or undefined if not found
switchActiveUser
Switches the active sub-account.New active sub-account ID
Authority for the new active account
Market Data Methods
getPerpMarketAccount
Returns perpetual market account data.Perpetual market index
Perpetual market account data
getSpotMarketAccount
Returns spot market account data.Spot market index
Spot market account data
getOraclePriceDataAndSlot
Returns oracle price data with the slot number.Oracle account public key
Oracle source type (e.g., Pyth, Switchboard)
Oracle price data with slot number
State Methods
getStateAccount
Returns the Drift protocol state account.Drift protocol state containing global parameters
getStatePublicKey
Returns the public key of the state account.State account public key
Wallet Management
updateWallet
Updates the wallet used for signing transactions and re-subscribes to user accounts.New wallet adapter
Sub-account IDs to subscribe to
New active sub-account ID
Whether to include delegated accounts
Map of authority addresses to sub-account IDs
Returns
true if wallet update and re-subscription was successfulHelper Methods
convertToSpotPrecision
Converts an amount to the correct precision for a spot market.Spot market index
Amount to convert
Amount in spot market precision
convertToPerpPrecision
Converts an amount to perpetual market precision (1e9).Amount to convert
Amount in BASE_PRECISION (1e9)
convertToPricePrecision
Converts an amount to price precision (1e6).Amount to convert
Amount in PRICE_PRECISION (1e6)
Properties
Solana RPC connection instance
Wallet adapter for signing transactions
Anchor program instance for Drift Protocol
Current authority public key
Currently active sub-account ID
Whether the client is currently subscribed to accounts