This repository was archived by the owner on Jun 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
367 lines (332 loc) · 11.3 KB
/
client.js
File metadata and controls
367 lines (332 loc) · 11.3 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
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
const axios = require("axios").default;
const io = require("socket.io-client");
const errors = require("./errors");
const EventEmitter = require("events");
const pkg = require("../package.json");
exports.Client = class Client extends EventEmitter {
constructor() {
super();
/**
* The base API Url
* @type {String}
*/
this.API_URL = "https://api.unminable.com";
/**
* The payouts API Url
* @type {String}
*/
this.API_URL_PAYOUTS = "https://payouts.unmineable.com";
/**
* The WebSocket Url
* @type {String}
*/
this.WEBSOCKET_URL = "wss://ws.unminable.com";
}
/**
* Make request against the API
* @param {String} method request method
* @param {String} path API endpoint
* @param {Object} [options] request options
* @return {Promise} promise
* @private
*/
#request(method, version, path, options = {}) {
Object.assign(options, {
method: method,
url: options.url ? options.url : `${this.API_URL}/${version}/${path}`,
headers: {
"User-Agent": `unmineable.js ${pkg.version} (https://github.com/LockBlock-dev/unmineable.js)`,
"Content-Type": "application/json",
},
});
return axios(options)
.then((response) => {
if (typeof response.data === "object") {
return response.data;
} else {
try {
let data = JSON.parse(response.data);
return data;
} catch (err) {
throw new errors.ParseError(
response.data,
response.status,
options.method,
options.url
);
}
}
})
.catch((error) => {
if (error.type == "ParseError") {
return error;
} else {
return new errors.APIError(
error,
error.response,
error.response.status,
options.method,
options.url
);
}
});
}
//----- WS ------
/**
* Start the WebSocket.
* @param {String} uuid uuid
* @example start("0cc5691e-cedb-11eb-b8bc-0242ac130003")
*/
start(uuid) {
const socket = io(this.WEBSOCKET_URL, { path: "/", transports: ["websocket"] });
socket.on("connect", () => {
socket.emit("join_account", uuid);
});
socket.on("disconnect", (reason) => {
if (reason === "io server disconnect") {
// the disconnection was initiated by the server, you need to reconnect manually
socket.connect();
}
// else the socket will automatically try to reconnect
});
socket.on("hr_worker", (data) => {
this.emit("worker", {
name: data.n,
reportedHashrate: data.rh,
calculatedHashrate: data.ch,
lastShareAt: data.b,
online: data.o,
});
});
socket.on("stats_paid", (data) => {
/**
* Emits the total paid by the pool in BTC.
* @event Client#total_paid
* @type {Object}
* @property {number} total BTC total
*/
this.emit("total_paid", { total: parseFloat(data) });
});
socket.on("stats_workers", (data) => {
/**
* Emits the total of active workers on the pool.
* @event Client#total_workers
* @type {Object}
* @property {number} total BTC total
*/
this.emit("total_workers", { total: data });
});
socket.on("payment_status", (data) => {
this.emit("payment_status", data);
});
socket.on("reward", (data) => {
this.emit("reward", data);
});
socket.on("refresh", (data) => {
this.emit("refresh", data);
});
socket.on("stats_ethash_last_reward", (data) => {
/**
* Emits when the pool gets a new ethash block reward.
* @event Client#ethash_last_reward
* @type {Object}
* @property {number} timestamp
*/
this.emit("ethash_last_reward", { timestamp: data });
});
socket.on("stats_etchash_last_reward", (data) => {
/**
* Emits when the pool gets a new etchash block reward.
* @event Client#etchash_last_reward
* @type {Object}
* @property {number} timestamp
*/
this.emit("etchash_last_reward", { timestamp: data });
});
socket.on("stats_randomx_last_reward", (data) => {
/**
* Emits when the pool gets a new randomx block reward.
* @event Client#randomx_last_reward
* @type {Object}
* @property {number} timestamp
*/
this.emit("randomx_last_reward", { timestamp: data });
});
socket.on("stats_x16rv2_last_reward", (data) => {
/**
* Emits when the pool gets a new RVN block reward.
* @event Client#x16rv2_last_reward
* @type {Object}
* @property {number} timestamp
*/
this.emit("x16rv2_last_reward", { timestamp: data });
});
socket.on("stats_autolykos_last_reward", (data) => {
/**
* Emits when the pool gets a new autolykos block reward.
* @event Client#autolykos_last_reward
* @type {Object}
* @property {number} timestamp
*/
this.emit("autolykos_last_reward", { timestamp: data });
});
}
//----- WEB API ------
/**
* Get the pool stats.
* @example stats()
* @return {Promise<Object>} promise
*/
stats() {
return this.#request("GET", "v3", "stats");
}
/**
* Get the pool info.
* @example pool()
* @return {Promise<Object>} promise
*/
pool() {
return this.#request("GET", "v4", "pool");
}
/**
* Get coins list | a coin info.
* @param {String} [coin] coin symbol
* @example coin("ETH")
* @return {Promise<Object>} promise
*/
coins(coin = "") {
return this.#request("GET", "v4", `coin/${coin}`);
}
/**
* Get the website | miner version.
* @param {Object} params query params
* @param {Object} params.type type:
* - website
* - miner
* @example version({ type: "website" })
* version({ type: "miner" })
* @return {Promise<Object>} promise
*/
version(params = {}) {
if (params.type == "website") return this.#request("GET", "v3", "site/version");
if (params.type == "miner") return this.#request("GET", "v3", "miner/version");
}
/**
* Get the pool news.
* @example news()
* @return {Promise<Object>} promise
*/
news() {
return this.#request("GET", "v3", "site/notice?site=start");
}
/**
* Get a wallet | account info.
* @param {Object} params query params
* @param {String} [params.address] wallet address
* @param {String} [params.uuid] uuid
* @param {String} [params.coin] coin symbol
* @example wallet({ address: "0xfd2D76F7Cf04863F2B221E56Af6fF94105EC2e5e", coin: "ETH" })
* wallet({ uuid: "0cc5691e-cedb-11eb-b8bc-0242ac130003" })
* @return {Promise<Object>} promise
*/
wallet(params = {}) {
if (params.uuid) return this.#request("GET", "v4", `account/${params.uuid}`);
if (params.address)
return this.#request("GET", "v4", `address/${params.address}?coin=${params.coin}`);
}
/**
* Get an account stats.
* @param {String} uuid uuid
* @example accountStats("0cc5691e-cedb-11eb-b8bc-0242ac130003")
* @return {Promise<Object>} promise
*/
accountStats(uuid) {
return this.#request("GET", "v4", `account/${uuid}/stats`);
}
/**
* Get a coin payouts.
* @param {String} uuid uuid
* @param {Object} params query parameters
* @param {Number} [params.page=1] payouts page
* @example payouts("0cc5691e-cedb-11eb-b8bc-0242ac130003", { page: 2 })
* @return {Promise<Object>} promise
*/
payouts(uuid, params = {}) {
params = new URLSearchParams(params);
return this.#request("GET", "v4", `account/${uuid}/payments?${params.toString()}`);
}
/**
* Get your referral code.
* @param {String} uuid uuid
* @example referral("0cc5691e-cedb-11eb-b8bc-0242ac130003")
* @return {Promise<Object>} promise
*/
referral(uuid) {
return this.#request("GET", "v4", `account/${uuid}/referrals`);
}
/**
* Get your workers stats.
* @param {String} uuid uuid
* @example workers("0cc5691e-cedb-11eb-b8bc-0242ac130003")
* @return {Promise<Object>} promise
*/
workers(uuid) {
return this.#request("GET", "v4", `account/${uuid}/workers`);
}
/**
* Get calculated reward for a coin.
* @param {Object} body request body
* @param {String} body.algo algorithms:
* - ethash
* - etchash
* - kawpow
* - randomx
* - autolykos
* @param {String} body.coin coin symbol
* @param {Number} body.hashrate hashrate for this algorithm
* - ethash, etchash, x16rv2: MH/s
* - randomx: H/s
* @example calculate({ algo: "ethash", coin: "DOGE", hashrate: 30 })
* @return {Promise<Object>} promise
*/
calculate(body = {}) {
body.mh = body.hashrate.toString();
delete body.hashrate;
return this.#request("POST", "v3", "calculate/reward", { data: body });
}
/**
* Set settings for a coin (1 at a time).
* @param {Object} body request body
* @param {String} body.uuid uuid
* @param {Boolean} [body.autoPay=false] auto pay setting
* @param {String} body.network network setting
* - ETH
* - BSC
* - ARBITRUM
* @example settings({ uuid: "0cc5691e-cedb-11eb-b8bc-0242ac130003", autoPay: true })
* settings({ uuid: "0cc5691e-cedb-11eb-b8bc-0242ac130003", network: "BSC" })
* @return {Promise<Object>} promise
*/
settings(body = {}) {
if (body.autoPay) {
return this.#request("POST", "v1", `address/${body.uuid}/setting/auto_pay`, {
data: { setting: body.autoPay },
});
} else if (body.network) {
return this.#request("POST", null, null, {
url: `${this.API_URL_PAYOUTS}/v3/address/${body.uuid}/switch?network=${body.network}`,
});
}
}
/**
* Ask a payment for a coin.
* @param {String} uuid uuid
* @example pay("0cc5691e-cedb-11eb-b8bc-0242ac130003")
* @return {Promise<Object>} promise
*/
pay(uuid) {
return this.#request("POST", null, null, {
url: `${this.API_URL_PAYOUTS}/v3/address/${uuid}/pay`,
});
}
};