diff options
| author | Adam Soltys <[email protected]> | 2020-05-17 21:31:59 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-05-17 21:31:59 -0700 |
| commit | 56f05870ff6cc9100683ba017b6f6b4818a47f7f (patch) | |
| tree | f79e55078225b7818a056606a0059b47a1c7a43c /routes/bitcoin | |
| parent | f35282246da3a77d2d7e6dc193e12997832d1423 (diff) | |
| download | coinos-server-56f05870ff6cc9100683ba017b6f6b4818a47f7f.tar.gz coinos-server-56f05870ff6cc9100683ba017b6f6b4818a47f7f.zip | |
try catch around queue processing
Diffstat (limited to 'routes/bitcoin')
| -rw-r--r-- | routes/bitcoin/receive.js | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 9b01fc6a..77aabd68 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -25,7 +25,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) => { const { value } = o; let address; @@ -40,16 +40,16 @@ 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: "BTC" + network: "BTC", }, - order: [["id", "DESC"]] + order: [["id", "DESC"]], }); const currency = invoice ? invoice.currency : user.currency; @@ -61,8 +61,8 @@ zmqRawTx.on("message", async (topic, message, sequence) => { const account = await db.Account.findOne({ where: { user_id: user.id, - asset: config.liquid.btcasset - } + asset: config.liquid.btcasset, + }, }); account.pending += value; @@ -81,10 +81,10 @@ zmqRawTx.on("message", async (topic, message, sequence) => { 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.toString('hex')); + let hex = await bc.getRawTransaction(hash.toString("hex")); let inputTx = bitcoin.Transaction.fromHex(hex); totalInputs += inputTx.outs[index].value; - } + } let fee = totalInputs - totalOutputs; let payment = await db.Payment.create({ @@ -99,7 +99,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { tip, confirmed, address, - network: "BTC" + network: "BTC", }); payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); @@ -117,46 +117,50 @@ let queue = {}; zmqRawBlock.on("message", async (topic, message, sequence) => { const payments = await db.Payment.findAll({ - where: { confirmed: false } + where: { confirmed: false }, }); - const hashes = payments.map(p => p.hash); + const hashes = payments.map((p) => p.hash); let block = bitcoin.Block.fromHex(message.toString("hex")); - block.transactions.map(tx => { + block.transactions.map((tx) => { let hash = reverse(tx.getHash()).toString("hex"); if (hashes.includes(hash)) queue[hash] = 1; }); }); setInterval(async () => { - let arr = Object.keys(queue); - for (let i = 0; i < arr.length; i++) { - let hash = arr[i]; + try { + let arr = Object.keys(queue); + for (let i = 0; i < arr.length; i++) { + let hash = arr[i]; - let p = await db.Payment.findOne({ - where: { hash, confirmed: 0, received: 1 }, - include: { - model: db.Account, - as: "account" - } - }); + let p = await db.Payment.findOne({ + where: { hash, confirmed: 0, received: 1 }, + include: { + model: db.Account, + as: "account", + }, + }); - if (p) { - p.confirmed = 1; - p.account.balance += p.amount + p.tip; - p.account.pending -= Math.min(p.account.pending, p.amount + p.tip); + if (p) { + p.confirmed = 1; + p.account.balance += p.amount + p.tip; + p.account.pending -= Math.min(p.account.pending, p.amount + p.tip); - await p.account.save(); - await p.save(); + await p.account.save(); + await p.save(); - let user = await getUserById(p.user_id); - emit(user.username, "user", user); - emit(user.username, "payment", p); - l.info("bitcoin confirmed", user.username, p.amount, p.tip); - delete queue[hash]; - } else { - l.warn("Couldn't find payment", hash); - } + let user = await getUserById(p.user_id); + emit(user.username, "user", user); + emit(user.username, "payment", p); + l.info("bitcoin confirmed", user.username, p.amount, p.tip); + delete queue[hash]; + } else { + l.warn("Couldn't find payment", hash); + } + } + } catch (e) { + l.error("problem processing queued bitcoin transaction", e.message); } }, 1000); |
