aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning/receive.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-03-11 15:10:40 -0700
committerAdam Soltys <[email protected]>2020-03-11 15:10:40 -0700
commitc0888fab024cc9f7c99812bfabf9d7774312dbf7 (patch)
treecad95683af97432f6092a2c2bae10fbb7a588db5 /routes/lightning/receive.js
parentbf2b51f0c2ae9b8f0f330dde6411e45fc0da8a25 (diff)
downloadcoinos-server-c0888fab024cc9f7c99812bfabf9d7774312dbf7.tar.gz
coinos-server-c0888fab024cc9f7c99812bfabf9d7774312dbf7.zip
refactor
Diffstat (limited to 'routes/lightning/receive.js')
-rw-r--r--routes/lightning/receive.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
new file mode 100644
index 00000000..6befc7a3
--- /dev/null
+++ b/routes/lightning/receive.js
@@ -0,0 +1,36 @@
+module.exports = {
+ const handlePayment = async msg => {
+ if (!msg.settled) return;
+
+ let payment = await db.Payment.findOne({
+ include: { model: db.User, as: "user" },
+ where: {
+ hash: msg.payment_request
+ }
+ });
+
+ if (!payment) return;
+
+ const { user } = payment;
+
+ payment.received = true;
+ user.balance += parseInt(msg.value);
+ payment.rate = app.get("rates")[user.currency];
+ payment.confirmed = true;
+ payment.currency = user.currency;
+
+ await payment.save();
+ await user.save();
+ payments.push(msg.payment_request);
+ l.info("lightning payment received", user.username, payment.amount);
+
+ emit(user.username, "payment", payment);
+ emit(user.username, "user", user);
+ };
+
+ const invoices = lna.subscribeInvoices({});
+ invoices.on("data", handlePayment);
+
+ const invoicesb = lnb.subscribeInvoices({});
+ invoicesb.on("data", handlePayment);
+};