diff --git a/lib/Utils.ts b/lib/Utils.ts index 42e888a6..881d5fdf 100644 --- a/lib/Utils.ts +++ b/lib/Utils.ts @@ -639,3 +639,6 @@ export const arrayToChunks = (array: T[], chunkSize: number): T[][] => { }; export const bigIntMax = (a: bigint, b: bigint) => (a > b ? a : b); + +export const roundToDecimals = (value: number, decimals: number): number => + Number(value.toFixed(decimals)); diff --git a/lib/rates/providers/RateProviderTaproot.ts b/lib/rates/providers/RateProviderTaproot.ts index b524cf92..05554240 100644 --- a/lib/rates/providers/RateProviderTaproot.ts +++ b/lib/rates/providers/RateProviderTaproot.ts @@ -6,6 +6,7 @@ import { getSendingChain, hashString, mapToObject, + roundToDecimals, splitPairId, } from '../../Utils'; import { @@ -543,14 +544,17 @@ class RateProviderTaproot extends RateProviderBase { if (maxRoutingFeeOverride !== undefined) { (result as SubmarinePairTypeTaproot).fees.maximalRoutingFee = - maxRoutingFeeOverride * 100; + roundToDecimals(maxRoutingFeeOverride * 100, 4); } else { const currency = this.currencies.get(to); (result as SubmarinePairTypeTaproot).fees.maximalRoutingFee = - (currency?.lndClient?.maxPaymentFeeRatio || - currency?.clnClient?.maxPaymentFeeRatio || - 0) * 100; + roundToDecimals( + (currency?.lndClient?.maxPaymentFeeRatio || + currency?.clnClient?.maxPaymentFeeRatio || + 0) * 100, + 4, + ); } } diff --git a/test/unit/Utils.spec.ts b/test/unit/Utils.spec.ts index 80fb9f9c..1bd8360e 100644 --- a/test/unit/Utils.spec.ts +++ b/test/unit/Utils.spec.ts @@ -456,4 +456,14 @@ describe('Utils', () => { `('should get maximal bigint', ({ a, b, expected }) => { expect(bigIntMax(a, b)).toEqual(expected); }); + + test.each` + value | decimals | expected + ${1.23456789} | ${2} | ${1.23} + ${1.23456789} | ${4} | ${1.2346} + ${0.35001} | ${4} | ${0.35} + ${0.3500999} | ${4} | ${0.3501} + `('should round to decimals', ({ value, decimals, expected }) => { + expect(utils.roundToDecimals(value, decimals)).toEqual(expected); + }); });