-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
838 lines (684 loc) · 28.7 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
const MongoDB = require('./Services/MongoDB');
const TheGraph = require('./Services/TheGraph');
const Coingecko = require('./Services/Coingecko');
const Twitter = require('./Services/Twitter');
const Etherscan = require('./Services/Etherscan');
const DailyStatHandler = require('./Handlers/DailyStatHandler');
const h = require('./Helpers/helpers');
const p = require('./Helpers/prices');
const fetchRetry = h.fetchRetry;
const FETCH_SIZE = h.FETCH_SIZE;
const sleep = h.sleep;
const log = h.log;
const CONFIG = h.CONFIG;
var DEBUG = CONFIG.debug;
const TheGraph_ETHEREUM = new TheGraph(h.ETHEREUM);
const TheGraph_PULSECHAIN = new TheGraph(h.PULSECHAIN);
const MongoDB_ETHEREUM = new MongoDB(h.ETHEREUM);
const MongoDB_PULSECHAIN = new MongoDB(h.PULSECHAIN);
const DailyStatHandler_ETHEREUM = new DailyStatHandler(h.ETHEREUM);
const DailyStatHandler_PULSECHAIN = new DailyStatHandler(h.PULSECHAIN);
const http = require('http');
require('es6-promise').polyfill();
const express = require('express');
const path = require('path');
const fs = require('fs');
const https = require('https');
const schedule = require('node-schedule');
var cron = require('node-cron');
var cors = require('cors');
const { JSDOM } = require( "jsdom" );
const { window } = new JSDOM( "" );
const $ = require( "jquery" )( window );
var hexPrice = '';
var connections = {};
var currentDayGlobal = 0;
var getAndSet_currentGlobalDay_Running = false;
var liveData = undefined;
var getLiveDataRUNNING = false;
var ethereumData = undefined;
var getEthereumDataRUNNING = false;
var currencyRates = undefined;
var getCurrencyDataRunning = false;
var DailyStatMaintenance_ETHEREUM = false;
var DailyStatMaintenance_PULSECHAIN = false;
var rowData = undefined;
var rowData_PULSECHAIN = undefined;
var rowDataObjects = undefined;
var rowDataObjects_PULSECHAIN = undefined;
var hexSiteData = undefined;
async function getRowData() {
if (MongoDB_ETHEREUM && !MongoDB_ETHEREUM.getRowDataRunning){
returnPackage = await MongoDB_ETHEREUM.getRowData();
rowData = returnPackage.rowData;
rowDataObjects = returnPackage.rowDataObjects;
hexSiteData = await buildHexSiteData(rowDataObjects);
io.emit("rowData", rowData);
}
if (MongoDB_PULSECHAIN && !MongoDB_PULSECHAIN.getRowDataRunning){
returnPackage_PULSECHAIN = await MongoDB_PULSECHAIN.getRowData();
rowData_PULSECHAIN = returnPackage_PULSECHAIN.rowData;
rowDataObjects_PULSECHAIN = returnPackage_PULSECHAIN.rowDataObjects;
io.emit("rowData_PULSECHAIN", rowData_PULSECHAIN);
}
}; getRowData();
const getAndSet_currentGlobalDayThrottled = throttle(getAndSet_currentGlobalDay, 30000);
async function getAndSet_currentGlobalDay(){
getAndSet_currentGlobalDay_Running = true;
try {
var currentDay = await Etherscan.getCurrentDay() + 1;
log("currentDay: " + currentDay);
if (currentDay != currentDayGlobal && currentDay > currentDayGlobal) {
currentDayGlobal = currentDay;
io.emit("currentDay", currentDayGlobal);
}
} catch (error){
log("ERROR: " + "getAndSet_currentGlobalDay()");
console.log(error);
} finally {
getAndSet_currentGlobalDay_Running = false;
}
await sleep(1000);
}
var hostname = CONFIG.hostname;
if (DEBUG){ hostname = '127.0.0.1'; }
var httpPort = 80;
if (DEBUG){ httpPort = 3000; }
const httpsPort = 443;
var httpsOptions = undefined;
if(!DEBUG){ httpsOptions = {
cert: fs.readFileSync(CONFIG.https.cert),
ca: fs.readFileSync(CONFIG.https.ca),
key: fs.readFileSync(CONFIG.https.key)
};}
const app = express();
app.use(function(req, res, next) {
try {
if (!DEBUG && req.path === "/" && req.ip){
connections[req.ip] = Date.now();
const connection = new MongoDB_ETHEREUM.connection({
created: Date.now(),
ipaddress: req.ip
});
MongoDB_ETHEREUM.connection.save(function (err) {
if (err) return log(err);
});
}
} catch (error) {
log('APP ----- Connection ' + error);
}
if (!getAndSet_currentGlobalDay_Running && !DailyStatHandler_ETHEREUM.getDataRunning && !getLiveDataRUNNING) { getAndSet_currentGlobalDayThrottled() }
//if (!getRowDataRunning){ getRowData(); }
next();
});
const httpServer = http.createServer(app);
var httpsServer = undefined;
if(!DEBUG){ httpsServer = https.createServer(httpsOptions, app);}
if(!DEBUG){ app.use((req, res, next) =>
{
if(req.protocol === 'http') {
res.redirect(301, 'https://' + hostname);
}
next();
}); }
app.use(express.static(path.join(__dirname, 'public')));
app.get("/", function(req, res){ res.sendFile('/index.html', {root: __dirname}); });
app.get("/" + CONFIG.urls.grabdata, function (req, res) {
grabData();
res.send(new Date().toISOString() + ' - Grab Data!');
});
app.get('/fulldata', cors(), function (req, res) {
if (rowDataObjects) { res.send(JSON.parse(JSON.stringify(rowDataObjects))); } else {res.status(404).send({ error: "fullData not populated yet" });};
});
app.get('/fulldatapulsechain', cors(), function (req, res) {
if (rowDataObjects_PULSECHAIN) { res.send(JSON.parse(JSON.stringify(rowDataObjects_PULSECHAIN))); } else {res.status(404).send({ error: "fullDataPulseChain not populated yet" });};
});
app.get('/livedata', cors(), function (req, res) {
if (liveData) { res.send(JSON.parse(JSON.stringify(liveData))); } else {res.status(404).send({ error: "liveData not populated yet" });};
});
app.get('/hexsite', cors(), function (req, res) {
if (hexSiteData) {
try {
res.send(JSON.parse(JSON.stringify(hexSiteData)));
} catch (error) {
log("/hexsite");
log(error);
}
}
else {res.status(404).send({ error: "hexsite not populated yet" });};
});
async function buildHexSiteData(rowDataObjects){
if (rowDataObjects) {
try {
var highestTshareRateUSD = Math.max.apply(Math, rowDataObjects.map(function(a) { return a.tshareRateUSD; }))
var prices = rowDataObjects.map(a => a.priceUV2UV3).reverse();
var pricesBitcoin = await Coingecko.getPriceHistory_Bitcoin(currentDayGlobal); await sleep(500);
var pricesEthereum = await Coingecko.getPriceHistory_Ethereum(currentDayGlobal); await sleep(500);
var pricesGold = await Coingecko.getPriceHistory_Gold(currentDayGlobal); await sleep(500);
var priceHistory = {
btc: pricesBitcoin,
eth: pricesEthereum,
gold: pricesGold
}
var priceATH = await Coingecko.getPriceAllTimeHigh(); //await sleep(300);
var json = {
averageStakeLength: rowDataObjects[0].averageStakeLength,
numberOfHolders: rowDataObjects[0].numberOfHolders,
numberOfHoldersChange: rowDataObjects[0].numberOfHoldersChange,
currentStakerCount: rowDataObjects[0].currentStakerCount,
currentStakerCountChange: rowDataObjects[0].currentStakerCountChange,
totalValueLocked: rowDataObjects[0].totalValueLocked,
stakedHEX: liveData ? liveData.stakedHEX : rowDataObjects[0].stakedHEX,
circulatingHEX: liveData ? liveData.circulatingHEX : rowDataObjects[0].circulatingHEX,
tshareRateUSD_Highest: highestTshareRateUSD,
priceATH: priceATH,
priceUV2UV3_Array: prices,
priceHistory: priceHistory,
}
return json;
} catch (error) {
log("buildHexSiteData()");
log(error);
}
} else {
log("buildHexSiteData() - rowDataObjects not ready");
}
}
async function grabData() {
await getAndSet_currentGlobalDay();
if (!getLiveDataRUNNING){ await runLiveData(); }
if (!getEthereumDataRUNNING){ await runEthereumData(); }
//if (!getCurrencyDataRunning){ getCurrencyData(); };
getRowData();
//if (!DailyStatHandler_ETHEREUM.getDataRunning){ await DailyStatHandler_ETHEREUM.getDailyData(); }
//if (!DailyStatHandler_PULSECHAIN.getDataRunning){ await DailyStatHandler_PULSECHAIN.getDailyData(); }
await getBitcoinCSV();
await getEthereumCSV();
}
httpServer.listen(httpPort, hostname, () => { log(`Server running at http://${hostname}:${httpPort}/`);});
if(!DEBUG){ httpsServer.listen(httpsPort, hostname, () => {
log('listening on *:' + httpsPort);
grabData();
});
}
var io = undefined;
if(DEBUG){ io = require('socket.io')(httpServer);
} else { io = require('socket.io')(httpsServer, {secure: true}); }
io.on('connection', (socket) => {
log('SOCKET -- ************* CONNECTED: ' + socket.id + ' *************');
if (rowData){ socket.emit("rowData", rowData); }; //rowData.slice(0, 49)); };
if (rowData_PULSECHAIN) { socket.emit("rowData_PULSECHAIN", rowData_PULSECHAIN); }
//if (DailyStatHandler_ETHEREUM.getDataRunning){ await DailyStatHandler_ETHEREUM.getDailyData(); }
//if (DailyStatHandler_PULSECHAIN.getDataRunning){ await DailyStatHandler_PULSECHAIN.getDailyData(); }
//getRowData();
socket.emit("hexPrice", hexPrice);
socket.emit("currentDay", currentDayGlobal);
socket.emit("liveData", liveData);
socket.emit("currencyRates", currencyRates);
socket.emit("ethereumData", ethereumData);
//MongoDB_PULSECHAIN.create_roiMultiplierFromATLs();
/*
socket.on("sendLatestData", () => {
if (rowData) {
log("sendLatestData - SUCCESS");
socket.emit("entireRowData", rowData);
}
});
*/
socket.on("needRowData", () => {
socket.emit('rowData', rowData.slice(0, 49));
});
});
async function getCurrencyData() {
log("getCurrencyData() - START");
getCurrencyDataRunning = true;
try {
var rates = await getCurrencyRates();
if (rates) {
currencyRates = rates;
log('SOCKET -- ****EMIT: currencyRates');
io.emit("currencyRates", currencyRates);
}
} catch (error) {
log("getCurrencyData() - ERROR: " + error);
} finally {
getCurrencyDataRunning = false;
}
}
//if (CONFIG.price.enabled) {
// var priceTimer = CONFIG.price.timer * 60 * 1000;
// setInterval(function() {
// updatePrice();
// }, priceTimer); }
var jobLive = schedule.scheduleJob("*/1 * * * *", function() {
getAllLiveData();
});
async function getAllLiveData(){
await runLiveData();
await runEthereumData();
}
var jobLive15 = schedule.scheduleJob("*/15 * * * *", function() {
getHexSiteData();
});
async function getHexSiteData(){
if (rowDataObjects) { hexSiteData = await buildHexSiteData(rowDataObjects); }
}
const ruleCurrentDay = new schedule.RecurrenceRule();
ruleCurrentDay.hour = 0;
ruleCurrentDay.minute = 0;
ruleCurrentDay.second = 30;
ruleCurrentDay.tz = 'Etc/UTC';
const jobCurrentDay = schedule.scheduleJob(ruleCurrentDay, function(){
log('**** DAILY DATA TIMER 30S!');
if (!getAndSet_currentGlobalDay_Running) { getAndSet_currentGlobalDay(); }
});
var jobCurrencyRates = schedule.scheduleJob("0 */3 * * *", function() {
if (!getCurrencyDataRunning) { getCurrencyData(); };
});
async function runEthereumData() {
try {
await sleep(300);
if (!getEthereumDataRUNNING){
var ethereumDataNew = await getEthereumData();
//console.log(ethereumDataNew);
if (ethereumDataNew && (JSON.stringify(ethereumDataNew) !== JSON.stringify(ethereumData))){
ethereumData = ethereumDataNew;
io.emit("ethereumData", ethereumData);
}
}
} catch (error){
log("runEthereumData() --- ERROR --- " + error.toString());
} finally {
getEthereumDataRUNNING = false;
}
}
async function getEthereumData() {
getEthereumDataRUNNING = true;
log("getEthereumData()");
try {
if (!DailyStatHandler_ETHEREUM.getDataRunning){
var price = await Etherscan.getEthereumPrice(); await sleep(2000);
var {low, average, high} = await Etherscan.getGas(); await sleep(1000);
return {
price: price,
erc20transfer: (average * 65000 / 1000000000 * price),
uniswapSwap: (average * 200000 / 1000000000 * price),
addLiquidity: (average * 175000 / 1000000000 * price),
gwei: average,
};
}
} catch (error){
log("getEthereumData() --- ERROR --- " + error.toString());
} finally {
getEthereumDataRUNNING = false;
}
}
async function runLiveData() {
try {
await sleep(300);
if (!DailyStatHandler_ETHEREUM.getDataRunning && !DailyStatHandler_PULSECHAIN.getDataRunning && !getLiveDataRUNNING){
var liveDataNew = await getLiveData();
//console.log(liveDataNew);
if (liveDataNew && (JSON.stringify(liveDataNew) !== JSON.stringify(liveData))){
liveData = liveDataNew;
io.emit("liveData", liveData);
if (liveData.price) {
hexPrice = liveData.price.toFixed(4);
io.emit("hexPrice", hexPrice);
}
}
}
} catch (error){
log("runLiveData() --- ERROR --- " + error.toString());
} finally {
getLiveDataRUNNING = false;
}
}
async function getLiveData() {
getLiveDataRUNNING = true;
log("getLiveData()");
try {
if (!DailyStatHandler_ETHEREUM.getDataRunning && !DailyStatHandler_PULSECHAIN.getDataRunning){
///////////////////////////////// ETHEREUM NETWORK
//var priceUV2 = await TheGraph_ETHEREUM.getUniswapV2HEXDailyPrice(); await sleep(1000);
var priceUV3 = await TheGraph_ETHEREUM.getUniswapV3HEXDailyPrice(); await sleep(1000);
var priceUV2 = priceUV3;
//var { liquidityUV2_HEXUSDC, liquidityUV2_USDC } = await TheGraph_ETHEREUM.getUniswapV2HEXUSDC_Polling(); await sleep(1000);
//var { liquidityUV2_HEXETH, liquidityUV2_ETH } = await TheGraph_ETHEREUM.getUniswapV2HEXETH(); await sleep(1000);
var liquidityUV2_HEXUSDC = 0;
var liquidityUV2_USDC = 0;
var liquidityUV2_HEXETH = 0;
var liquidityUV2_ETH = 0;
var { liquidityUV3_HEX, liquidityUV3_USDC, liquidityUV3_ETH, liquidityUV3_DAI } = await TheGraph_ETHEREUM.getUniswapV3(); await sleep(1000);
var liquidityUV2UV3_HEX = parseInt(liquidityUV2_HEXUSDC + liquidityUV2_HEXETH + liquidityUV3_HEX);
var liquidityUV2UV3_USDC = parseInt(liquidityUV2_USDC + liquidityUV3_USDC);
var liquidityUV2UV3_ETH = parseInt(liquidityUV2_ETH + liquidityUV3_ETH);
var liquidityUV2UV3_DAI = parseInt(liquidityUV3_DAI);
//var priceUV2UV3 = parseFloat(((priceUV2 * (liquidityUV2_USDC / liquidityUV2UV3_USDC)) +
//(priceUV3 * (liquidityUV3_USDC / liquidityUV2UV3_USDC))).toFixed(8));
var priceUV2UV3 = priceUV3;
var tshareRateHEX = await TheGraph_ETHEREUM.get_shareRateChange(); await sleep(500);
tshareRateHEX = parseFloat(tshareRateHEX);
var tshareRateUSD = parseFloat((tshareRateHEX * priceUV2UV3).toFixed(4));
if (liquidityUV2_HEXUSDC == 0 || liquidityUV2_USDC == 0 || liquidityUV2_HEXETH == 0 || liquidityUV2_ETH == 0) {
//return undefined;
}
var { circulatingHEX, stakedHEX, totalTshares, penaltiesHEX } = await Etherscan.getGlobalInfo(); await sleep(500);
var payout = ((circulatingHEX + stakedHEX) * 10000 / 100448995) + (penaltiesHEX / 2.0);
var payoutPerTshare = (payout / totalTshares);
/////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// PULSECHAIN NETWORK
var priceHEX_PulseX_Pulsechain = await TheGraph_PULSECHAIN.getPulseXPrice("HEX"); await sleep(500);
var pricePLS_PulseX_Pulsechain = await TheGraph_PULSECHAIN.getPulseXPrice("PULSECHAIN"); await sleep(500);
var pricePLSX_PulseX_Pulsechain = await TheGraph_PULSECHAIN.getPulseXPrice("PULSEX"); await sleep(500);
var priceINC_PulseX_Pulsechain = await TheGraph_PULSECHAIN.getPulseXPrice("INC"); await sleep(500);
var HEXPLSpair = await TheGraph_PULSECHAIN.getPulseXPairLiquidity(h.PULSECHAIN_HEXPLS); await sleep(1000);
var HEXEHEXpair = await TheGraph_PULSECHAIN.getPulseXPairLiquidity(h.PULSECHAIN_HEXEHEX); await sleep(1000);
//var PLSpair = await TheGraph_PULSECHAIN.getPulseXPairLiquidity(h.PULSECHAIN_WPLSDAI); await sleep(1000);
//var PLSXpair = await TheGraph_PULSECHAIN.getPulseXPairLiquidity(h.PULSECHAIN_WPLSPLSX); await sleep(1000);
//var INCpair = await TheGraph_PULSECHAIN.getPulseXPairLiquidity(h.PULSECHAIN_WPLSINC); await sleep(1000);
var liquidityHEX_Pulsechain = 0;
if (HEXPLSpair && HEXEHEXpair){
liquidityHEX_Pulsechain = (Number(HEXPLSpair.reserve0) + Number(HEXEHEXpair.reserve0))
}
var liquidityPLS_Pulsechain = 0;
if (HEXPLSpair){
liquidityPLS_Pulsechain = HEXPLSpair.reserve1;
}
var liquidityEHEX_Pulsechain = 0;
if (HEXEHEXpair){
liquidityEHEX_Pulsechain = HEXEHEXpair.reserve1;
}
var tshareRateHEX_Pulsechain = await TheGraph_PULSECHAIN.get_shareRateChange(); await sleep(500);
tshareRateHEX_Pulsechain = parseFloat(tshareRateHEX_Pulsechain);
var tshareRateUSD_Pulsechain = parseFloat((tshareRateHEX_Pulsechain * priceHEX_PulseX_Pulsechain).toFixed(4));
var globalInfo = await TheGraph_PULSECHAIN.get_globalInfo(); await sleep(500);
//console.log("globalInfo");
//console.log(globalInfo);
var circulatingHEX_Pulsechain = globalInfo[0].totalHeartsinCirculation / 100000000;
var stakedHEX_Pulsechain = globalInfo[0].lockedHeartsTotal / 100000000;
var totalTshares_Pulsechain = globalInfo[0].stakeSharesTotal / 1000000000000;
var penaltiesHEX_Pulsechain = globalInfo[0].stakePenaltyTotal / 100000000;
var payout_Pulsechain = ((circulatingHEX_Pulsechain + stakedHEX_Pulsechain) * 10000 / 100448995) + (penaltiesHEX_Pulsechain / 2.0);
var payoutPerTshare_Pulsechain = (payout_Pulsechain / totalTshares_Pulsechain);
var {rapid, fast} = await Etherscan.getGas_Pulsechain(); await sleep(1000);
var erc20transfer_Pulsechain = (fast / 20000 / 1000000000 * pricePLS_PulseX_Pulsechain);
var pulseXSwap_Pulsechain = (fast / 4650 / 1000000000 * pricePLS_PulseX_Pulsechain);
var addLiquidity_Pulsechain = (fast / 3600 / 1000000000 * pricePLS_PulseX_Pulsechain);
var beat = (fast / 1000000000);
return {
price: priceUV2UV3,
tsharePrice: tshareRateUSD,
tshareRateHEX: tshareRateHEX,
liquidityHEX: liquidityUV2UV3_HEX,
liquidityUSDC: liquidityUV2UV3_USDC,
liquidityETH: liquidityUV2UV3_ETH,
liquidityDAI: liquidityUV2UV3_DAI,
penaltiesHEX: penaltiesHEX,
payoutPerTshare: payoutPerTshare,
stakedHEX: stakedHEX,
circulatingHEX: circulatingHEX,
price_Pulsechain: priceHEX_PulseX_Pulsechain,
tsharePrice_Pulsechain: tshareRateUSD_Pulsechain,
tshareRateHEX_Pulsechain: tshareRateHEX_Pulsechain,
liquidityHEX_Pulsechain: liquidityHEX_Pulsechain,
liquidityPLS_Pulsechain: liquidityPLS_Pulsechain,
liquidityEHEX_Pulsechain: liquidityEHEX_Pulsechain,
penaltiesHEX_Pulsechain: penaltiesHEX_Pulsechain,
payoutPerTshare_Pulsechain: payoutPerTshare_Pulsechain,
stakedHEX_Pulsechain: stakedHEX_Pulsechain,
circulatingHEX_Pulsechain: circulatingHEX_Pulsechain,
pricePLS_Pulsechain: pricePLS_PulseX_Pulsechain,
pricePLSX_Pulsechain: pricePLSX_PulseX_Pulsechain,
priceINC_Pulsechain: priceINC_PulseX_Pulsechain,
erc20transfer_Pulsechain: erc20transfer_Pulsechain,
pulseXSwap_Pulsechain: pulseXSwap_Pulsechain,
addLiquidity_Pulsechain: addLiquidity_Pulsechain,
beat: beat,
};
}
} catch (error){
log("getLiveData() --- ERROR --- " + error.toString());
} finally {
getLiveDataRUNNING = false;
}
}
let test = async () => {
//await MongoDb_ETHEREUM.updateOneColumn(756, "tshareRateHEX", null);
//var test = await MongoDb_ETHEREUM.dailyStat.find({currentDay:null});
//var test2 = test;
}; test();
cron.schedule('10 */6 * * *', async () => {
await getBitcoinCSV();
await getEthereumCSV();
});
async function getEthereumCSV(){
try {
log("getEthereumCSV()")
var pricesETH = await Coingecko.getPriceHistory_EthereumWithTime((3000 + currentDayGlobal)); await sleep(500);
pricesETH = pricesETH.slice(0, -1);
var pricesETHList = [];
var count = 1;
pricesETH.forEach(row => {
var date = new Date(Number(row[0]));
date.setUTCHours(0, 0, 0, 0);
var dateString = date.getUTCFullYear() + "-" + h.minTwoDigits(date.getUTCMonth() + 1) + "-" + h.minTwoDigits(date.getUTCDate());
var newRow = {
Day: count,
Date: dateString,
Price: Number(row[1]),
}
pricesETHList.push(newRow);
count+=1;
});
var pricesETHCSV = h.convertCSV(pricesETHList);
fs.writeFileSync('./public/pricesETH.csv', pricesETHCSV);
} catch (e) { log(e); }
}
async function getBitcoinCSV(){
try {
log("getBitcoinCSV()")
var prices_start = p.PRICES_BTC_START;
var days = (currentDayGlobal - 1237);
var pricesList = [];
var count = 1;
prices_start.every(row => {
var date = new Date(row[0]);
date.setUTCHours(0, 0, 0, 0);
var dateString = date.getUTCFullYear() + "-" + h.minTwoDigits(date.getUTCMonth() + 1) + "-" + h.minTwoDigits(date.getUTCDate());
var newRow = {
Day: count,
DayLaunch: (count + 274),
Date: dateString,
Price: Number(row[1]),
}
pricesList.push(newRow);
count+=1;
return true;
});
var prices_end = await Coingecko.getPriceHistory_BitcoinWithTime(days); await sleep(500);
prices_end = prices_end.slice(0, -1);
prices_end.every(row => {
var date = new Date(Number(row[0]));
date.setUTCHours(0, 0, 0, 0);
var dateString = date.getUTCFullYear() + "-" + h.minTwoDigits(date.getUTCMonth() + 1) + "-" + h.minTwoDigits(date.getUTCDate());
var newRow = {
Day: count,
DayLaunch: (count + 274),
Date: dateString,
Price: Number(row[1]),
}
pricesList.push(newRow);
count+=1;
return true;
});
var pricesCSV = h.convertCSV(pricesList);
fs.writeFileSync('./public/pricesBTC.csv', pricesCSV);
} catch (e) { log(e); }
}
cron.schedule('15 * * * * *', async () => {
log("**** DAILY DATA MAINTENANCE TIMER! -- getDataRunning: " + DailyStatHandler_ETHEREUM.getDataRunning + " --- DailyStatMaintenance_ETHEREUM: " + DailyStatMaintenance_ETHEREUM + " -- ETHEREUM");
if (!DailyStatHandler_ETHEREUM.getDataRunning && !DailyStatMaintenance_ETHEREUM){
try{
let daysBackToCheck = 4;
let latestDay = await TheGraph_ETHEREUM.get_latestDay();
let latestDailyData = await MongoDB_ETHEREUM.dailyStat.find().limit(daysBackToCheck).sort({currentDay:-1});
//console.log("latestDay: " +latestDay + " latestDailyData: " + latestDailyData);
let latestDailyDataCurrentDay = latestDailyData[0].currentDay;
let dailyDataCurrentDayStart = latestDailyData[latestDailyData.length-1].currentDay;
//console.log("latestDailyDataCurrentDay: " +latestDailyDataCurrentDay + " dailyDataCurrentDayStart: " + dailyDataCurrentDayStart);
for (let i = dailyDataCurrentDayStart; i <= latestDailyDataCurrentDay; i++) {
let ds = await MongoDB_ETHEREUM.dailyStat.find({currentDay:i});
//console.log("ds: " + ds);
for(var key in ds[0]){
if(key != '$op' && ds[0][key] === null && key != "priceUV1" && key != "liquidityUV1_HEX" && key != "liquidityUV1_ETH"){
console.log("key: " + key + " value: " + ds[0][key]);
DailyStatMaintenance_ETHEREUM = true;
log("**** DAILY DATA -- getDailyData() -- DailyStatMaintenance SET TRUE: " + DailyStatMaintenance_ETHEREUM + " -- ETHEREUM");
await DailyStatHandler_ETHEREUM.getDailyData(i);
getRowData();
io.emit("currentDay", currentDayGlobal);
break;
}
}
}
if(latestDay > latestDailyDataCurrentDay) {
DailyStatMaintenance_ETHEREUM = true;
log("**** DAILY DATA -- getDailyData() 2 -- DailyStatMaintenance SET TRUE: " + DailyStatMaintenance_ETHEREUM) + " -- ETHEREUM";
for (let i = latestDailyDataCurrentDay + 1; i <= latestDay; i++) {
await DailyStatHandler_ETHEREUM.getDailyData(i);
getRowData();
io.emit("currentDay", currentDayGlobal);
}
}
}
catch (err) {
log('DAILY DATA MAINTENANCE TIMER () ----- ERROR ---' + err.toString() + " - " + err.stack + " -- ETHEREUM");
} finally {
DailyStatMaintenance_ETHEREUM = false;
log("**** DAILY DATA -- DailyStatMaintenance SET FALSE: " + DailyStatMaintenance_ETHEREUM + " -- ETHEREUM");
}
}
});
cron.schedule('* * 3 * * *', async () => {
let latestDay = await TheGraph_ETHEREUM.get_latestDay();
for(let i = 1; i <= latestDay; i++){
let present = await MongoDB_ETHEREUM.find({currentDay: i}).limit(1);
if(isEmpty(present)){
await DailyStatHandler_ETHEREUM.getDailyData(i);
}
}
});
cron.schedule('45 * * * * *', async () => {
log("**** DAILY DATA MAINTENANCE TIMER! -- getDataRunning: " + DailyStatHandler_PULSECHAIN.getDataRunning + " -- PULSECHAIN");
if (!DailyStatHandler_PULSECHAIN.getDataRunning && !DailyStatMaintenance_PULSECHAIN){
try{
let daysBackToCheck = 4;
let latestDay = await TheGraph_PULSECHAIN.get_latestDay();
let latestDailyData = await MongoDB_PULSECHAIN.dailyStat.find().limit(daysBackToCheck).sort({currentDay:-1});
//console.log("latestDay: " +latestDay + " latestDailyData: " + latestDailyData);
let latestDailyDataCurrentDay = latestDailyData[0].currentDay;
let dailyDataCurrentDayStart = latestDailyData[latestDailyData.length-1].currentDay;
//console.log("latestDailyDataCurrentDay: " +latestDailyDataCurrentDay + " dailyDataCurrentDayStart: " + dailyDataCurrentDayStart);
for (let i = dailyDataCurrentDayStart; i <= latestDailyDataCurrentDay; i++) {
let ds = await MongoDB_PULSECHAIN.dailyStat.find({currentDay:i});
for(var key in ds[0]){
if(key != '$op' && ds[0][key] === null && key != "priceUV1" && key != "liquidityUV1_HEX" && key != "liquidityUV1_ETH"){
//console.log("key: " + key + " value: " + ds[0][key]);
DailyStatMaintenance_PULSECHAIN = true;
log("**** DAILY DATA -- getDailyData() -- DailyStatMaintenance SET TRUE: " + DailyStatMaintenance_PULSECHAIN) + " -- PULSECHAIN";
await DailyStatHandler_PULSECHAIN.getDailyData(i);
getRowData();
io.emit("currentDay", currentDayGlobal);
break;
}
}
}
if(latestDay > latestDailyDataCurrentDay) {
DailyStatMaintenance_PULSECHAIN = true;
log("**** DAILY DATA -- getDailyData() 2 -- DailyStatMaintenance SET TRUE: " + DailyStatMaintenance_PULSECHAIN + " -- PULSECHAIN");
for (let i = latestDailyDataCurrentDay + 1; i <= latestDay; i++) {
await DailyStatHandler_PULSECHAIN.getDailyData(i);
getRowData();
io.emit("currentDay", currentDayGlobal);
}
}
}
catch (err) {
log('DAILY DATA MAINTENANCE TIMER () ----- ERROR ---' + err.toString() + " - " + err.stack) + " -- PULSECHAIN";
} finally {
DailyStatMaintenance_PULSECHAIN = false;
log("**** DAILY DATA -- DailyStatMaintenance SET FALSE: " + DailyStatMaintenance_PULSECHAIN + " -- PULSECHAIN");
}
}
});
cron.schedule('* 1 3 * * *', async () => {
let latestDay = await TheGraph_PULSECHAIN.get_latestDay();
for(let i = 1; i <= latestDay; i++){
let present = await MongoDB_PULSECHAIN.find({currentDay: i}).limit(1);
if(isEmpty(present)){
await DailyStatHandler_PULSECHAIN.getDailyData(i);
}
}
});
//////////////////////////////////////
//// HELPER
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
const objectsEqual = (o1, o2) =>
Object.keys(o1).length === Object.keys(o2).length
&& Object.keys(o1).every(p => o1[p] === o2[p]);
function throttle (callback, limit) {
var wait = false; // Initially, we're not waiting
return function () { // We return a throttled function
if (!wait) { // If we're not waiting
callback.call(); // Execute users function
wait = true; // Prevent future invocations
setTimeout(function () { // After a period of time
wait = false; // And allow future invocations
}, limit);
}
}
}
/////////////////////////////////////////////
// PRICE
/*
var priceUrl = "https://api.nomics.com/v1/currencies/ticker?key=" + CONFIG.price.nomicsKey + "&ids=HEX";
async function updatePrice(){
try {
const resp = await fetch(priceUrl);
const data = await resp.json();
if (data && data.length >= 1) {
var hexData = data[0];
if (hexData && hexData.price) {
hexPrice = parseFloat(hexData.price).toFixed(4).toString();
io.emit("hexPrice", hexPrice);
}
}
} catch (err) {
log("PRICE --- ERROR - updatePrice() - " + err + "\n" + err.stack);
}
}
*/
///////////////////////////////////////////////////
// CURRENCY RATES
async function getCurrencyRates(){
var url = "http://api.exchangeratesapi.io/v1/latest?access_key=" + CONFIG.exchangerates.key + "&format=1"; // + "&base=" + base; // Paid Plan
return await fetchRetry(url, {
method: 'GET',
highWaterMark: FETCH_SIZE,
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(res => {
if (res && res.success && res.rates) {
return res.rates;
}
return undefined;
});
}