import { TEN } from "./constants.js"; /** * A type that is structurally compatible with BN. */ export type BNLike = { toString(base?: number | "hex", length?: number): string; gcd(b: BNLike): BNLike; egcd(b: BNLike): { a: BNLike; b: BNLike; gcd: BNLike }; invm(b: BNLike): BNLike; }; const BN_WORD_SIZE = 26; /** * Checks if an object is a BN. */ export const isBN = (num: BNLike) => { return ( num !== null && typeof num === "object" && (num.constructor as { wordSize?: number }).wordSize === BN_WORD_SIZE && Array.isArray((num as { words?: unknown }).words) ); }; /** * Bigint-like number. */ export type BigintIsh = string | number | bigint | BNLike; /** * Parses a {@link BigintIsh} into a {@link JSBI}. * @param bigintIsh * @returns */ export function parseBigintIsh(bigintIsh: BigintIsh): bigint { return typeof bigintIsh === "bigint" ? bigintIsh : typeof bigintIsh === "string" || typeof bigintIsh === "number" ? BigInt(bigintIsh) : BigInt(bigintIsh.toString()); } const decimalMultipliersCache: Record = {}; /** * Creates the multiplier for an amount of decimals. * @param decimals * @returns */ export const makeDecimalMultiplier = (decimals: number): bigint => { const cached = decimalMultipliersCache[decimals]; if (cached) { return cached; } if (decimals <= 18) { return (decimalMultipliersCache[decimals] = BigInt(10 ** decimals)); } return (decimalMultipliersCache[decimals] = TEN ** BigInt(decimals)); };