rsibot-utils 1.0.8 → 1.0.9

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