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 | |
| parent | 306357c24d3014b55ae1cd25b954a695ea2e5cc6 (diff) | |
| download | coinos-server-fae6e8b55ea12989d688eb777c002222199c169b.tar.gz coinos-server-fae6e8b55ea12989d688eb777c002222199c169b.zip | |
improved assets support
Diffstat (limited to 'routes/lightning')
| -rw-r--r-- | routes/lightning/receive.js | 32 | ||||
| -rw-r--r-- | routes/lightning/send.js | 82 | ||||
| -rw-r--r-- | routes/lightning/user.js | 2 |
3 files changed, 73 insertions, 43 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..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); diff --git a/routes/lightning/user.js b/routes/lightning/user.js index 34659a6c..36af2718 100644 --- a/routes/lightning/user.js +++ b/routes/lightning/user.js @@ -34,7 +34,7 @@ module.exports = async (req, res) => { tip: 0, confirmed: true, received: false, - asset: "LNBTC" + network : "LNBTC" }); req.url = "/lightning/send"; |
