diff options
| author | Adam Soltys <[email protected]> | 2020-04-29 20:59:05 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-04-29 21:03:33 -0700 |
| commit | fa84d5a65844602c7e97e9bf0e5a51befdebaf53 (patch) | |
| tree | 3e0335eea275a5de9040eb3cc6dfc62c6e190575 /routes/lightning | |
| parent | 001c0291ad6eabe5684138422adfa0e851d507a6 (diff) | |
| download | coinos-server-fa84d5a65844602c7e97e9bf0e5a51befdebaf53.tar.gz coinos-server-fa84d5a65844602c7e97e9bf0e5a51befdebaf53.zip | |
update account balance properly after lightning payment
Diffstat (limited to 'routes/lightning')
| -rw-r--r-- | routes/lightning/receive.js | 3 | ||||
| -rw-r--r-- | routes/lightning/send.js | 127 |
2 files changed, 58 insertions, 72 deletions
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 403eb999..980d9e01 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -27,6 +27,7 @@ const handlePayment = async msg => { hash, amount, currency, + preimage: msg.r_preimage.toString('hex'), rate, received: true, confirmed: true, @@ -53,7 +54,7 @@ const handlePayment = async msg => { "lightning payment received", user.username, payment.amount, - payment.tip + payment.tip, ); }; diff --git a/routes/lightning/send.js b/routes/lightning/send.js index 22c7627a..d273471a 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -15,93 +15,78 @@ module.exports = async (req, res) => { try { await db.transaction(async transaction => { - let { balance } = await db.User.findOne( - { - where: { - username: user.username - } + let account = await db.Account.findOne({ + where: { + user_id: user.id, + asset: config.liquid.btcasset }, - { transaction } - ); + lock: transaction.LOCK.UPDATE, + transaction + }); - if (balance < payreq.satoshis) { - throw new Error("insufficient funds"); + if (account.balance < payreq.satoshis) { + throw new Error("Insufficient funds"); } - user.balance -= payreq.satoshis; - await user.save({ transaction }); - }); - } catch (e) { - l.warn( - "insufficient funds for lightning payment", - user.username, - user.balance - ); - return res.status(500).send("Not enough satoshis"); - } + let m = await lna.sendPaymentSync({ + payment_request: hash + }); - let m; - try { - let paymentHash = payreq.tags.find(t => t.tagName === "payment_hash").data; - m = await lna.sendPaymentSync({ - payment_request: hash - }); + if (m.payment_error) return res.status(500).send(m.payment_error); - 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); + throw new Error("Duplicate payment detected"); + } - 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; - let total = parseInt(m.payment_route.total_amt); - let fee = m.payment_route.total_fees; + account.balance -= total; + await account.save({ transaction }); - const account = await db.Account.findOne({ - where: { + let payment = await db.Payment.create({ + amount: -total, + account_id: account.id, user_id: user.id, - asset: config.liquid.btcasset - } - }); - - account.balance -= total - payreq.satoshis; - await account.save() - - let payment = await db.Payment.create({ - amount: -total, - account_id: account.id, - user_id: user.id, - hash, - rate: app.get("rates")[user.currency], - currency: user.currency, - confirmed: true, - network: "LNBTC" - }); + hash, + preimage: m.payment_preimage.toString("hex"), + rate: app.get("rates")[user.currency], + currency: user.currency, + confirmed: true, + network: "LNBTC" + }, { transaction }); - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); - seen.push(m.payment_preimage); - l.info("sent lightning", user.username, -payment.amount); + seen.push(m.payment_preimage); + l.info("sent lightning", user.username, -payment.amount); - user = await getUser(user.username); - emit(user.username, "user", user); + user = await getUser(user.username, transaction); + emit(user.username, "user", user); - if (payreq.payeeNodeKey === config.lnb.id) { - lna.addInvoice({ value: payreq.satoshis }, (err, invoice) => { - let payback = lnb.sendPayment(lnb.meta, {}); + if (payreq.payeeNodeKey === config.lnb.id) { + lna.addInvoice({ value: payreq.satoshis }, (err, invoice) => { + let payback = lnb.sendPayment(lnb.meta, {}); - /* eslint-disable-next-line */ - let { payment_request } = invoice; - /* eslint-disable-next-line */ - payback.write({ payment_request }); - }); - } + /* eslint-disable-next-line */ + let { payment_request } = invoice; + /* eslint-disable-next-line */ + payback.write({ payment_request }); + }); + } - seen.push(hash); - res.send(payment); + seen.push(hash); + res.send(payment); + }); } catch (e) { - l.info("error sending lightning", e.message); - return res.status(500).send(e); + l.error( + "problem sending lightning payment", + user.username, + user.balance, + e.message + ); + return res.status(500).send(e.message); } }; |
