Skip to main content
The leverage module provides functions for calculating leverage ratios, buying power, and maximum trade sizes for user accounts.

Functions

get_leverage

Calculate the current leverage ratio for a user account.
pub fn get_leverage(client: &DriftClient, user: &User) -> SdkResult<u128>
Parameters:
  • client - Reference to the DriftClient
  • user - The user account to calculate leverage for
Returns: The leverage ratio (PRICE_PRECISION / 1e6)
  • Positive value indicates normal leverage
  • Negative value indicates user has negative equity
Example:
let leverage = get_leverage(&drift_client, &user_account)?;
let leverage_ratio = leverage as f64 / 1_000_000.0;
println!("User leverage: {}x", leverage_ratio);

get_spot_asset_value

Calculate the net spot asset value for a user.
pub fn get_spot_asset_value(client: &DriftClient, user: &User) -> SdkResult<i128>
Parameters:
  • client - Reference to the DriftClient
  • user - The user account
Returns: Net spot asset value (spot assets - spot liabilities) in QUOTE_PRECISION

calculate_perp_liability_value

Calculate the liability value of a perp position.
pub fn calculate_perp_liability_value(
    base_asset_amount: i64,
    price: i64,
    is_prediction_market: bool,
) -> u64
Parameters:
  • base_asset_amount - Position size (BASE_PRECISION)
  • price - Current price (PRICE_PRECISION)
  • is_prediction_market - Whether this is a prediction market
Returns: The liability value in QUOTE_PRECISION Notes:
  • For regular markets: abs(base_asset_amount * price)
  • For prediction markets: Accounts for the bounded nature of prediction market prices

UserMargin Trait

The UserMargin trait extends DriftClient with margin calculation helpers.

max_trade_size

Calculate the maximum trade size a user can take in a given market and direction.
fn max_trade_size(
    &self,
    user: &Pubkey,
    market: MarketId,
    trade_side: PositionDirection,
) -> SdkResult<u64>
Parameters:
  • user - The user account pubkey
  • market - The market to trade
  • trade_side - Long or Short
Returns: Maximum trade size in USDC (PRICE_PRECISION) Notes:
  • Accounts for existing positions on the opposite side
  • Reduce-only trades can use the liability value of opposite positions
  • Currently only implemented for perp markets
Example:
use drift_rs::{UserMargin, MarketId, PositionDirection};

let max_long = drift_client.max_trade_size(
    &user_pubkey,
    MarketId::perp(0), // SOL-PERP
    PositionDirection::Long,
)?;

println!("Max long size: ${}", max_long as f64 / 1_000_000.0);

calculate_perp_buying_power

Calculate the buying power for a specific perp market.
fn calculate_perp_buying_power(
    &self,
    user: &User,
    market: &PerpMarket,
    oracle_price: i64,
    collateral_buffer: u64,
) -> SdkResult<u128>
Parameters:
  • user - The user account
  • market - The perp market
  • oracle_price - Current oracle price (PRICE_PRECISION)
  • collateral_buffer - Buffer to subtract from free collateral
Returns: Buying power in QUOTE_PRECISION Formula:
buying_power = (free_collateral - buffer) * MARGIN_PRECISION / margin_ratio
Notes:
  • Takes into account the user’s margin mode (standard or high leverage)
  • Uses the worse of market margin ratio or user’s custom margin ratio
  • Accounts for LP positions via collateral buffer

calculate_margin_info

Calculate detailed margin information for a user.
fn calculate_margin_info(&self, user: &User) -> SdkResult<MarginCalculation>
Parameters:
  • user - The user account
Returns: MarginCalculation struct containing:
  • margin_requirement - Required margin
  • total_collateral - Total collateral value
  • total_perp_liability_value - Perp position liability
  • total_spot_liability_value - Spot position liability
  • total_spot_asset_value - Spot asset value
Example:
let margin_info = drift_client.calculate_margin_info(&user_account)?;
let free_collateral = margin_info.get_free_collateral();

println!("Free collateral: ${}", free_collateral as f64 / 1_000_000.0);
println!("Margin requirement: ${}", margin_info.margin_requirement as f64 / 1_000_000.0);

Internal Functions

These functions are used internally but may be useful for advanced use cases.

calculate_net_asset_value

fn calculate_net_asset_value(
    total_collateral: i128,
    total_spot_liability_value: u128
) -> i128
Calculate net asset value with overflow handling.

calculate_leverage

fn calculate_leverage(
    total_liability_value: u128,
    net_asset_value: i128
) -> u128
Calculate leverage ratio from liability and net asset values. Formula:
leverage = total_liability_value / abs(net_asset_value) * PRICE_PRECISION

Build docs developers (and LLMs) love