Skip to content

Commit

Permalink
Merge pull request #30 from LN-Zap/bitcoind-fixes
Browse files Browse the repository at this point in the history
Improve floating point fee manipulation
  • Loading branch information
mrfelton authored Feb 13, 2024
2 parents e71e018 + 1ec7517 commit c3d71e3
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ 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 {
const factor: number = Math.pow(10, decimalPlaces);
const product: number = Math.floor(a * factor) * Math.floor(b * factor);
return product / Math.pow(10, decimalPlaces);
}

// NOTE: fetch signal abortcontroller does not work on Bun.
// See https://github.com/oven-sh/bun/issues/2489
async function fetchWithTimeout(url: string, timeout: number = TIMEOUT): Promise<Response> {
Expand Down Expand Up @@ -228,18 +237,14 @@ async function fetchBitcoindFees() : Promise<FeeByBlockTarget | null> {
targets.forEach((target, i) => {
var feeRate = response[i].result?.feerate;
if (feeRate) {
console.debug(`Raw bitcoind estimate for target ${target}: ${feeRate} BTC`);

// convert the returned value to satoshis, as it's currently returned in BTC.
const satPerKB = feeRate * 100000000;

console.debug(`Converted bitcoind estimate for target ${target}: ${satPerKB} sat/vb`);
const satPerKB = multiplyFixedPoint(feeRate, 1e8, 8);
result[target] = applyFeeMultiplier(satPerKB);
} else {
console.error(`Failed to fetch fee estimate from bitcoind for confirmation target ${target}`, response[i].result?.errors);
}
});

console.debug('Fetched bitcoind fees', result);
resolve(result);
}
});
Expand Down

0 comments on commit c3d71e3

Please sign in to comment.