From 73616ca9e11e65b4f5b8b3b70ac15b88e4b4996f Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Tue, 23 Apr 2024 12:58:35 +0200 Subject: [PATCH] Round fee estimates to avoid large fp issues --- src/lib/DataProviderManager.ts | 15 ++++++++++++--- test/DataProviderManager-merge.test.ts | 18 +++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/lib/DataProviderManager.ts b/src/lib/DataProviderManager.ts index a66ca83..b2533d7 100644 --- a/src/lib/DataProviderManager.ts +++ b/src/lib/DataProviderManager.ts @@ -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, @@ -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`, diff --git a/test/DataProviderManager-merge.test.ts b/test/DataProviderManager-merge.test.ts index 1454be0..0a785e3 100644 --- a/test/DataProviderManager-merge.test.ts +++ b/test/DataProviderManager-merge.test.ts @@ -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({ @@ -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, }, }); } @@ -109,6 +113,6 @@ test("should merge fee estimates from multiple providers correctly", async () => "1": 60000, "2": 40000, "3": 10000, - "5": 6000, + "5": 7130, }); });