2024-01-08 22:37:58 +00:00
|
|
|
import {
|
2024-11-27 13:39:05 +00:00
|
|
|
getInvoice,
|
2024-01-08 22:37:58 +00:00
|
|
|
subscribeToInvoices, subscribeToPayments, subscribeToInvoice
|
|
|
|
} from 'ln-service'
|
2024-11-27 13:39:05 +00:00
|
|
|
import { getPaymentOrNotSent } from '@/api/lnd'
|
|
|
|
import { sleep } from '@/lib/time'
|
2024-01-10 15:50:42 +00:00
|
|
|
import retry from 'async-retry'
|
2024-08-13 14:48:30 +00:00
|
|
|
import {
|
|
|
|
paidActionPaid, paidActionForwarded,
|
|
|
|
paidActionFailedForward, paidActionHeld, paidActionFailed,
|
|
|
|
paidActionForwarding,
|
|
|
|
paidActionCanceling
|
2024-11-07 15:03:54 +00:00
|
|
|
} from './paidAction'
|
2024-11-27 13:39:05 +00:00
|
|
|
import { payingActionConfirmed, payingActionFailed } from './payingAction'
|
2022-01-17 17:41:17 +00:00
|
|
|
|
2024-01-08 22:37:58 +00:00
|
|
|
export async function subscribeToWallet (args) {
|
|
|
|
await subscribeToDeposits(args)
|
|
|
|
await subscribeToWithdrawals(args)
|
|
|
|
}
|
|
|
|
|
2024-01-10 15:50:42 +00:00
|
|
|
// lnd subscriptions can fail, so they need to be retried
|
|
|
|
function subscribeForever (subscribe) {
|
|
|
|
retry(async bail => {
|
|
|
|
let sub
|
|
|
|
try {
|
|
|
|
return await new Promise((resolve, reject) => {
|
2024-04-02 19:36:00 +00:00
|
|
|
sub = subscribe(resolve, bail)
|
2024-01-10 15:50:42 +00:00
|
|
|
if (!sub) {
|
2024-01-20 22:57:52 +00:00
|
|
|
return bail(new Error('function passed to subscribeForever must return a subscription object or promise'))
|
|
|
|
}
|
|
|
|
if (sub.then) {
|
|
|
|
// sub is promise
|
2024-10-10 01:13:53 +00:00
|
|
|
sub.then(resolved => {
|
|
|
|
sub = resolved
|
|
|
|
sub.on('error', reject)
|
|
|
|
})
|
2024-01-20 22:57:52 +00:00
|
|
|
} else {
|
|
|
|
sub.on('error', reject)
|
2024-01-10 15:50:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch (error) {
|
2024-01-07 17:00:24 +00:00
|
|
|
console.error(error)
|
2024-01-10 15:50:42 +00:00
|
|
|
throw new Error('error subscribing - trying again')
|
|
|
|
} finally {
|
|
|
|
sub?.removeAllListeners()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// retry every .1-10 seconds forever
|
|
|
|
{ forever: true, minTimeout: 100, maxTimeout: 10000, onRetry: e => console.error(e.message) })
|
|
|
|
}
|
|
|
|
|
2024-01-08 22:37:58 +00:00
|
|
|
const logEvent = (name, args) => console.log(`event ${name} triggered with args`, args)
|
|
|
|
const logEventError = (name, error) => console.error(`error running ${name}`, error)
|
|
|
|
|
|
|
|
async function subscribeToDeposits (args) {
|
|
|
|
const { models, lnd } = args
|
|
|
|
|
2024-01-20 22:57:52 +00:00
|
|
|
subscribeForever(async () => {
|
|
|
|
const [lastConfirmed] = await models.$queryRaw`
|
2024-01-08 22:37:58 +00:00
|
|
|
SELECT "confirmedIndex"
|
|
|
|
FROM "Invoice"
|
|
|
|
ORDER BY "confirmedIndex" DESC NULLS LAST
|
|
|
|
LIMIT 1`
|
2024-01-10 15:50:42 +00:00
|
|
|
const sub = subscribeToInvoices({ lnd, confirmed_after: lastConfirmed?.confirmedIndex })
|
|
|
|
|
|
|
|
sub.on('invoice_updated', async (inv) => {
|
|
|
|
try {
|
2024-08-13 14:48:30 +00:00
|
|
|
logEvent('invoice_updated', inv)
|
2024-01-10 15:50:42 +00:00
|
|
|
if (inv.secret) {
|
2024-10-02 20:03:30 +00:00
|
|
|
// subscribeToInvoices only returns when added or settled
|
|
|
|
await checkInvoice({ data: { hash: inv.id, invoice: inv }, ...args })
|
2024-01-10 15:50:42 +00:00
|
|
|
} else {
|
|
|
|
// this is a HODL invoice. We need to use SubscribeToInvoice which has is_held transitions
|
2024-10-02 20:03:30 +00:00
|
|
|
// and is_canceled transitions https://api.lightning.community/api/lnd/invoices/subscribe-single-invoice
|
2024-01-10 15:50:42 +00:00
|
|
|
// SubscribeToInvoices is only for invoice creation and settlement transitions
|
|
|
|
// https://api.lightning.community/api/lnd/lightning/subscribe-invoices
|
|
|
|
subscribeToHodlInvoice({ hash: inv.id, ...args })
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logEventError('invoice_updated', error)
|
2024-01-08 22:37:58 +00:00
|
|
|
}
|
2024-01-10 15:50:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return sub
|
2024-01-08 22:37:58 +00:00
|
|
|
})
|
2022-01-17 17:41:17 +00:00
|
|
|
|
2024-07-01 17:02:29 +00:00
|
|
|
// check pending deposits as a redundancy in case we failed to rehcord
|
2024-01-08 22:37:58 +00:00
|
|
|
// an invoice_updated event
|
|
|
|
await checkPendingDeposits(args)
|
|
|
|
}
|
|
|
|
|
2024-01-10 15:50:42 +00:00
|
|
|
function subscribeToHodlInvoice (args) {
|
|
|
|
const { lnd, hash } = args
|
|
|
|
|
|
|
|
subscribeForever((resolve, reject) => {
|
|
|
|
const sub = subscribeToInvoice({ id: hash, lnd })
|
|
|
|
|
|
|
|
sub.on('invoice_updated', async (inv) => {
|
|
|
|
logEvent('hodl_invoice_updated', inv)
|
|
|
|
try {
|
2024-10-02 20:03:30 +00:00
|
|
|
await checkInvoice({ data: { hash: inv.id, invoice: inv }, ...args })
|
2024-08-13 14:48:30 +00:00
|
|
|
// after settle or confirm we can stop listening for updates
|
|
|
|
if (inv.is_confirmed || inv.is_canceled) {
|
2024-01-10 15:50:42 +00:00
|
|
|
resolve()
|
2024-01-08 22:37:58 +00:00
|
|
|
}
|
2024-01-10 15:50:42 +00:00
|
|
|
} catch (error) {
|
|
|
|
logEventError('hodl_invoice_updated', error)
|
|
|
|
reject(error)
|
|
|
|
}
|
2024-01-08 22:37:58 +00:00
|
|
|
})
|
2024-01-10 15:50:42 +00:00
|
|
|
|
|
|
|
return sub
|
|
|
|
})
|
2024-01-08 22:37:58 +00:00
|
|
|
}
|
2022-01-17 17:41:17 +00:00
|
|
|
|
2024-10-02 20:03:30 +00:00
|
|
|
// if we already have the invoice from a subscription event or previous call,
|
|
|
|
// we can skip a getInvoice call
|
|
|
|
export async function checkInvoice ({ data: { hash, invoice }, boss, models, lnd }) {
|
|
|
|
const inv = invoice ?? await getInvoice({ id: hash, lnd })
|
2023-08-31 02:48:49 +00:00
|
|
|
|
2024-01-08 22:37:58 +00:00
|
|
|
// invoice could be created by LND but wasn't inserted into the database yet
|
|
|
|
// this is expected and the function will be called again with the updates
|
2024-08-13 14:48:30 +00:00
|
|
|
const dbInv = await models.invoice.findUnique({
|
|
|
|
where: { hash },
|
|
|
|
include: {
|
|
|
|
invoiceForward: {
|
|
|
|
include: {
|
|
|
|
withdrawl: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2024-01-08 22:37:58 +00:00
|
|
|
if (!dbInv) {
|
|
|
|
console.log('invoice not found in database', hash)
|
|
|
|
return
|
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
|
2024-01-08 22:37:58 +00:00
|
|
|
if (inv.is_confirmed) {
|
2024-07-01 17:02:29 +00:00
|
|
|
if (dbInv.actionType) {
|
2024-10-02 20:03:30 +00:00
|
|
|
return await paidActionPaid({ data: { invoiceId: dbInv.id, invoice: inv }, models, lnd, boss })
|
2024-07-01 17:02:29 +00:00
|
|
|
}
|
2023-11-21 23:32:22 +00:00
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
|
2024-01-10 15:50:42 +00:00
|
|
|
if (inv.is_held) {
|
2024-07-01 17:02:29 +00:00
|
|
|
if (dbInv.actionType) {
|
2024-08-13 14:48:30 +00:00
|
|
|
if (dbInv.invoiceForward) {
|
|
|
|
if (dbInv.invoiceForward.withdrawl) {
|
|
|
|
// transitions when held are dependent on the withdrawl status
|
2024-10-02 20:03:30 +00:00
|
|
|
return await checkWithdrawal({ data: { hash: dbInv.invoiceForward.withdrawl.hash, invoice: inv }, models, lnd, boss })
|
2024-08-13 14:48:30 +00:00
|
|
|
}
|
2024-10-02 20:03:30 +00:00
|
|
|
return await paidActionForwarding({ data: { invoiceId: dbInv.id, invoice: inv }, models, lnd, boss })
|
2024-08-13 14:48:30 +00:00
|
|
|
}
|
2024-10-02 20:03:30 +00:00
|
|
|
return await paidActionHeld({ data: { invoiceId: dbInv.id, invoice: inv }, models, lnd, boss })
|
2024-07-01 17:02:29 +00:00
|
|
|
}
|
2024-01-10 15:50:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 23:32:22 +00:00
|
|
|
if (inv.is_canceled) {
|
2024-07-01 17:02:29 +00:00
|
|
|
if (dbInv.actionType) {
|
2024-10-02 20:03:30 +00:00
|
|
|
return await paidActionFailed({ data: { invoiceId: dbInv.id, invoice: inv }, models, lnd, boss })
|
2024-07-01 17:02:29 +00:00
|
|
|
}
|
2023-11-21 23:32:22 +00:00
|
|
|
}
|
2024-01-08 22:37:58 +00:00
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
|
2024-01-08 22:37:58 +00:00
|
|
|
async function subscribeToWithdrawals (args) {
|
|
|
|
const { lnd } = args
|
2023-08-31 02:48:49 +00:00
|
|
|
|
2024-01-08 22:37:58 +00:00
|
|
|
// https://www.npmjs.com/package/ln-service#subscribetopayments
|
2024-01-10 15:50:42 +00:00
|
|
|
subscribeForever(() => {
|
|
|
|
const sub = subscribeToPayments({ lnd })
|
|
|
|
|
|
|
|
sub.on('confirmed', async (payment) => {
|
|
|
|
logEvent('confirmed', payment)
|
|
|
|
try {
|
2024-10-02 20:03:30 +00:00
|
|
|
// see https://github.com/alexbosworth/lightning/blob/ddf1f214ebddf62e9e19fd32a57fbeeba713340d/lnd_methods/offchain/subscribe_to_payments.js
|
|
|
|
const withdrawal = { payment, is_confirmed: true }
|
|
|
|
await checkWithdrawal({ data: { hash: payment.id, withdrawal }, ...args })
|
2024-01-10 15:50:42 +00:00
|
|
|
} catch (error) {
|
|
|
|
logEventError('confirmed', error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
sub.on('failed', async (payment) => {
|
|
|
|
logEvent('failed', payment)
|
|
|
|
try {
|
2024-10-02 20:03:30 +00:00
|
|
|
// see https://github.com/alexbosworth/lightning/blob/ddf1f214ebddf62e9e19fd32a57fbeeba713340d/lnd_methods/offchain/subscribe_to_payments.js
|
|
|
|
const withdrawal = { failed: payment, is_failed: true }
|
|
|
|
await checkWithdrawal({ data: { hash: payment.id, withdrawal }, ...args })
|
2024-01-10 15:50:42 +00:00
|
|
|
} catch (error) {
|
|
|
|
logEventError('failed', error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return sub
|
2024-01-08 22:37:58 +00:00
|
|
|
})
|
2023-08-31 02:48:49 +00:00
|
|
|
|
2024-01-08 22:37:58 +00:00
|
|
|
// check pending withdrawals since they might have been paid while worker was down
|
|
|
|
await checkPendingWithdrawals(args)
|
2022-01-17 17:41:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 20:03:30 +00:00
|
|
|
// if we already have the payment from a subscription event or previous call,
|
|
|
|
// we can skip a getPayment call
|
|
|
|
export async function checkWithdrawal ({ data: { hash, withdrawal, invoice }, boss, models, lnd }) {
|
2024-08-19 15:10:34 +00:00
|
|
|
// get the withdrawl if pending or it's an invoiceForward
|
2024-08-13 14:48:30 +00:00
|
|
|
const dbWdrwl = await models.withdrawl.findFirst({
|
|
|
|
where: {
|
2024-08-19 15:10:34 +00:00
|
|
|
hash,
|
|
|
|
OR: [
|
|
|
|
{ status: null },
|
2024-11-27 13:39:05 +00:00
|
|
|
{ invoiceForward: { isNot: null } }
|
2024-08-19 15:10:34 +00:00
|
|
|
]
|
2024-08-13 14:48:30 +00:00
|
|
|
},
|
|
|
|
include: {
|
|
|
|
wallet: true,
|
|
|
|
invoiceForward: {
|
|
|
|
include: {
|
|
|
|
invoice: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2024-01-08 22:37:58 +00:00
|
|
|
|
2024-08-19 15:10:34 +00:00
|
|
|
// nothing to do if the withdrawl is already recorded and it isn't an invoiceForward
|
|
|
|
if (!dbWdrwl) return
|
2024-08-13 14:48:30 +00:00
|
|
|
|
2024-11-27 13:39:05 +00:00
|
|
|
const wdrwl = withdrawal ?? await getPaymentOrNotSent({ id: hash, lnd, createdAt: dbWdrwl.createdAt })
|
2024-11-08 19:26:40 +00:00
|
|
|
|
2023-11-21 23:32:22 +00:00
|
|
|
if (wdrwl?.is_confirmed) {
|
2024-11-27 13:39:05 +00:00
|
|
|
if (dbWdrwl.invoiceForward) {
|
|
|
|
return await paidActionForwarded({ data: { invoiceId: dbWdrwl.invoiceForward.invoice.id, withdrawal: wdrwl, invoice }, models, lnd, boss })
|
2024-08-13 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
2024-11-27 13:39:05 +00:00
|
|
|
return await payingActionConfirmed({ data: { withdrawalId: dbWdrwl.id, withdrawal: wdrwl }, models, lnd, boss })
|
|
|
|
} else if (wdrwl?.is_failed || wdrwl?.notSent) {
|
|
|
|
if (dbWdrwl.invoiceForward) {
|
|
|
|
return await paidActionFailedForward({ data: { invoiceId: dbWdrwl.invoiceForward.invoice.id, withdrawal: wdrwl, invoice }, models, lnd, boss })
|
2024-08-13 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
2024-11-27 13:39:05 +00:00
|
|
|
return await payingActionFailed({ data: { withdrawalId: dbWdrwl.id, withdrawal: wdrwl }, models, lnd, boss })
|
2024-02-14 23:31:25 +00:00
|
|
|
}
|
2023-11-09 17:50:43 +00:00
|
|
|
}
|
2024-01-08 22:37:58 +00:00
|
|
|
|
2024-02-16 18:27:15 +00:00
|
|
|
// The callback subscriptions above will NOT get called for JIT invoices that are already paid.
|
2024-01-08 22:37:58 +00:00
|
|
|
// So we manually cancel the HODL invoice here if it wasn't settled by user action
|
2024-08-13 14:48:30 +00:00
|
|
|
export async function finalizeHodlInvoice ({ data: { hash }, models, lnd, boss, ...args }) {
|
2024-01-08 22:37:58 +00:00
|
|
|
const inv = await getInvoice({ id: hash, lnd })
|
|
|
|
if (inv.is_confirmed) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:39:05 +00:00
|
|
|
const dbInv = await models.invoice.findUnique({ where: { hash } })
|
2024-08-13 14:48:30 +00:00
|
|
|
if (!dbInv) {
|
|
|
|
console.log('invoice not found in database', hash)
|
|
|
|
return
|
|
|
|
}
|
2024-02-01 16:05:16 +00:00
|
|
|
|
2024-11-27 13:39:05 +00:00
|
|
|
await paidActionCanceling({ data: { invoiceId: dbInv.id, invoice: inv }, models, lnd, boss })
|
2024-08-13 14:48:30 +00:00
|
|
|
|
2024-02-01 16:05:16 +00:00
|
|
|
// sync LND invoice status with invoice status in database
|
2024-11-08 19:26:40 +00:00
|
|
|
await checkInvoice({ data: { hash }, models, lnd, boss })
|
2024-01-08 22:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function checkPendingDeposits (args) {
|
|
|
|
const { models } = args
|
|
|
|
const pendingDeposits = await models.invoice.findMany({ where: { confirmedAt: null, cancelled: false } })
|
|
|
|
for (const d of pendingDeposits) {
|
|
|
|
try {
|
2024-11-12 14:50:54 +00:00
|
|
|
await checkInvoice({ ...args, data: { hash: d.hash } })
|
2024-01-08 22:37:58 +00:00
|
|
|
await sleep(10)
|
|
|
|
} catch {
|
|
|
|
console.error('error checking invoice', d.hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function checkPendingWithdrawals (args) {
|
|
|
|
const { models } = args
|
|
|
|
const pendingWithdrawals = await models.withdrawl.findMany({ where: { status: null } })
|
|
|
|
for (const w of pendingWithdrawals) {
|
|
|
|
try {
|
2024-11-12 14:50:54 +00:00
|
|
|
await checkWithdrawal({ ...args, data: { hash: w.hash } })
|
2024-01-08 22:37:58 +00:00
|
|
|
await sleep(10)
|
2024-08-13 14:48:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error('error checking withdrawal', w.hash, err)
|
2024-01-08 22:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|