COMMON_UTILS
TheCOMMON_UTILS object provides a collection of utility functions for working with Drift markets, performing calculations, and manipulating data structures.
Import
Market Data Utilities
getIfVaultBalance
Gets the insurance fund vault balance.getIfStakingVaultApr
Gets the APR for the insurance fund staking vault.getCurrentOpenInterestForMarket
Returns the quote amount of the current open interest for a market using the current oracle price.marketIndex- The index of the marketmarketType- The type of market (PERP or SPOT)driftClient- Instance of DriftClient
number - The open interest in quote asset terms
Note: This function only works for PERP markets and will throw an error for SPOT markets.
getDepositAprForMarket
Gets the deposit APR for a spot market, returned as a percentage.marketIndex- The index of the spot marketmarketType- Must beMarketType.SPOTdriftClient- Instance of DriftClient
number - The deposit APR as a percentage
getBorrowAprForMarket
Gets the borrow APR for a spot market, returned as a percentage.marketIndex- The index of the spot marketmarketType- Must beMarketType.SPOTdriftClient- Instance of DriftClient
number - The borrow APR as a percentage
getTotalBorrowsForMarket
Gets the total borrows for a spot market in quote asset terms.number - Total borrows in quote asset, fixed to 2 decimal places
getTotalDepositsForMarket
Gets the total deposits for a spot market.totalDepositsBase- Total deposits in base assettotalDepositsQuote- Total deposits in quote asset
String Utilities
toSnakeCase
Converts a string to snake_case format.toCamelCase
Converts a string to camelCase format.normalizeBaseAssetSymbol
Normalizes base asset symbols by removing prefixes like “1M”.Math Utilities
MATH.NUM - Number Math Operations
mean
Calculates the mean (average) of an array of numbers.median
Calculates the median of an array of numbers.MATH.BN - BigNumber Math Operations
bnMax
Finds the maximum value in an array of BN (BigNumber) values.bnMin
Finds the minimum value in an array of BN values.bnMean
Calculates the mean of an array of BN values.bnMedian
Calculates the median of an array of BN values.calculateZScore
Calculates the number of standard deviations between a target value and a history of values.Data Manipulation Utilities
dividesExactly
Checks if numbers divide exactly, accounting for floating point precision issues.boolean - True if numbers divide exactly
getTieredSortScore
Helper utility for multi-level sorting. Returns a sort score based on multiple criteria.glueArray
Groups array elements into sub-arrays of a specified size.chunks
Splits an array into chunks of a specified size.Async Utilities
timedPromise
Wraps a promise and returns both the result and the execution time.promiseResult- The result of the promisepromiseTime- Execution time in milliseconds
getMultipleAccountsInfoChunked
Fetches multiple Solana accounts in chunks to avoid RPC limits.Promise<(AccountInfo<Buffer> | null)[]>
Note: Automatically chunks requests into groups of 100 (the RPC limit) and combines results.
Related
- ENUM_UTILS - Enum manipulation utilities
- Math Utilities - Additional math functions
- Circular Buffers - Data structure utilities