From 710897d194c4021316c4d13ae52e6415afe3222e Mon Sep 17 00:00:00 2001 From: Blair Currey <12960453+BlairCurrey@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:32:58 -0400 Subject: [PATCH] fix: spent amount null state --- .../open_payments/payment/outgoing/model.ts | 41 +++++++------------ .../payment/outgoing/routes.test.ts | 10 ----- .../payment/outgoing/service.test.ts | 4 +- 3 files changed, 17 insertions(+), 38 deletions(-) diff --git a/packages/backend/src/open_payments/payment/outgoing/model.ts b/packages/backend/src/open_payments/payment/outgoing/model.ts index 670dbea9c1..dd9cce05e6 100644 --- a/packages/backend/src/open_payments/payment/outgoing/model.ts +++ b/packages/backend/src/open_payments/payment/outgoing/model.ts @@ -66,29 +66,9 @@ export class OutgoingPayment return this.quote.receiveAmount } - private grantSpentDebitAmountValue?: bigint - public get grantSpentDebitAmount(): Amount { - return { - value: this.grantSpentDebitAmountValue || BigInt(0), - assetCode: this.asset.code, - assetScale: this.asset.scale - } - } - public set grantSpentDebitAmount(amount: Amount) { - this.grantSpentDebitAmountValue = amount.value - } + public grantSpentDebitAmount?: Amount - private grantSpentReceiveAmountValue?: bigint - public get grantSpentReceiveAmount(): Amount { - return { - value: this.grantSpentReceiveAmountValue || BigInt(0), - assetCode: this.asset.code, - assetScale: this.asset.scale - } - } - public set grantSpentReceiveAmount(amount: Amount) { - this.grantSpentReceiveAmountValue = amount.value - } + public grantSpentReceiveAmount?: Amount public metadata?: Record @@ -203,11 +183,20 @@ export class OutgoingPayment public toOpenPaymentsWithSpentAmountsType( walletAddress: WalletAddress ): OutgoingPaymentWithSpentAmounts { - return { - ...this.toOpenPaymentsType(walletAddress), - grantSpentDebitAmount: serializeAmount(this.grantSpentDebitAmount), - grantSpentReceiveAmount: serializeAmount(this.grantSpentReceiveAmount) + const outgoingPaymentWithSpentAmounts: OutgoingPaymentWithSpentAmounts = { + ...this.toOpenPaymentsType(walletAddress) + } + if (this.grantSpentReceiveAmount) { + outgoingPaymentWithSpentAmounts.grantSpentReceiveAmount = serializeAmount( + this.grantSpentReceiveAmount + ) + } + if (this.grantSpentDebitAmount) { + outgoingPaymentWithSpentAmounts.grantSpentDebitAmount = serializeAmount( + this.grantSpentDebitAmount + ) } + return outgoingPaymentWithSpentAmounts } } diff --git a/packages/backend/src/open_payments/payment/outgoing/routes.test.ts b/packages/backend/src/open_payments/payment/outgoing/routes.test.ts index 63f2798f30..85a327e69f 100644 --- a/packages/backend/src/open_payments/payment/outgoing/routes.test.ts +++ b/packages/backend/src/open_payments/payment/outgoing/routes.test.ts @@ -259,16 +259,6 @@ describe('Outgoing Payment Routes', (): void => { assetCode: walletAddress.asset.code, assetScale: walletAddress.asset.scale }, - grantSpentDebitAmount: { - value: '0', - assetCode: walletAddress.asset.code, - assetScale: walletAddress.asset.scale - }, - grantSpentReceiveAmount: { - value: '0', - assetCode: walletAddress.asset.code, - assetScale: walletAddress.asset.scale - }, failed: false, createdAt: expect.any(String), updatedAt: expect.any(String) diff --git a/packages/backend/src/open_payments/payment/outgoing/service.test.ts b/packages/backend/src/open_payments/payment/outgoing/service.test.ts index 59cdfcf85e..e5bc874f9b 100644 --- a/packages/backend/src/open_payments/payment/outgoing/service.test.ts +++ b/packages/backend/src/open_payments/payment/outgoing/service.test.ts @@ -531,7 +531,7 @@ describe('OutgoingPaymentService', (): void => { for (let i = 0; i < 3; i++) { const payment = await outgoingPaymentService.create(options) assert.ok(!isOutgoingPaymentError(payment)) - expect(payment.grantSpentDebitAmount.value).toBe( + expect(payment.grantSpentDebitAmount?.value).toBe( BigInt(debitAmount.value * BigInt(i)) ) } @@ -576,7 +576,7 @@ describe('OutgoingPaymentService', (): void => { for (let i = 0; i < 3; i++) { const payment = await outgoingPaymentService.create(options) assert.ok(!isOutgoingPaymentError(payment)) - expect(payment.grantSpentReceiveAmount.value).toBe( + expect(payment.grantSpentReceiveAmount?.value).toBe( // Must account for interledger/pay off-by-one issue (even with 0 slippage/fees) BigInt((debitAmount.value - BigInt(1)) * BigInt(i)) )