aboutsummaryrefslogtreecommitdiffstats
path: root/routes/bitcoin/send.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-01-15 15:31:46 -0800
committerAdam Soltys <[email protected]>2021-01-15 15:31:46 -0800
commitd01159ce9eee956dca32fd69525c0edd5aae9477 (patch)
treee6a4d4a928daffb5bcedbc3411a0dfded6ff422f /routes/bitcoin/send.js
parentd03dad4d85ccd107068719c4b68cbd6a255864f6 (diff)
downloadcoinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.tar.gz
coinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.zip
add more transactions and locks
Diffstat (limited to 'routes/bitcoin/send.js')
-rw-r--r--routes/bitcoin/send.js43
1 files changed, 23 insertions, 20 deletions
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);