-
Notifications
You must be signed in to change notification settings - Fork 21
/
app.js
executable file
·388 lines (364 loc) · 17.5 KB
/
app.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env node
/**
*
* Node.JS pricefeed script for hive
* Created by @Someguy213
* https://github.com/someguy123
* Updated by @Rishi556
* https://github.com/rishi556
* Released under GNU GPL 3.0
* Requires Node v8.11.4
*/
var settings = require('./lib/settings');
var exchange = require('./lib/exchange');
var hive = require('@hiveio/hive-js');
var config = settings.config,
retry_conf = settings.retry_conf;
console.log('-------------');
log(`Loaded configuration:
Username: ${config.name}
Bias: ${config.peg ? config.peg_multi : 'Disabled'}
RPC Node: ${config.node}
Alternate Nodes: ${config.alternate_nodes.join(', ')}`);
console.log('-------------');
global.verbose = false;
for (var t_arg of process.argv) {
if (t_arg == '-v') {
global.verbose = true;
}
}
hive.api.setOptions({url: config.node, address_prefix: config.address_prefix ,chain_id: config.chain_id})
// used for re-trying failed promises
function delay(t) {
return new Promise((r_resolve) => setTimeout(r_resolve, t));
}
class HiveAcc {
constructor(username, active_wif, signing_keys) {
/**
* Initialises object with username and active private key
* @param {string} username The username (without @)
* @param {string} active_wif The active private key
* @param {array} signing_keys Array of signing keys for account
* @throws {Error<string:msg>} If private key is invalid
*/
if (!hive.auth.isWif(config.wif) && signing_keys && !Object.keys(signing_keys).length) {
throw new Error('The private key you specified is not valid. Be aware Hive private keys start with a 5.');
}
this.user_data = {username, active_wif, signing_keys};
this.wif_valid = null;
}
loadAccount(reload = false, tries = 0) {
/**
* Loads an account's info (public keys) into this.user_data
* then returns it. Automatically caches the data
* @param {bool} reload Refresh the account cache
* @param {int} tries Internal parameter used for retries on failure
* @return {Promise.<user_data, str:err>}
*/
var {user_data} = this;
var {username} = user_data;
log(`Loading account data for ${username}`);
// If we already have the account loaded, and no refresh was requested
// just use the cache.
if (('auths' in user_data) && !reload) {
return new Promise((resolve) => {
return resolve(user_data);
});
}
return new Promise((resolve, reject) => {
hive.api.getAccounts([username], (err, res) => {
if (err) {
console.error(`A problem occurred while locating the account ${username}`);
console.error('Most likely the RPC node is down.');
var msg = ('message' in err) ? err.message : err;
console.error('The error returned was:', msg);
if (tries < retry_conf.login_attempts) {
switchNode();
console.error(`Will retry in ${retry_conf.login_delay} seconds`);
console.error('-------------');
return delay(retry_conf.login_delay * 1000)
.then(() => resolve(this.loadAccount(reload, tries + 1)))
.catch((e) => console.error(e));
}
console.error(`Giving up. Tried ${tries} times`);
return reject(`Failed to log in after ${tries} attempts.`);
}
log(`Successfully connected. Getting data for ${username}`);
if (res.length < 1) {
console.error(`ERROR: Account ${username} wasn't found.`);
return reject('account not found');
}
var account = res[0];
// load the signing key from the account
hive.api.getWitnessByAccount(username, (err, result) => {
if (err) {
console.error(`A problem occurred while locating the witness ${username}`);
console.error('Most likely the RPC node is down.');
var msg = ('message' in err) ? err.message : err;
console.error('The error returned was:', msg);
if (tries < retry_conf.login_attempts) {
switchNode();
console.error(`Will retry in ${retry_conf.login_delay} seconds`);
console.error('-------------');
return delay(retry_conf.login_delay * 1000)
.then(() => resolve(this.loadAccount(reload, tries + 1)))
.catch((e) => console.error(e));
}
console.error(`Giving up. Tried ${tries} times`);
return reject(`Failed to log in after ${tries} attempts.`);
}
if (!result) {
console.error(`A problem occurred while locating the witness ${username}`);
console.error('Most likely the account is not a witness.');
if (tries < retry_conf.login_attempts) {
switchNode();
console.error(`Will retry in ${retry_conf.login_delay} seconds`);
console.error('-------------');
return delay(retry_conf.login_delay * 1000)
.then(() => resolve(this.loadAccount(reload, tries + 1)))
.catch((e) => console.error(e));
}
console.error(`Giving up. Tried ${tries} times`);
return reject(`Failed to log in after ${tries} attempts.`);
}
var ud = {
...user_data,
auths: {
owner: account.owner.key_auths[0][0],
active: account.active.key_auths[0][0],
posting: account.posting.key_auths[0][0],
signing: result.signing_key
},
};
this.user_data = ud;
return resolve(ud);
});
});
});
}
login(reload = false) {
/**
* Checks if an account + active private key or signing keys match.
* Resolves with user data they do, rejects if there's a problem
*/
if (this.wif_valid) {
return new Promise((resolve) => resolve(this.user_data))
}
return new Promise((resolve, reject) => {
this.loadAccount(reload).then((user_data) => {
var {username, active_wif, signing_keys, auths} = user_data;
var is_valid = false;
try {
is_valid = hive.auth.wifIsValid(active_wif, auths.active);
if (is_valid) {
this.wif_valid = true;
}
} catch (e) {
}
try {
if (signing_keys && signing_keys.hasOwnProperty(auths.signing)) {
this.signing_valid = true;
}
} catch (e) {
}
if (this.wif_valid || this.signing_valid) {
return resolve(user_data);
}
return reject('Private key WIF does not match key on account nor do the signing keys');
}).catch((e) => reject(e));
});
}
async publish_feed(rate, tries = 0) {
try {
// var tr = new TransactionBuilder();
var ex_data = rate.toFixed(3) + ` ${config.base_symbol}`;
var quote = 1;
if (config.peg) {
var pcnt = ((1 - config.peg_multi) * 100).toFixed(2);
log(`Pegging is enabled. Reducing price by ${pcnt}% (set config.peg to false to disable)`);
log(`Original price (pre-peg): ${ex_data}`);
quote = 1 / config.peg_multi;
}
var exchangeRate = {base: ex_data, quote: quote.toFixed(3) + ` ${config.quote_symbol}`};
var {username, active_wif, signing_keys} = this.user_data;
if (this.signing_valid) {
hive.api.getWitnessByAccount(username, async (err, result) => {
if (err) {
console.error(`A problem occurred while getting current signing key`);
console.error('Most likely the RPC node is down.');
var msg = ('message' in err) ? err.message : err;
console.error('The error returned was:', msg);
if (tries < retry_conf.feed_attempts) {
switchNode();
console.error(`Will retry in ${retry_conf.feed_delay} seconds`);
console.error('-------------');
return delay(retry_conf.feed_attempts * 1000)
.then(() => this.publish_feed(feed, tries + 1))
.catch((e) => console.error(e));
}
console.error(`Giving up. Tried ${tries} times`);
return reject(`Failed to publish in after ${tries} attempts.`);
}
if (!result) {
console.error(`A problem occurred while getting current signing key`);
console.error('Most likely the account is not a witness.');
if (tries < retry_conf.feed_attempts) {
await this.login();
switchNode();
console.error(`Will retry in ${retry_conf.feed_delay} seconds`);
console.error('-------------');
return delay(retry_conf.feed_attempts * 1000)
.then(() => this.publish_feed(feed, tries + 1))
.catch((e) => console.error(e));
}
console.error(`Giving up. Tried ${tries} times`);
return reject(`Failed to publish in after ${tries} attempts.`);
}
let signing = result.signing_key;
if (signing_keys.hasOwnProperty(signing)) {
let props = {
'key': signing,
'hbd_exchange_rate': exchangeRate
};
let op = hive.utils.buildWitnessUpdateOp(username, props);
hive.broadcast.witnessSetProperties(signing_keys[signing], username, op[1].props, [], async (err, result) => {
if (err) {
console.error('Failed to publish feed...');
var msg = ('message' in err) ? err.message : err;
console.error(`reason: ${msg}`);
if (tries < retry_conf.feed_attempts) {
await this.login();
switchNode();
console.error(`Will retry in ${retry_conf.feed_delay} seconds`);
console.error('-------------');
return delay(retry_conf.feed_delay * 1000)
.then(() => this.publish_feed(rate, tries + 1))
.catch(console.error);
}
console.error(`Giving up. Tried ${tries} times`);
return reject(err);
}
log(`Data published at: ${new Date()}`); //Not cure if this one will work
log('Successfully published feed.');
log(`TXID: ${result.id}`);
log('-------------');
});
} else {
this.signing_valid = false;
return delay(retry_conf.feed_attempts * 1000)
.then(() => this.publish_feed(feed, tries + 1))
.catch((e) => console.error(e));
}
}
);
} else if (this.wif_valid) {
hive.broadcast.feedPublish(active_wif, username, exchangeRate,
(err, r) => {
if (err) {
console.error('Failed to publish feed...');
var msg = ('message' in err) ? err.message : err;
console.error(`reason: ${msg}`);
if (tries < retry_conf.feed_attempts) {
switchNode();
console.error(`Will retry in ${retry_conf.feed_delay} seconds`);
console.error('-------------');
return delay(retry_conf.feed_delay * 1000)
.then(() => this.publish_feed(rate, tries + 1))
.catch(console.error);
}
console.error(`Giving up. Tried ${tries} times`);
return reject(err);
}
log(`Data published at: ${new Date()}`); //Not cure if this one will work
log('Successfully published feed.');
log(`TXID: ${r.id}`);
log('-------------');
});
} else {
console.error('Failed to publish feed... neither signing key or wif are valid');
await this.login();
if (tries < retry_conf.feed_attempts) {
switchNode();
console.error(`Will retry in ${retry_conf.feed_delay} seconds`);
console.error('-------------');
return delay(retry_conf.feed_delay * 1000)
.then(() => this.publish_feed(rate, tries + 1))
.catch(console.error);
}
console.error(`Giving up. Tried ${tries} times`);
return reject(err);
}
} catch (e) {
console.error(e);
}
}
}
try {
var accountmgr = new HiveAcc(config.name, config.wif, config.signing_keys);
} catch (e) {
console.error(`A serious error occurred while checking your account: ${e}`);
process.exit(1);
}
var shouldPublish = settings.shouldPublish;
var dryRun = settings.dryRun;
var cap_sym = config.ex_symbol.toUpperCase(),
cap_comp = config.ex_compare.toUpperCase();
function get_price(callback) {
exchange.get_pair(config.ex_symbol, config.ex_compare,
(err, price) => callback(err, parseFloat(price))
);
}
function main() {
get_price((err, price) => {
if (err) {
return console.error('error loading prices, will retry later');
}
log(`${cap_sym}/${cap_comp} is: ${price.toFixed(3)} ${cap_comp} per ${cap_sym}`);
log('Attempting to publish feed...');
if (!dryRun) {
accountmgr.publish_feed(price);
} else {
console.log('Dry Run. Not actually publishing.');
}
});
}
function switchNode() {
let currentNode = config.alternate_nodes.shift()
hive.api.setOptions({url: currentNode})
config.alternate_nodes.push(currentNode)
console.log(`Switched node to ${currentNode}.`)
}
log('Attempting to login into account', config.name);
function startup(){
accountmgr.login().then((user_data) => {
var {username} = user_data;
log(`Successfully logged into ${username}`);
if (settings.publishOnce) {
log('Argument "publishonce" passed. Publishing immediately, then exiting.');
return main();
} else if (shouldPublish || dryRun) {
log(`Publishing immediately, then every ${config.interval} minute(s) `);
main();
} else {
log('Not publishing immediately');
log('If you want to update your price feed RIGHT NOW, use node app.js publishnow');
}
console.log();
// convert interval from minutes to ms
var interval = parseInt(config.interval) * 1000 * 60;
setInterval(() => main(), interval);
}).catch((e) => {
console.error(`An error occurred attempting to log into ${config.name}...`);
console.error(`Reason: ${e}`, e);
if (config.signing_keys === undefined || !Object.keys(config.signing_keys).length){
console.error('Exiting');
process.exit(1);
} else {
console.log(`Trying again in ${config.interval} minute(s)`);
setTimeout(() => {
startup();
}, 1000 * 60 * config.interval);
}
});
}
startup();