From 98eceb8d3ad40a9952d143c89fa3cbc5a40db668 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 12 Feb 2022 08:18:07 -0500 Subject: log payreq --- lib/sockets.js | 4 ++-- lib/sync.js | 40 +++++++++++++++++++++++++++++++--------- routes/lightning/send.js | 2 +- routes/lnurl.js | 8 ++++---- routes/send.js | 6 +++--- 5 files changed, 41 insertions(+), 19 deletions(-) diff --git a/lib/sockets.js b/lib/sockets.js index 6a7d0b6b..e876663b 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -20,8 +20,8 @@ emit = (username, type, data) => { let s = subscriptions[i]; if ( s.invoice.amount === data.amount || - s.invoice.address === data.address || - s.invoice.unconfidential === data.address + (data.address && s.invoice.address === data.address) || + (data.address && s.invoice.unconfidential === data.address) ) { s.ws.send(JSON.stringify({ type, data })); subscriptions.splice(i, 1); diff --git a/lib/sync.js b/lib/sync.js index dd1ff9aa..dda83eab 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -48,16 +48,32 @@ const init = async () => { } catch (e) { console.log(e); } -}; - -init(); -const sync = async () => { - const payments = ( + payments = ( await db.Payment.findAll({ attributes: ["hash"] }) ).map(p => p.hash); +}; + +init(); + +const sync = async () => { + const twoWeeksAgo = new Date(new Date().setDate(new Date().getDate() - 14)); + const payments = await db.Payment.findAll({ + attributes: ["hash", "confirmed", "preimage"], + where: { createdAt: { [Op.gt]: twoWeeksAgo } }, + include: [ + { + model: db.Account, + as: "account" + }, + { + model: db.User, + as: "user" + } + ] + }); try { let { invoices } = await lnp.listInvoices({ @@ -181,7 +197,10 @@ const sync = async () => { unaccounted = []; transactions.map(tx => { - if (!payments.includes(tx.txid) && !exceptions.includes(tx.txid)) { + if ( + !payments.find(p => p.hash === tx.txid) && + !exceptions.includes(tx.txid) + ) { unaccounted.push(tx); } }); @@ -270,12 +289,11 @@ const sync = async () => { }; setTimeout(sync, 5000); -setInterval(sync, 720000); +setInterval(sync, 300000); app.get( "/conversions", ah(async (req, res) => { - const transactions = [ ...(await bc.listTransactions("*", 500)), ...(await lq.listTransactions("*", 500)) @@ -294,7 +312,11 @@ app.get( Object.keys(conversions) .filter(c => conversions[c].address === addr) .filter(c => { - return invoices.find(i => i.payment_request === c && i.state === 'SETTLED') || transactions.find(tx => tx.txid === c) + return ( + invoices.find( + i => i.payment_request === c && i.state === "SETTLED" + ) || transactions.find(tx => tx.txid === c) + ); }) .filter(c => !payments.includes(c)) ); diff --git a/routes/lightning/send.js b/routes/lightning/send.js index f6bd1b36..c0a472d8 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -5,7 +5,7 @@ module.exports = ah(async (req, res) => { try { res.send(await send(amount, memo, payreq, user)); } catch (e) { - l.error("problem sending lightning payment", user.username, e.message); + l.error("problem sending lightning payment", user.username, payreq, e.message); res.status(500).send(e.message); } }); diff --git a/routes/lnurl.js b/routes/lnurl.js index 8987dab0..0466e143 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -201,7 +201,7 @@ app.post( try { const parts = callback.split("/"); const secret = parts[parts.length - 1]; - lnurlPayments[secret] = user; + lnurlPayments[secret] = user.id; if (recipients[secret]) { url = `${req.protocol}://${req.get("host")}/api/send`; @@ -324,12 +324,12 @@ lnurlServer.bindToHook( amount = Math.round(msats / 1000); const parts = req.originalUrl.split("/"); const secret = parts[parts.length - 1].split("?")[0]; - user = lnurlPayments[secret]; + let user_id = lnurlPayments[secret]; - if (user) { + if (user_id) { let account = await db.Account.findOne({ where: { - user_id: user.id, + user_id, asset: config.liquid.btcasset, pubkey: null } diff --git a/routes/send.js b/routes/send.js index f5087f37..e44d3224 100644 --- a/routes/send.js +++ b/routes/send.js @@ -66,7 +66,7 @@ module.exports = ah(async (req, res, next) => { }; if (!username) { - // throw new Error("Vouchers temporarily disabled"); + throw new Error("Vouchers temporarily disabled"); l.info("creating redeemable payment"); params.redeemcode = uuidv4(); params.hash = `${req.get("origin")}/redeem/${params.redeemcode}`; @@ -94,12 +94,12 @@ module.exports = ah(async (req, res, next) => { { transaction } ); - let text = address || payreq; params = { where: { user_id: recipient.id }, order: [["id", "DESC"]] }; - if (text) params.where.text = text; + if (address) params.where.address = address; + else if (payreq) params.where.text = payreq; let invoice = await db.Invoice.findOne(params); let a2; -- cgit v1.2.3