aboutsummaryrefslogtreecommitdiffstats
path: root/routes/bitcoin/broadcast.js
blob: a32e9067a67f6fcaf7484ed5f00da2ca2fb01020 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module.exports = ah(async (req, res) => {
  try {
    const { user } = req;
    let hash = await bc.sendRawTransaction(req.body.tx);

    if (user) {
      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 }
        );

        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);
      });
    } else {
      res.send(result);
    }
  } catch (e) {
    res.status(500).send(e.message);
  }
});