Skip to content

Commit

Permalink
Round fee estimates to avoid large fp issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Apr 23, 2024
1 parent 2d73bdc commit 73616ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
15 changes: 12 additions & 3 deletions src/lib/DataProviderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ export class DataProviderManager {
try {
const blockHeight = await p.getBlockHeight();
const blockHash = await p.getBlockHash();
const feeEstimates = await p.getFeeEstimates();
let feeEstimates = await p.getFeeEstimates();

// Parse and round the fee estimates to 3 decimal places
feeEstimates = Object.fromEntries(
Object.entries(feeEstimates).map(([key, value]) => [
key,
Math.round((value + Number.EPSILON) * 1000) / 1000,
])
);

return {
provider: p,
Expand Down Expand Up @@ -198,8 +206,9 @@ export class DataProviderManager {
keys.forEach((key) => {
// Only add the estimate if it has a higher confirmation target and a lower fee.
if (
key > Math.max(...Object.keys(mergedEstimates).map(Number)) &&
estimates[key] < Math.min(...Object.values(mergedEstimates))
(Object.keys(mergedEstimates).length === 0) ||
(key > Math.max(...Object.keys(mergedEstimates).map(Number)) &&
estimates[key] < Math.min(...Object.values(mergedEstimates)))
) {
log.debug({
msg: `Adding estimate from ${providerName} with target ${key} and fee ${estimates[key]} to mergedEstimates`,
Expand Down
18 changes: 11 additions & 7 deletions test/DataProviderManager-merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ class MockProvider3 implements Provider {
"1": 25,
"2": 15,
"3": 5,
"5": 3,
"6": 3,
"7": 3,
"5": 3.564999999999998,
"6": 3.564999999999998,
"7": 3.564999999999998,
"8": 3.564999999999998,
"9": 3.564999999999998,
});
getAllData = () =>
Promise.resolve({
Expand All @@ -80,9 +82,11 @@ class MockProvider3 implements Provider {
"1": 25,
"2": 15,
"3": 5,
"5": 3,
"6": 3,
"7": 3,
"5": 3.564999999999998,
"6": 3.564999999999998,
"7": 3.564999999999998,
"8": 3.564999999999998,
"9": 3.564999999999998,
},
});
}
Expand All @@ -109,6 +113,6 @@ test("should merge fee estimates from multiple providers correctly", async () =>
"1": 60000,
"2": 40000,
"3": 10000,
"5": 6000,
"5": 7130,
});
});

0 comments on commit 73616ca

Please sign in to comment.