From 1fbae57bb9540b1c116efc367809ac01ae2e9df2 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 3 Aug 2021 04:47:20 -0400 Subject: move lightning invoice fetch out of db transaction --- routes/lightning/receive.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'routes/lightning/receive.js') diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 55193d2d..492320d3 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -1,20 +1,18 @@ const handlePayment = async msg => { + if (!msg.settled) return; let account, total, user; - try { - 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); + + try { + await db.transaction(async transaction => { const { text: hash, currency, memo, rate, tip, user_id } = invoice; const amount = parseInt(msg.amt_paid_sat) - tip; @@ -83,7 +81,12 @@ const handlePayment = async msg => { let c = convert[msg.payment_request]; if (msg.payment_request && c) { - l.info("lightning detected for conversion request", msg.payment_request, c.address, user.username); + l.info( + "lightning detected for conversion request", + msg.payment_request, + c.address, + user.username + ); user.account = account; -- cgit v1.2.3 From 5858323cf0bb920c9f967239b7a1ef7915cf0824 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 4 Aug 2021 17:08:25 -0700 Subject: don't wait for webhook to complete --- routes/assets.js | 11 ++++++++++- routes/bitcoin/receive.js | 4 ++-- routes/lightning/receive.js | 2 +- routes/liquid/receive.js | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) (limited to 'routes/lightning/receive.js') diff --git a/routes/assets.js b/routes/assets.js index fe667dd9..939eb76c 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -111,7 +111,15 @@ app.post( if (filename) contract.filename = filename; - l.info("attempting issuance", req.user.username, contract, "address", address, "pubkey", pubkey); + l.info( + "attempting issuance", + req.user.username, + contract, + "address", + address, + "pubkey", + pubkey + ); sha256.update(JSON.stringify(contract)); const hash = sha256.digest("hex"); @@ -128,6 +136,7 @@ app.post( contract_hash }; + params.asset_amount = asset_amount / (SATS / 10 ** precision); if (token_amount) { diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index d782627d..67ccdb45 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -114,7 +114,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { emit(user.username, "payment", payment); l.info("bitcoin detected", user.username, value); notify(user, `${value} SAT payment detected`); - await callWebhook(invoice, payment); + callWebhook(invoice, payment); } } catch (e) { console.log(e); @@ -189,7 +189,7 @@ setInterval(async () => { emit(user.username, "payment", p); l.info("bitcoin confirmed", user.username, p.amount, p.tip); notify(user, `${total} SAT payment confirmed`); - await callWebhook(p.invoice, p); + callWebhook(p.invoice, p); } else { l.warn("couldn't find bitcoin payment", hash); } diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 8d2aa750..39c5be15 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -67,7 +67,7 @@ const handlePayment = async msg => { payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); - await callWebhook(invoice, payment); + callWebhook(invoice, payment); emit(user.username, "payment", payment); emit(user.username, "account", payment.account); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 259db7c7..d3d4aec8 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -175,7 +175,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { emit(user.username, "account", payment.account); l.info("liquid detected", address, user.username, asset, value); notify(user, `${value} SAT payment detected`); - await callWebhook(invoice, payment); + callWebhook(invoice, payment); }); } } catch (e) { -- cgit v1.2.3 From 97de1b7d9552d56a53c572b39fc7c7bc8cae242b Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 5 Aug 2021 11:35:16 -0700 Subject: capture invoice for ln and lbtc payments --- routes/lightning/receive.js | 3 ++- routes/liquid/receive.js | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'routes/lightning/receive.js') diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 39c5be15..8d1e8917 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -50,7 +50,8 @@ const handlePayment = async msg => { received: true, confirmed: true, network: "lightning", - tip + tip, + invoice_id: invoice.id }, { transaction } ); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index d3d4aec8..2c88075c 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -162,7 +162,8 @@ zmqRawTx.on("message", async (topic, message, sequence) => { tip, confirmed, address, - network: "liquid" + network: "liquid", + invoice_id: invoice.id }, { transaction } ); @@ -338,7 +339,7 @@ setInterval(async () => { ); notify(user, `${total} SAT payment confirmed`); - await callWebhook(p.invoice, p); + callWebhook(p.invoice, p); } else { l.warn("couldn't find liquid payment", hash); } -- cgit v1.2.3 From 2b7679cb963f42c10b97b48e794fd8594607e04f Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 5 Aug 2021 15:16:26 -0700 Subject: prettier --- routes/lightning/receive.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'routes/lightning/receive.js') diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 8d1e8917..d974860e 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -1,19 +1,18 @@ -const handlePayment = async msg => { +const handlePayment = async (msg) => { if (!msg.settled) return; let account, total, user; const invoice = await db.Invoice.findOne({ where: { - text: msg.payment_request - } + text: msg.payment_request, + }, }); if (!invoice) return l.warn("received lightning with no invoice", msg.payment_request); try { - await db.transaction(async transaction => { - + await db.transaction(async (transaction) => { const { text: hash, currency, memo, rate, tip, user_id } = invoice; const amount = parseInt(msg.amt_paid_sat) - tip; if (amount > 10000000 || amount < 0) @@ -23,14 +22,14 @@ const handlePayment = async msg => { where: { user_id, asset: config.liquid.btcasset, - pubkey: null + pubkey: null, }, include: { model: db.User, - as: "user" + as: "user", }, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); ({ user } = account); @@ -51,7 +50,7 @@ const handlePayment = async msg => { confirmed: true, network: "lightning", tip, - invoice_id: invoice.id + invoice_id: invoice.id, }, { transaction } ); @@ -98,7 +97,7 @@ const handlePayment = async msg => { address: c.address, amount: total - 100, user, - limit: total + limit: total, }); } catch (e) { l.error("problem sending liquid payment", e.message, e.stack); @@ -110,14 +109,14 @@ const handlePayment = async msg => { }; 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"; @@ -127,13 +126,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); -- cgit v1.2.3