Skip to main content
The Drift Common utilities provide essential helper functions and classes for working with numbers, logging, tokens, WebSockets, and priority fees.

Available Utilities

Math Utilities

Calculate spreads, bid/ask prices, and mark prices for orderbooks

NumLib

Number formatting and conversion utilities for UI display and blockchain precision

Logger

Winston-based logging with Slack integration and custom log levels

Token Utilities

SPL token address and account management utilities

Priority Fees

Calculate and manage Solana transaction priority fees

WebSocket

Multiplexed WebSocket connections with automatic reconnection

Common Use Cases

Number Formatting

import { NumLib } from '@drift-labs/common';

// Format notional dollar values
const formatted = NumLib.formatNum.toNotionalDisplay(1234.567);
// "$1,234.57"

// Convert to trade precision
const tradePrecision = NumLib.formatNum.toTradePrecision(0.123456789);
// 0.123457

// Format large numbers
const millified = NumLib.millify(1234567);
// { displayString: "1.23M", mantissa: 1000000, ... }

Logging

import { logger } from '@drift-labs/common';

logger.info('Processing transaction');
logger.error('Transaction failed');
logger.alert('Critical system alert'); // Sends to Slack if configured

Token Management

import { getTokenAddress, createTokenAccountIx } from '@drift-labs/common';
import { PublicKey } from '@solana/web3.js';

// Get associated token address
const tokenAddress = await getTokenAddress(
  'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC mint
  userPublicKey.toString()
);

// Create token account instruction
const createIx = await createTokenAccountIx(
  new PublicKey(userPublicKey),
  new PublicKey(mintAddress)
);

Priority Fees

import { PriorityFeeCalculator } from '@drift-labs/common';

// Calculate dynamic compute unit price
const cuPrice = PriorityFeeCalculator.calculateDynamicCUPriceToUse({
  latestFeeSample: 5000,
  boostMultiplier: 1.5,
  extraMultiplier: 1.0
});

// Convert to SOL amount
const feeInSol = PriorityFeeCalculator.getPriorityFeeInSolForComputeUnitPrice(
  cuPrice,
  600_000 // compute units
);

WebSocket Connections

import { MultiplexWebSocket } from '@drift-labs/common';

const { unsubscribe } = MultiplexWebSocket.createWebSocketSubscription({
  wsUrl: 'wss://api.example.com/ws',
  subscriptionId: 'my-subscription',
  subscribeMessage: JSON.stringify({ action: 'subscribe', channel: 'trades' }),
  unsubscribeMessage: JSON.stringify({ action: 'unsubscribe', channel: 'trades' }),
  onMessage: (message) => {
    console.log('Received:', message);
  },
  onError: (error) => {
    console.error('WebSocket error:', error);
  },
  enableHeartbeatMonitoring: true
});

// Later, unsubscribe
unsubscribe();

Import Paths

All utilities can be imported from @drift-labs/common:
import {
  NumLib,
  logger,
  getTokenAddress,
  PriorityFeeCalculator,
  MultiplexWebSocket,
  COMMON_MATH
} from '@drift-labs/common';

Build docs developers (and LLMs) love