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/liquid/broadcast.js | |
| parent | d03dad4d85ccd107068719c4b68cbd6a255864f6 (diff) | |
| download | coinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.tar.gz coinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.zip | |
add more transactions and locks
Diffstat (limited to 'routes/liquid/broadcast.js')
| -rw-r--r-- | routes/liquid/broadcast.js | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/routes/liquid/broadcast.js b/routes/liquid/broadcast.js index a39349a1..1db166c5 100644 --- a/routes/liquid/broadcast.js +++ b/routes/liquid/broadcast.js @@ -4,29 +4,44 @@ module.exports = ah(async (req, res) => { let hash = await lq.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: "liquid", - }); + 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: "liquid" + }, + { 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); } |
