aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/account.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/account.js b/lib/account.js
new file mode 100644
index 00000000..9355bd97
--- /dev/null
+++ b/lib/account.js
@@ -0,0 +1,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);
+};