diff options
Diffstat (limited to 'routes/lightning')
| -rw-r--r-- | routes/lightning/receive.js | 32 | ||||
| -rw-r--r-- | routes/lightning/send.js | 36 |
2 files changed, 50 insertions, 18 deletions
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 6ac4c947..c300d8ea 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -4,19 +4,29 @@ const handlePayment = async msg => { if (!msg.settled) return; const invoice = await db.Invoice.findOne({ - include: { model: db.User, as: "user" }, where: { text: msg.payment_request } }); + if (!invoice) return; - const { text: hash, currency, rate, tip, user } = invoice; + const { text: hash, currency, rate, tip, user_id } = invoice; const amount = parseInt(msg.value) - tip; + let user = await getUserById(user_id); + + const account = await db.Account.findOne({ + where: { + user_id, + asset: config.liquid.btcasset + } + }); + const payment = await db.Payment.create({ - user_id: user.id, + account_id: account.id, + user_id, hash, amount, currency, @@ -24,17 +34,27 @@ const handlePayment = async msg => { received: true, confirmed: true, asset, - tip, + tip }); invoice.received += amount + tip; - user.balance += amount + tip; + account.balance += amount + tip; + await account.save(); await invoice.save(); await payment.save(); await user.save(); + payments.push(msg.payment_request); - l.info("lightning payment received", user.username, payment.amount, payment.tip); + l.info( + "lightning payment received", + user.username, + payment.amount, + payment.tip + ); + + user = await getUser(user.username) + l.info(user.account.balance); emit(user.username, "payment", payment); emit(user.username, "user", user); diff --git a/routes/lightning/send.js b/routes/lightning/send.js index 822fe0e4..c2551896 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -32,50 +32,63 @@ module.exports = async (req, res) => { await user.save({ transaction }); }); } catch (e) { - l.warn("insufficient funds for lightning payment", user.username, user.balance); + l.warn( + "insufficient funds for lightning payment", + user.username, + user.balance + ); return res.status(500).send("Not enough satoshis"); } let m; try { - let paymentHash = payreq.tags.find(t => t.tagName === 'payment_hash').data; + let paymentHash = payreq.tags.find(t => t.tagName === "payment_hash").data; m = await lna.sendPaymentSync({ - "payment_request": hash, + payment_request: hash }); - + if (m.payment_error) return res.status(500).send(m.payment_error); if (seen.includes(m.payment_preimage)) { l.warn("duplicate payment detected", m.payment_preimage); return res.status(500).send("duplicate payment detected"); - } + } let total = parseInt(m.payment_route.total_amt); let fee = m.payment_route.total_fees; user.balance -= total - payreq.satoshis; - await db.transaction(async transaction => { - await user.save({ transaction }); + await user.save(); + + const account = await db.Account.findOne({ + where: { + user_id: user.id, + asset: config.liquid.btcasset + } + }); const payment = await db.Payment.create( { amount: -total, - fee, + account_id: account.id, user_id: user.id, hash, rate: app.get("rates")[user.currency], currency: user.currency, confirmed: true, - asset: 'LNBTC', - }, - { transaction } + asset: "LNBTC" + } ); + user = await getUser(user.username); + seen.push(m.payment_preimage); l.info("sent lightning", user.username, -payment.amount); emit(user.username, "payment", payment); + + l.info("user payments", user.payments); emit(user.username, "user", user); if (payreq.payeeNodeKey === config.lnb.id) { @@ -91,7 +104,6 @@ module.exports = async (req, res) => { seen.push(hash); res.send(payment); - }); } catch (e) { l.info("error sending lightning", e); return res.status(500).send(e); |
