Skip to main content
Perpetual markets allow users to trade with leverage without an expiry date. Each perp market has its own account containing market configuration, AMM state, and funding rate data.

Get Perp Market Account

Retrieve a perpetual market account by its market index:
const marketIndex = 0; // SOL-PERP
const perpMarket = driftClient.getPerpMarketAccount(marketIndex);

Force Fetch from RPC

Force a fetch from RPC before returning the account (useful for testing):
const perpMarket = await driftClient.forceGetPerpMarketAccount(marketIndex);

Get All Perp Markets

Retrieve all perpetual market accounts:
const allPerpMarkets = driftClient.getPerpMarketAccounts();

PerpMarketAccount Structure

The PerpMarketAccount type contains all data for a perpetual market.
marketIndex
number
required
The unique identifier for this perp market
pubkey
PublicKey
required
The on-chain address of this market account
status
MarketStatus
required
Current status of the market (e.g., active, paused, settlement, delisted)
contractType
ContractType
required
Type of contract: perpetual, future, or prediction
contractTier
ContractTier
required
Risk tier classification: A, B, C, speculative, highly speculative, or isolated
name
number[]
required
Market name as byte array (use decodeName() to convert to string)
amm
AMM
required
Automated Market Maker state containing reserves, oracle info, and funding data
marginRatioInitial
number
required
Initial margin ratio required to open positions (in margin precision 10000)
marginRatioMaintenance
number
required
Maintenance margin ratio to avoid liquidation (in margin precision 10000)
imfFactor
number
required
Initial margin fraction factor for size-based margin increases
unrealizedPnlImfFactor
number
required
IMF factor applied to unrealized PnL for margin calculations
unrealizedPnlInitialAssetWeight
number
required
Asset weight applied to positive unrealized PnL for initial margin
unrealizedPnlMaintenanceAssetWeight
number
required
Asset weight applied to positive unrealized PnL for maintenance margin
numberOfUsersWithBase
number
required
Count of users with open positions in this market
numberOfUsers
number
required
Total count of users interacting with this market
pnlPool
PoolBalance
required
PnL pool used to settle positive user PnL
liquidatorFee
number
required
Fee paid to liquidators (in percentage precision)
ifLiquidationFee
number
required
Fee paid to insurance fund during liquidations
expiryTs
BN
required
Expiry timestamp for future contracts (0 for perpetuals)
expiryPrice
BN
required
Settlement price for expired futures

Calculate Market Prices

Calculate bid and ask prices for a perp market:
import { calculateBidPrice, calculateAskPrice } from '@drift-labs/sdk';

const oraclePriceData = driftClient.getOraclePriceDataAndSlot(
  perpMarket.amm.oracle,
  perpMarket.amm.oracleSource
);

const bidPrice = calculateBidPrice(perpMarket, oraclePriceData.data);
const askPrice = calculateAskPrice(perpMarket, oraclePriceData.data);

console.log('Bid:', bidPrice.toString());
console.log('Ask:', askPrice.toString());

Calculate Market Margin

Calculate margin requirements for a given position size:
import { calculateMarketMarginRatio, BASE_PRECISION } from '@drift-labs/sdk';

const size = BASE_PRECISION; // 1 base unit
const marginCategory = 'Initial'; // or 'Maintenance'

const marginRatio = calculateMarketMarginRatio(
  perpMarket,
  size,
  marginCategory
);

console.log('Margin ratio:', marginRatio);

Build docs developers (and LLMs) love