Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: always renegotiate overpaid chain swaps #788

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
},
);
});
});
Loading