aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-04-27 21:42:10 -0700
committerAdam Soltys <[email protected]>2020-04-28 22:33:02 -0700
commit994dd93f6c45c590ae59a699cbf425d96518da1e (patch)
tree89b2a980fbad1e964e91ca3750b89051ddfb32f0 /routes/lightning
parente166ab2c743e4c66eacb51c6484711c5965dd090 (diff)
downloadcoinos-server-994dd93f6c45c590ae59a699cbf425d96518da1e.tar.gz
coinos-server-994dd93f6c45c590ae59a699cbf425d96518da1e.zip
fix lightning payments
Diffstat (limited to 'routes/lightning')
-rw-r--r--routes/lightning/receive.js21
-rw-r--r--routes/lightning/send.js14
-rw-r--r--routes/lightning/user.js22
3 files changed, 23 insertions, 34 deletions
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
index 3083eca7..0805c878 100644
--- a/routes/lightning/receive.js
+++ b/routes/lightning/receive.js
@@ -14,8 +14,6 @@ const handlePayment = async msg => {
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,
@@ -32,7 +30,7 @@ const handlePayment = async msg => {
rate,
received: true,
confirmed: true,
- asset,
+ network: 'LNBTC',
tip
});
@@ -42,21 +40,20 @@ const handlePayment = async msg => {
await account.save();
await invoice.save();
await payment.save();
- await user.save();
-
payments.push(msg.payment_request);
+
+ let user = await getUserById(user_id)
+
+ emit(user.username, "payment", payment);
+ emit(user.username, "user", user);
+
l.info(
"lightning payment received",
user.username,
payment.amount,
- payment.tip
+ payment.tip,
+ msg
);
-
- user = await getUser(user.username)
- l.info(user.account.balance);
-
- emit(user.username, "payment", payment);
- emit(user.username, "user", user);
};
const invoices = lna.subscribeInvoices({});
diff --git a/routes/lightning/send.js b/routes/lightning/send.js
index 0d2025ad..02149aa8 100644
--- a/routes/lightning/send.js
+++ b/routes/lightning/send.js
@@ -57,10 +57,6 @@ module.exports = async (req, res) => {
let total = parseInt(m.payment_route.total_amt);
let fee = m.payment_route.total_fees;
- user.balance -= total - payreq.satoshis;
-
- await user.save();
-
const account = await db.Account.findOne({
where: {
user_id: user.id,
@@ -68,6 +64,9 @@ module.exports = async (req, res) => {
}
});
+ account.balance -= total - payreq.satoshis;
+ await account.save()
+
const payment = await db.Payment.create({
amount: -total,
account_id: account.id,
@@ -79,14 +78,11 @@ module.exports = async (req, res) => {
network: "LNBTC"
});
- user = await getUser(user.username);
-
seen.push(m.payment_preimage);
+ l.info("sent lightning", user.username, -payment.amount, m);
- l.info("sent lightning", user.username, -payment.amount);
+ user = await getUser(user.username);
emit(user.username, "payment", payment);
-
- l.info("user payments", user.payments);
emit(user.username, "user", user);
if (payreq.payeeNodeKey === config.lnb.id) {
diff --git a/routes/lightning/user.js b/routes/lightning/user.js
index 36af2718..41bd507f 100644
--- a/routes/lightning/user.js
+++ b/routes/lightning/user.js
@@ -9,6 +9,13 @@ module.exports = async (req, res) => {
}
});
+ const account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset: config.liquid.btcasset
+ }
+ });
+
l.info("paying user", req.user.username, payuser);
if (!user) {
@@ -27,6 +34,7 @@ module.exports = async (req, res) => {
await db.Payment.create({
user_id: user.id,
+ account_id: account.id,
hash,
amount,
rate: app.get("rates")[req.user.currency],
@@ -34,23 +42,11 @@ module.exports = async (req, res) => {
tip: 0,
confirmed: true,
received: false,
- network : "LNBTC"
+ network: "LNBTC"
});
req.url = "/lightning/send";
let payreq = bolt11.decode(hash);
- try {
- const { routes } = await lna.queryRoutes({
- pub_key: payreq.payeeNodeKey,
- amt: payreq.satoshis
- });
- if (routes.length) req.body.route = routes[0];
- else return res.status(500).send("No route available");
- } catch (e) {
- l.warn("no route available", e);
- return res.status(500).send("No route available");
- }
req.body.payreq = hash;
-
return app._router.handle(req, res);
};