rsibot-utils 1.0.6 → 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 +20 -126
  2. package/index.js +159 -31
  3. package/package.json +1 -1
package/db.js CHANGED
@@ -1,10 +1,8 @@
1
1
  const mongoose = require('mongoose');
2
- const { DB_USER, DB_PASSWORD } = require('./config');
3
- const utils = require('./index.js');
2
+ const { DB_USER, DB_PASSWORD } = process.env;
4
3
 
5
4
  mongoose.Promise = global.Promise;
6
5
  const { Schema } = mongoose;
7
- // Create a schema
8
6
 
9
7
  const hotCoinsSchema = new Schema(
10
8
  {
@@ -34,8 +32,20 @@ const queueSchema = new Schema(
34
32
  versionKey: false,
35
33
  },
36
34
  );
37
- const {DB_USER, DB_PASSWORD} = process.env;
38
- // Create a model
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
+
39
49
  mongoose
40
50
  .connect(`mongodb+srv://${DB_USER}:${DB_PASSWORD}@rsi-bot.d6kbq.mongodb.net/rsi?retryWrites=true&w=majority`, {
41
51
  useNewUrlParser: true,
@@ -51,127 +61,11 @@ mongoose
51
61
  const HotCoin = mongoose.model('hot_coins', hotCoinsSchema);
52
62
  const ActiveCoin = mongoose.model('active_coins', activeSchema);
53
63
  const Queue = mongoose.model('queue_coins', queueSchema);
64
+ const Historical = mongoose.model('historical_coins', historicalSchema);
54
65
 
55
66
  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
- utils.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
- utils.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
- utils.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
- utils.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
- utils.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
- utils.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
- utils.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
- utils.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
- utils.write(error.stack, true);
154
- }
155
- },
156
- deleteHotCoins: async function () {
157
- try {
158
- await HotCoin.deleteMany({ symbol: { $exists: true } });
159
- } catch (error) {
160
- utils.write(error, true);
161
- }
162
- },
163
- deleteActiveCoins: async function () {
164
- try {
165
- await ActiveCoin.deleteMany({ symbol: { $exists: true } });
166
- } catch (error) {
167
- utils.write(error, true);
168
- }
169
- },
170
- deleteQueueCoins: async function () {
171
- try {
172
- await Queue.deleteMany({ created: { $exists: true } });
173
- } catch (error) {
174
- utils.write(error, true);
175
- }
176
- },
67
+ HotCoin,
68
+ ActiveCoin,
69
+ Queue,
70
+ Historical,
177
71
  };
package/index.js CHANGED
@@ -2,40 +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')
6
- const cname = process.env.CNAME;
7
- const LOGS_PATH = cname?`${cname}_logs.txt`:"commander_logs.txt";
5
+ const { HotCoin, ActiveCoin, Queue, Historical } = require('./db');
6
+ const CNAME = process.env.CNAME;
7
+ const LOGS_PATH = CNAME ? `${CNAME}_logs.txt` : 'commander_logs.txt';
8
8
 
9
- module.exports = (credentials) => {
10
-
11
- } {
12
- ...dbUtils,
13
- sortByPercent: function (a, b) {
14
- return b.percentChange - a.percentChange;
15
- },
16
- write: function (msg, isError = false) {
17
- let dateStr = new Date().toLocaleString('he-IL');
18
- 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]';
19
15
 
20
- fs.appendFileSync(path.resolve(LOGS_PATH), `${dateStr}-${prefixType} ${msg}\n`, 'utf8');
21
- },
22
- execute: async function (command) {
23
- 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) {
24
90
  try {
25
- const { error, stdout, stderr } = await exec(command);
26
- if (stderr) {
27
- console.log(`${stderr}`);
28
- return stderr;
29
- }
30
- // delete coin and archive data
31
- console.log(`${stdout}`);
32
- return stdout;
91
+ await Queue.deleteOne({ created: { $exists: true } });
92
+ const newQueue = new Queue({ queue });
93
+ await newQueue.save();
33
94
  } catch (error) {
34
- console.log(error);
35
- throw Error(error);
95
+ write(error.stack);
36
96
  }
37
- },
38
- getServiceName: function (symbol) {
39
- return `rsibot_${symbol.slice(0, -4)}_${symbol.slice(-4)}`.toLowerCase();
40
- },
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,
41
169
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rsibot-utils",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {