diff options
| -rw-r--r-- | routes/bitcoin/receive.js | 34 | ||||
| -rw-r--r-- | routes/invoices.js | 6 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 44 |
3 files changed, 50 insertions, 34 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 30a3852a..ccaabc06 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -56,8 +56,8 @@ zmqRawTx.on("message", async (topic, message, sequence) => { order: [["id", "DESC"]], include: { model: db.Account, - as: "account", - }, + as: "account" + } }); if (!invoice) return; @@ -136,6 +136,7 @@ setInterval(async () => { for (let i = 0; i < arr.length; i++) { const hash = arr[i]; + let account, address, user; await db.transaction(async transaction => { let p = await db.Payment.findOne({ where: { hash, confirmed: 0, received: 1 }, @@ -153,24 +154,25 @@ setInterval(async () => { transaction }); - const { user } = p; + ({ account, address, user } = p); if (p && p.account) { let total = p.amount + p.tip; p.confirmed = 1; - await p.account.save({ transaction }); - await p.account.increment({ balance: total }, { transaction }); - await p.account.decrement( - { pending: Math.min(p.account.pending, total) }, + await account.save({ transaction }); + + await account.increment({ balance: total }, { transaction }); + await account.decrement( + { pending: Math.min(account.pending, total) }, { transaction } ); - await p.account.reload({ transaction }); + await account.reload({ transaction }); await p.save({ transaction }); p = p.get({ plain: true }); - emit(user.username, "account", p.account); + emit(user.username, "account", account); emit(user.username, "payment", p); l.info("bitcoin confirmed", user.username, p.amount, p.tip); notify(user, `${total} SAT payment confirmed`); @@ -180,6 +182,20 @@ setInterval(async () => { delete queue[hash]; }); + + let c = convert[address]; + if (c) { + let { tx } = c; + user.account = account; + + delete queue[hash]; + + await sendLiquid({ + address, + user, + tx + }); + } } } catch (e) { l.error("problem processing queued bitcoin transaction", e.message); diff --git a/routes/invoices.js b/routes/invoices.js index 9a24a8c0..cdf8ca91 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -24,10 +24,10 @@ app.post( optionalAuth, ah(async (req, res, next) => { try { - let { liquidAddress, invoice, user } = req.body; + let { liquidAddress, invoice, user, tx } = req.body; let { blindkey } = invoice; - if (liquidAddress) convert[invoice.text] = liquidAddress; + if (liquidAddress) convert[invoice.text] = { address: liquidAddress, tx }; if (!user) ({ user } = req); else { @@ -39,6 +39,7 @@ app.post( } if (!user) throw new Error("user not provided"); if (!invoice.currency) invoice.currency = user.currency; + if (!invoice.rate) invoice.rate = app.get("rates")[invoice.currency]; invoice.user_id = user.id; invoice.account_id = user.account_id; @@ -73,6 +74,7 @@ app.post( addresses[invoice.unconfidential] = user.username; if (blindkey) await lq.importBlindingKey(invoice.address, blindkey); } + res.send(invoice); } catch (e) { l.error(e.message, e.stack); diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 2144470f..8778bb22 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -1,4 +1,5 @@ const handlePayment = async msg => { + let account, total, user; try { await db.transaction(async transaction => { if (!msg.settled) return; @@ -18,7 +19,7 @@ const handlePayment = async msg => { const { text: hash, currency, memo, rate, tip, user_id } = invoice; const amount = parseInt(msg.amt_paid_sat) - tip; - const account = await db.Account.findOne({ + account = await db.Account.findOne({ where: { user_id, asset: config.liquid.btcasset, @@ -32,27 +33,7 @@ const handlePayment = async msg => { transaction }); - const { user } = account; - - let address = convert[msg.payment_request]; - if (address) { - user.account = account; - - let { tx } = await liquidTx({ - address, - asset: config.liquid.btcasset, - amount, - feeRate: 1000, - replaceable: true, - user - }); - - return sendLiquid({ - address, - user, - tx - }); - } + ({ user } = account); let preimage = msg.r_preimage.toString("hex"); @@ -74,7 +55,7 @@ const handlePayment = async msg => { { transaction } ); - let total = amount + tip; + total = amount + tip; invoice.received += total; await account.increment({ balance: total }, { transaction }); @@ -97,6 +78,23 @@ const handlePayment = async msg => { payment.tip ); }); + + let c = convert[msg.payment_request]; + if (c) { + let { address, tx } = c; + user.account = account; + + try { + await sendLiquid({ + address, + user, + tx, + limit: total + }); + } catch (e) { + l.error("problem sending liquid payment"); + } + } } catch (e) { l.error("problem receiving lightning payment", e.message); } |
