From e6f82e1ae0e5d7d63ad1d730b78be6b6ea7e8c66 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 9 Aug 2021 21:01:03 -0700 Subject: fix variable assignment --- routes/payments.js | 1 - 1 file changed, 1 deletion(-) diff --git a/routes/payments.js b/routes/payments.js index efb01079..70b711da 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -96,7 +96,6 @@ ah(async () => { if (config.lna.clightning) { lna = require("clightning-client")(config.lna.dir); } else { - lnd = require("../lib/lnd"); lna = lnd.default; lnp = [ "addInvoice", -- cgit v1.2.3 From fff03f6ba675d61b31cc981bcf751dc1b2fadd21 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 9 Aug 2021 21:05:23 -0700 Subject: log failed tx --- routes/assets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/assets.js b/routes/assets.js index 939eb76c..0a71bbb8 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -157,7 +157,7 @@ app.post( const brt = await lq.blindRawTransaction(hex, true, [], false); const srt = await lq.signRawTransactionWithWallet(brt); const allowed = (await lq.testMempoolAccept([srt.hex]))[0].allowed; - if (!allowed) throw new Error("issuance rejected by mempool"); + if (!allowed) throw new Error("issuance rejected by mempool", srt.hex); const txid = await lq.sendRawTransaction(srt.hex); await db.transaction(async transaction => { -- cgit v1.2.3 From 6510e2e72adc107fb6fb341c8dd4641f84586634 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 10 Aug 2021 16:06:22 -0700 Subject: call webhook for internal transfers --- routes/bitcoin/receive.js | 18 +++++++-------- routes/send.js | 59 ++++++++++++++++++++++++++--------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 603db47b..d9274935 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -126,17 +126,17 @@ zmqRawTx.on("message", async (topic, message, sequence) => { }); zmqRawBlock.on("message", async (topic, message, sequence) => { - const payments = await db.Payment.findAll({ - where: { confirmed: false }, - }); - - let block = bitcoin.Block.fromHex(message.toString("hex")); - block.transactions.map((tx) => { - let hash = reverse(tx.getHash()).toString("hex"); - if (payments.find((p) => p.hash === hash)) queue[hash] = 1; - }); + const payments = await db.Payment.findAll({ + where: { confirmed: false }, }); + let block = bitcoin.Block.fromHex(message.toString("hex")); + block.transactions.map((tx) => { + let hash = reverse(tx.getHash()).toString("hex"); + if (payments.find((p) => p.hash === hash)) queue[hash] = 1; + }); +}); + setInterval(async () => { try { const arr = Object.keys(queue); diff --git a/routes/send.js b/routes/send.js index 3aec9fb6..3f240e85 100644 --- a/routes/send.js +++ b/routes/send.js @@ -12,15 +12,15 @@ module.exports = ah(async (req, res, next) => { return res.status(500).send("Amount must be greater than zero"); try { - await db.transaction(async transaction => { + await db.transaction(async (transaction) => { let account = await db.Account.findOne({ where: { user_id: user.id, asset, - pubkey: null + pubkey: null, }, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); if (account.balance < amount) { @@ -41,7 +41,7 @@ module.exports = ah(async (req, res, next) => { currency: user.currency, confirmed: true, hash: username ? `Payment to ${username}` : "Internal Transfer", - network: "COINOS" + network: "COINOS", }; if (!username) { @@ -68,17 +68,21 @@ module.exports = ah(async (req, res, next) => { where: { username }, include: { model: db.Account, - as: "account" - } + as: "account", + }, }, { transaction } ); + let invoice = await db.Invoice.findOne({ + where: { amount, user_id: recipient.id }, + }); + let a2; let acc = { user_id: recipient.id, asset, - pubkey: null + pubkey: null, }; if (recipient.account.asset === asset && !recipient.account.pubkey) @@ -87,7 +91,7 @@ module.exports = ah(async (req, res, next) => { a2 = await db.Account.findOne({ where: acc, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); } @@ -107,12 +111,12 @@ module.exports = ah(async (req, res, next) => { } else { const existing = await db.Account.findOne({ where: { - asset + asset, }, order: [["id", "ASC"]], limit: 1, lock: transaction.LOCK.UPDATE, - transaction + transaction, }); if (existing) { @@ -128,22 +132,24 @@ module.exports = ah(async (req, res, next) => { } let spread = user.username === "coinos" ? 1.02 : 1; - amount = Math.round(amount/spread); - let p2 = await db.Payment.create( - { - amount, - account_id: a2.id, - user_id: recipient.id, - rate: app.get("rates")[recipient.currency] * spread, - currency: recipient.currency, - confirmed: true, - hash: "Payment from " + user.username, - memo, - network: "COINOS", - received: true - }, - { transaction } - ); + amount = Math.round(amount / spread); + + params = { + amount, + account_id: a2.id, + user_id: recipient.id, + rate: app.get("rates")[recipient.currency] * spread, + currency: recipient.currency, + confirmed: true, + hash: "Payment from " + user.username, + memo, + network: "COINOS", + received: true, + }; + + if (invoice) params.invoice_id = invoice.id; + + let p2 = await db.Payment.create(params, { transaction }); p2 = p2.get({ plain: true }); p2.account = a2.get({ plain: true }); @@ -152,6 +158,7 @@ module.exports = ah(async (req, res, next) => { l.info("received internal", recipient.username, amount); notify(recipient, `Received ${amount} ${a2.ticker} sats`); + callWebhook(invoice, p2); } res.send(payment); }); -- cgit v1.2.3