forked from PierrickLozach/napbots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
278 lines (228 loc) · 6.35 KB
/
Copy pathindex.js
File metadata and controls
278 lines (228 loc) · 6.35 KB
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
// DYNAMIC NAPBOTS ALLOCATION
//
// SEE README FOR DETAILS
const axios = require('axios').default;
const compositions = require('./compositions').compositions;
//#region Account details
require('dotenv').config();
let email = process.env.NAPBOTS_EMAIL;
if (!email) {
console.error('Missing NAPBOTS_EMAIL, please look at the README.md file to find out how to configure your environment before running this program.');
return;
}
console.log('email', email)
let password = process.env.NAPBOTS_PASSWORD;
if (!password) {
console.error('Missing NAPBOTS_PASSWORD, please look at the README.md file to find out how to configure your environment before running this program.');
return;
}
//#endregion
//#region Script (do not change)
const deepEqual = (x, y) => {
if (x === y) {
return true;
} else if (
typeof x == 'object' &&
x != null &&
typeof y == 'object' &&
y != null
) {
if (Object.keys(x).length != Object.keys(y).length) return false;
for (var prop in x) {
if (y.hasOwnProperty(prop)) {
if (!deepEqual(x[prop], y[prop])) return false;
} else return false;
}
return true;
} else return false;
};
const getCryptoWeater = async () => {
let weatherApi = await axios({
url: 'https://middle.napbots.com/v1/crypto-weather',
});
if (!weatherApi) {
console.error('No weather information found.');
return;
}
console.log('Current weather:', weatherApi.data.data.weather.weather);
return weatherApi.data.data.weather.weather;
};
const getAuthToken = async () => {
let loginResponse = await axios({
url: 'https://middle.napbots.com/v1/user/login',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
data: {
email: email,
password: password,
},
});
let authToken = loginResponse.data.data.accessToken;
if (!authToken) {
console.error('No Auth Token');
return;
}
return authToken;
};
const getCurrentAllocations = async (authToken, userId) => {
// Get current allocation for all exchanges
let currentAllocationResponse = await axios({
url: 'https://middle.napbots.com/v1/account/for-user/' + userId,
method: 'GET',
headers: {
'Content-Type': 'application/json',
token: authToken,
},
});
let currentAllocation = currentAllocationResponse.data.data;
// Rebuild exchanges array
let exchanges = [];
for (let index = 0; index < currentAllocation.length; index++) {
const allocation = currentAllocation[index];
if (!allocation.accountId || !allocation.compo || !allocation.tradingActive) {
continue; // Next
}
exchanges.push({
id: allocation.accountId,
compo: allocation.compo,
});
}
return exchanges;
};
const getUserId = async (authToken) => {
let userMeApi = await axios({
url: 'https://middle.napbots.com/v1/user/me',
method: 'GET',
headers: {
'Content-Type': 'application/json',
token: authToken,
},
});
if (!userMeApi) {
console.error('Failed to retrieve my user info.');
return;
}
console.log('User id:', userMeApi.data.data.userId);
return userMeApi.data.data.userId;
};
const main = async () => {
//#region Check command-line arguments
let args = process.argv;
let forcedComposition;
if (args.length > 2) {
switch (args[2]) {
case 'force_mild_bull':
forcedComposition = 'mild_bull';
}
}
//#endregion
let compositionToSet;
if (!forcedComposition) {
//#region Get weather
let weather;
try {
weather = await getCryptoWeater();
} catch (error) {
console.error(error);
return;
}
//#endregion
//#region Set composition based on weather
console.log('Compositions:', compositions)
switch (weather) {
case 'Extreme markets':
compositionToSet = compositions.extreme;
break;
case 'Mild bull markets':
compositionToSet = compositions.mild_bull;
break;
case 'Mild bear or range markets':
compositionToSet = compositions.mild_bear;
break;
default:
console.error('Unknown weather condition:', weather);
return;
}
} else {
console.log('Forcing composition to:', forcedComposition);
compositionToSet = compositions[forcedComposition];
}
//#endregion
//#region Login
let authToken;
console.log('Authenticating...');
try {
authToken = await getAuthToken();
} catch (error) {
console.error(error);
return;
}
// Get User Id
let userId;
try {
userId = await getUserId(authToken);
} catch (error) {
console.error(error);
return;
}
//#endregion
//#region Get Exchanges
let exchanges;
try {
exchanges = await getCurrentAllocations(authToken, userId);
} catch (error) {
console.error(error);
return;
}
//#endregion
//#region Update allocations
// For each exchange, update allocation if different from the current crypto weather
for (let index = 0; index < exchanges.length; index++) {
const exchange = exchanges[index];
let toUpdate = false;
// If leverage different, set to update
if (exchange.compo.leverage.toFixed(2) !== compositionToSet.leverage.toFixed(2)) {
console.log('=> Leverage is different');
toUpdate = true;
}
// If composition different, set to update
let equalCompos = deepEqual(exchange.compo.compo, compositionToSet.compo);
if (!equalCompos) {
console.log('=> Compositions are different');
toUpdate = true;
}
// If composition different, update allocation for this exchange
if (toUpdate) {
// Rebuild string for composition
let params = {
botOnly: compositionToSet.botOnly,
compo: {
leverage: compositionToSet.leverage.toFixed(2).toString(),
compo: compositionToSet.compo,
},
};
try {
console.log('Updating allocation to:', params);
await axios({
url: 'https://middle.napbots.com/v1/account/' + exchange.id,
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
token: authToken,
},
data: params,
});
console.log('Success!');
} catch (error) {
console.error(error.response ? error.response.data : error);
}
} else {
console.log('No updates are necessary.');
}
}
//#endregion
};
main();
//#endregion