diff options
| author | Adam Soltys <[email protected]> | 2021-08-09 20:55:17 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-08-09 20:55:17 -0700 |
| commit | 56559c7b9749fe62fe2fe0ba90725391d83b8bf7 (patch) | |
| tree | 48d6a0630d05d6787b4410708a5efceff472636b /routes/bitcoin | |
| parent | 803ecab2b5c3a00d107372ea6757cd6ce0f8224d (diff) | |
| parent | 8d6f65079868ae6134f593752e3380180591623d (diff) | |
| download | coinos-server-56559c7b9749fe62fe2fe0ba90725391d83b8bf7.tar.gz coinos-server-56559c7b9749fe62fe2fe0ba90725391d83b8bf7.zip | |
Merge branch 'v1.0.0'
Diffstat (limited to 'routes/bitcoin')
| -rw-r--r-- | routes/bitcoin/receive.js | 48 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 55 |
2 files changed, 51 insertions, 52 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 67ccdb45..603db47b 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -18,6 +18,8 @@ const network = config.bitcoin.network === "mainnet" ? "bitcoin" : config.bitcoin.network ]; +const queue = {}; + zmqRawTx.on("message", async (topic, message, sequence) => { const hex = message.toString("hex"); let tx = bitcoin.Transaction.fromHex(message); @@ -27,7 +29,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { if (payments.includes(hash)) return; Promise.all( - tx.outs.map(async o => { + tx.outs.map(async (o) => { try { const { value } = o; @@ -46,20 +48,20 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let user = await db.User.findOne({ where: { - username: addresses[address] - } + username: addresses[address], + }, }); const invoice = await db.Invoice.findOne({ where: { user_id: user.id, - network: "bitcoin" + network: "bitcoin", }, order: [["id", "DESC"]], include: { model: db.Account, - as: "account" - } + as: "account", + }, }); if (!invoice) return; @@ -104,7 +106,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { confirmed, address, network: "bitcoin", - invoice_id: invoice.id + invoice_id: invoice.id, }); payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); @@ -123,19 +125,17 @@ zmqRawTx.on("message", async (topic, message, sequence) => { ); }); -let queue = {}; - zmqRawBlock.on("message", async (topic, message, sequence) => { - const payments = await db.Payment.findAll({ - where: { confirmed: false } - }); + 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; + 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 { @@ -144,25 +144,25 @@ setInterval(async () => { const hash = arr[i]; let account, address, user, total; - await db.transaction(async transaction => { + await db.transaction(async (transaction) => { let p = await db.Payment.findOne({ where: { hash, confirmed: 0, received: 1 }, include: [ { model: db.Account, - as: "account" + as: "account", }, { model: db.Invoice, - as: "invoice" + as: "invoice", }, { model: db.User, - as: "user" - } + as: "user", + }, ], lock: transaction.LOCK.UPDATE, - transaction + transaction, }); if (p && p.address) address = p.address; @@ -211,7 +211,7 @@ setInterval(async () => { address: c.address, amount: total - 100, user, - limit: total + limit: total, }); } } diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index cab79e06..470889b3 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -1,4 +1,3 @@ -const bitcoin = require("bitcoinjs-lib"); const btc = config.liquid.btcasset; module.exports = ah(async (req, res) => { @@ -32,10 +31,10 @@ module.exports = ah(async (req, res) => { total = total - change + fee; let amount = total - fee; - let params; + let account, params; try { await db.transaction(async transaction => { - let account = await db.Account.findOne({ + account = await db.Account.findOne({ where: { id: user.account_id }, @@ -62,38 +61,38 @@ module.exports = ah(async (req, res) => { await account.decrement({ balance: total }, { transaction }); await account.reload({ transaction }); + }); - params = { - amount: -amount, - fee, - memo, - account_id: account.id, - user_id: user.id, - rate: app.get("rates")[user.currency], - currency: user.currency, - address, - confirmed: true, - received: false, - network: "bitcoin" - }; + params = { + amount: -amount, + fee, + memo, + account_id: account.id, + user_id: user.id, + rate: app.get("rates")[user.currency], + currency: user.currency, + address, + confirmed: true, + received: false, + network: "bitcoin" + }; - if (config.bitcoin.walletpass) - await bc.walletPassphrase(config.bitcoin.walletpass, 300); + if (config.bitcoin.walletpass) + await bc.walletPassphrase(config.bitcoin.walletpass, 300); - hex = (await bc.signRawTransactionWithWallet(hex)).hex; - params.hash = await bc.sendRawTransaction(hex); + hex = (await bc.signRawTransactionWithWallet(hex)).hex; + params.hash = await bc.sendRawTransaction(hex); - let payment = await db.Payment.create(params, { transaction }); + let payment = await db.Payment.create(params); - 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); - res.send(payment); + emit(user.username, "payment", payment); + res.send(payment); - payments.push(params.hash); - l.info("sent bitcoin", user.username, total); - }); + payments.push(params.hash); + l.info("sent bitcoin", user.username, total); } catch (e) { l.error("error sending bitcoin", e.message); return res.status(500).send(e.message); |
