Skip to content

Commit

Permalink
fix: spent amount null state
Browse files Browse the repository at this point in the history
  • Loading branch information
BlairCurrey committed Jun 18, 2024
1 parent 8b4938d commit 710897d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 38 deletions.
41 changes: 15 additions & 26 deletions packages/backend/src/open_payments/payment/outgoing/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>

Expand Down Expand Up @@ -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
}
}

Expand Down
10 changes: 0 additions & 10 deletions packages/backend/src/open_payments/payment/outgoing/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
}
Expand Down Expand Up @@ -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))
)
Expand Down

0 comments on commit 710897d

Please sign in to comment.