aboutsummaryrefslogtreecommitdiffstats
path: root/routes/lightning
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-08-03 21:38:29 -0700
committerAdam Soltys <[email protected]>2020-08-03 21:38:29 -0700
commit2bcd5e9cd2233934b6d78724a07172b187ceb0b0 (patch)
treedba0c6451c1fa962dda1c3aa848abaa36d7012ab /routes/lightning
parent1f44dadfb24913d9a6835f3b18340ede580e886a (diff)
downloadcoinos-server-2bcd5e9cd2233934b6d78724a07172b187ceb0b0.tar.gz
coinos-server-2bcd5e9cd2233934b6d78724a07172b187ceb0b0.zip
user urls
Diffstat (limited to 'routes/lightning')
-rw-r--r--routes/lightning/invoice.js2
-rw-r--r--routes/lightning/receive.js139
2 files changed, 73 insertions, 68 deletions
diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js
index 689c696a..51eff332 100644
--- a/routes/lightning/invoice.js
+++ b/routes/lightning/invoice.js
@@ -1,8 +1,6 @@
module.exports = ah(async (req, res) => {
const { amount, memo, tip } = req.body;
- l.info("adding lightning invoice", req.user.username, amount, tip);
-
try {
if (config.lna.clightning) {
const invoice = await lnb.invoice(`${amount + tip}sat` || "any", new Date(), memo, 360);
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
index 34ac1fc3..e8246ab9 100644
--- a/routes/lightning/receive.js
+++ b/routes/lightning/receive.js
@@ -1,90 +1,97 @@
const asset = "LNBTC";
-const handlePayment = async msg => {
+const handlePayment = async (msg) => {
try {
- await db.transaction(async transaction => {
- if (!msg.settled) return;
+ await db.transaction(async (transaction) => {
+ if (!msg.settled) return;
- const invoice = await db.Invoice.findOne({
- where: {
- text: msg.payment_request
- }
- });
+ const invoice = await db.Invoice.findOne({
+ where: {
+ text: msg.payment_request,
+ },
+ });
+
+ if (!invoice)
+ return l.warn(
+ "received lightning with no invoice",
+ msg.payment_request
+ );
- if (!invoice) return l.warn("received lightning with no invoice", msg.payment_request);
+ const { text: hash, currency, memo, rate, tip, user_id } = invoice;
+ const amount = parseInt(msg.amt_paid_sat) - tip;
- const { text: hash, currency, memo, rate, tip, user_id } = invoice;
- const amount = parseInt(msg.amt_paid_sat) - tip;
+ const account = await db.Account.findOne({
+ where: {
+ user_id,
+ asset: config.liquid.btcasset,
+ },
+ include: {
+ model: db.User,
+ as: "user",
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction,
+ });
- const account = await db.Account.findOne({
- where: {
- user_id,
- asset: config.liquid.btcasset
- },
- include: {
- model: db.User,
- as: 'user',
- },
- lock: transaction.LOCK.UPDATE,
- transaction
- });
+ const { user } = account;
- const { user } = account;
+ let preimage = msg.r_preimage.toString("hex");
- let preimage = msg.r_preimage.toString("hex");
+ let payment = await db.Payment.create(
+ {
+ account_id: account.id,
+ user_id,
+ hash,
+ memo,
+ amount,
+ currency,
+ preimage,
+ rate,
+ received: true,
+ confirmed: true,
+ network: "LNBTC",
+ tip,
+ },
+ { transaction }
+ );
- let payment = await db.Payment.create({
- account_id: account.id,
- user_id,
- hash,
- memo,
- amount,
- currency,
- preimage,
- rate,
- received: true,
- confirmed: true,
- network: "LNBTC",
- tip
- },
- { transaction });
+ let total = amount + tip;
+ invoice.received += total;
+ account.balance += total;
- let total = amount + tip;
- invoice.received += total;
- account.balance += total;
+ await account.save({ transaction });
+ await invoice.save({ transaction });
+ await payment.save({ transaction });
+ payments.push(msg.payment_request);
- await account.save({ transaction });
- await invoice.save({ transaction });
- await payment.save({ transaction });
- payments.push(msg.payment_request);
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
- emit(user.username, "payment", payment);
- emit(user.username, "account", payment.account);
- notify(user, `Received ${total} SAT`);
+ emit(user.username, "payment", payment);
+ emit(user.username, "account", payment.account);
+ notify(user, `Received ${total} SAT`);
- l.info(
- "lightning payment received",
- user.username,
- payment.amount,
- payment.tip
- );
- });
- } catch(e) {
+ l.info(
+ "lightning payment received",
+ user.username,
+ payment.amount,
+ payment.tip
+ );
+ });
+ } catch (e) {
l.error("problem receiving lightning payment", e.message);
- }
+ }
};
if (config.lna.clightning) {
- const poll = async ln => {
- const wait = async i => {
+ const poll = async (ln) => {
+ const wait = async (i) => {
const {
bolt11: payment_request,
pay_index,
status,
msatoshi_received,
- payment_preimage: r_preimage
+ payment_preimage: r_preimage,
} = await ln.waitanyinvoice(i);
let settled = status === "paid";
@@ -94,13 +101,13 @@ if (config.lna.clightning) {
payment_request,
settled,
amt_paid_sat,
- r_preimage
+ r_preimage,
});
wait(pay_index);
};
const { invoices } = await ln.listinvoices();
- wait(Math.max(...invoices.map(i => i.pay_index).filter(n => n)));
+ wait(Math.max(...invoices.map((i) => i.pay_index).filter((n) => n)));
};
poll(lna);