-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
94 lines (75 loc) · 2.17 KB
/
index.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
"use strict";
const fs = require("fs");
const merge = require("merge");
const Logger = require("./lib/Logger");
const Bot = require("./lib/Bot");
const log = new Logger({
format: [
"{{timestamp}} <{{title}}> {{message}}",
{
error: "{{timestamp}} <{{title}}> {{message}} ({{file}}:{{method}}:{{line}}:{{pos}})",
},
],
dateformat: "mm-dd HH:MM:ss.L",
});
function prepareConfig(rawConfig) {
if(!rawConfig) {
throw new Error("Config file not found");
}
const requiredProps = ["username", "password", "shared_secret", "identity_secret"];
const defaultProps = {
"auto_confirm": true,
"accept_gifts": true, // can be true|false|null. On null incoming gifts are ignored
"accept_incoming": null // can be true|false|null. On null incoming trades except gifts are ignored
};
let config = JSON.parse(rawConfig);
if(!Array.isArray(config)) {
config = [config];
}
for(let i = 0; i < config.length; i++) {
let conf = config[i];
requiredProps.forEach((prop) => {
if(!conf.hasOwnProperty(prop) || !conf[prop].length) {
throw new Error("Config should contain non-empty field " + prop);
}
});
config[i] = merge({}, defaultProps, conf);
}
return config;
}
function startNextBot() {
let conf = config[conf_i];
if(!conf) {
log.info("# All bots checked");
return;
}
bot = new Bot(conf, log);
bot.start();
conf_i++;
let checker = setInterval(() => {
if(bot._initialized && bot.hasNoConfirmations()) {
clearInterval(checker);
bot.stop();
log.info("Switching bot..\n");
startNextBot();
}
}, 1000);
}
function startOneBot() {
bot = new Bot(config[0], log);
bot.start();
}
let rawConfig = fs.readFileSync("./config.json");
let config = prepareConfig(rawConfig);
log.info("Config loaded");
let conf_i = 0, bot;
if(config.length > 1) {
startNextBot();
} else {
startOneBot();
}
process.on("SIGINT", function() {
log.info("Stopping bot..");
bot.stop();
setTimeout(process.exit, 1000, 0);
});