This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbalance.js
164 lines (146 loc) · 4.65 KB
/
balance.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const path = require('path');
const fs = require('fs');
const rp = require('request-promise');
const yaml = require('js-yaml');
const co = require('co');
const urljoin = require('url-join');
const { validateConfig } = require('./utils');
const { APPLICATION, API_ENDPOINTS } = require('./properties');
let data = {
username: null,
hostname: null,
address: null,
balance: null
}
/**
* Entry point for strato balance command
*/
function getBalance() {
return new Promise((resolve, reject) => {
co(function* () {
yield validateConfig()
.catch((err) => {
reject(err);
});
yield readYAML()
.catch((err) => {
reject(err);
});
yield getUserAddress()
.catch((err) => {
reject(err);
});
yield fetchBalance()
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
});
}
/**
* Read config.yaml file
* @returns {Promise}
*/
function readYAML() {
return new Promise((resolve, reject) => {
const YAMLFile = path.join(APPLICATION.HOME_PATH, APPLICATION.CONFIG_FOLDER, APPLICATION.CONFIG_FILE);
try {
const config = yaml.safeLoad(fs.readFileSync(YAMLFile, 'utf8'));
data.username = config.username;
data.hostname = config.hostname;
resolve();
} catch (err) {
reject(err);
}
});
}
/**
* Fetch user address
* @returns {Promise}
*/
function getUserAddress() {
return new Promise((resolve, reject) => {
const url = urljoin(data.hostname, API_ENDPOINTS.BLOC_GET_USER_ADDRESS, data.username);
let options = {
method: 'GET',
url: url,
followRedirect: true,
}
rp(options)
.then((response) => {
try {
let address = JSON.parse(response);
if (address.length > 0) {
data.address = address[0];
resolve();
} else {
reject('username not found. try running strato config to modify username');
}
} catch (err) {
reject('the hostname provided is not the STRATO node');
}
})
.catch((err) => {
if (err.error.code === 'ECONNREFUSED') {
reject('could not connect to the host. try running strato config to modify the hostname');
} else if (err.error.code === 'ENOTFOUND') {
reject('could not connect to the host');
} else {
if (err.error.code) {
reject('status code: ' + err.error.code);
} else {
reject('status code: ' + err.statusCode);
}
}
});
});
}
/**
* Fetch user balance
* @returns {Promise}
*/
function fetchBalance() {
return new Promise((resolve, reject) => {
let url = urljoin(data.hostname, API_ENDPOINTS.STRATO_GET_BALANCE);
let options = {
method: 'GET',
url: url,
qs: {
address: data.address
},
followRedirect: true,
}
rp(options)
.then((response) => {
try {
let balance = JSON.parse(response);
if (balance.length > 0) {
data.balance = balance[0].balance;
resolve(data);
} else {
data.balance = -1 ;
resolve(data);
}
} catch (err) {
reject('the hostname provided is not the STRATO node');
}
})
.catch((err) => {
if (err.error.code === 'ECONNREFUSED') {
reject('could not connect to the host. try running strato config to modify the hostname');
} else if (err.error.code === 'ENOTFOUND') {
reject('could not connect to the host');
} else {
if (err.error.code) {
reject('status code: ' + err.error.code);
} else {
reject('status code: ' + err.statusCode);
}
}
});
});
}
module.exports.getBalance = getBalance;