walletd supporting withdrawls
This commit is contained in:
parent
157488ea5d
commit
208980f302
|
@ -75,7 +75,8 @@ export default {
|
||||||
// mtokens also contains the fee
|
// mtokens also contains the fee
|
||||||
const fee = Number(e.fee_mtokens)
|
const fee = Number(e.fee_mtokens)
|
||||||
const paid = Number(e.mtokens) - fee
|
const paid = Number(e.mtokens) - fee
|
||||||
await models.$queryRaw`SELECT confirm_withdrawl(${withdrawl.id}, ${paid}, ${fee})`
|
await models.$queryRaw`
|
||||||
|
SELECT confirm_withdrawl(${withdrawl.id}, ${paid}, ${fee})`
|
||||||
})
|
})
|
||||||
|
|
||||||
// if the payment fails, we need to
|
// if the payment fails, we need to
|
||||||
|
@ -93,7 +94,8 @@ export default {
|
||||||
} else if (e.is_route_not_found) {
|
} else if (e.is_route_not_found) {
|
||||||
status = 'ROUTE_NOT_FOUND'
|
status = 'ROUTE_NOT_FOUND'
|
||||||
}
|
}
|
||||||
await models.$queryRaw`SELECT reverse_withdrawl(${withdrawl.id}, ${status})`
|
await models.$queryRaw`
|
||||||
|
SELECT reverse_withdrawl(${withdrawl.id}, ${status})`
|
||||||
})
|
})
|
||||||
|
|
||||||
return withdrawl
|
return withdrawl
|
||||||
|
|
|
@ -1,67 +0,0 @@
|
||||||
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) {
|
|
||||||
await models.$queryRaw`SELECT confirm_invoice(${inv.id}, ${Number(inv.received_mtokens)})`
|
|
||||||
} 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()
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
// in walletd
|
|
||||||
// for each payment that hasn't failed or succeeded after 30 seconds after creation
|
|
||||||
// request status from lnd and record
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
const { PrismaClient } = require('@prisma/client')
|
||||||
|
const { authenticatedLndGrpc, subscribeToInvoices, getInvoice, getPayment } = 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 recordInvoiceStatus (inv) {
|
||||||
|
console.log(inv)
|
||||||
|
if (inv.is_confirmed) {
|
||||||
|
await models.$queryRaw`SELECT confirm_invoice(${inv.id}, ${Number(inv.received_mtokens)})`
|
||||||
|
} 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', 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) {
|
||||||
|
console.log(error)
|
||||||
|
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
|
||||||
|
await models.$queryRaw`
|
||||||
|
SELECT confirm_withdrawl(${id}, ${paid}, ${fee})`
|
||||||
|
} 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'
|
||||||
|
}
|
||||||
|
await models.$queryRaw`
|
||||||
|
SELECT reverse_withdrawl(${id}, ${status})`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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: {
|
||||||
|
lt: new Date(new Date().setSeconds(new Date().getSeconds() + 30))
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
equals: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
leftovers.forEach(async withdrawl => {
|
||||||
|
try {
|
||||||
|
const wdrwl = await getPayment({ id: withdrawl.hash, lnd })
|
||||||
|
await recordWithdrawlStatus(withdrawl.id, wdrwl)
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// check withdrawls every 5 seconds
|
||||||
|
setTimeout(checkPendingWithdrawls, 5000)
|
||||||
|
}
|
||||||
|
|
||||||
|
checkPendingInvoices()
|
||||||
|
checkPendingWithdrawls()
|
Loading…
Reference in New Issue