Skip to content

Commit

Permalink
Merge pull request #32 from LN-Zap/bitcoind-fixes
Browse files Browse the repository at this point in the history
Retain decimal precision in fee conversions
  • Loading branch information
mrfelton authored Feb 13, 2024
2 parents 9e1badc + 550e4fb commit 31377bb
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,10 @@ function getValueFromFulfilledPromise(result: PromiseSettledResult<any>) {
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.
Expand Down Expand Up @@ -238,7 +235,7 @@ async function fetchBitcoindFees() : Promise<FeeByBlockTarget | null> {
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);
Expand Down

0 comments on commit 31377bb

Please sign in to comment.