From 3e9ff58b4cda6be67037b0d1e35fd6176b6c2d82 Mon Sep 17 00:00:00 2001 From: fewensa Date: Wed, 14 Aug 2024 02:49:36 +0000 Subject: [PATCH] fix float to bigint --- src/ecosys/tool.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ecosys/tool.js b/src/ecosys/tool.js index f8bfc88..b26025f 100644 --- a/src/ecosys/tool.js +++ b/src/ecosys/tool.js @@ -5,10 +5,12 @@ export function absBigInt(n) { export function floatToBigInt(value, decimal) { const floatStr = value.toString(); if (!floatStr.includes('.')) { - return BigInt(value) * (10n ** decimal); + return BigInt(value) * (10n ** BigInt(decimal)); } + const decimalPlaces = floatStr.split('.')[1].length; - return BigInt(value) * (10n ** (decimal - BigInt(decimalPlaces))) + const fixedValue = BigInt(value * (10 ** decimalPlaces)); + return fixedValue * (10n ** (BigInt(decimal) - BigInt(decimalPlaces))) } export function pickIndexEndpoint(chain) {