diff options
| author | Adam Soltys <[email protected]> | 2020-09-08 07:26:14 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-09-08 07:26:14 -0700 |
| commit | b5607df482440b55bd3e802f5bea4ead95e6491e (patch) | |
| tree | 6b8948fbb703c1feaca1aca10f3ac86c8062768b /routes/bitcoin | |
| parent | e40e68f49b10d6ef70546f7128bad891256525c0 (diff) | |
| download | coinos-server-b5607df482440b55bd3e802f5bea4ead95e6491e.tar.gz coinos-server-b5607df482440b55bd3e802f5bea4ead95e6491e.zip | |
bump account index
Diffstat (limited to 'routes/bitcoin')
| -rw-r--r-- | routes/bitcoin/receive.js | 54 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 5 |
2 files changed, 23 insertions, 36 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index 987f5079..63a34760 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -26,7 +26,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; @@ -44,18 +44,24 @@ 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"]], + include: { + model: db.Account, + as: "account", + }, }); + 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; @@ -63,27 +69,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let confirmed = false; - const accounts = await db.Account.findAll({ - where: { - user_id: user.id, - asset: config.liquid.btcasset, - }, - }); - - let account = accounts.find(({ index, pubkey }) => { - const { payments } = bitcoin; - index && - pubkey && - address === - payments.p2wpkh({ - pubkey: fromBase58(pubkey, network).derivePath( - `${index}` - ).publicKey, - network, - }).address; - }); - - if (!account) account = accounts[0]; + let { account } = invoice; account.pending += value; await account.save(); @@ -120,7 +106,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 }); @@ -138,13 +124,13 @@ let queue = {}; zmqRawBlock.on("message", async (topic, message, sequence) => { const payments = await db.Payment.findAll({ - where: { confirmed: false }, + where: { confirmed: false } }); let block = bitcoin.Block.fromHex(message.toString("hex")); - block.transactions.map((tx) => { + block.transactions.map(tx => { let hash = reverse(tx.getHash()).toString("hex"); - if (payments.find((p) => p.hash === hash)) queue[hash] = 1; + if (payments.find(p => p.hash === hash)) queue[hash] = 1; }); }); @@ -154,21 +140,21 @@ setInterval(async () => { for (let i = 0; i < arr.length; i++) { const hash = arr[i]; - 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.User, - as: "user", - }, + as: "user" + } ], lock: transaction.LOCK.UPDATE, - transaction, + transaction }); const { user } = p; diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 8745c3dd..5d22152e 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -1,4 +1,5 @@ const bitcoin = require("bitcoinjs-lib"); +const btc = config.liquid.btcasset; module.exports = ah(async (req, res) => { let { user } = req; @@ -34,11 +35,11 @@ module.exports = ah(async (req, res) => { let params; try { await db.transaction(async transaction => { - if (account.asset !== btcasset) { + if (account.asset !== btc) { account = await db.Account.findOne({ where: { user_id: user.id, - asset: config.liquid.btcasset + asset: btc, }, lock: transaction.LOCK.UPDATE, transaction |
