Skip to content

Commit

Permalink
Merge pull request #788 from BoltzExchange/more-fixes
Browse files Browse the repository at this point in the history
refactor: always renegotiate overpaid chain swaps
  • Loading branch information
michael1011 authored Jan 16, 2025
2 parents 7cd3b9b + ec5933d commit 2f6c5c6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
10 changes: 8 additions & 2 deletions lib/db/repositories/StatsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ type LockedFunds = {
};

class StatsRepository {
private static readonly expirationFailureReasons = [
'invoice expired',
'swap expired',
];

private static readonly queryVolume =
// language=PostgreSQL
`
Expand Down Expand Up @@ -196,7 +201,7 @@ WITH data AS (
'swap' AS type,
CASE
WHEN status = ? THEN 'success'
WHEN status = ? THEN 'failure'
WHEN status = ? AND "failureReason" NOT IN (?) THEN 'failure'
ELSE 'timeout'
END AS status
FROM swaps
Expand Down Expand Up @@ -390,7 +395,7 @@ GROUP BY pair, type;
SwapUpdateEvent.InvoiceFailedToPay,
SwapUpdateEvent.TransactionRefunded,
],
['invoice expired', 'swap expired'],
StatsRepository.expirationFailureReasons,
referral,
referral,
minYear,
Expand All @@ -406,6 +411,7 @@ GROUP BY pair, type;
values: [
SwapUpdateEvent.TransactionClaimed,
SwapUpdateEvent.InvoiceFailedToPay,
StatsRepository.expirationFailureReasons,
SwapUpdateEvent.InvoiceSettled,
[
SwapUpdateEvent.TransactionFailed,
Expand Down
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 2f6c5c6

Please sign in to comment.