From 550e4fbddb70ec3c36b6a66a9e7b3a49a6cbb293 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Tue, 13 Feb 2024 13:28:31 +0000 Subject: [PATCH] Retain decimal precesion in fee conversions --- src/server.tsx | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/server.tsx b/src/server.tsx index e6de4e2..3406834 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -81,13 +81,10 @@ function getValueFromFulfilledPromise(result: PromiseSettledResult) { return result && result.status === "fulfilled" && result.value ? result.value : null; } -/** - * Helper function to multiply two fixed point numbers. - */ -function multiplyFixedPoint(a: number, b: number, decimalPlaces: number): number { +function customRound(number: number, decimalPlaces: number): number { const factor: number = Math.pow(10, decimalPlaces); - const product: number = Math.floor(a * factor) * Math.floor(b * factor); - return product / Math.pow(10, decimalPlaces); + const rounded: number = Math.round(number * factor) / factor; + return rounded; } // NOTE: fetch signal abortcontroller does not work on Bun. @@ -238,7 +235,7 @@ async function fetchBitcoindFees() : Promise { var feeRate = response[i].result?.feerate; if (feeRate) { // convert the returned value to satoshis, as it's currently returned in BTC. - const satPerKB = multiplyFixedPoint(feeRate, 1e8, 8); + const satPerKB = customRound(feeRate, 1e8); result[target] = applyFeeMultiplier(satPerKB); } else { console.error(`Failed to fetch fee estimate from bitcoind for confirmation target ${target}`, response[i].result?.errors);