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.
The unique identifier for this perp market
The on-chain address of this market account
Current status of the market (e.g., active, paused, settlement, delisted)
Type of contract: perpetual, future, or prediction
Risk tier classification: A, B, C, speculative, highly speculative, or isolated
Market name as byte array (use decodeName() to convert to string)
Automated Market Maker state containing reserves, oracle info, and funding data Base asset reserve in AMM
Quote asset reserve in AMM
Square root of k (constant product invariant)
Oracle account providing price data
Oracle source type (Pyth, Switchboard, etc.)
cumulativeFundingRateLong
Cumulative funding rate for long positions
cumulativeFundingRateShort
Cumulative funding rate for short positions
Timestamp of last funding rate update
Net base asset amount with AMM
Total long base asset amount
Total short base asset amount
Initial margin ratio required to open positions (in margin precision 10000)
Maintenance margin ratio to avoid liquidation (in margin precision 10000)
Initial margin fraction factor for size-based margin increases
IMF factor applied to unrealized PnL for margin calculations
unrealizedPnlInitialAssetWeight
Asset weight applied to positive unrealized PnL for initial margin
unrealizedPnlMaintenanceAssetWeight
Asset weight applied to positive unrealized PnL for maintenance margin
Count of users with open positions in this market
Total count of users interacting with this market
PnL pool used to settle positive user PnL
Fee paid to liquidators (in percentage precision)
Fee paid to insurance fund during liquidations
Expiry timestamp for future contracts (0 for perpetuals)
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 );