102 lines
2.1 KiB
JavaScript
102 lines
2.1 KiB
JavaScript
|
const { PrismaClient } = require('@prisma/client')
|
||
|
const { authenticatedLndGrpc, subscribeToInvoices, getInvoice } = require('ln-service')
|
||
|
const dotenv = require('dotenv')
|
||
|
|
||
|
dotenv.config({ path: '..' })
|
||
|
|
||
|
const { lnd } = authenticatedLndGrpc({
|
||
|
cert: process.env.LND_CERT,
|
||
|
macaroon: process.env.LND_MACAROON,
|
||
|
socket: process.env.LND_SOCKET
|
||
|
})
|
||
|
|
||
|
const models = new PrismaClient()
|
||
|
|
||
|
async function recordStatus (inv) {
|
||
|
console.log(inv)
|
||
|
if (inv.is_confirmed) {
|
||
|
const received = Number(inv.received_mtokens)
|
||
|
|
||
|
// only increment iff this invoice has not yet confirmed
|
||
|
const updateUser = models.user.updateMany({
|
||
|
where: {
|
||
|
invoices: {
|
||
|
some: {
|
||
|
hash: inv.id,
|
||
|
confirmedAt: {
|
||
|
equals: null
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
data: {
|
||
|
msats: {
|
||
|
increment: received
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
// ATOMICALLY (with above) mark the invoice as confirmed
|
||
|
const updateInvoice = models.invoice.updateMany({
|
||
|
where: {
|
||
|
hash: inv.id,
|
||
|
AND: [
|
||
|
{
|
||
|
confirmedAt: {
|
||
|
equals: null
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
data: {
|
||
|
confirmedAt: inv.confirmed_at,
|
||
|
msatsReceived: received
|
||
|
}
|
||
|
})
|
||
|
|
||
|
models.$transaction([updateUser, updateInvoice])
|
||
|
} else if (inv.is_canceled) {
|
||
|
// mark as cancelled
|
||
|
models.invoice.update({
|
||
|
where: {
|
||
|
hash: inv.id
|
||
|
},
|
||
|
data: {
|
||
|
cancelled: true
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 1. subscribe to all invoices async
|
||
|
const sub = subscribeToInvoices({ lnd })
|
||
|
sub.on('invoice_updated', recordStatus)
|
||
|
|
||
|
// 2. check all pending invoices from db in lnd
|
||
|
async function checkPending () {
|
||
|
const now = new Date()
|
||
|
const active = await models.invoice.findMany({
|
||
|
where: {
|
||
|
expiresAt: {
|
||
|
gt: now
|
||
|
},
|
||
|
cancelled: false,
|
||
|
confirmedAt: {
|
||
|
equals: null
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
active.forEach(async invoice => {
|
||
|
try {
|
||
|
const inv = await getInvoice({ id: invoice.hash, lnd })
|
||
|
recordStatus(inv)
|
||
|
} catch (error) {
|
||
|
console.log(error)
|
||
|
process.exit(1)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
checkPending()
|