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

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

  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 = (await axios.get("https://assets.blockstream.info/")).data;

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

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

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