aboutsummaryrefslogtreecommitdiffstats
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
parente166ab2c743e4c66eacb51c6484711c5965dd090 (diff)
downloadcoinos-server-994dd93f6c45c590ae59a699cbf425d96518da1e.tar.gz
coinos-server-994dd93f6c45c590ae59a699cbf425d96518da1e.zip
fix lightning payments
-rw-r--r--db/models/invoices.js4
-rw-r--r--lib/utils.js6
-rw-r--r--routes/bitcoin/receive.js26
-rw-r--r--routes/bitcoin/send.js10
-rw-r--r--routes/invoices.js1
-rw-r--r--routes/lightning/receive.js21
-rw-r--r--routes/lightning/send.js14
-rw-r--r--routes/lightning/user.js22
8 files changed, 51 insertions, 53 deletions
diff --git a/db/models/invoices.js b/db/models/invoices.js
index 2a7f3013..a2485ec5 100644
--- a/db/models/invoices.js
+++ b/db/models/invoices.js
@@ -55,14 +55,14 @@ const attributes = {
comment: null,
field: "rate"
},
- asset: {
+ network: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
primaryKey: false,
autoIncrement: false,
comment: null,
- field: "asset"
+ field: "network"
},
currency: {
type: DataTypes.STRING(255),
diff --git a/lib/utils.js b/lib/utils.js
index 58bf29f9..98409efc 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -22,7 +22,7 @@ getUser = async username => {
order: [["id", "DESC"]],
where: {
user_id: user.id,
- account_id: user.account.id,
+ account_id: user.account_id,
[Op.or]: {
received: true,
amount: {
@@ -37,7 +37,7 @@ getUser = async username => {
};
getUserById = async id => {
- let user = db.User.findOne({
+ let user = await db.User.findOne({
include: [
{
model: db.Account,
@@ -57,7 +57,7 @@ getUserById = async id => {
order: [["id", "DESC"]],
where: {
user_id: user.id,
- account_id: user.account.id,
+ account_id: user.account_id,
[Op.or]: {
received: true,
amount: {
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js
index 74afb4fb..acc80b09 100644
--- a/routes/bitcoin/receive.js
+++ b/routes/bitcoin/receive.js
@@ -47,7 +47,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
const invoice = await db.Invoice.findOne({
where: {
user_id: user.id,
- network: 'BTC',
+ network: "BTC"
},
order: [["id", "DESC"]]
});
@@ -86,7 +86,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
tip,
confirmed,
address,
- network: 'BTC'
+ network: "BTC"
});
l.info("bitcoin detected", user.username, o.value);
@@ -125,16 +125,20 @@ setInterval(async () => {
}
});
- p.confirmed = 1;
- p.account.balance += p.amount + p.tip;
- p.account.pending -= Math.min(p.account.pending, p.amount + p.tip);
+ if (p) {
+ p.confirmed = 1;
+ p.account.balance += p.amount + p.tip;
+ p.account.pending -= Math.min(p.account.pending, p.amount + p.tip);
- await p.account.save();
- await p.save();
+ await p.account.save();
+ await p.save();
- let user = await getUserById(p.user_id);
- emit(user.username, "user", user);
- l.info("bitcoin confirmed", user.username, p.amount, p.tip);
- delete queue[hash];
+ let user = await getUserById(p.user_id);
+ emit(user.username, "user", user);
+ l.info("bitcoin confirmed", user.username, p.amount, p.tip);
+ delete queue[hash];
+ } else {
+ l.warn("Couldn't find payment", hash);
+ }
}
}, 1000);
diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js
index bb0cf97b..e7cba943 100644
--- a/routes/bitcoin/send.js
+++ b/routes/bitcoin/send.js
@@ -66,7 +66,7 @@ module.exports = async (req, res) => {
await db.transaction(async transaction => {
await user.save({ transaction });
- emit(user.username, "user", user);
+ user = await getUser(user.username);
const payment = await db.Payment.create(
{
@@ -85,12 +85,16 @@ module.exports = async (req, res) => {
{ transaction }
);
- l.info("sent bitcoin", user.username, total);
+ user = await getUser(user.username);
emit(user.username, "payment", payment);
+ emit(user.username, "user", user);
+
res.send(payment);
+
+ l.info("sent bitcoin", user.username, total);
});
} catch (e) {
- l.error("error sending bitcoin", e);
+ l.error("error sending bitcoin", e.message);
return res.status(500).send(e.message);
}
};
diff --git a/routes/invoices.js b/routes/invoices.js
index 651863d6..f36b497c 100644
--- a/routes/invoices.js
+++ b/routes/invoices.js
@@ -3,6 +3,7 @@ const { Op } = require("sequelize");
app.post("/invoice", auth, async (req, res) => {
const { invoice } = req.body;
invoice.user_id = req.user.id;
+
l.info("creating invoice", req.user.username, invoice.network, invoice.amount, invoice.tip, invoice.currency, invoice.rate.toFixed(2));
const exists = await db.Invoice.findOne({
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);
};