Skip to content

Commit

Permalink
fix: round max routing to 4 decimal places (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Jan 11, 2025
1 parent 20ad9fd commit 7dd78ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,6 @@ export const arrayToChunks = <T>(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));
12 changes: 8 additions & 4 deletions lib/rates/providers/RateProviderTaproot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getSendingChain,
hashString,
mapToObject,
roundToDecimals,
splitPairId,
} from '../../Utils';
import {
Expand Down Expand Up @@ -543,14 +544,17 @@ class RateProviderTaproot extends RateProviderBase<SwapTypes> {

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,
);
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/unit/Utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 7dd78ef

Please sign in to comment.