rsibot-utils 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/config.js +5 -0
- package/db.js +177 -0
- package/index.js +39 -0
- package/package.json +15 -0
package/config.js
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
module.exports.APIKEY = '09R8VYlX2u5k38wx3HyJdAeR9lrwnepzqwogKyXLLexuJhxNjWZPIUPNweiGqla8';
|
2
|
+
module.exports.APISECRET = 'Iy4k6ocN6GUaxMuLTHANS7DKJbI4hYgCz6gvYwiPq7TYbfBSycNah43zSAAq3dlY';
|
3
|
+
module.exports.DB_USER = 'shalom';
|
4
|
+
module.exports.DB_PASSWORD = 'AaSsDdFf1234';
|
5
|
+
|
package/db.js
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
const mongoose = require('mongoose');
|
2
|
+
const { DB_USER, DB_PASSWORD } = require('./config');
|
3
|
+
const { write } = require('./utils');
|
4
|
+
|
5
|
+
mongoose.Promise = global.Promise;
|
6
|
+
const { Schema } = mongoose;
|
7
|
+
// Create a schema
|
8
|
+
|
9
|
+
const hotCoinsSchema = new Schema(
|
10
|
+
{
|
11
|
+
symbol: { type: String, unique: true },
|
12
|
+
},
|
13
|
+
{
|
14
|
+
versionKey: false,
|
15
|
+
},
|
16
|
+
);
|
17
|
+
const activeSchema = new Schema(
|
18
|
+
{
|
19
|
+
symbol: { type: String, unique: true },
|
20
|
+
inPosition: { type: Boolean },
|
21
|
+
lastBuy: { type: Number },
|
22
|
+
abandoned: { type: Boolean },
|
23
|
+
},
|
24
|
+
{
|
25
|
+
versionKey: false,
|
26
|
+
},
|
27
|
+
);
|
28
|
+
const queueSchema = new Schema(
|
29
|
+
{
|
30
|
+
queue: [],
|
31
|
+
created: { type: Date, default: Date.now },
|
32
|
+
},
|
33
|
+
{
|
34
|
+
versionKey: false,
|
35
|
+
},
|
36
|
+
);
|
37
|
+
|
38
|
+
// Create a model
|
39
|
+
mongoose
|
40
|
+
.connect(`mongodb+srv://${DB_USER}:${DB_PASSWORD}@rsi-bot.d6kbq.mongodb.net/rsi?retryWrites=true&w=majority`, {
|
41
|
+
useNewUrlParser: true,
|
42
|
+
useUnifiedTopology: true,
|
43
|
+
})
|
44
|
+
.then((connection) => {
|
45
|
+
console.log('DB is connected');
|
46
|
+
})
|
47
|
+
.catch((err) => {
|
48
|
+
console.log(err);
|
49
|
+
});
|
50
|
+
|
51
|
+
const HotCoin = mongoose.model('hot_coins', hotCoinsSchema);
|
52
|
+
const ActiveCoin = mongoose.model('active_coins', activeSchema);
|
53
|
+
const Queue = mongoose.model('queue_coins', queueSchema);
|
54
|
+
|
55
|
+
module.exports = {
|
56
|
+
insertHotCoin: async function ({ symbol }) {
|
57
|
+
try {
|
58
|
+
const newHotCoin = new HotCoin({ symbol });
|
59
|
+
await newHotCoin.save();
|
60
|
+
return symbol;
|
61
|
+
} catch (error) {
|
62
|
+
console.error(error.stack);
|
63
|
+
write(error.stack, true);
|
64
|
+
return false;
|
65
|
+
}
|
66
|
+
},
|
67
|
+
getHotCoins: async function () {
|
68
|
+
try {
|
69
|
+
const results = await HotCoin.find();
|
70
|
+
return results;
|
71
|
+
} catch (error) {
|
72
|
+
console.error(error.stack);
|
73
|
+
write(error.stack, true);
|
74
|
+
return [];
|
75
|
+
}
|
76
|
+
},
|
77
|
+
removeHotCoin: async function (symbol) {
|
78
|
+
try {
|
79
|
+
await HotCoin.deleteOne({ symbol });
|
80
|
+
return symbol;
|
81
|
+
} catch (error) {
|
82
|
+
console.error(error.stack);
|
83
|
+
write(error.stack, true);
|
84
|
+
return false;
|
85
|
+
}
|
86
|
+
},
|
87
|
+
|
88
|
+
getActiveCoins: async function () {
|
89
|
+
try {
|
90
|
+
const results = await ActiveCoin.find();
|
91
|
+
return results;
|
92
|
+
} catch (error) {
|
93
|
+
console.error(error.stack);
|
94
|
+
write(error.stack, true);
|
95
|
+
return [];
|
96
|
+
}
|
97
|
+
},
|
98
|
+
getQueue: async function () {
|
99
|
+
try {
|
100
|
+
const results = await Queue.find();
|
101
|
+
if (results[0]) {
|
102
|
+
return results[0].queue;
|
103
|
+
} else {
|
104
|
+
return [];
|
105
|
+
}
|
106
|
+
} catch (error) {
|
107
|
+
console.error(error.stack);
|
108
|
+
write(error.stack, true);
|
109
|
+
return [];
|
110
|
+
}
|
111
|
+
},
|
112
|
+
setQueue: async function (queue) {
|
113
|
+
if (queue.length > 0) {
|
114
|
+
try {
|
115
|
+
await Queue.deleteOne({ created: { $exists: true } });
|
116
|
+
const newQueue = new Queue({ queue });
|
117
|
+
await newQueue.save();
|
118
|
+
} catch (error) {
|
119
|
+
console.error(error.stack);
|
120
|
+
write(error.stack, true);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
},
|
124
|
+
removeCoin: async function (symbol, queue) {
|
125
|
+
try {
|
126
|
+
await ActiveCoin.deleteOne({ symbol });
|
127
|
+
await Queue.deleteOne({ created: { $exists: true } });
|
128
|
+
const modifiedQueue = queue.filter((q) => q.symbol !== symbol);
|
129
|
+
const newQueue = new Queue({ queue: modifiedQueue });
|
130
|
+
await newQueue.save();
|
131
|
+
} catch (error) {
|
132
|
+
write(error, true);
|
133
|
+
}
|
134
|
+
},
|
135
|
+
initQueueListener: function () {
|
136
|
+
return Queue.watch();
|
137
|
+
},
|
138
|
+
getAbandonedCoins: async function () {
|
139
|
+
try {
|
140
|
+
const abandonedCoins = await ActiveCoin.find({ abandoned: true });
|
141
|
+
return abandonedCoins;
|
142
|
+
} catch (error) {
|
143
|
+
write(error, true);
|
144
|
+
return [];
|
145
|
+
}
|
146
|
+
},
|
147
|
+
createNewHistory: async function (coinObj) {
|
148
|
+
try {
|
149
|
+
const newHistory = new History(coinObj);
|
150
|
+
await newHistory.save();
|
151
|
+
} catch (error) {
|
152
|
+
console.error(error.stack);
|
153
|
+
write(error.stack, true);
|
154
|
+
}
|
155
|
+
},
|
156
|
+
deleteHotCoins: async function () {
|
157
|
+
try {
|
158
|
+
await HotCoin.deleteMany({ symbol: { $exists: true } });
|
159
|
+
} catch (error) {
|
160
|
+
write(error, true);
|
161
|
+
}
|
162
|
+
},
|
163
|
+
deleteActiveCoins: async function () {
|
164
|
+
try {
|
165
|
+
await ActiveCoin.deleteMany({ symbol: { $exists: true } });
|
166
|
+
} catch (error) {
|
167
|
+
write(error, true);
|
168
|
+
}
|
169
|
+
},
|
170
|
+
deleteQueueCoins: async function () {
|
171
|
+
try {
|
172
|
+
await Queue.deleteMany({ created: { $exists: true } });
|
173
|
+
} catch (error) {
|
174
|
+
write(error, true);
|
175
|
+
}
|
176
|
+
},
|
177
|
+
};
|
package/index.js
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
const fs = require('fs');
|
2
|
+
const path = require('path');
|
3
|
+
const util = require('util');
|
4
|
+
const exec = util.promisify(require('child_process').exec);
|
5
|
+
const dbUtils = require('./db')
|
6
|
+
const cname = process.env.CNAME;
|
7
|
+
const LOGS_PATH = cname?`${cname}_logs.txt`:"commander_logs.txt";
|
8
|
+
|
9
|
+
module.exports = {
|
10
|
+
...dbUtils,
|
11
|
+
sortByPercent: function (a, b) {
|
12
|
+
return b.percentChange - a.percentChange;
|
13
|
+
},
|
14
|
+
write: function (msg, isError = false) {
|
15
|
+
let dateStr = new Date().toLocaleString('he-IL');
|
16
|
+
let prefixType = isError ? '[ERROR]' : '[LOG]';
|
17
|
+
|
18
|
+
fs.appendFileSync(path.resolve(LOGS_PATH), `${dateStr}-${prefixType} ${msg}\n`, 'utf8');
|
19
|
+
},
|
20
|
+
execute: async function (command) {
|
21
|
+
console.log(`$ ${command}`);
|
22
|
+
try {
|
23
|
+
const { error, stdout, stderr } = await exec(command);
|
24
|
+
if (stderr) {
|
25
|
+
console.log(`${stderr}`);
|
26
|
+
return stderr;
|
27
|
+
}
|
28
|
+
// delete coin and archive data
|
29
|
+
console.log(`${stdout}`);
|
30
|
+
return stdout;
|
31
|
+
} catch (error) {
|
32
|
+
console.log(error);
|
33
|
+
throw Error(error);
|
34
|
+
}
|
35
|
+
},
|
36
|
+
getServiceName: function (symbol) {
|
37
|
+
return `rsibot_${symbol.slice(0, -4)}_${symbol.slice(-4)}`.toLowerCase();
|
38
|
+
},
|
39
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
{
|
2
|
+
"name": "rsibot-utils",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"keywords": [],
|
10
|
+
"author": "",
|
11
|
+
"license": "ISC",
|
12
|
+
"dependencies": {
|
13
|
+
"mongoose": "^6.2.9"
|
14
|
+
}
|
15
|
+
}
|