diff options
| author | Adam Soltys <[email protected]> | 2021-01-15 15:31:46 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-01-15 15:31:46 -0800 |
| commit | d01159ce9eee956dca32fd69525c0edd5aae9477 (patch) | |
| tree | e6a4d4a928daffb5bcedbc3411a0dfded6ff422f /routes/bitcoin | |
| parent | d03dad4d85ccd107068719c4b68cbd6a255864f6 (diff) | |
| download | coinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.tar.gz coinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.zip | |
add more transactions and locks
Diffstat (limited to 'routes/bitcoin')
| -rw-r--r-- | routes/bitcoin/broadcast.js | 56 | ||||
| -rw-r--r-- | routes/bitcoin/receive.js | 9 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 43 |
3 files changed, 64 insertions, 44 deletions
diff --git a/routes/bitcoin/broadcast.js b/routes/bitcoin/broadcast.js index 77b487de..a32e9067 100644 --- a/routes/bitcoin/broadcast.js +++ b/routes/bitcoin/broadcast.js @@ -4,29 +4,43 @@ module.exports = ah(async (req, res) => { let hash = await bc.sendRawTransaction(req.body.tx); if (user) { - let { account } = user; - let payment = await db.Payment.create({ - ...req.body.payment, - hash, - amount: -req.body.payment.amount, - tip: 0, - account_id: account.id, - user_id: user.id, - rate: app.get("rates")[user.currency], - currency: user.currency, - confirmed: true, - received: false, - network: "bitcoin", - }); + await db.transaction(async transaction => { + let account = await db.Account.findOne({ + where: { + id: user.account_id + }, + lock: transaction.LOCK.UPDATE, + transaction + }); + let payment = await db.Payment.create( + { + ...req.body.payment, + hash, + amount: -req.body.payment.amount, + tip: 0, + account_id: account.id, + user_id: user.id, + rate: app.get("rates")[user.currency], + currency: user.currency, + confirmed: true, + received: false, + network: "bitcoin" + }, + { transaction } + ); - account.balance += payment.amount - payment.fee; - await account.save(); + await account.increment( + { balance: payment.amount - payment.fee }, + { transaction } + ); + await account.reload({ transaction }); - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); - emit(user.username, "payment", payment); - emit(user.username, "account", payment.account); - res.send(payment); + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); + emit(user.username, "payment", payment); + emit(user.username, "account", payment.account); + res.send(payment); + }); } else { res.send(result); } diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index e0e3828b..2a6a7954 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -159,10 +159,13 @@ setInterval(async () => { let total = p.amount + p.tip; p.confirmed = 1; - p.account.balance += total; - p.account.pending -= Math.min(p.account.pending, total); - await p.account.save({ transaction }); + await p.account.increment({ balance: total }, { transaction }); + await p.account.decrement( + { pending: Math.min(p.account.pending, total) }, + { transaction } + ); + await p.account.reload({ transaction }); await p.save({ transaction }); p = p.get({ plain: true }); diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index 2089adfa..f1814b08 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -31,15 +31,23 @@ module.exports = ah(async (req, res) => { total = total - change + fee; let amount = total - fee; - let { account } = user; let params; try { await db.transaction(async transaction => { + let account = await db.Account.findOne({ + where: { + id: user.account_id + }, + lock: transaction.LOCK.UPDATE, + transaction + }); + if (account.asset !== btc) { account = await db.Account.findOne({ where: { user_id: user.id, asset: btc, + pubkey: null }, lock: transaction.LOCK.UPDATE, transaction @@ -51,8 +59,8 @@ module.exports = ah(async (req, res) => { throw new Error("Insufficient funds"); } - account.balance -= total; - await account.save({ transaction }); + await account.decrement({ balance: total }, { transaction }); + await account.reload({ transaction }); params = { amount: -amount, @@ -67,29 +75,24 @@ module.exports = ah(async (req, res) => { received: false, network: "bitcoin" }; - }); - } catch (e) { - l.error("problem creating bitcoin payment", user.username, e.message); - return res.status(500).send(e.message); - } - try { - 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); + let payment = await db.Payment.create(params, { transaction }); - 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); |
