Skip to content

Commit

Permalink
Fix purging of expired invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
mutatrum committed Feb 20, 2023
1 parent 205c762 commit 33c3ca8
Show file tree
Hide file tree
Showing 3 changed files with 19 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
10 changes: 8 additions & 2 deletions src/backends/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ 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 canceledInvoice = backend.invoicesToWatch[indexToRemove]
canceledInvoice.status = EInvoiceStatus.Cancelled
backend.invoiceEmitter.emit("invoice-updated", canceledInvoice);
backend.invoicesToWatch.splice(indexToRemove);
})
}
}).catch(err => console.error('Unable to fetch pending invoices', err))
}, intervalMs ?? 5000)
Expand Down

0 comments on commit 33c3ca8

Please sign in to comment.