diff options
| -rw-r--r-- | routes/bitcoin/receive.js | 151 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 51 |
2 files changed, 107 insertions, 95 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 45dadbbe..b11a5de7 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -49,88 +49,99 @@ zmqRawTx.on("message", async (topic, message, sequence) => { Object.keys(addresses).includes(address) && !change.includes(address) ) { - let user = await db.User.findOne({ - where: { - username: addresses[address] - } - }); + await db.transaction(async transaction => { + let user = await db.User.findOne({ + where: { + username: addresses[address] + }, + transaction + }); - const invoice = await db.Invoice.findOne({ - where: { - address, - user_id: user.id, - network: "bitcoin" - }, - order: [["id", "DESC"]], - include: { - model: db.Account, - as: "account" - } - }); + const invoice = await db.Invoice.findOne({ + where: { + address, + user_id: user.id, + network: "bitcoin" + }, + order: [["id", "DESC"]], + include: { + model: db.Account, + as: "account" + }, + transaction + }); - if (!invoice) return; + if (!invoice) return; - const currency = invoice ? invoice.currency : user.currency; - const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; - const tip = invoice ? invoice.tip : 0; - const memo = invoice ? invoice.memo : ""; + const currency = invoice ? invoice.currency : user.currency; + const rate = invoice + ? invoice.rate + : app.get("rates")[user.currency]; + const tip = invoice ? invoice.tip : 0; + const memo = invoice ? invoice.memo : ""; - let confirmed = false; + let confirmed = false; - let { account } = invoice; + let { account } = invoice; - if (account.asset !== config.liquid.btcasset) { - account = await db.Account.findOne({ - where: { - user_id: user.id, - asset: config.liquid.btcasset, - pubkey: null - } - }); - } + if (account.asset !== config.liquid.btcasset) { + account = await db.Account.findOne({ + where: { + user_id: user.id, + asset: config.liquid.btcasset, + pubkey: null + }, + transaction + }); + } - account.pending += value; - await account.save(); + await account.increment({ pending: value }, { transaction }); + await account.save({ transaction }); - if (config.bitcoin.walletpass) - await bc.walletPassphrase(config.bitcoin.walletpass, 300); + if (config.bitcoin.walletpass) + await bc.walletPassphrase(config.bitcoin.walletpass, 300); - let totalOutputs = tx.outs.reduce((a, b) => a + b.value, 0); - let totalInputs = 0; - for (let i = 0; i < tx.ins.length; i++) { - let { hash, index } = tx.ins[i]; - hash = reverse(hash).toString("hex"); - let hex = await bc.getRawTransaction(hash); - let inputTx = bitcoin.Transaction.fromHex(hex); - totalInputs += inputTx.outs[index].value; - } - let fee = totalInputs - totalOutputs; + let totalOutputs = tx.outs.reduce((a, b) => a + b.value, 0); + let totalInputs = 0; + for (let i = 0; i < tx.ins.length; i++) { + let { hash, index } = tx.ins[i]; + hash = reverse(hash).toString("hex"); + let hex = await bc.getRawTransaction(hash); + let inputTx = bitcoin.Transaction.fromHex(hex); + totalInputs += inputTx.outs[index].value; + } + let fee = totalInputs - totalOutputs; - let payment = await db.Payment.create({ - account_id: account.id, - user_id: user.id, - hash, - fee, - memo, - amount: value - tip, - currency, - rate, - received: true, - tip, - confirmed, - address, - network: "bitcoin", - invoice_id: invoice.id - }); - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); + let payment = await db.Payment.create( + { + account_id: account.id, + user_id: user.id, + hash, + fee, + memo, + amount: value - tip, + currency, + rate, + received: true, + tip, + confirmed, + address, + network: "bitcoin", + invoice_id: invoice.id + }, + { transaction } + ); - emit(user.username, "account", account); + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); - emit(user.username, "payment", payment); - l.info("bitcoin detected", user.username, value); - notify(user, `${value} SAT payment detected`); - callWebhook(invoice, payment); + emit(user.username, "account", account); + + emit(user.username, "payment", payment); + l.info("bitcoin detected", user.username, value); + notify(user, `${value} SAT payment detected`); + callWebhook(invoice, payment); + }); } } catch (e) { console.log(e); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 2bd86805..312613e2 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -27,12 +27,28 @@ const getAccount = async (params, transaction) => { return account; } - let { asset } = params; + let { asset, pubkey } = params; let name = asset.substr(0, 6); let domain = ""; let ticker = asset.substr(0, 3).toUpperCase(); let precision = 8; + if (pubkey) { + const nc = await db.Account.findOne({ + where: { pubkey }, + order: [["id", "ASC"]], + limit: 1, + lock: transaction.LOCK.UPDATE, + transaction + }); + + params.index = 0; + params.path = nc.path; + params.pubkey = nc.pubkey; + params.privkey = nc.privkey; + params.seed = nc.seed; + } + const assets = app.get("assets"); if (assets[asset]) { @@ -56,6 +72,7 @@ const getAccount = async (params, transaction) => { params = { ...params, ...{ domain, ticker, precision, name } }; params.balance = 0; + params.pending = 0; params.network = "liquid"; return db.Account.create(params, { transaction }); }; @@ -119,48 +136,32 @@ zmqRawTx.on("message", async (topic, message, sequence) => { if (!invoice) return; - let payment = await db.Payment.findOne({ - where: { - invoice_id: invoice.id, - }, - order: [["id", "DESC"]], - transaction - }); - - if (payment) return; - let confirmed = 0; let account = await db.Account.findOne({ where: { - id: invoice.account_id, - asset + id: invoice.account_id }, lock: transaction.LOCK.UPDATE, transaction }); - if ( - account.asset === asset && - (!account.pubkey || account.network === "liquid") - ) { - await account.increment({ pending: value }, { transaction }); - await account.reload({ transaction }); - } else { + if (!(account && account.asset === asset)) { + let pubkey = account ? account.pubkey : null; + account = await getAccount( { - seed: account.seed, - path: account.path, user_id: user.id, asset, - pubkey: account.pubkey, - pending: value, - index: 0 + pubkey }, transaction ); } + await account.increment({ pending: value }, { transaction }); + await account.reload({ transaction }); + if (config.liquid.walletpass) await lq.walletPassphrase(config.liquid.walletpass, 300); |
