-
Notifications
You must be signed in to change notification settings - Fork 1
/
NiceHashJs.js
370 lines (328 loc) · 10.5 KB
/
NiceHashJs.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const _ = require('lodash');
const axios = require('axios');
const pkg = require('./package.json');
const API_BASE_URL = 'https://api.nicehash.com/api';
const axiosConfig = {
baseURL: API_BASE_URL,
timeout: 1000,
headers: {
'user-agent': `NiceHashJs/${pkg.version} (https://github.com/dannychua/nicehashjs)`
}
}
const LOCATIONS = {
0: 'Europe',
1: 'USA',
2: 'Hong Kong',
3: 'Japan'
};
const ALGORITHMS = {
0: 'Scrypt',
1: 'SHA256',
2: 'ScryptNf',
3: 'X11',
4: 'X13',
5: 'Keccak',
6: 'X15',
7: 'Nist5',
8: 'NeoScrypt',
9: 'Lyra2RE',
10: 'WhirlpoolX',
11: 'Qubit',
12: 'Quark',
13: 'Axiom',
14: 'Lyra2REv2',
15: 'ScryptJaneNf16',
16: 'Blake256r8',
17: 'Blake256r14',
18: 'Blake256r8vnl',
19: 'Hodl',
20: 'DaggerHashimoto',
21: 'Decred',
22: 'CryptoNight',
23: 'Lbry',
24: 'Equihash',
25: 'Pascal',
26: 'X11Gost',
27: 'Sia',
28: 'Blake2s',
29: 'Skunk',
30: 'CryptoNightV7',
31: 'CryptoNightHeavy',
32: 'Lyra2Z',
33: 'X16R',
34: 'CryptoNightV8',
35: 'SHA256AsicBoost',
36: 'Zhash',
37: 'Beam',
38: 'GrinCuckaroo29',
39: 'GrinCuckatoo31',
40: 'Lyra2REv3',
41: 'MTP',
42: 'CryptoNightR',
43: 'CuckooCycle',
};
ALGORITHM_UNITS = {
0: 'MH/s',
1: 'TH/s',
2: 'MH/s',
3: 'MH/s',
4: 'MH/s',
5: 'MH/s',
6: 'MH/s',
7: 'MH/s',
8: 'MH/s',
9: 'MH/s',
10: 'MH/s',
11: 'MH/s',
12: 'MH/s',
13: 'kH/s',
14: 'MH/s',
15: 'kH/s',
16: 'GH/s',
17: 'GH/s',
18: 'GH/s',
19: 'kH/s',
20: 'MH/s',
21: 'GH/s',
22: 'kH/s',
23: 'GH/s',
24: 'Sol/s',
25: 'GH/s',
26: 'MH/s',
27: 'GH/s',
28: 'GH/s',
29: 'MH/s',
30: 'kH/s',
31: 'kH/s',
32: 'MH/s',
33: 'MH/s',
34: 'kH/s',
35: 'TH/s',
36: 'Sol/s',
37: 'Sol/s',
38: 'G/s',
39: 'G/s',
40: 'MH/s',
41: 'MH/s',
42: 'kH/s',
43: 'G/s',
}
const ORDER_TYPES = {
0: 'standard',
1: 'fixed'
};
class NiceHashClient {
/**
* Creates a new client
* @param options Object
* @param options.apiId String - API ID
* @param options.apiKey String - API Key (note: Do not use read-only)
*/
constructor(options) {
this.apiId = _.get(options, 'apiId');
this.apiKey = _.get(options, 'apiKey');
this.axios = axios.create(axiosConfig);
}
hasAuthTokens() {
return !!this.apiId && !!this.apiKey;;
}
getAuthParams() {
return { id: this.apiId, key: this.apiKey };
}
getRequestPromise(methodName, queryParams) {
const methodObj = { method: methodName };
const payload = _.merge({}, {params: _.merge(methodObj, queryParams || {})});
return this.axios.get('', payload)
}
// AUTHENTICATED API ENDPOINTS
/**
* Get all orders for certain algorithm owned by the customer. Refreshed every 30 seconds.
* @param location - 0 for Europe (NiceHash), 1 for USA (WestHash).
* @param algo - Algorithm marked with ID
*/
getMyOrders(location, algo) {
const params = _.merge({location, algo, my: ''}, this.getAuthParams());
return this.getRequestPromise('orders.get', params);
}
/**
* Create new order. Only standard orders can be created with use of API.
* @param orderOptions Object
* @param orderOptions.location Number - 0 for Europe (NiceHash), 1 for USA (WestHash).
* @param orderOptions.algo Number - Algorithm marked with ID.
* @param orderOptions.amount Number - Pay amount in BTC.
* @param orderOptions.price Number - Price in BTC/GH/Day or BTC/TH/Day.
* @param orderOptions.limit Number - Speed limit in GH/s or TH/s (0 for no limit).
* @param orderOptions.code - Required code if 2FA is enabled
* @param orderOptions.pool_host String - Pool hostname or IP.
* @param orderOptions.pool_port String - Pool port.
* @param orderOptions.pool_user String - Pool username.
* @param orderOptions.pool_pass String - Pool password.
*/
createOrder(orderOptions) {
const params = _.merge(orderOptions, this.getAuthParams());
return this.getRequestPromise('orders.create', params);
}
/**
* Refill order with extra Bitcoins.
* @param orderOptions Object
* @param orderOptions.location Number - 0 for Europe (NiceHash), 1 for USA (WestHash).
* @param orderOptions.algo Number - Algorithm marked with ID.
* @param orderOptions.order Number - Existing orderId.
* @param orderOptions.amount Number - Pay amount in BTC.
*/
refillOrder(orderOptions) {
const params = _.merge(orderOptions, this.getAuthParams());
return this.getRequestPromise('orders.refill', params);
}
/**
* Remove existing order.
* @param orderOptions Object
* @param orderOptions.location Number - 0 for Europe (NiceHash), 1 for USA (WestHash).
* @param orderOptions.algo Number - Algorithm marked with ID.
* @param orderOptions.order Number - Existing orderId.
*/
removeOrder(orderOptions) {
const params = _.merge(orderOptions, this.getAuthParams());
return this.getRequestPromise('orders.remove', params);
}
/**
* Set new price for the existing order. Only increase is possible.
* @param orderOptions Object
* @param orderOptions.location Number - 0 for Europe (NiceHash), 1 for USA (WestHash).
* @param orderOptions.algo Number - Algorithm marked with ID.
* @param orderOptions.order Number - Existing orderId.
* @param orderOptions.price Number - Price in BTC/GH/Day or BTC/TH/Day
*/
setOrderPrice(orderOptions) {
const params = _.merge(orderOptions, this.getAuthParams());
return this.getRequestPromise('orders.set.price', params);
}
/**
* Decrease price for the existing order. Price decrease possible every 10 minutes.
* @param orderOptions Object
* @param orderOptions.location Number - 0 for Europe (NiceHash), 1 for USA (WestHash).
* @param orderOptions.algo Number - Algorithm marked with ID.
* @param orderOptions.order Number - Existing orderId.
*/
decreaseOrderPrice(orderOptions) {
const params = _.merge(orderOptions, this.getAuthParams());
return this.getRequestPromise('orders.set.price.decrease', params);
}
/**
* Set new limit for existing order
* @param orderOptions Object
* @param orderOptions.location Number - 0 for Europe (NiceHash), 1 for USA (WestHash).
* @param orderOptions.algo Number - Algorithm marked with ID.
* @param orderOptions.order Number - Existing orderId.
* @param orderOptions.limit Number - Speed limit in GH/s or TH/s (0 for no limit).
*/
setOrderLimit(orderOptions) {
const params = _.merge(orderOptions, this.getAuthParams());
return this.getRequestPromise('orders.set.limit', params);
}
getMyBalance() {
return this.getRequestPromise('balance', this.getAuthParams());
}
// PUBLIC API ENDPOINTS
static getApiVersion() {
return axios.get(API_BASE_URL, axiosConfig);
}
static getNiceHashAlgorithmNumberByName(algoName) {
return _.findKey(ALGORITHMS, (i) => { return i === algoName }) || null;
}
static getAlgorithmNameByNiceHashNumber(number) {
return ALGORITHMS[number] || null;
}
static getLocationNameByNiceHashNumber(number) {
return LOCATIONS[number] || null;
}
static getAlgorithmUnitsByNiceHashNumber(number) {
return ALGORITHM_UNITS[number] || null;
}
/**
* Get current profitability (price) and hashing speed for all algorithms. Refreshed every 30 seconds
* @param location {Number} - Location, 0 for Europe, 1 for USA. Optional
* @return {*}
*/
getGlobalCurrentStats(location) {
return this.getRequestPromise('stats.global.current', location ? {location} : null);
}
/**
* Get average profitability (price) and hashing speed for all algorithms in past 24 hours.
* @return {*}
*/
getGlobal24hStats() {
return this.getRequestPromise('stats.global.24h');
}
/**
* Get current stats for provider for all algorithms. Refreshed every 30 seconds. It also returns past 56 payments.
* @param addr
* @return {*}
*/
getProviderStats(addr) {
return this.getRequestPromise('stats.provider', { addr });
}
/**
* Get details stats for provider for all algorithms including history data and past 56 payments
* @param addr String - Providers BTC address
* @param from String['0'] - Get history from this time (Unix timestamp)
* @return {*}
*/
getDetailedProviderStats(addr, from) {
if (!from) {
from = '0';
}
return this.getRequestPromise('stats.provider.ex', { addr, from });
}
/**
* Get detailed stats for provider's workers (rigs).
* @param addr String - Provider's BTC Address
* @param algo Number - Algorithm marked with ID
* @return {*}
*/
getProviderWorkersStats(addr, algo) {
return this.getRequestPromise('stats.provider.workers', { addr, algo });
}
/**
* Get detailed stats for provider's workers (rigs).
* @param addr String - Provider's BTC Address
* @param algo Number - Algorithm marked with ID
* @return {*}
*/
getAllProviderWorkersStats(addr) {
return this.getRequestPromise('stats.provider.workers', { addr });
}
/**
* Get all orders for certain algorithm. Refreshed every 30 seconds.
* @param location Number - 0 for Europe (NiceHash), 1 for USA (WestHash)
* @param algo Number - Algorithm marked with ID
* @return {*}
*/
getOrders(location, algo) {
return this.getRequestPromise('orders.get', { location, algo });
}
/**
* Get information about Multi-Algorithm Mining.
* @return {*}
*/
getMultiAlgorithmMiningInfo() {
return this.getRequestPromise('multialgo.info');
}
/**
* Get information about Simple Multi-Algorithm Mining.
* More here: https://www.nicehash.com/?p=simplemultialgo
* @return {*}
*/
getSimpleMultiAlgorithmMiningInfo() {
return this.getRequestPromise('simplemultialgo.info');
}
/**
* Get needed information for buying hashing power using NiceHashBot.
* https://github.com/nicehash/NiceHashBot
* @return {*}
*/
getNeededBuyingInfo() {
return this.getRequestPromise('buy.info');
}
}
module.exports = NiceHashClient;