From ab380a5797d65aeb81eb77f48dcf6b7d4eb44356 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Mon, 15 Jul 2024 17:43:56 +0200 Subject: [PATCH] Ensure that fee minimum estimate is returned when no estimates --- src/lib/DataProviderManager.ts | 5 ++ test/DataProviderManager-minfee.test.ts | 73 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 test/DataProviderManager-minfee.test.ts diff --git a/src/lib/DataProviderManager.ts b/src/lib/DataProviderManager.ts index 45b9990..7b05ddc 100644 --- a/src/lib/DataProviderManager.ts +++ b/src/lib/DataProviderManager.ts @@ -59,6 +59,11 @@ export class DataProviderManager { ); } + // If we don't have any estimates that are above the fee minimum, add a single estimate at the fee minimum. + if (Object.keys(feeEstimates).length === 0) { + feeEstimates["1"] = this.feeMinimum * 1000; + } + data = { current_block_height: blockHeight, current_block_hash: blockHash, diff --git a/test/DataProviderManager-minfee.test.ts b/test/DataProviderManager-minfee.test.ts new file mode 100644 index 0000000..fcb9416 --- /dev/null +++ b/test/DataProviderManager-minfee.test.ts @@ -0,0 +1,73 @@ +import { expect, test } from "bun:test"; +import { DataProviderManager } from "../src/lib/DataProviderManager"; + +test("should exclude estimates that are below the fee minimum", async () => { + const feeEstimates = { + "1": 3, + "2": 2, + "3": 1, + }; + class MockProvider implements Provider { + getBlockHeight = () => Promise.resolve(1001); + getBlockHash = () => Promise.resolve("hash1001"); + getFeeEstimates = () => Promise.resolve(feeEstimates); + getAllData = () => + Promise.resolve({ + blockHeight: 1001, + blockHash: "hash1001", + feeEstimates, + }); + } + + const maxHeightDelta = 1; + const feeMultiplier = 1; + const feeMinimum = 2; + const manager = new DataProviderManager( + { stdTTL: 0, checkperiod: 0 }, + maxHeightDelta, + feeMultiplier, + feeMinimum, + ); + manager.registerProvider(new MockProvider()); + + const mergedData = await manager.getData(); + expect(mergedData.fee_by_block_target).toEqual({ + "1": 3000, + "2": 2000, + }); +}); + +test("should return single estimate at fee minimum if no valid estimates are available", async () => { + const feeEstimates = { + "1": 1, + "2": 1, + "3": 1, + }; + class MockProvider implements Provider { + getBlockHeight = () => Promise.resolve(1001); + getBlockHash = () => Promise.resolve("hash1001"); + getFeeEstimates = () => Promise.resolve(feeEstimates); + getAllData = () => + Promise.resolve({ + blockHeight: 1001, + blockHash: "hash1001", + feeEstimates, + }); + } + + const maxHeightDelta = 1; + const feeMultiplier = 1; + const feeMinimum = 2; + const manager = new DataProviderManager( + { stdTTL: 0, checkperiod: 0 }, + maxHeightDelta, + feeMultiplier, + feeMinimum, + ); + manager.registerProvider(new MockProvider()); + + const mergedData = await manager.getData(); + expect(mergedData.fee_by_block_target).toEqual({ + "1": 2000, + }); +});