From 7dd78ef5f334655d531b3b7c8e4c68f138292ff9 Mon Sep 17 00:00:00 2001 From: michael1011 Date: Sat, 11 Jan 2025 21:27:27 +0100 Subject: [PATCH] fix: round max routing to 4 decimal places (#778) --- lib/Utils.ts | 3 +++ lib/rates/providers/RateProviderTaproot.ts | 12 ++++++++---- test/unit/Utils.spec.ts | 10 ++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) 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); + }); });