2021-05-14 00:11:22 +00:00
|
|
|
const { PrismaClient } = require('@prisma/client')
|
|
|
|
const { authenticatedLndGrpc, subscribeToInvoices, getInvoice, getPayment } = require('ln-service')
|
|
|
|
const dotenv = require('dotenv')
|
2021-05-20 01:09:32 +00:00
|
|
|
const serialize = require('../api/resolvers/serial')
|
2021-05-14 00:11:22 +00:00
|
|
|
|
|
|
|
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) {
|
2021-05-20 01:09:32 +00:00
|
|
|
await serialize(models,
|
|
|
|
models.$executeRaw`SELECT confirm_invoice(${inv.id}, ${Number(inv.received_mtokens)})`)
|
2021-05-14 00:11:22 +00:00
|
|
|
} else if (inv.is_canceled) {
|
|
|
|
// mark as cancelled
|
2021-05-20 01:09:32 +00:00
|
|
|
await serialize(models,
|
|
|
|
models.invoice.update({
|
|
|
|
where: {
|
|
|
|
hash: inv.id
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
cancelled: true
|
|
|
|
}
|
|
|
|
}))
|
2021-05-14 00:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. subscribe to all invoices async
|
|
|
|
const sub = subscribeToInvoices({ lnd })
|
|
|
|
sub.on('invoice_updated', recordInvoiceStatus)
|
|
|
|
|
|
|
|
// 2. check all pending invoices from db in lnd
|
|
|
|
async function checkPendingInvoices () {
|
|
|
|
// invoices
|
|
|
|
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 })
|
|
|
|
await recordInvoiceStatus(inv)
|
|
|
|
} catch (error) {
|
2021-08-12 21:21:56 +00:00
|
|
|
console.log(invoice, error)
|
2021-05-14 00:11:22 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function recordWithdrawlStatus (id, wdrwl) {
|
|
|
|
console.log(wdrwl)
|
|
|
|
if (wdrwl.is_confirmed) {
|
|
|
|
// mtokens also contains the fee?
|
|
|
|
// is this true for getPayment?
|
|
|
|
const fee = Number(wdrwl.payment.fee_mtokens)
|
|
|
|
const paid = Number(wdrwl.mtokens) - fee
|
2021-05-20 01:09:32 +00:00
|
|
|
await serialize(models, models.$executeRaw`
|
|
|
|
SELECT confirm_withdrawl(${id}, ${paid}, ${fee})`)
|
2021-05-14 00:11:22 +00:00
|
|
|
} else if (wdrwl.is_failed) {
|
|
|
|
let status = 'UNKNOWN_FAILURE'
|
|
|
|
if (wdrwl.failed.is_insufficient_balance) {
|
|
|
|
status = 'INSUFFICIENT_BALANCE'
|
|
|
|
} else if (wdrwl.failed.is_invalid_payment) {
|
|
|
|
status = 'INVALID_PAYMENT'
|
|
|
|
} else if (wdrwl.failed.is_pathfinding_timeout) {
|
|
|
|
status = 'PATHFINDING_TIMEOUT'
|
|
|
|
} else if (wdrwl.failed.is_route_not_found) {
|
|
|
|
status = 'ROUTE_NOT_FOUND'
|
|
|
|
}
|
2021-05-20 01:09:32 +00:00
|
|
|
await serialize(models, models.$executeRaw`
|
|
|
|
SELECT reverse_withdrawl(${id}, ${status})`)
|
2021-05-14 00:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkPendingWithdrawls () {
|
|
|
|
// look for withdrawls that are 30 seconds old but don't have a status
|
|
|
|
const leftovers = await models.withdrawl.findMany({
|
|
|
|
where: {
|
|
|
|
createdAt: {
|
2021-05-20 01:09:32 +00:00
|
|
|
lt: new Date(new Date().setSeconds(new Date().getSeconds() - 30))
|
2021-05-14 00:11:22 +00:00
|
|
|
},
|
|
|
|
status: {
|
|
|
|
equals: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
leftovers.forEach(async withdrawl => {
|
|
|
|
try {
|
|
|
|
const wdrwl = await getPayment({ id: withdrawl.hash, lnd })
|
|
|
|
await recordWithdrawlStatus(withdrawl.id, wdrwl)
|
|
|
|
} catch (error) {
|
2021-08-12 21:21:56 +00:00
|
|
|
console.log(withdrawl, error)
|
2021-05-14 00:11:22 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// check withdrawls every 5 seconds
|
|
|
|
setTimeout(checkPendingWithdrawls, 5000)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPendingInvoices()
|
|
|
|
checkPendingWithdrawls()
|