Skip to content

Commit

Permalink
Fix purging of cancelled invoices (#37)
Browse files Browse the repository at this point in the history
Fix a bug in purging of settled invoices on the LND REST API backend.
This caused future pending invoice queries to fail because the internal
administration no longer aligned with the LND pending invoices and
caused the invoice watch to fail.
  • Loading branch information
mutatrum authored Feb 24, 2023
1 parent 205c762 commit ddd75c5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/backends/lnd-rest/i-invoice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface ILndInvoice {
export interface ILndInvoice extends ILndResponse {
memo: string
r_preimage: string
r_hash: string
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/backends/lnd-rest/lnd-rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 10 additions & 2 deletions src/backends/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ddd75c5

Please sign in to comment.