aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning/send.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-04-29 20:59:05 -0700
committerAdam Soltys <[email protected]>2020-04-29 21:03:33 -0700
commitfa84d5a65844602c7e97e9bf0e5a51befdebaf53 (patch)
tree3e0335eea275a5de9040eb3cc6dfc62c6e190575 /routes/lightning/send.js
parent001c0291ad6eabe5684138422adfa0e851d507a6 (diff)
downloadcoinos-server-fa84d5a65844602c7e97e9bf0e5a51befdebaf53.tar.gz
coinos-server-fa84d5a65844602c7e97e9bf0e5a51befdebaf53.zip
update account balance properly after lightning payment
Diffstat (limited to 'routes/lightning/send.js')
-rw-r--r--routes/lightning/send.js127
1 files changed, 56 insertions, 71 deletions
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);
}
};