Skip to content

Commit

Permalink
Add listener for HODL invoice updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Jan 6, 2024
1 parent d5a5103 commit 4852fba
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion worker/wallet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import serialize from '../api/resolvers/serial.js'
import { getInvoice, getPayment, cancelHodlInvoice, subscribeToInvoices, subscribeToPayments } from 'ln-service'
import { getInvoice, getPayment, cancelHodlInvoice, subscribeToInvoices, subscribeToPayments, subscribeToInvoice } from 'ln-service'
import { sendUserNotification } from '../api/webPush/index.js'
import { msatsToSats, numWithUnits } from '../lib/format'
import { INVOICE_RETENTION_DAYS } from '../lib/constants'
Expand All @@ -20,6 +20,13 @@ async function subscribeToDeposits (args) {
// https://www.npmjs.com/package/ln-service#subscribetoinvoices
const sub = subscribeToInvoices({ lnd, confirmed_after: lastConfirmed?.confirmedIndex })
sub.on('invoice_updated', async (inv) => {
if (!inv.secret) {
// this is a HODL invoice. We need to use SubscribeToInvoice
// to get all state transition since SubscribeToInvoices is only for invoice creation and settlement.
// see https://api.lightning.community/api/lnd/invoices/subscribe-single-invoice
// vs https://api.lightning.community/api/lnd/lightning/subscribe-invoices
return subscribeToHodlInvoice({ hash: inv.id, ...args }).catch(console.error)
}
logEvent('invoice_updated', inv)
try {
await checkInvoice({ data: { hash: inv.id }, ...args })
Expand All @@ -37,6 +44,36 @@ async function subscribeToDeposits (args) {
await checkPendingDeposits(args)
}

async function subscribeToHodlInvoice (args) {
const { lnd, hash } = args
let sub
try {
await new Promise((resolve, reject) => {
// https://www.npmjs.com/package/ln-service#subscribetoinvoice
sub = subscribeToInvoice({ id: hash, lnd })
sub.on('invoice_updated', async (inv) => {
logEvent('invoice_updated', inv)
try {
await checkInvoice({ data: { hash: inv.id }, ...args })
const expired = new Date(inv.expires_at) <= new Date()
// on any of these conditions, the invoice was finalized
// and we are thus no longer interested in invoice updates
if (expired || inv.is_held || inv.is_canceled) {
return resolve()
}
} catch (error) {
logEventError('invoice_updated', error)
reject(error)
}
})
sub.on('error', reject)
})
} catch (error) {
console.error(error)
}
sub?.removeAllListeners()
}

async function subscribeToWithdrawals (args) {
const { lnd } = args

Expand Down

0 comments on commit 4852fba

Please sign in to comment.