-
Notifications
You must be signed in to change notification settings - Fork 4
/
Explorer.js
1086 lines (977 loc) · 37.8 KB
/
Explorer.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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Node Trinity source code
* See LICENCE file at the top of the source tree
*
* ******************************************
*
* Explorer.js
* Blockchain explorer instance
*
* ******************************************
*
* Authors: K. Zhidanov, A. Prudanov, M. Vasil'ev
*/
const express = require('express');
const cors = require('cors');
const {ContractError} = require('./errors');
const {ExplorerService, ExplorerServicePlain} = require('./explorer.service');
const NodeapiService = require('./nodeapi.service').NodeapiService;
const Utils = require('./Utils');
const Transport = require('./Transport').Tip;
const ContractMachine = require('./SmartContracts');
//const swaggerJSDoc = require('swagger-jsdoc');
let swaggerSpec;
class Explorer {
constructor(port, db, pending, stake_limits, config) {
this.app = express();
this.app.use(cors());
this.db = db;
this.config = config;
this.pending = pending;
this.stake_limits = stake_limits;
this.service = new ExplorerService(db);
this.servicePlain = new ExplorerServicePlain(db);
this.serviceNodeapi = new NodeapiService(db);
this.transport = new Transport(config.id, 'explorer');
this.app.listen(port, function(){
console.info('Explorer started at ::', port);
const options = {
definition: {
openapi: '3.0.0', // Specification (optional, defaults to swagger: '2.0')
info: {
title: 'Enecuum Node API', // Title (required)
version: '1.0.0', // Version (required)
description: `|
Enecuum is a cryptocurrency project designed to involve both mobile and desktop devices into one blockchain network. Enecuum provides a payment platform with the use of a native token. Enecuum's native token, ENQ, can be bought on the exchange, by card, or mined on PCs and smartphones.
Below, you can read a short explanation of the basic terms and concepts related to Enecuum and cryptocurrency in general. The description will help you better understand the Enecuum API.
When a user creates an account in the Enecuum network, a pair of public and private keys is generated.
**The public key** is a user's identification. It is used as a wallet address. The user can safely share the public key to receive payments.
**The private key** acts as a password. It is used to access the account. The user must never share the private key. The private key needs to be backed up. If a private key is lost, the account will never be recovered.
Enecuum has a native token, ENQ, which is used for payments in the Enecuum network. Enecuum also allows the issue of custom tokens. Each token has two unique identifiers: ticker and hash. A **ticker** is a 1-6 letter name used for convenience. A **hash** is a 64-character sequence that acts as a technical identification.
Each token token has a fixed number of **decimal places**. When using Enecuum API, the methods will return the token amount as an integer. Because of this, the returned amount is multiplied by 10^*d*, where *d* stands for the number of decimal places. For example, ENQ has 10 decimal places. The returned amount 250050000000 correlates with 25.005 ENQ.
`
},
servers:{
url: 'https://bit.enecuum.com/api/v1'
},
},
// Path to the API docs
apis: ['./Explorer.js'],
};
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
//swaggerSpec = swaggerJSDoc(options);
//console.log(swaggerSpec);
});
this.app.use(express.json());
this.app.get('/api/v1/network_info', async (req, res) => {
console.trace('requested network_info', req.query);
let native_token = (await this.db.get_tokens_info([this.config.native_token_hash]))[0];
let data = {
target_speed: this.config.target_speed,
reward_ratio: this.config.reward_ratio,
transfer_lock: this.config.transfer_lock,
pos_min_stake: this.config.pos_min_stake,
native_token: native_token,
snapshot_interval: this.config.snapshot_intervcal,
origin: this.config.ORIGIN,
fee_shares : this.config.fee_shares,
MPK : this.config.ecc[this.config.ecc.ecc_mode].MPK,
mblock_slots: {
size:this.config.mblock_slots.size,
count:this.config.mblock_slots.count,
min_stake:this.config.mblock_slots.min_stake
},
dex : this.config.dex
};
res.send(data);
});
this.app.get('/api/v1/native_token', async (req, res) => {
console.trace('requested native_token', req.query);
let data = (await this.db.get_tokens_info([this.config.native_token_hash]))[0];
res.send(data);
});
this.app.get('/api/v1/page', async (req, res) => {
console.trace('requested page', req.query);
let data = await this.db.get_page(req.query.n, 10);
res.send(data);
});
this.app.get('/api/v1/macroblock', async (req, res) => {
console.trace('requested macroblock', req.query);
let data = await this.db.get_macroblock_header(req.query.hash);
res.send(data);
});
this.app.get('/api/v1/macroblock_by_height', async (req, res) => {
console.trace('requested macroblock by height(', req.query);
let data = await this.db.get_macroblock_header_by_height(req.query.height);
res.send(data);
});
this.app.get('/api/v1/mblock', async (req, res) => {
console.trace('requested mblock', req.query);
let data = await this.db.get_mblock_data(req.query.hash);
res.send(data);
});
this.app.get('/api/v1/sblock', async (req, res) => {
console.trace('requested sblock', req.query);
let data = await this.db.get_sblock_data(req.query.hash);
res.send(data);
});
this.app.get('/api/v1/lastblocks', async (req, res) => {
console.trace('requested lastblocks', req.query);
let blocks = await this.db.get_lastblocks(10);
res.send({blocks});
});
this.app.get('/api/v1/lasttxs', async (req, res) => {
console.trace('requested lasttxs', req.query);
let data = await this.db.get_lasttxs(10);
res.send(data);
});
this.app.get('/api/v1/tps', async (req, res) => {
console.trace('requested tps', req.query);
let data = await this.db.get_tps(300);
await this.db.update_max_tps(data.tps);
res.send(data);
});
this.app.get('/api/v1/account', async (req, res) => {
BigInt.prototype.toJSON = function() { return this.toString() }
console.trace('requested account', req.query);
let data = await this.db.get_account_all(req.query.id, req.query.page, 10);
if(data.records !== undefined)
for(let i = 0; i< data.records.length; i++){
if(data.records[i].fee_type !== null) {
let tokendata = {
fee_type: data.records[i].fee_type,
fee_value: data.records[i].fee_value,
fee_min: data.records[i].fee_min
};
if (data.records[i].input !== null) {
let fee = Utils.calc_fee(tokendata, data.records[i].input);
data.records[i].input = BigInt(data.records[i].input) - fee;
data.records[i].fee = fee;
} else if (data.records[i].output !== null) {
let fee = Utils.calc_fee(tokendata, data.records[i].output);
data.records[i].output = BigInt(data.records[i].output) - fee;
data.records[i].fee = fee;
}
}
}
res.send(data);
});
this.app.get('/api/v1/account_in', async (req, res) => {
console.trace('requested account_in', req.query);
let data = await this.db.get_account_in(req.query.id, req.query.page, 10);
if(data.records != undefined)
for(let i = 0; i< data.records.length; i++){
if(data.records[i].fee_type !== null) {
let tokendata = {
fee_type: data.records[i].fee_type,
fee_value: data.records[i].fee_value,
fee_min: data.records[i].fee_min
};
let fee = Utils.calc_fee(tokendata, data.records[i].input);
data.records[i].input = BigInt(data.records[i].input) - fee;
data.records[i].fee = fee;
}
}
res.send(data);
});
this.app.get('/api/v1/account_out', async (req, res) => {
console.trace('requested account_out', req.query);
let data = await this.db.get_account_out(req.query.id, req.query.page, 10);
if(data.records != undefined)
for(let i = 0; i< data.records.length; i++){
if(data.records[i].fee_type !== null) {
let tokendata = {
fee_type: data.records[i].fee_type,
fee_value: data.records[i].fee_value,
fee_min: data.records[i].fee_min
};
let fee = Utils.calc_fee(tokendata, data.records[i].output);
data.records[i].output = BigInt(data.records[i].output) - fee;
data.records[i].fee = fee;
}
}
res.send(data);
});
this.app.get('/api/v1/account_transactions', async (req, res) => {
console.trace('requested account_transactions', req.query);
let data = await this.db.get_account_transactions(req.query.id, req.query.page, 10);
res.send(data);
});
this.app.get('/api/v1/account_rewards', async (req, res) => {
console.trace('requested account_mreward', req.query);
let data = await this.db.get_account_rewards(req.query.id, req.query.page, 10);
res.send(data);
});
this.app.get('/api/v1/account_mreward', async (req, res) => {
console.trace('requested account_mreward', req.query);
let data = await this.db.get_account_mreward(req.query.id, req.query.page, 10);
res.send(data);
});
this.app.get('/api/v1/account_sreward', async (req, res) => {
console.trace('requested account_sreward', req.query);
let data = await this.db.get_account_sreward(req.query.id, req.query.page, 10);
res.send(data);
});
this.app.get('/api/v1/account_refreward', async (req, res) => {
console.trace('requested account_refreward', req.query);
let data = await this.db.get_account_refreward(req.query.id, req.query.page, 10);
res.send(data);
});
this.app.get('/api/v1/account_kreward', async (req, res) => {
console.trace('requested account_kreward', req.query);
let data = await this.db.get_account_kreward(req.query.id, req.query.page, 10);
res.send(data);
});
this.app.get('/api/v1/eindex_by_hash', async (req, res) => {
console.trace('requested eindex_by_hash', req.query);
let data = await this.db.get_eindex_by_hash(req.query.hash);
res.send(data);
});
this.app.get('/api/v1/top_accounts', async (req, res) => {
let data = await this.db.get_top_accounts(req.query.page, 20, req.query.token_hash);
for(let i = 0; i < data.accounts.length; i++){
data.accounts[i].percentage = data.accounts[i].amount * (100) / data.total;
}
res.send({accounts : data.accounts, page_count : data.page_count});
});
this.app.get('/api/v1/get_token_info_page', async (req, res) => {
let data = await this.db.get_token_info_page(parseInt(req.query.page), 20, req.query.type);
if(data.tokens) {
data.tokens.forEach(t => {
t.price_raw = {
cg_price: t.cg_price,
dex_price: t.dex_price,
decimals: t.price_decimals
};
delete t.cg_price;
delete t.dex_price;
delete t.price_decimals
});
}
res.send({tokens : data.tokens, page_size : 20, page_count : data.page_count});
});
this.app.get('/api/v1/get_tokens_by_owner', async (req, res) => {
console.trace('requested get_tokens_by_owner', req.query);
let owner = req.query.owner;
let data = await this.db.get_tokens_by_owner(owner);
console.trace(`get_tokens_by_owner = `, JSON.stringify(data));
res.send(data);
});
this.app.get('/api/v1/balance', async (req, res) => {
console.trace('requested balance', req.query);
let id = req.query.id;
let token = req.query.token || Utils.ENQ_TOKEN_NAME;
let data = await this.db.get_balance(id, token);
console.trace(`balance = `, JSON.stringify(data));
let delegated_data = (await this.db.get_delegated_balance(id));
data.delegated = delegated_data.delegated;
data.reward = delegated_data.reward;
console.trace(`reward = `, JSON.stringify(delegated_data.reward));
console.trace(`delegated = `, JSON.stringify(delegated_data.delegated));
let transit = (await this.db.get_transit_balance(id));
data.transit = transit;
console.trace(`transit = `, JSON.stringify(transit));
let undelegated = (await this.db.get_undelegated_balance(id));
data.undelegated = undelegated;
console.trace(`undelegated = `, JSON.stringify(undelegated));
res.send(data);
});
this.app.get('/api/v1/balance_all', async (req, res) => {
console.trace('requested balances', req.query);
let id = req.query.id;
let data = await this.db.get_balance_all(id);
let that = this;
data = data.filter(item => that.config.tokens_white_list.includes(item.token));
console.trace(`balances = `, JSON.stringify(data));
res.send(data);
});
this.app.get('/api/v1/balance_all_unfiltered', async (req, res) => {
console.trace('requested balance all unfiltered', req.query);
let id = req.query.id;
let data = await this.db.get_balance_all(id);
console.trace(`balances = `, JSON.stringify(data));
res.send(data);
});
this.app.get('/api/v1/balance_mineable', async (req, res) => {
console.trace('requested balance mineable', req.query);
let id = req.query.id;
let data = await this.db.get_balance_mineable(id);
console.trace(`balances = `, JSON.stringify(data));
res.send(data);
});
this.app.get('/api/v1/balance_reissuable', async (req, res) => {
console.trace('requested balance reissuable', req.query);
let id = req.query.id;
let data = await this.db.get_balance_reissuable(id);
console.trace(`balances = `, JSON.stringify(data));
res.send(data);
});
this.app.get('/api/v1/token_info', async (req, res) => {
console.trace('requested token_info', req.query);
let hash = req.query.hash;
let data = await this.db.get_tokens_info([hash]);
console.trace(`token_info = `, JSON.stringify(data));
res.send(data);
});
this.app.get('/api/v1/tx', async (req, res) => {
console.trace(`requested tx ${JSON.stringify(req.query)}`);
let tx = (await this.db.get_tx(req.query.hash))[0];
if(tx !== undefined && tx.fee_type !== null) {
let tokendata = { fee_type: tx.fee_type, fee_value: tx.fee_value, fee_min: tx.fee_min};
tx.fee = (Utils.calc_fee(tokendata, tx.total_amount));
tx.amount = (BigInt(tx.total_amount) - tx.fee).toString();
tx.fee = tx.fee.toString();
}
res.send(tx);
});
this.app.get('/api/v1/success_tx_by_height', async (req, res) => {
console.trace(`requested success_tx_by_height ${JSON.stringify(req.query)}`);
let txs = await this.db.get_successful_txs_by_height(req.query.height);
res.send(txs);
});
this.app.get('/api/v1/peer_map', async (req, res) => {
console.trace('requested poa', req.query);
let poa = await this.db.get_clients();
res.send(poa);
});
this.app.get('/api/v1/poa_nodes_online', async (req, res) => {
console.trace('requested poa_nodes_online', req.query);
let poa = await this.db.get_clients_list();
res.send(poa);
});
this.app.get('/api/v1/search', async (req, res) => {
console.trace('requested search', req.query);
let resp = [];
let tx = await this.db.get_tx(req.query.value);
if (tx.length > 0) {
resp.push({type:'tx', info:tx});
} else {
let account = await this.db.get_accounts_all(req.query.value);
if (account.length > 0){
resp.push({type:'account', info:{balances: account}});
}
let kblock = await this.db.get_kblock(req.query.value);
if (kblock.length > 0) {
resp.push({type:'kblock', info:kblock});
}
let mblock = await this.db.get_mblock(req.query.value);
if (mblock.length > 0) {
resp.push({type:'mblock', info:mblock});
}
let sblock = await this.db.get_sblock_data(req.query.value);
if (sblock.header !== undefined) {
resp.push({type:'sblock', info:sblock});
}
}
let token = await this.db.get_tokens_info([req.query.value]);
if (token.length > 0 && token[0].hash !== undefined) {
resp.push({type:'token', info:token});
let pool = await this.db.dex_get_pools_all();
if (pool.length > 0 && pool[0].pair_id !== undefined) {
pool.forEach(item => {
if (item.token_hash === req.query.value)
resp.push({type:'pool', info:item});
})
}
} else {
let pool = await this.db.dex_get_pools([req.query.value]);
if (pool.length > 0) {
resp.push({type:'pool', info:pool});
}
}
let pos = await this.db.get_pos_contract_info(req.query.value);
if (pos.length > 0 && pos[0].pos_id !== undefined) {
resp.push({type:'pos', info:pos});
}
let farm = await this.db.get_dex_farms('',[req.query.value]);
if (farm.length > 0 && farm[0].farm_id !== undefined) {
resp.push({type:'farm', info:farm});
}
res.send(resp);
});
this.app.get('/api/v1/stats', async (req, res) => {
console.trace('requested stats', req.query);
let stats = await this.db.get_stat();
let result = {};
for(let stat of stats){
if(['cg_btc', 'cg_eth', 'cg_usd', 'difficulty'].includes(stat.key) === false)
stat.value = parseInt(stat.value);
result[stat.key] = stat.value;
}
console.trace(`stats = ${JSON.stringify(stats)}`);
res.send(result);
});
this.app.post('/api/v1/tx', async (req, res, next) => {
let txs = req.body;
console.trace(`post txs`, JSON.stringify(txs));
if (txs.length !== 1)
return res.send({err: 1, message: "Only 1 TX can be sent"});
try{
if (txs.length !== 1)
return res.send({err: 1, message: "Only 1 TX can be sent"});
let result = await this.serviceNodeapi.post_tx(txs[0]);
if(result.err === 0)
this.transport.broadcast("post_tx", txs[0]);
return res.send(result);
}
catch(err){
next(err);
}
});
this.app.get('/api/v1/ver', async (req, res) => {
console.trace(`get ver`, JSON.stringify(req.body));
let ver = await this.db.get_ver();
res.send(ver);
});
this.app.get('/api/v1/get_min_stake', async (req, res) => {
console.trace(`get min_stake`, JSON.stringify(req.body));
res.send({min_stake:parseFloat(this.stake_limits.min_stake)});
});
this.app.get('/api/v1/get_stake_limits', async (req, res) => {
console.trace(`get stake_limits`, JSON.stringify(req.body));
res.send(this.stake_limits);
});
this.app.get('/api/v1/referrer_stake', async (req, res) => {
console.trace(`get referrer_stake`, JSON.stringify(req.body));
let referrer_stake = await this.db.get_referrer_stake();
res.send({referrer_stake:parseFloat(referrer_stake.referrer_stake)});
});
this.app.get('/api/v1/roi', async (req, res) => {
console.trace(`get roi`, JSON.stringify(req.query));
let options = {
hash : req.query.hash || Utils.ENQ_TOKEN_NAME
};
let roi = await this.db.get_roi(options.hash);
res.send(roi);
});
this.app.get('/api/v1/mblocks', async (req, res) => {
console.trace('requested mblocks', req.query);
let offset = parseInt(req.query.offset) || 0;
let limit = parseInt(req.query.limit) || 0;
if(limit > 10)
limit = 10;
let data = await this.db.get_mblocks_info(offset, offset + limit - 1);
res.send(data);
});
this.app.get('/api/v1/height', async (req, res) => {
console.trace('requested height');
let data = await this.db.get_mblocks_height();
let result = {};
if(data.height){
result.height = parseInt(data.height);
}
res.send(result);
});
// TODO: can be replaced with stat field
this.app.get('/api/v1/poa_with_stake', async (req, res) => {
console.trace(`get total poa with stake`, JSON.stringify(req.query));
let count = await this.db.get_staking_poa_count();
res.send(count);
});
this.app.get('/api/v1/roi_by_stake', async (req, res) => {
console.trace(`get roi_by_stake`, JSON.stringify(req.query));
let options = {
hash : req.query.hash || Utils.ENQ_TOKEN_NAME,
stake : parseInt(req.query.stake || 0)
};
if(!req.query.hasOwnProperty('stake'))
return res.send({error: 1, msg : 'Stake not specified'});
let rois = await this.db.get_roi(options.hash);
let stake = options.stake;
if(stake < rois[0].stake || stake > rois[rois.length-1].stake)
return res.send({error: 1, msg : 'Stake value is out of range'});
let roi = stake;
for (let i = 0; i < rois.length; i++) {
if(stake === rois[i].stake){
roi = rois[i].roi;
break;
}
else if(stake < rois[i].stake){
roi = (rois[i-1].roi + ((stake - rois[i-1].stake) / (rois[i].stake - rois[i-1].stake)) * (rois[i].roi - rois[i-1].roi))
break;
}
}
let ratio = ((roi - stake) / stake);
res.send({
hash : options.hash,
percent: [ratio*100, ratio*100*7, ratio*100*30, ratio*100*365],
enq: [ratio*stake, ratio*stake*7, ratio*stake*30, ratio*stake*365]
});
});
this.app.get('/api/v1/stat/agent_info', async (req, res) => {
console.trace(`agent_info`, JSON.stringify(req.query));
let info = await this.db.get_agent_info(req.query.id);
res.json(info);
});
this.app.get('/api/v1/pending_size', async (req, res) => {
console.trace('pending_size', req.query);
let data = await this.db.get_pending_size();
res.send(data);
});
this.app.get('/api/v1/pending_tx_hash', async (req, res) => {
console.trace('pending_tx_hash', req.query);
let data = await this.db.get_pending_by_hash(req.query.hash);
res.send(data);
});
this.app.get('/api/v1/pending_tx_account', async (req, res) => {
console.trace('pending_tx_account', req.query);
let options = {
id : req.query.id,
filter : req.query.filter
};
let data = await this.db.get_pending_by_id(options);
res.send(data);
});
this.app.get('/api/v1/contract_pricelist', async (req, res) => {
console.trace('contract_pricelist');
let n = (await this.db.get_mblocks_height()).height;
let CMachine = ContractMachine.getContractMachine(this.config.FORKS, n);
let cont = new CMachine.Contract();
res.send(cont.pricelist);
});
this.app.get('/api/v1/difficulty', async (req, res) => {
console.trace('difficulty', req.query);
let data = await this.db.get_difficulty(100);
res.send(data);
});
this.app.get('/api/v1/get_top_pos', async (req, res) => {
console.trace('get_top_pos', req.query);
let top_pos = await this.db.get_top_pos();
let r = await this.db.get_total_pos_stake();
console.info(r.total_daily_pos_stake);
for(let i = 0; i < top_pos.length; i++){
top_pos[i].percent = top_pos[i].stake/r.total_daily_pos_stake * 100;
}
res.send({top_pos});
});
this.app.get('/api/v1/get_tokens_count', async (req, res) => {
console.trace('get_tokens_count', req.query);
let count = await this.db.get_tokens_count();
res.send(count);
});
this.app.get('/api/v1/get_token_holder_count', async (req, res) => {
console.trace('get token holder count', req.query);
let count = await this.db.get_accounts_count(req.query.token_hash);
res.send(count);
});
/**
* @swagger
* /get_tickers_all:
* get:
* description: Returns tickers
* tags:
* - Tickers
* produces:
* - application/json
* responses:
* 200:
* description: tickers
*/
this.app.get('/api/v1/get_tickers_all', async (req, res) => {
console.trace('get_tickers_all', req.query);
let ticker_all = await this.db.get_tickers_all();
let that = this;
ticker_all.filter(item => that.config.tokens_white_list.includes(item.hash));
res.send(ticker_all);
});
this.app.get('/api/v1/get_tickers_all_unfiltered', async (req, res) => {
console.trace('get_tickers_all_unfiltered', req.query);
let ticker_all = await this.db.get_tickers_all();
res.send(ticker_all);
});
/**
* @swagger
* /get_pos_total_stake:
* get:
* description: Return total stake
* tags:
* - Tickers
* produces:
* - application/json
* responses:
* 200:
* description: total_stake
*/
this.app.get('/api/v1/get_pos_total_stake', async (req, res) => {
console.trace('get_pos_total_stake', req.query);
let total_stake = await this.db.get_pos_total_stake();
res.send(total_stake);
});
this.app.get('/api/v1/get_pos_active_total_stake', async (req, res) => {
console.trace('get_pos_active_total_stake', req.query);
let total_stake = await this.db.get_pos_active_total_stake();
res.send(total_stake);
});
/**
* @swagger
* /get_pos_list_count:
* get:
* description: Return pos count
* tags:
* - Pos
* produces:
* - application/json
* responses:
* 200:
* description: pos_count
*/
this.app.get('/api/v1/get_pos_list_count', async (req, res) => {
console.trace('get_pos_list_count', req.query);
let cnt = await this.db.get_pos_contract_count();
res.send(cnt);
});
/**
* @swagger
* /get_pos_list_all:
* get:
* description: Return pos list all
* tags:
* - Pos
* parameters:
* - name: page_num
* produces:
* - application/json
* responses:
* 200:
* description: [{pos_id, pos_owner, fee, pos_stake}. …]
*/
this.app.get('/api/v1/get_pos_list_page', async (req, res) => {
console.trace('get_pos_list_page', req.query);
let data = await this.db.get_pos_contract_info_page(req.query.page, 20);
res.send(data);
});
this.app.get('/api/v1/get_pos_info', async (req, res) => {
console.trace('get_pos_info', req.query);
let data = await this.db.get_pos_contract_info(req.query.pos_id);
res.send(data);
});
this.app.get('/api/v1/get_pos_list_all', async (req, res) => {
console.trace('get_pos_list_all', req.query);
let data = await this.db.get_pos_contract_info_all();
res.send(data);
});
/**
* @swagger
* /get_pos_list:
* get:
* description: Return pos list
* tags:
* - Pos
* parameters:
* - name: owner
* description: owner addres (public key).
* produces:
* - application/json
* responses:
* 200:
* description: [{pos_id, pos_owner, fee, pos_stake}, ...]
*/
this.app.get('/api/v1/get_pos_list', async (req, res) => {
console.trace('get_pos_list', req.query);
let data = await this.db.get_pos_contract_list(req.query.owner);
res.send(data);
});
/**
* @swagger
* /get_delegated_list:
* get:
* description: Return delegated list
* tags:
* - Pos
* parameters:
* - name: delegator
* description: delegator addres (public key).
* produces:
* - application/json
* responses:
* 200:
* description: [{pos_id, amount}, …]
*/
this.app.get('/api/v1/get_delegated_list', async (req, res) => {
console.trace('get_delegated_list', req.query);
let data = await this.db.get_pos_delegated_list(req.query.delegator);
res.send(data);
});
/**
* @swagger
* /get_delegated_page:
* get:
* description: Return delegated page
* tags:
* - Pos
* parameters:
* - name: delegator
* description: delegator addres (public key).
* - name: page
* produces:
* - application/json
* responses:
* 200:
* description: {pos_degated: [{pos_id, amount}, …], page_count}
*/
this.app.get('/api/v1/get_delegated_page', async (req, res) => {
console.trace('get_delegated_page', req.query);
let data = await this.db.get_pos_delegated_page(req.query.delegator, req.query.page, 20);
res.send(data);
});
/**
* @swagger
* /get_undelegated_list:
* get:
* description: Return undelegated list
* tags:
* - Pos
* parameters:
* - name: delegator
* description: delegator addres (public key).
* produces:
* - application/json
* responses:
* 200:
* description: [{tx_hash, pos_id, amount, height, timestamp}, …]
*/
this.app.get('/api/v1/get_undelegated_list', async (req, res) => {
console.trace('get_undelegated_list', req.query);
let data = await this.db.get_pos_undelegated_list(req.query.delegator);
res.send(data);
});
/**
* @swagger
* /get_undelegated_page:
* get:
* description: Return undelegated page
* tags:
* - Pos
* parameters:
* - name: delegator
* description: delegator addres (public key).
* - name: page
* produces:
* - application/json
* responses:
* 200:
* description: {pos_undegated: [{pos_id, amount}, …], page_count}
*/
this.app.get('/api/v1/get_undelegated_page', async (req, res) => {
console.trace('get_undelegated_page', req.query);
let data = await this.db.get_pos_undelegated_page(req.query.delegator, req.query.page, 20);
res.send(data);
});
/**
* @swagger
* /get_delegators_list:
* get:
* description: Return delegators list
* tags:
* - Pos
* parameters:
* - name: pos_id
* description: pos_id (hash created tx).
* produces:
* - application/json
* responses:
* 200:
* description: [{delegator, amount}, …]
*/
this.app.get('/api/v1/get_delegators_list', async (req, res) => {
console.trace('get_delegators_list', req.query);
let data = await this.db.get_pos_delegators(req.query.pos_id);
res.send(data);
});
/**
* @swagger
* /get_delegators_page:
* get:
* description: Return delegators page
* tags:
* - Pos
* parameters:
* - name: pos_id
* description: pos_id (hash created tx).
* - name: page
* produces:
* - application/json
* responses:
* 200:
* description: {delegators:[{delegator, amount}, …], page_count}
*/
this.app.get('/api/v1/get_delegators_page', async (req, res) => {
console.trace('get_delegators_page', req.query);
let data = await this.db.get_pos_delegators_page(req.query.pos_id, req.query.page, 20);
res.send(data);
});
/**
* @swagger
* /get_transfer_lock:
* get:
* description: Return transfer lock
* tags:
* - Pos
* produces:
* - application/json
* responses:
* 200:
* description: {transfer_lock}
*/
this.app.get('/api/v1/get_transfer_lock', async (req, res) => {
console.trace('get_transfer_lock', req.query);
let transfer_lock = this.db.app_config.transfer_lock;
res.send({transfer_lock});
});
/**
* @swagger
* /get_pos_names:
* get:
* description: Return pos name list
* tags:
* - Pos
* produces:
* - application/json
* responses:
* 200:
* description: [{pos_id, pos_name},…]
*/
this.app.get('/api/v1/get_pos_names', async (req, res) => {
console.trace('get_pos_names', req.query);
let data = await this.db.get_pos_names();
res.send(data);
});
this.app.get('/api/v1/get_dex_pools', async (req, res) => {
console.trace('get_dex_pools', req.query);
let data = await this.db.dex_get_pools_all();
for (let rec of data) {
let tokens_info = await this.db.get_tokens_info([rec.asset_1, rec.asset_2]);
let price_token_1 = ((tokens_info.find(t => t.hash === rec.asset_1)).cg_price_usd !== null) ? (tokens_info.find(t => t.hash === rec.asset_1)).cg_price_usd : (tokens_info.find(t => t.hash === rec.asset_1)).dex_price_usd;
let price_token_2 = ((tokens_info.find(t => t.hash === rec.asset_2)).cg_price_usd !== null) ? (tokens_info.find(t => t.hash === rec.asset_2)).cg_price_usd : (tokens_info.find(t => t.hash === rec.asset_2)).dex_price_usd;
if( price_token_1 && price_token_2 ){
rec.liquidity = rec.volume_1 / Math.pow(10, tokens_info.find(t => t.hash === rec.asset_1).decimals) * price_token_1 +
rec.volume_2 / Math.pow(10, tokens_info.find(t => t.hash === rec.asset_2).decimals) * price_token_2;
} else if (price_token_1) {
rec.liquidity = rec.volume_1 / Math.pow(10, tokens_info.find(t => t.hash === rec.asset_1).decimals) * price_token_1 * 2;
} else if (price_token_2) {
rec.liquidity = rec.volume_2 / Math.pow(10, tokens_info.find(t => t.hash === rec.asset_2).decimals) * price_token_2 * 2;
} else
rec.liquidity = null;
}
res.send(data);
});
this.app.get('/api/v1/get_sstation_pools', async (req, res) => {
console.trace('get_sstation_pools', req.query);
let data = await this.db.dex_get_sstation_pools();
for (let rec of data) {
let tokens_info = await this.db.get_tokens_info([rec.asset_LP, rec.asset_ENX]);
let LP_price_token = ((tokens_info.find(t => t.hash === rec.asset_LP)).cg_price_usd !== null) ? (tokens_info.find(t => t.hash === rec.asset_LP)).cg_price_usd : (tokens_info.find(t => t.hash === rec.asset_LP)).dex_price_usd;
let ENX_price_token = ((tokens_info.find(t => t.hash === rec.asset_ENX)).cg_price_usd !== null) ? (tokens_info.find(t => t.hash === rec.asset_ENX)).cg_price_usd : (tokens_info.find(t => t.hash === rec.asset_ENX)).dex_price_usd;
rec.decimals_LP = tokens_info.find(t => t.hash === rec.asset_LP).decimals;
rec.ticker_LP = tokens_info.find(t => t.hash === rec.asset_LP).ticker;
rec.decimals_ENX = tokens_info.find(t => t.hash === rec.asset_ENX).decimals;
rec.ticker_ENX = tokens_info.find(t => t.hash === rec.asset_ENX).ticker;
if( LP_price_token && ENX_price_token ){
rec.liquidity = rec.volume_LP / Math.pow(10, tokens_info.find(t => t.hash === rec.asset_LP).decimals) * LP_price_token +
rec.volume_ENX / Math.pow(10, tokens_info.find(t => t.hash === rec.asset_ENX).decimals) * ENX_price_token;
} else if (LP_price_token) {
rec.liquidity = rec.volume_LP / Math.pow(10, tokens_info.find(t => t.hash === rec.asset_LP).decimals) * LP_price_token * 2;
} else if (ENX_price_token) {
rec.liquidity = rec.volume_ENX / Math.pow(10, tokens_info.find(t => t.hash === rec.asset_ENX).decimals) * ENX_price_token * 2;
} else
rec.liquidity = null;
}
res.send(data);
});
this.app.get('/api/v1/get_farms', async (req, res) => {
console.trace('get_farms', req.query);
let data = await this.db.get_farms_all();
res.send(data);
});
this.app.get('/api/v1/get_dex_farms', async (req, res) => {
console.trace('get_dex_farms', req.query);
//filter white list
let data = await this.db.get_dex_farms(req.query.farmer_id, req.query.farms);
let n = (await this.db.get_mblocks_height()).height;
for (let rec of data) {
rec.apy = null;
rec.liquidity = null;
rec.earned = null;
rec.blocks_left = null;
if(rec.total_stake > 0) {
//Token Price
let tokens_info = await this.db.get_tokens_info([rec.reward_token_hash, rec.stake_token_hash]);
let reward_token_price = ((tokens_info.find(t => t.hash === rec.reward_token_hash)).cg_price_usd !== null) ? (tokens_info.find(t => t.hash === rec.reward_token_hash)).cg_price_usd : (tokens_info.find(t => t.hash === rec.reward_token_hash)).dex_price_usd;
reward_token_price = reward_token_price === undefined ? 0 : reward_token_price / 1e10;