Skip to Content
DevelopersDrift SDKPrecision and Types

Precision and Types

The Drift SDK uses integer big numbers (BN) for most values. Always convert using the SDK’s precision constants and helpers.

Precision Constants

Each constant is a BN representing a power of 10. Divide a raw value by its precision constant to get the human-readable number.

ConstantValueUsed For
PRICE_PRECISION1e6Oracle prices, order prices
BASE_PRECISION1e9Perp position sizes
QUOTE_PRECISION1e6USD amounts, collateral

Converting Between Raw and Human-Readable Values

BN → Human Number

import { BN, PRICE_PRECISION, BASE_PRECISION, QUOTE_PRECISION, convertToNumber } from "@drift-labs/sdk"; // Oracle price: raw 150_500_000 → 150.5 USD const rawPrice = new BN(150_500_000); const price = convertToNumber(rawPrice, PRICE_PRECISION); console.log(price); // 150.5 // Position size: raw 2_500_000_000 → 2.5 SOL const rawBase = new BN(2_500_000_000); const size = convertToNumber(rawBase, BASE_PRECISION); console.log(size); // 2.5 // Collateral: raw 10_000_000 → 10 USDC const rawQuote = new BN(10_000_000); const usd = convertToNumber(rawQuote, QUOTE_PRECISION); console.log(usd); // 10
Function convertToNumberReference ↗
Parameters:
NameTypeDefault
bigNumberany
precisionany
Returns:
number

Human Number → BN

import { BN, BASE_PRECISION, PRICE_PRECISION } from "@drift-labs/sdk"; // 1 SOL in base precision const oneSol = new BN(1).mul(BASE_PRECISION); // BN(1_000_000_000) // $21.23 in price precision const price = new BN(21_230_000); // 21.23 * 1e6 // Or use DriftClient convenience helpers (available after setup): const size = driftClient.convertToPerpPrecision(1); // 1 base unit as BN const px = driftClient.convertToPricePrecision(21.23); // price as BN const spot = driftClient.convertToSpotPrecision(0, 100); // 100 USDC as BN

Token Math Helpers

import { getTokenAmount, SpotBalanceType, convertToNumber } from "@drift-labs/sdk"; // getTokenAmount converts a raw scaled balance into a token amount, // accounting for cumulative interest. // Params: // balanceAmount: BN - the raw balance from the user's spot position // spotMarket: SpotMarketAccount - the spot market account (has interest data) // balanceType: SpotBalanceType - DEPOSIT or BORROW const spotMarket = driftClient.getSpotMarketAccount(0); // e.g. USDC const user = driftClient.getUser(); const spotPosition = user.getUserAccount().spotPositions[0]; const tokenAmount = getTokenAmount( spotPosition.scaledBalance, spotMarket, spotPosition.balanceType ); console.log("Token amount:", tokenAmount.toString());
Function getTokenAmountReference ↗
Parameters:
NameTypeDefault
balanceAmountany

The balance amount, typically from SpotPosition.scaledBalance

spotMarketSpotMarketAccount

The spot market account details

balanceTypeSpotBalanceType

The balance type to be used for calculation

Returns:
BN

The calculated token amount, scaled by SpotMarketConfig.precision

import { getTokenAmount, getSignedTokenAmount } from "@drift-labs/sdk"; // getSignedTokenAmount returns a positive BN for deposits, negative for borrows. // Params: // tokenAmount: BN - from getTokenAmount() // balanceType: SpotBalanceType - DEPOSIT or BORROW const spotMarket = driftClient.getSpotMarketAccount(0); const spotPosition = driftClient.getUser().getUserAccount().spotPositions[0]; const tokenAmount = getTokenAmount( spotPosition.scaledBalance, spotMarket, spotPosition.balanceType ); const signed = getSignedTokenAmount(tokenAmount, spotPosition.balanceType); // signed > 0 means deposit, signed < 0 means borrow console.log("Signed amount:", signed.toString());
Function getSignedTokenAmountReference ↗
Parameters:
NameTypeDefault
tokenAmountany

The token amount to convert (from getTokenAmount)

balanceTypeSpotBalanceType

The balance type to determine the sign of the token amount.

Returns:
BN
  • The signed token amount, scaled by SpotMarketConfig.precision
Last updated on