diff --git a/src/backends/lnd-rest/i-invoice.ts b/src/backends/lnd-rest/i-invoice.ts index ae8bac6..54e9c59 100644 --- a/src/backends/lnd-rest/i-invoice.ts +++ b/src/backends/lnd-rest/i-invoice.ts @@ -1,4 +1,4 @@ -export interface ILndInvoice { +export interface ILndInvoice extends ILndResponse { memo: string r_preimage: string r_hash: string @@ -26,6 +26,12 @@ export interface ILndInvoice { payment_addr: string } +interface ILndResponse { + code: number + message: string + details: any[] +} + interface IFeature { name: string is_required: boolean diff --git a/src/backends/lnd-rest/lnd-rest.ts b/src/backends/lnd-rest/lnd-rest.ts index cef1935..4014fa1 100644 --- a/src/backends/lnd-rest/lnd-rest.ts +++ b/src/backends/lnd-rest/lnd-rest.ts @@ -38,6 +38,10 @@ export default class LndRest extends Backend { const options = this.getRequestOptions(EHttpVerb.GET, '/v1/invoice/' + hash) const response = await this.request(options) as ILndInvoice + // With certain LND configurations, invoices are purged immediately on expiry + // https://github.com/lightningnetwork/lnd/issues/6299 + if (response.code) return Promise.reject(new Error(response.message)) + return this.toInvoice(response) } diff --git a/src/backends/tools.ts b/src/backends/tools.ts index ffc3f49..9784601 100644 --- a/src/backends/tools.ts +++ b/src/backends/tools.ts @@ -24,11 +24,19 @@ export const watchInvoices = (backend: IBackend, intervalMs: number | null = nul if (invoice.status !== invoiceToWatch.status) { backend.invoiceEmitter.emit('invoice-updated', invoice) if (invoice.status !== EInvoiceStatus.Pending) { - const indexToRemove = backend.invoicesToWatch.findIndex(i => i.paymentHash !== invoiceToWatch.paymentHash) + const indexToRemove = backend.invoicesToWatch.findIndex(i => i.paymentHash === invoiceToWatch.paymentHash) backend.invoicesToWatch.splice(indexToRemove) } } - }).catch(err => console.error('Unable to fetch invoice', err)) + }).catch(err => { + const indexToRemove = backend.invoicesToWatch.findIndex((i) => i.paymentHash === invoiceToWatch.paymentHash); + const cancelledInvoice = backend.invoicesToWatch[indexToRemove] + if (cancelledInvoice) { + cancelledInvoice.status = EInvoiceStatus.Cancelled + backend.invoiceEmitter.emit("invoice-updated", cancelledInvoice); + backend.invoicesToWatch.splice(indexToRemove); + } + }) } }).catch(err => console.error('Unable to fetch pending invoices', err)) }, intervalMs ?? 5000)