@drift-labs/common
Version:
Common functions for Drift
107 lines (106 loc) • 4.4 kB
TypeScript
import { BN, BigNum } from '@drift-labs/sdk';
/**
* Utilities to convert numbers and BigNumbers (BN) to different formats for the UI.
*/
export declare class NumLib {
private static locale;
static setLocale: (locale: string) => void;
/**
* Converts a Big Number to its regular number representation.
*
* This won't work when the precision, or bn/precision is larger than MAX_SAFE_INTEGER .. This shouldn't happen though unless using extremely large numbers
* */
private static toRawNum;
static formatNum: {
/**
* Converts a number to a precision suitable to trade with
* @param num
* @returns
*/
toTradePrecision: (num: number) => number;
toTradePrecisionString: (num: number, toLocaleString?: boolean) => string;
/**
* Formats a notional dollar value for UI. Goes to max. 2 decimals (accurate to 1 cent)
* @param num
* @returns
*/
toNotionalDisplay: (num: number) => string;
/**
* Formats a notional dollar value. Goes to max. 2 decimals (accurate to 1 cent)
* @param num
* @returns
*/
toNotionalNum: (num: number) => number;
/**
* This function prints the base amount of an asset with a number of decimals relative to the price of the asset, because for high priced assets we care about more accuracy in the base amount. Number of decimals corresponds to accuracy to ~ 1 cent
* @param baseAmount
* @param assetPrice in dollars
* @param skipLocaleFormatting Format using toFixed rather than localeString, which can't be parsed with regular number parsing
* @returns
*/
toBaseDisplay: (baseAmount: number, _assetPrice?: number, _skipLocaleFormatting?: boolean, customSigFigs?: number) => string;
/**
* This function prints the base amount of an asset with a number of decimals relative to the price of the asset, because for high priced assets we care about more accuracy in the base amount. Number of decimals corresponds to accuracy to ~ 1 cent
* @param baseAmount
* @param assetPrice in dollars
* @param skipLocaleFormatting Format using toFixed rather than localeString, which can't be parsed with regular number parsing
* @returns
*/
toBase: (baseAmount: number, assetPrice?: number) => number;
toBaseBN: (baseAmount: number) => BN;
toQuoteBN: (quoteAmount: number) => BN;
/**
* Formats to price in locale style
* @param assetPrice
* @returns
*/
toDisplayPrice: (assetPrice: number) => string;
/**
* Formats a price
* @param assetPrice
* @returns
*/
toPrice: (assetPrice: number) => number;
/**
* Convert a number to a BN based on the required precision
* @param num
* @param precision
* @returns
*/
toRawBn: (num: number, precision: BN) => BN;
/**
* Truncates a number to a certain number of decimal places. This differs from .toFixed() in that it rounds down, whereas .toFixed() rounds to the nearest number.
* @param num
* @param decimalPlaces
* @returns
*/
toDecimalPlaces: (num: number, decimalPlaces: number, noPadding?: boolean) => string;
};
static formatBn: {
toRawNum: (bn: BN, precision: BN, fixedAccuracity?: number) => number;
fromQuote: (bn: BN) => number;
fromBase: (bn: BN) => number;
};
/**
* Outputs information and formatted string for UI based on its log10 value
* @param value
* @returns
*/
static millify: (value: number) => {
mantissa: number;
symbol: string;
sigFigs: number;
displayValue: number;
displayString: string;
};
/**
* Get the precision to use for an asset so that base asset amounts are on the same scale as USD cents
* @param assetPrice
* @returns
*/
static getDisplayPrecision: (assetPrice: BigNum) => number;
static bp: (num: number) => number;
static isInvalid: (num: number) => boolean;
static sumBigNums: (nums: BigNum[], precision: BN) => BigNum;
static averageBigNums: (nums: BigNum[], precision: BN) => BigNum;
}