From d3cac04f571435508477515873e79d54318ec97e Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Wed, 20 Dec 2023 13:53:56 +0100 Subject: [PATCH] fix(type): use number | bigint --- src/format.ts | 18 +++++++++++------- src/utils.ts | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/format.ts b/src/format.ts index a4f5b1f..790c420 100644 --- a/src/format.ts +++ b/src/format.ts @@ -1,8 +1,8 @@ import { pow10 } from "./utils"; -export const format = (x: bigint, decimals: number = 18, digits?: number) => { - decimals = Math.floor(decimals); - digits = Math.floor(digits ?? decimals); +export const format = (x: bigint, decimals: number | bigint = 18, digits?: number | bigint) => { + decimals = Math.floor(Number(decimals)); + digits = Math.floor(Number(digits ?? decimals)); if (decimals === 0) return x.toString(); @@ -23,16 +23,20 @@ export const format = (x: bigint, decimals: number = 18, digits?: number) => { return full; }; -export const toFloat = (x: bigint, decimals?: number) => { +export const toFloat = (x: bigint, decimals?: number | bigint) => { return parseFloat(format(x, decimals)); }; -export const toDecimals = (x: bigint, decimals: number, scaleDecimals: number) => { +export const toDecimals = ( + x: bigint, + decimals: number | bigint, + scaleDecimals: number | bigint, +) => { if (decimals <= scaleDecimals) { - const ratio = pow10(BigInt(Math.floor(scaleDecimals - decimals))); + const ratio = pow10(BigInt(Math.floor(Number(scaleDecimals) - Number(decimals)))); return (x + ratio / 2n) / ratio; } - return x * pow10(BigInt(Math.floor(decimals - scaleDecimals))); + return x * pow10(BigInt(Math.floor(Number(decimals) - Number(scaleDecimals)))); }; diff --git a/src/utils.ts b/src/utils.ts index c1ae9ae..4224bf2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ export type MulDiv = (x: bigint, y: bigint, scale: bigint) => bigint; -export const pow10 = (power: bigint) => { - return 10n ** power; +export const pow10 = (power: number | bigint) => { + return 10n ** BigInt(power); }; export const approxEqAbs = (x: bigint, y: bigint, tolerance: bigint = 0n) => {