diff --git a/Procfile b/Procfile index 8f65bb0f..5d7517d2 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1,3 @@ web: npm run start -worker: node --trace-warnings worker/index.js \ No newline at end of file +worker: node --trace-warnings worker/index.js +check: node checkInvoices.js \ No newline at end of file diff --git a/checkInvoices.js b/checkInvoices.js new file mode 100644 index 00000000..ad62e3a9 --- /dev/null +++ b/checkInvoices.js @@ -0,0 +1,58 @@ +const { PrismaClient } = require('@prisma/client') +const { authenticatedLndGrpc, getInvoice } = require('ln-service') +const dotenv = require('dotenv') +const serialize = require('./api/resolvers/serial') + +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 recordInvoiceStatus (inv) { + console.log(inv) + if (inv.is_confirmed) { + await serialize(models, + models.$executeRaw`SELECT confirm_invoice(${inv.id}, ${Number(inv.received_mtokens)})`) + } else if (inv.is_canceled) { + // mark as cancelled + await serialize(models, + models.invoice.update({ + where: { + hash: inv.id + }, + data: { + cancelled: true + } + })) + } +} + +// 2. check all pending invoices from db in lnd +async function checkPendingInvoices () { + // invoices + const active = await models.invoice.findMany({ + where: { + cancelled: false, + confirmedAt: { + equals: null + } + } + }) + + active.forEach(async invoice => { + try { + const inv = await getInvoice({ id: invoice.hash, lnd }) + await recordInvoiceStatus(inv) + } catch (error) { + console.log(invoice, error) + process.exit(1) + } + }) +} + +checkPendingInvoices() diff --git a/pages/api/lnurlp/[username]/pay.js b/pages/api/lnurlp/[username]/pay.js index 656194e3..af28ebab 100644 --- a/pages/api/lnurlp/[username]/pay.js +++ b/pages/api/lnurlp/[username]/pay.js @@ -29,7 +29,7 @@ export default async ({ query: { username, amount } }, res) => { await serialize(models, models.$queryRaw`SELECT * FROM create_invoice(${invoice.id}, ${invoice.request}, - ${expiresAt}, ${amount * 1000}, ${user.id})`) + ${expiresAt}, ${amount}, ${user.id})`) return res.status(200).json({ pr: invoice.request