aboutsummaryrefslogtreecommitdiffstats
path: root/package.json
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-07-25 11:24:08 -0700
committerAdam Soltys <[email protected]>2022-07-25 11:24:08 -0700
commit5640d62d160cb6e00289db28d7ff25b6881db71d (patch)
treef906e966bdeda4c32bc8e9831d0838f427e14ca2 /package.json
parentb6907bfae1a96f1d1ef99c7575598ca591c0d6d7 (diff)
downloadcoinos-ui-5640d62d160cb6e00289db28d7ff25b6881db71d.tar.gz
coinos-ui-5640d62d160cb6e00289db28d7ff25b6881db71d.zip
rm npmrc
Diffstat (limited to 'package.json')
0 files changed, 0 insertions, 0 deletions
href='#n141'>141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
const axios = require("axios");
const uuidv4 = require("uuid/v4");

module.exports = ah(async (req, res, next) => {
  let { amount, asset, memo, username } = req.body;
  let { user } = req;
  amount = parseInt(amount);

  if (!asset) asset = config.liquid.btcasset;

  if (!amount || amount < 0)
    return res.status(500).send("Amount must be greater than zero");

  try {
    await db.transaction(async transaction => {
      let account = await db.Account.findOne({
        where: {
          user_id: user.id,
          asset,
          pubkey: null
        },
        lock: transaction.LOCK.UPDATE,
        transaction
      });

      if (account.balance < amount) {
        throw new Error("Insufficient funds");
      }

      let fee = 0;

      await account.decrement({ balance: amount }, { transaction });
      await account.reload({ transaction });

      let params = {
        amount: -amount,
        account_id: account.id,
        memo,
        user_id: user.id,
        rate: app.get("rates")[user.currency],
        currency: user.currency,
        confirmed: true,
        hash: username ? `Payment to ${username}` : "Internal Transfer",
        network: "COINOS"
      };

      if (!username) {
        return res.status(500).send("Vouchers temporarily disabled");
        l.info("creating redeemable payment");
        params.redeemcode = uuidv4();
        params.hash = `${req.get("origin")}/redeem/${params.redeemcode}`;
      }

      let payment = await db.Payment.create(params, { transaction });

      payment = payment.get({ plain: true });
      payment.account = account.get({ plain: true });

      l.info("sent internal", user.username, -payment.amount);

      emit(user.username, "payment", payment);
      emit(user.username, "account", account);
      emit(user.username, "user", user);

      if (username) {
        let recipient = await db.User.findOne(
          {
            where: { username },
            include: {
              model: db.Account,
              as: "account"
            }
          },
          { transaction }
        );

        let a2;
        let acc = {
          user_id: recipient.id,
          asset,
          pubkey: null
        };

        if (recipient.account.asset === asset && !recipient.account.pubkey)
          a2 = recipient.account;
        else {
          a2 = await db.Account.findOne({
            where: acc,
            lock: transaction.LOCK.UPDATE,
            transaction
          });
        }

        if (a2) {
          await a2.increment({ balance: amount }, { transaction });
          await a2.reload({ transaction });
        } else {
          let name = asset.substr(0, 6);
          let domain;
          let ticker = asset.substr(0, 3).toUpperCase();
          let precision = 8;

          const assets = app.get("assets");

          if (assets[asset]) {
            ({ domain, ticker, precision, name } = assets[asset]);
          } else {
            const existing = await db.Account.findOne({
              where: {
                asset
              },
              order: [["id", "ASC"]],
              limit: 1,
              lock: transaction.LOCK.UPDATE,
              transaction
            });

            if (existing) {
              ({ domain, ticker, precision, name } = existing);
            }
          }

          acc = { ...acc, ...{ domain, ticker, precision, name } };
          acc.balance = amount;
          acc.pending = 0;
          acc.network = "liquid";
          a2 = await db.Account.create(acc, { transaction });
        }

        let spread = user.username === "coinos" ? 1.02 : 1;
        amount = Math.round(amount/spread);
        let p2 = await db.Payment.create(
          {
            amount,
            account_id: a2.id,
            user_id: recipient.id,
            rate: app.get("rates")[recipient.currency] * spread,
            currency: recipient.currency,
            confirmed: true,
            hash: "Payment from " + user.username,
            memo,
            network: "COINOS",
            received: true
          },
          { transaction }
        );

        p2 = p2.get({ plain: true });
        p2.account = a2.get({ plain: true });
        emit(recipient.username, "account", p2.account);
        emit(recipient.username, "payment", p2);

        l.info("received internal", recipient.username, amount);
        notify(recipient, `Received ${amount} ${a2.ticker} sats`);
      }
      res.send(payment);
    });
  } catch (e) {
    l.error(
      "problem sending internal payment",
      user.username,
      user.balance,
      e.message
    );
    return res.status(500).send(e.message);
  }
});