aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-03-28 23:13:13 -0700
committerAdam Soltys <[email protected]>2020-03-28 23:13:13 -0700
commita0422300e2b13896634eda12930959bc64ba7dc9 (patch)
tree481915dc9e311d7dfc31d0b237b2c06ad5fa26a5 /routes/lightning
parent5142c8cfcffd59d04ec18e67183a1866324d9209 (diff)
downloadcoinos-server-a0422300e2b13896634eda12930959bc64ba7dc9.tar.gz
coinos-server-a0422300e2b13896634eda12930959bc64ba7dc9.zip
handle tips and invoices better
Diffstat (limited to 'routes/lightning')
-rw-r--r--routes/lightning/invoice.js14
-rw-r--r--routes/lightning/receive.js24
-rw-r--r--routes/lightning/send.js4
3 files changed, 23 insertions, 19 deletions
diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js
index 50be961d..f19b3484 100644
--- a/routes/lightning/invoice.js
+++ b/routes/lightning/invoice.js
@@ -1,13 +1,13 @@
module.exports = async (req, res) => {
- let err = m => res.status(500).send(m);
- let { amount, tip } = req.body;
+ const { amount, tip } = req.body;
+
+ l.info("adding lightning invoice", req.user.username, amount, tip);
- let invoice;
try {
- invoice = await lnb.addInvoice({ value: amount + tip });
+ const invoice = await lnb.addInvoice({ value: amount + tip });
+ res.send(invoice.payment_request);
} catch (e) {
- return err(e.message);
+ l.error("problem creating invoice", e.message);
+ res.status(500).send(e.message);
}
-
- res.send(invoice.payment_request);
};
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
index c4ce2414..6ac4c947 100644
--- a/routes/lightning/receive.js
+++ b/routes/lightning/receive.js
@@ -1,8 +1,9 @@
+const asset = "LNBTC";
+
const handlePayment = async msg => {
if (!msg.settled) return;
- l.info("lightning strikes!", msg.payment_request);
- let invoice = await db.Invoice.findOne({
+ const invoice = await db.Invoice.findOne({
include: { model: db.User, as: "user" },
where: {
text: msg.payment_request
@@ -11,28 +12,29 @@ const handlePayment = async msg => {
if (!invoice) return;
- const { user } = invoice;
- const amount = parseInt(msg.value);
+ const { text: hash, currency, rate, tip, user } = invoice;
+ const amount = parseInt(msg.value) - tip;
const payment = await db.Payment.create({
user_id: user.id,
- hash: invoice.text,
+ hash,
amount,
- currency: user.currency,
- rate: app.get("rates")[user.currency],
+ currency,
+ rate,
received: true,
confirmed: true,
- asset: 'LNBTC',
+ asset,
+ tip,
});
- invoice.received = msg.value;
- user.balance += amount;
+ invoice.received += amount + tip;
+ user.balance += amount + tip;
await invoice.save();
await payment.save();
await user.save();
payments.push(msg.payment_request);
- l.info("lightning payment received", user.username, payment.amount);
+ l.info("lightning payment received", user.username, payment.amount, payment.tip);
emit(user.username, "payment", payment);
emit(user.username, "user", user);
diff --git a/routes/lightning/send.js b/routes/lightning/send.js
index 38023a1d..de363a0a 100644
--- a/routes/lightning/send.js
+++ b/routes/lightning/send.js
@@ -6,6 +6,8 @@ module.exports = async (req, res) => {
let { route } = req.body;
let { user } = req;
+ l.info("attempting lightning payment", user.username, payreq.satoshis);
+
if (seen.includes(hash)) {
l.warn("attempted to pay a paid invoice", user.username);
return res.status(500).send("Invoice has been paid, can't pay again");
@@ -30,7 +32,7 @@ module.exports = async (req, res) => {
await user.save({ transaction });
});
} catch (e) {
- l.warn("insufficient funds for lightning payment", user.username);
+ l.warn("insufficient funds for lightning payment", user.username, user.balance);
return res.status(500).send("Not enough satoshis");
}