User
TheUser class provides methods for managing individual user accounts, tracking positions, calculating margins, and analyzing account health.
Constructor
UserConfig
required
Configuration object for initializing the User
Subscription Methods
subscribe
Subscribes to user account state updates.UserAccount
Optional pre-fetched user account data
Promise<boolean>
Returns
true if subscription was successfulunsubscribe
Unsubscribes from user account updates.fetchAccounts
Forces a fetch of fresh account data from RPC.Account Data Methods
getUserAccount
Returns the current user account data.UserAccount
User account containing positions, orders, and settings
getUserAccountAndSlot
Returns user account data with the slot number.DataAndSlot<UserAccount> | undefined
User account data with slot information
exists
Checks if the user account exists on-chain.Promise<boolean>
Returns
true if the account existsPosition Methods
getPerpPosition
Returns the user’s position for a specific perpetual market.number
required
Perpetual market index
PerpPosition | undefined
Perpetual position data or undefined if no position exists
getSpotPosition
Returns the user’s position for a specific spot market.number
required
Spot market index
SpotPosition | undefined
Spot position data or undefined if no position exists
getActivePerpPositions
Returns all active perpetual positions.PerpPosition[]
Array of active perpetual positions
getActiveSpotPositions
Returns all active spot positions.SpotPosition[]
Array of active spot positions
getTokenAmount
Returns the token amount for a spot market position in the token’s native precision.number
required
Spot market index
BN
Token amount (positive for deposits, negative for borrows)
Order Methods
getOrder
Returns an order by its order ID.number
required
Order ID to retrieve
Order | undefined
Order data or undefined if not found
getOrderByUserOrderId
Returns an order by its user-defined order ID.number
required
User-defined order ID
Order | undefined
Order data or undefined if not found
getOpenOrders
Returns all open orders.Order[]
Array of open orders
Margin & Collateral Methods
getFreeCollateral
Calculates free collateral available for trading.MarginCategory
default:"Initial"
Margin category: ‘Initial’ or ‘Maintenance’
boolean
default:false
Whether to calculate for high leverage mode
number
Specific perp market index for isolated margin calculation
BN
Free collateral in USDC precision (1e6)
getTotalCollateral
Calculates total collateral including unrealized PnL.MarginCategory
default:"Initial"
Margin category: ‘Initial’ or ‘Maintenance’
boolean
default:false
Whether to use strict oracle prices
boolean
default:true
Whether to include open orders in calculation
BN
Liquidation buffer to apply
number
Specific perp market for isolated margin
BN
Total collateral in USDC precision (1e6)
getMarginRequirement
Calculates the margin requirement for the account.MarginCategory
required
Margin category: ‘Initial’ or ‘Maintenance’
BN
Additional buffer for liquidation calculations
boolean
Whether to use strict pricing
boolean
Whether to include open orders
boolean
Whether entering high leverage mode
number
Specific perp market for isolated margin
BN
Margin requirement in USDC precision (1e6)
getInitialMarginRequirement
Calculates the initial margin requirement.boolean
default:false
Whether to calculate for high leverage mode
number
Specific perp market for isolated margin
BN
Initial margin requirement in USDC precision (1e6)
getMaintenanceMarginRequirement
Calculates the maintenance margin requirement.BN
Liquidation buffer to apply
number
Specific perp market for isolated margin
BN
Maintenance margin requirement in USDC precision (1e6)
PnL & Value Methods
getUnrealizedPNL
Calculates unrealized profit and loss.boolean
Whether to include funding payments
number
Specific market index (or all markets if undefined)
MarginCategory
Apply margin category weights to PnL
boolean
Use strict oracle pricing
BN
Liquidation buffer to apply
BN
Unrealized PnL in USDC precision (1e6)
getUnrealizedFundingPNL
Calculates unrealized funding payment PnL.number
Specific market index (or all markets if undefined)
BN
Unrealized funding PnL in USDC precision (1e6)
getNetUsdValue
Calculates the net USD value of the account.BN
Net USD value in USDC precision (1e6)
getTotalAllTimePnl
Calculates all-time profit and loss.BN
All-time PnL in USDC precision (1e6)
Leverage & Risk Methods
getLeverage
Calculates current account leverage.boolean
default:true
Whether to include open orders
number
Specific perp market for isolated position
BN
Leverage with precision TEN_THOUSAND (1e4)
getMarginRatio
Calculates the margin ratio (inverse of leverage).BN
Margin ratio with precision TEN_THOUSAND (1e4)
getHealth
Calculates account health as a percentage.number
Specific perp market for isolated position health
number
Health percentage from 0-100 (0 = liquidatable, 100 = max health)
canBeLiquidated
Checks if the account can be liquidated.object
Liquidation status for cross margin and isolated positions
liquidationPrice
Calculates the liquidation price for a perpetual position.number
required
Perpetual market index
BN
default:"ZERO"
Change in position size to calculate for
BN
default:"ZERO"
Estimated entry price for the trade
MarginCategory
default:"Maintenance"
Margin category to use
boolean
default:false
Whether to include open orders
BN
default:"ZERO"
Additional collateral to add
boolean
default:false
Whether entering high leverage mode
MarginType
‘Cross’ or ‘Isolated’
BN
Liquidation price in PRICE_PRECISION (1e6), or -1 if position won’t liquidate
Properties
DriftClient
Reference to the DriftClient instance
PublicKey
Public key of the user account
boolean
Whether the user is subscribed to account updates