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