This repository has been archived by the owner on Jun 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·468 lines (391 loc) · 13.9 KB
/
index.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env node
function requireEnv(key) {
const value = process.env[key];
if (!value) {
print(`$${key} required but not set.`);
process.exit(1);
}
return value;
}
const tele_token = requireEnv("TELEGRAM_TOKEN")
const nimiqx_token = requireEnv("NIMIQX_TOKEN");
const plotly_user = requireEnv("PLOTLY_USER");
const plotly_token = requireEnv("PLOTLY_TOKEN");
const TelegramBot = require('node-telegram-bot-api');
const request = require('request');
const Nimiq = require('@nimiq/core');
const plotly = require('plotly')(plotly_user, plotly_token);
const {Base64Decode} = require('base64-stream');
const svg2png = require("svg2png");
const bot = new TelegramBot(tele_token, {polling: true});
const nimiqx = url => new Promise((resolve, reject) => {
request(url + '?api_key=' + nimiqx_token, { json: true }, (err, res, body) => {
if (err)
reject(err);
else
resolve(body);
});
});
const nimiqWatch = url => new Promise((resolve, reject) => {
request(url, { json: true }, (err, _res, body) => {
if (err)
reject(err);
else
resolve(body);
});
});
function sendError(msg, err, customMsg) {
bot.sendMessage(msg.chat.id, customMsg || "I can't reach the Nimiq network :( @terorie, do something!");
console.log(err);
}
function humanHashes(bytes) {
let thresh = 1000;
if(Math.abs(bytes) < thresh) {
return bytes + ' H/s';
}
let units = ['kH/s','MH/s','GH/s','TH/s','PH/s','EH/s','ZH/s','YH/s'];
let u = -1;
do {
bytes /= thresh;
++u;
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1)+' '+units[u];
}
function humanNIM(nims) {
if (nims < 1)
return `${nims.toFixed(5)} NIM`;
if (nims < 1e5)
return `${nims.toFixed(3)} NIM`;
nims /= 1e6;
if (nims < 1e3)
return `${nims.toFixed(2)}m NIM`;
nims /= 1e3;
return `${nims.toFixed(0)}b NIM`;
}
bot.onText(/\/start/, (msg, match) => {
bot.sendMessage(msg.chat.id,
"Hey, I'm HexaPoolBot 👋 I provide information about the Nimiq network. Add me to a group to start 😌. Type / for a list of commands.");
});
// /balance
// Get the balance of an account
bot.onText(/\/balance( (.+))?/, async (msg, match) => {
if (!match[1]) {
bot.sendMessage(msg.chat.id, "I need an address to check 🙉\nExample: /balance NQ11 P00L 2HYP TUK8 VY6L 2N22 MMBU MHHR BSAA");
return;
}
let address;
try {
address = Nimiq.Address.fromUserFriendlyAddress(match[2]);
} catch (e) {
bot.sendMessage(msg.chat.id, "That's not a valid Nimiq address 🥺");
return;
}
const body = await nimiqx('https://api.nimiqx.com/account/' + encodeURI(address.toUserFriendlyAddress()));
if (!body.balance) {
bot.sendMessage(msg.chat.id, "I can't find that address 🤔");
return;
}
body.balance = Nimiq.Policy.satoshisToCoins(body.balance);
bot.sendMessage(msg.chat.id, "Balance: " + body.balance + " NIM");
});
async function printGlobalHashrate(msg) {
const hr = await nimiqx('https://api.nimiqx.com/network-stats/');
const caption = "Global hashrate: " + humanHashes(hr.hashrate);
const totalBlocks = (prev, e) => prev + e.blocks;
let entries = await nimiqx('https://api.nimiqx.com/hashing-distribution/24h');
const totalBlockCount = entries.reduce(totalBlocks);
console.log(totalBlockCount);
entries = entries.slice(0, 8);
const truncBlockCount = entries.reduce(totalBlocks);
const otherBlockCount = totalBlockCount - truncBlockCount;
const values = entries.map(e => e.blocks);
const labels = entries.map(e => e.label || e.address);
if (otherBlockCount > 0) {
values.push(otherBlockCount);
values.push("Other");
}
const figure = { 'data': [{
values: values,
labels: labels,
type: 'pie'
}]};
const opts = { width: 800, height: 500, format: 'png' };
const stream = await new Promise((resolve, reject) => {
plotly.getImage(figure, opts, (err, stream) => {
if (err) reject(err);
else resolve(stream);
});
});
await bot.sendPhoto(
msg.chat.id,
(new Base64Decode).pipe(stream),
{ caption: caption },
{ contentType: 'image/png' }
);
}
async function printHashrateOfAddress(msg, iban) {
let address;
try {
address = Nimiq.Address.fromUserFriendlyAddress(iban);
} catch (e) {
bot.sendMessage(msg.chat.id, "That's not a valid Nimiq address 🥺");
return;
}
try {
const stats = await nimiqx('https://api.nimiqx.com/network-stats/');
const hashrate = stats.hashrate;
const addresses = await nimiqx('https://api.nimiqx.com/hashing-distribution/24h');
let found;
for (entry of addresses) {
if (entry.address == address.toUserFriendlyAddress()) {
found = entry;
break;
}
}
if (!found) {
bot.sendMessage(msg.chat.id, "That address didn't mine any blocks this day 🤥. Note that pool hashrate is not yet displayed.");
return;
}
const blockSplit = entry.blocks / (24 * 60);
bot.sendMessage(msg.chat.id, "Hashrate: " + humanHashes(blockSplit * hashrate));
} catch (e) {
sendError(msg, e);
return;
}
}
// /hashrate
// Network hashrate (global or per address)
bot.onText(/\/hashrate( (.+))?/, async (msg, match) => {
if (!match[1] || match[2] == "global") {
await printGlobalHashrate(msg);
} else {
await printHashrateOfAddress(msg, match[2]);
}
});
// /whales
// Biggest accounts $$$ 🤩
bot.onText(/\/whales/, async (msg, match) => {
try {
const balances = await nimiqx('https://api.nimiqx.com/top-account-balances/10');
let text = "Biggest accounts 🤑\n\n";
for (const account of balances) {
const watchURL = `https://nimiq.watch/#${encodeURI(account.address)}`;
const balance = humanNIM(account.balance);
if (account.label) {
text += `[${account.label}](${watchURL}): ${balance}\n`
} else {
text += `[${account.address.substr(0, 15)}...](${watchURL}): ${balance}\n`
}
}
bot.sendMessage(msg.chat.id, text, {
parse_mode: 'Markdown',
disable_web_page_preview: true,
disable_notification: true,
});
} catch (e) {
sendError(msg, e);
return;
}
});
async function printGlobalProfit(msg) {
try {
const stats = await nimiqx('https://api.nimiqx.com/network-stats/');
const nimPerKh = parseFloat(stats.nim_day_kh);
let message = '';
message += `NIM / kH / day: ${nimPerKh} NIM\n`
message += `NIM / kH / week: ${humanNIM(nimPerKh * 7)}\n`
message += `NIM / kH / month: ${humanNIM(nimPerKh * 30.4375)}\n`
message += `Block reward: ${Nimiq.Policy.satoshisToCoins(stats.last_reward).toFixed(1)} NIM\n`
message += `Difficulty: ${stats.difficulty.toFixed(0)}\n`
message += `Global hashrate: ${humanHashes(stats.hashrate)}\n`
bot.sendMessage(msg.chat.id, message, {
parse_mode: 'Markdown',
disable_web_page_preview: true,
disable_notification: true,
});
} catch (e) {
sendError(msg, e);
return;
}
}
async function printProfitOfHashrate(msg, hrStr) {
try {
hrStr = hrStr.toLowerCase();
hrStr = hrStr.replace(/'`\/s h/g, '');
hrStr = hrStr.replace(/,/g, '.');
let mult;
if (hrStr.indexOf("k") != -1) {
mult = 1e3;
} else if (hrStr.indexOf("m") != -1) {
mult = 1e6;
} else if (hrStr.indexOf("g") != -1) {
mult = 1e9;
} else {
mult = 1;
}
hrStr = hrStr.replace(/kmg/g, '');
let hashrate;
try {
hashrate = parseInt(hrStr);
if (isNaN(hashrate))
throw new Error("nan");
} catch (e) {
bot.sendMessage(msg.chat.id, "I can't decipher that 😗 Example: /profit 420 kH/s");
return;
}
hashrate *= mult;
const stats = await nimiqx('https://api.nimiqx.com/network-stats/');
const price = await nimiqx('https://api.nimiqx.com/price/usd');
let message = '';
if (hashrate > stats.hashrate) {
message += 'Is that a 101 % attack?\n';
}
const nimPerKh = parseFloat(stats.nim_day_kh) * hashrate / 1000;
const nimDay = nimPerKh;
const nimWeek = nimPerKh * 7;
const nimMonth = nimPerKh * 30.4375;
price.usd = parseFloat(price.usd);
const format = nim => `${humanNIM(nim)} / $${(nim * price.usd).toFixed(2)}`;
message += `Day: ${format(nimDay)}\n`
message += `Week: ${format(nimWeek)}\n`
message += `Month: ${format(nimMonth)}\n`
message += `Blocks / day: ${((hashrate / stats.hashrate) * (60 * 24)).toFixed(4)}\n`
message += `Block reward: ${Nimiq.Policy.satoshisToCoins(stats.last_reward).toFixed(1)} NIM\n`
message += `Difficulty: ${stats.difficulty.toFixed(0)}\n`
message += `Global hashrate: ${humanHashes(stats.hashrate)}\n`
bot.sendMessage(msg.chat.id, message, {
parse_mode: 'Markdown',
disable_web_page_preview: true,
disable_notification: true,
});
} catch (e) {
sendError(msg, e);
return;
}
}
// /profit
// Check how much you earn by mining
bot.onText(/\/profit( (.+))?/, async (msg, match) => {
if (!match[1]) {
await printGlobalProfit(msg);
} else {
await printProfitOfHashrate(msg, match[2]);
}
});
function formatTable(rows, cols) {
const widths = {};
for (col of cols) {
let maxW = 0;
for (row of rows) {
if (row[col] > maxW)
maxW = row[col];
}
widths[col] = maxW;
}
let out;
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
for (let j = 0; j < cols.length; j++) {
const col = cols[j];
let entry = row[col];
for (let w = widths[col] - entry.length; w >= 0; w--)
entry += ' ';
out += entry;
if (j != cols.length - 1)
out += ' ';
}
if (i != rows.length - 1)
out += '\n';
}
return out;
}
// /supply
// Supply info
bot.onText(/\/supply/, async (msg, match) => {
try {
// Get current height from Nimiq Watch
const stats = await nimiqWatch("https://api.nimiq.watch/latest/1");
const height = stats[0].height;
const knownCoins = Nimiq.Policy.satoshisToCoins(Nimiq.Policy.supplyAfter(height));
const totalCoins = 21e9;
const percentage = 100 * (knownCoins / totalCoins);
const reward = Nimiq.Policy.satoshisToCoins(Nimiq.Policy.blockRewardAt(height));
let message = '';
message += `Known coins: ${humanNIM(knownCoins)} (${percentage.toFixed(1)} %)\n`
message += `Total supply: ${humanNIM(totalCoins)}\n`
message += `Left to mine: ${humanNIM(totalCoins - knownCoins)}\n`
message += `Last block reward: ${reward.toFixed(1)} NIM\n`
bot.sendMessage(msg.chat.id, message, {
parse_mode: 'Markdown',
disable_web_page_preview: true,
disable_notification: true,
});
} catch (e) {
sendError(msg, e);
return;
}
});
// /price
// Check the current NIM price
bot.onText(/\/price/, async (msg, match) => {
try {
const stats = await nimiqx('https://api.nimiqx.com/price/btc,usd,eur');
const lines = [
{ "code": "usd", "sym": "US$" },
{ "code": "eur", "sym": "€ " }
];
let message = '';
message += "```"
const btcPrice = parseFloat(stats["btc"])
message += '\n' + `1 NIM = ${(1000 * btcPrice).toFixed(5)} mBTC`;
message += '\n' + `1 BTC = ${humanNIM(1 / btcPrice)}`;
for (line of lines) {
line.value = parseFloat(stats[line.code]);
line.valueStr = `1 NIM = ${line.value.toFixed(5)} ${line.sym}`;
message += '\n' + line.valueStr;
line.price = 1 / line.value;
line.priceStr = `1 ${line.sym} = ${humanNIM(line.price)}`;
message += '\n' + line.priceStr;
}
//message += formatTable(lines, ["priceStr", "valueStr"])
message += "```"
bot.sendMessage(msg.chat.id, message, {
parse_mode: 'Markdown',
disable_web_page_preview: true,
disable_notification: true,
});
} catch (e) {
sendError(msg, e);
return;
}
});
// /iqon
// Generate your Nimiq iqon
bot.onText(/\/iqon( (.+))?/, async (msg, match) => {
if (!match[1]) {
bot.sendMessage(msg.chat.id, "You need to give me an address! Example: /iqon NQ11 P00L 2HYP TUK8 VY6L 2N22 MMBU MHHR BSAA");
return;
}
try {
const iban = match[2];
let addr;
try {
addr = Nimiq.Address.fromUserFriendlyAddress(iban);
} catch (e) {
bot.sendMessage(msg.chat.id, "That's not a valid Nimiq address 🥺");
return;
}
const iqonResp = await nimiqx(`https://api.nimiqx.com/iqon/${encodeURI(addr.toUserFriendlyAddress())}`);
const svgB64 = iqonResp.img_src.substr(26);
const svgBuf = Buffer.from(svgB64, 'base64');
const png = await svg2png(svgBuf, { width: 400, height: 400 });
await bot.sendSticker(
msg.chat.id,
png,
{ caption: addr.toUserFriendlyAddress() },
{ contentType: 'image/png' }
);
} catch (e) {
sendError(msg, e, "Encoder failed 🤦🏻 @terorie pls fix.");
}
});