Skip to content

Commit

Permalink
feat: Add MAX_PAYMENTS_PER_INVOICE
Browse files Browse the repository at this point in the history
  • Loading branch information
anarkrypto committed Sep 15, 2024
1 parent 4796ad6 commit 2d32a6b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export const INVOICE_MIN_AMOUNT = 0.00001;
export const WEBHOOK_DELIVERY_TIMEOUT = 15000; // 15 seconds
export const INVOICE_EXPIRATION = 1000 * 60 * 10; // 10 minutes
export const MAX_WEBSOCKET_SESSIONS_PER_PAYMENT_NOTIFIER = 10;
export const MAX_PAYMENTS_PER_INVOICE = 10;
8 changes: 7 additions & 1 deletion src/durable/payment-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NanoWebsocket, { SendEvent } from '../nano/websocket';
import { Invoice, MessageBody, Payment, Service, Webhook } from '../types';
import { rawToNano } from '../utils';
import { logger } from '../logger';
import { INVOICE_MIN_AMOUNT } from '../constants';
import { INVOICE_MIN_AMOUNT, MAX_PAYMENTS_PER_INVOICE } from '../constants';
import { PaymentNotifier } from './payment-notifier';

export class PaymentListener extends DurableObject<Env> {
Expand Down Expand Up @@ -116,6 +116,12 @@ export class PaymentListener extends DurableObject<Env> {
if (paid_total >= invoice.price) {
await this.removePendingInvoice(invoice.id, invoice.pay_address);
await this.paymentReceiver(payments, invoice);
} else if (payments.length >= MAX_PAYMENTS_PER_INVOICE) {
await this.removePendingInvoice(invoice.id, invoice.pay_address);
logger.warn(`Max payments reached for invoice: ${invoice.id}`, {
invoice,
payments,
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/durable/payment-notifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DurableObject } from 'cloudflare:workers';
import { logger } from '../logger';
import { MAX_WEBSOCKET_SESSIONS_PER_PAYMENT_NOTIFIER } from '../constants';
import { MAX_PAYMENTS_PER_INVOICE, MAX_WEBSOCKET_SESSIONS_PER_PAYMENT_NOTIFIER } from '../constants';

export type PaymentNotification = {
from: string;
Expand Down Expand Up @@ -84,7 +84,7 @@ export class PaymentNotifier extends DurableObject<Env> {

// Load the last 10 payments from the history stored on disk, and send them to the
// client.
const payments = await this.storage.list<Record<any, any>>({ reverse: true, limit: 10, prefix: 'payment_' });
const payments = await this.storage.list<Record<any, any>>({ reverse: true, limit: MAX_PAYMENTS_PER_INVOICE, prefix: 'payment_' });
[...payments.values()].forEach((value) => {
webSocket.send(JSON.stringify(value));
});
Expand Down

0 comments on commit 2d32a6b

Please sign in to comment.