diff options
| author | Adam Soltys <[email protected]> | 2020-04-24 22:46:45 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-04-28 22:33:02 -0700 |
| commit | fae6e8b55ea12989d688eb777c002222199c169b (patch) | |
| tree | 3efed4974408fc1bc8911aa9fb9fc657456d69ee /routes/lightning/send.js | |
| parent | 306357c24d3014b55ae1cd25b954a695ea2e5cc6 (diff) | |
| download | coinos-server-fae6e8b55ea12989d688eb777c002222199c169b.tar.gz coinos-server-fae6e8b55ea12989d688eb777c002222199c169b.zip | |
improved assets support
Diffstat (limited to 'routes/lightning/send.js')
| -rw-r--r-- | routes/lightning/send.js | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/routes/lightning/send.js b/routes/lightning/send.js index 822fe0e4..0d2025ad 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -32,66 +32,76 @@ 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 payment = await db.Payment.create( - { - amount: -total, - fee, - user_id: user.id, - hash, - rate: app.get("rates")[user.currency], - currency: user.currency, - confirmed: true, - asset: 'LNBTC', - }, - { transaction } - ); + const account = await db.Account.findOne({ + where: { + user_id: user.id, + asset: config.liquid.btcasset + } + }); - seen.push(m.payment_preimage); + const 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" + }); - l.info("sent lightning", user.username, -payment.amount); - emit(user.username, "payment", payment); - emit(user.username, "user", user); + user = await getUser(user.username); - if (payreq.payeeNodeKey === config.lnb.id) { - lna.addInvoice({ value: payreq.satoshis }, (err, invoice) => { - let payback = lnb.sendPayment(lnb.meta, {}); + seen.push(m.payment_preimage); - /* eslint-disable-next-line */ - let { payment_request } = invoice; - /* eslint-disable-next-line */ - payback.write({ payment_request }); - }); - } + l.info("sent lightning", user.username, -payment.amount); + emit(user.username, "payment", payment); - seen.push(hash); - res.send(payment); - }); + l.info("user payments", user.payments); + emit(user.username, "user", user); + + 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 }); + }); + } + + seen.push(hash); + res.send(payment); } catch (e) { l.info("error sending lightning", e); return res.status(500).send(e); |
