diff options
| author | Adam Soltys <[email protected]> | 2020-05-12 00:13:55 -0400 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-05-12 00:13:55 -0400 |
| commit | ea58d17b9ccee5b102f75125d16444dbc61b77b2 (patch) | |
| tree | 96156b0b30238300b2e6efcb3770c72f7b69e2f9 | |
| parent | 2afea831615a76e18f57cb1a87f6733908db410a (diff) | |
| download | coinos-server-ea58d17b9ccee5b102f75125d16444dbc61b77b2.tar.gz coinos-server-ea58d17b9ccee5b102f75125d16444dbc61b77b2.zip | |
use db transaction for lightning receive
| -rw-r--r-- | routes/invoices.js | 2 | ||||
| -rw-r--r-- | routes/lightning/invoice.js | 11 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 23 |
3 files changed, 17 insertions, 19 deletions
diff --git a/routes/invoices.js b/routes/invoices.js index f36b497c..c6bf4b6e 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -4,7 +4,7 @@ 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)); + l.info("creating invoice", invoice.text, req.user.username, invoice.network, invoice.amount, invoice.tip, invoice.currency, invoice.rate.toFixed(2)); const exists = await db.Invoice.findOne({ where: { diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js index 3f4ab1db..3b453a73 100644 --- a/routes/lightning/invoice.js +++ b/routes/lightning/invoice.js @@ -3,17 +3,6 @@ module.exports = async (req, res) => { l.info("adding lightning invoice", req.user.username, amount, tip); - /* - const exists = await db.Invoice.findOne({ - where: { - [Op.or]: { - address: invoice.address || "undefined", - text: invoice.text - } - } - }); - */ - try { if (config.lna.clightning) { const invoice = await lnb.invoice(`${amount + tip}sat` || "any", new Date(), "", 360); diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 3082c205..0e8d7e22 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -1,6 +1,8 @@ const asset = "LNBTC"; const handlePayment = async msg => { + try { + await db.transaction(async transaction => { if (!msg.settled) return; const invoice = await db.Invoice.findOne({ @@ -9,7 +11,7 @@ const handlePayment = async msg => { } }); - if (!invoice) return; + if (!invoice) return l.warn("received lightning with no invoice", msg.payment_request); const { text: hash, currency, rate, tip, user_id } = invoice; const amount = parseInt(msg.amt_paid_sat) - tip; @@ -18,7 +20,9 @@ const handlePayment = async msg => { where: { user_id, asset: config.liquid.btcasset - } + }, + lock: transaction.LOCK.UPDATE, + transaction }); let preimage = msg.r_preimage.toString("hex"); @@ -35,17 +39,18 @@ const handlePayment = async msg => { confirmed: true, network: "LNBTC", tip - }); + }, + { transaction }); invoice.received += amount + tip; account.balance += amount + tip; - await account.save(); - await invoice.save(); - await payment.save(); + await account.save({ transaction }); + await invoice.save({ transaction }); + await payment.save({ transaction }); payments.push(msg.payment_request); - let user = await getUserById(user_id); + let user = await getUserById(user_id, transaction); payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); @@ -58,6 +63,10 @@ const handlePayment = async msg => { payment.amount, payment.tip ); + }); + } catch(e) { + l.error("problem receiving lightning payment", e.message); + } }; if (config.lna.clightning) { |
