Skip to Content
DevelopersDrift TS SDKPrecision & Types

Precision & Types

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

Margin system (high-level)

At a high level, Drift checks:

  • Initial margin: can you open/increase positions?
  • Maintenance margin: should you be liquidated?

The system uses weighted collateral and weighted liabilities. The exact weights/inputs depend on margin category (initial vs maintenance) and oracle vs oracle-twap pricing.

Precision constants

import { QUOTE_PRECISION } from "@drift-labs/sdk";
Variable QUOTE_PRECISIONReference ↗
TypeScript docs unavailable for QUOTE_PRECISION.
import { BASE_PRECISION } from "@drift-labs/sdk";
Variable BASE_PRECISIONReference ↗
TypeScript docs unavailable for BASE_PRECISION.
import { PRICE_PRECISION } from "@drift-labs/sdk";
Variable PRICE_PRECISIONReference ↗
TypeScript docs unavailable for PRICE_PRECISION.
import { MARGIN_PRECISION } from "@drift-labs/sdk";
Variable MARGIN_PRECISIONReference ↗
TypeScript docs unavailable for MARGIN_PRECISION.

Token math helpers

import { getTokenAmount } from "@drift-labs/sdk";
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 { getSignedTokenAmount } from "@drift-labs/sdk";
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
import { TEN } from "@drift-labs/sdk";
Variable TENReference ↗
TypeScript docs unavailable for TEN.

Core enums you’ll use in bots

import { OrderType } from "@drift-labs/sdk"; // OrderType.MARKET / LIMIT / TRIGGER_MARKET / TRIGGER_LIMIT / ORACLE
Enum OrderTypeReference ↗
TypeScript docs unavailable for OrderType.
import { MarketType } from "@drift-labs/sdk"; // MarketType.PERP / MarketType.SPOT
Class MarketTypeReference ↗
TypeScript docs unavailable for MarketType.
import { PositionDirection } from "@drift-labs/sdk"; // PositionDirection.LONG / PositionDirection.SHORT
Class PositionDirectionReference ↗
TypeScript docs unavailable for PositionDirection.
import { PostOnlyParams } from "@drift-labs/sdk"; // PostOnlyParams.NONE / MUST_POST_ONLY / TRY_POST_ONLY / SLIDE
Class PostOnlyParamsReference ↗
TypeScript docs unavailable for PostOnlyParams.
import { OrderTriggerCondition } from "@drift-labs/sdk";
Class OrderTriggerConditionReference ↗
TypeScript docs unavailable for OrderTriggerCondition.
Last updated on