aboutsummaryrefslogtreecommitdiffstats
path: root/lib/account.js
blob: 931638e0bd59953a328dd2706a8e46413f2e512b (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
const axios = require("axios");

module.exports = async (asset, user, transaction) => {
  const account = await db.Account.findOne(
    {
      where: {
        user_id: user.id,
        asset,
        pubkey: null
      }
    },
    { transaction }
  );

  if (account) return account;

  let params = {
    user_id: user.id,
    asset
  };
  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,
    }, { transaction });

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

  params = { ...params, ...{ domain, ticker, precision, name } };
  params.balance = 0;
  params.pending = 0;
  return db.Account.create(params);
};