aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-07-29 16:40:30 -0700
committerAdam Soltys <[email protected]>2020-07-29 16:40:30 -0700
commit99d71f034cfb7c1675263539fa6aecb17429d134 (patch)
tree6d29806937436fde454f357a8dd5f7a30fa6b8bd /lib
parentffee4c7051982f3677d6717d24e7b357a89802eb (diff)
downloadcoinos-server-99d71f034cfb7c1675263539fa6aecb17429d134.tar.gz
coinos-server-99d71f034cfb7c1675263539fa6aecb17429d134.zip
redeem codes
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);
+};