Skip to content

Commit

Permalink
refactor(bg/PaymentSession): never return undefined from pay() (#702
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sidvishnoi authored Nov 7, 2024
1 parent 251847c commit 859cd85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
5 changes: 4 additions & 1 deletion src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ export class MonetizationService {

const totalSentAmount = results
.filter((e) => e.status === 'fulfilled')
.reduce((acc, curr) => acc + BigInt(curr.value?.value ?? 0), 0n);
.reduce(
(acc, curr) => acc + BigInt(curr.value?.debitAmount.value ?? 0),
0n,
);
if (totalSentAmount === 0n) {
const isNotEnoughFunds = results
.filter((e) => e.status === 'rejected')
Expand Down
55 changes: 25 additions & 30 deletions src/background/services/paymentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export class PaymentSession {
return incomingPayment;
}

async pay(amount: number) {
async pay(amount: number): Promise<OutgoingPayment> {
if (this.isDisabled) {
throw new Error('Attempted to send a payment to a disabled session.');
}
Expand All @@ -377,51 +377,46 @@ export class PaymentSession {
(error) => {
if (isKeyRevokedError(error)) {
this.events.emit('open_payments.key_revoked');
return;
}
throw error;
},
);
if (!incomingPayment) return;

let outgoingPayment: OutgoingPayment | undefined;

try {
outgoingPayment = await this.openPaymentsService.createOutgoingPayment({
walletAddress: this.sender,
incomingPaymentId: incomingPayment.id,
amount: (amount * 10 ** this.sender.assetScale).toFixed(0),
const outgoingPayment =
await this.openPaymentsService.createOutgoingPayment({
walletAddress: this.sender,
incomingPaymentId: incomingPayment.id,
amount: (amount * 10 ** this.sender.assetScale).toFixed(0),
});

this.sendMonetizationEvent({
requestId: this.requestId,
details: {
amountSent: {
currency: outgoingPayment.receiveAmount.assetCode,
value: transformBalance(
outgoingPayment.receiveAmount.value,
outgoingPayment.receiveAmount.assetScale,
),
},
incomingPayment: outgoingPayment.receiver,
paymentPointer: this.receiver.id,
},
});

return outgoingPayment;
} catch (e) {
if (isKeyRevokedError(e)) {
this.events.emit('open_payments.key_revoked');
throw e;
} else if (isTokenExpiredError(e)) {
await this.openPaymentsService.rotateToken();
return await this.pay(amount); // retry
} else {
throw e;
}
} finally {
if (outgoingPayment) {
const { receiveAmount, receiver: incomingPayment } = outgoingPayment;

this.sendMonetizationEvent({
requestId: this.requestId,
details: {
amountSent: {
currency: receiveAmount.assetCode,
value: transformBalance(
receiveAmount.value,
receiveAmount.assetScale,
),
},
incomingPayment,
paymentPointer: this.receiver.id,
},
});
}
}

return outgoingPayment?.debitAmount;
}

private setAmount(amount: bigint): void {
Expand Down

0 comments on commit 859cd85

Please sign in to comment.