Skip to content

Commit

Permalink
refactor: always renegotiate overpaid chain swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 15, 2025
1 parent e7d802f commit ec5933d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/swap/EthereumNursery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class EthereumNursery extends TypedEventEmitter<{

if (
this.overpaymentProtector.isUnacceptableOverpay(
swap.type,
expectedAmount,
actualAmountSat,
)
Expand Down Expand Up @@ -349,6 +350,7 @@ class EthereumNursery extends TypedEventEmitter<{

if (
this.overpaymentProtector.isUnacceptableOverpay(
swap.type,
expectedAmount,
actualAmount,
)
Expand Down
25 changes: 19 additions & 6 deletions lib/swap/OverpaymentProtector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OverPaymentConfig } from '../Config';
import Logger from '../Logger';
import { SwapType } from '../consts/Enums';

class OverpaymentProtector {
private static readonly defaultConfig = {
Expand All @@ -25,12 +26,24 @@ class OverpaymentProtector {
);
}

public isUnacceptableOverpay = (expected: number, actual: number): boolean =>
actual - expected >
Math.max(
this.overPaymentExemptAmount,
actual * this.overPaymentMaxPercentage,
);
public isUnacceptableOverpay = (
type: SwapType,
expected: number,
actual: number,
): boolean => {
// For chain swaps we renegotiate overpayments
if (type === SwapType.Chain) {
return actual > expected;
} else {
return (
actual - expected >
Math.max(
this.overPaymentExemptAmount,
actual * this.overPaymentMaxPercentage,
)
);
}
};
}

export default OverpaymentProtector;
2 changes: 2 additions & 0 deletions lib/swap/UtxoNursery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class UtxoNursery extends TypedEventEmitter<{

if (
this.overpaymentProtector.isUnacceptableOverpay(
swap.type,
swap.receivingData.expectedAmount,
outputValue,
)
Expand Down Expand Up @@ -770,6 +771,7 @@ class UtxoNursery extends TypedEventEmitter<{

if (
this.overpaymentProtector.isUnacceptableOverpay(
swap.type,
updatedSwap.expectedAmount,
outputValue,
)
Expand Down
26 changes: 23 additions & 3 deletions test/unit/swap/OverpaymentProtector.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OverPaymentConfig } from '../../../lib/Config';
import Logger from '../../../lib/Logger';
import { SwapType } from '../../../lib/consts/Enums';
import OverpaymentProtector from '../../../lib/swap/OverpaymentProtector';

describe('OverpaymentProtector', () => {
Expand Down Expand Up @@ -68,7 +69,7 @@ describe('OverpaymentProtector', () => {
new OverpaymentProtector(
Logger.disabledLogger,
config,
).isUnacceptableOverpay(expected, actual),
).isUnacceptableOverpay(SwapType.Submarine, expected, actual),
).toEqual(false);
},
);
Expand All @@ -85,7 +86,7 @@ describe('OverpaymentProtector', () => {
new OverpaymentProtector(
Logger.disabledLogger,
config,
).isUnacceptableOverpay(expected, actual),
).isUnacceptableOverpay(SwapType.Submarine, expected, actual),
).toEqual(false);
},
);
Expand All @@ -102,9 +103,28 @@ describe('OverpaymentProtector', () => {
new OverpaymentProtector(
Logger.disabledLogger,
config,
).isUnacceptableOverpay(expected, actual),
).isUnacceptableOverpay(SwapType.Submarine, expected, actual),
).toEqual(true);
},
);

test.each`
expected | actual | expectedResult
${100} | ${101} | ${true}
${100} | ${105} | ${true}
${100} | ${110} | ${true}
${100} | ${100} | ${false}
`(
'should not allow overpayment for chain swaps',
({ expected, actual, expectedResult }) => {
expect(
new OverpaymentProtector(Logger.disabledLogger).isUnacceptableOverpay(
SwapType.Chain,
expected,
actual,
),
).toEqual(expectedResult);
},
);
});
});

0 comments on commit ec5933d

Please sign in to comment.