-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
1392 lines (1196 loc) · 42 KB
/
api.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
const { Router } = require('express');
const Client = require('bitcoin-core');
const sqlite3 = require('better-sqlite3');
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const { Queries } = require('./queries');
const client = new Client({
host: 'bitcoind',
network: 'mainnet',
username: 'rpc',
password: 'rpc'
});
// only connect to counterparty db if bitcoin is synced
let db;
Promise.resolve()
.then(() => {
return client.command([
{ method: 'getblockchaininfo', parameters: [] },
]);
})
.then((results) => {
if (!results[0].initialblockdownload) {
const DB_PATH = '/data/counterparty/counterparty.db';
db = sqlite3(DB_PATH, { readonly: true }); // read only!
}
});
const BITCOIN_VERSION = '0.21.1';
const COUNTERPARTY_VERSION = '10.1.2.CNTRPRTY';
const apiRouter = Router();
// DRY utils
async function getAssetMetadataMaybeQuery(db, asset_name) {
let start;
let end;
let asset_metadata;
let query1_timems = null;
let query2_timems = null;
if (asset_name === 'BTC') {
asset_metadata = {
asset: 'BTC',
asset_longname: null,
divisible: true,
};
}
else if (asset_name === 'XCP') {
asset_metadata = {
asset: 'XCP',
asset_longname: null,
divisible: true,
};
}
else {
start = new Date().getTime();
asset_metadata = await Queries.getIssuanceMetadataByAssetName(db, asset_name);
end = new Date().getTime();
query1_timems = end - start;
// depends on COUNTERPARTY_VERSION
// detecting reset assets (this project started from 9.59.6 and then 9.60 added reset)
if (!COUNTERPARTY_VERSION.startsWith('9.59')) {
start = new Date().getTime();
const query2 = await Queries.getIssuanceMetadataResetsCheck(db, asset_name);
end = new Date().getTime();
query2_timems = end - start;
if (query2.length) {
asset_metadata.resets = query2;
}
}
}
return {
asset_metadata,
query1_timems,
query2_timems,
};
}
function timeIsoFormat(block_time) {
return (new Date(block_time * 1000).toISOString()).replace('.000Z', 'Z');
}
function range(start, stop, step) {
// range(0, 4, 1) => [0, 1, 2, 3, 4]
return Array.from({ length: (stop - start) / step + 1 }, (__, i) => start + (i * step));
}
apiRouter.get('/', async (req, res) => {
const bitcoind = await client.command([
{ method: 'getnetworkinfo', parameters: [] },
{ method: 'getblockchaininfo', parameters: [] },
]);
res.status(200).json({
// doc: 'https://xcp.dev/api',
node: { // only at tip (because is in the url now)
BITCOIN_VERSION,
COUNTERPARTY_VERSION,
bitcoind: {
getnetworkinfo: bitcoind[0],
getblockchaininfo: bitcoind[1],
},
},
});
// res.status(200).json({
// api: true,
// });
});
function serviceUnavailable(res) {
res.status(503).json({
error: '503 Service Unavailable',
});
}
apiRouter.get('/tip', async (req, res) => {
if (!db) return serviceUnavailable(res);
const start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
const end = new Date().getTime();
res.status(200).json({
tip_blocks_row,
query_timems: end - start,
});
});
///////////////////////////////////////
// homepage minute scheduled cached data
///////////////////////////////////////
async function updateMempoolCache() {
const start = new Date().getTime();
const lib_response = await libApiRequest('get_memmempool', {});
const end = new Date().getTime();
if (lib_response.result) {
cached_mempool = lib_response.result.result.cached_response;
// cached_mempool = lib_response.result.cached_response;
cached_mempool_timems = end - start;
}
// const start = new Date().getTime();
// const mempool = await Queries.getMempoolRows(db);
// const end = new Date().getTime();
// cached_mempool = mempool;
// cached_mempool_timems = end - start;
}
async function updateBlocksCache() {
let start;
let end;
start = new Date().getTime();
let blocks_all = await Queries.getBlocksLatest(db);
end = new Date().getTime();
cached_blocks_query1_timems = end - start;
const from_block_index_row = blocks_all[blocks_all.length - 1];
const to_block_index_row = blocks_all[0];
start = new Date().getTime();
const transactions_per_block = await Queries.getTransactionsCountFromBlockRange(db, from_block_index_row.block_index, to_block_index_row.block_index);
// const transactions_per_block = await Queries.getTransactionsCountFromBlockToTip(db, from_block_index_row.block_index);
end = new Date().getTime();
cached_blocks_query2_timems = end - start;
start = new Date().getTime();
const messages_per_block = await Queries.getMessagesCountFromBlockRange(db, from_block_index_row.block_index, to_block_index_row.block_index);
// const messages_per_block = await Queries.getMessagesCountFromBlockToTip(db, from_block_index_row.block_index);
end = new Date().getTime();
cached_blocks_query3_timems = end - start;
const block_transactions_dict = {};
for (const block of transactions_per_block) {
block_transactions_dict[block.block_index] = block.transactions;
}
const block_messages_dict = {};
for (const block of messages_per_block) {
block_messages_dict[block.block_index] = block.messages;
}
blocks_all = blocks_all.map((row) => {
let transactions_count = block_transactions_dict[row.block_index] ? block_transactions_dict[row.block_index] : 0;
let messages_count = block_messages_dict[row.block_index] ? block_messages_dict[row.block_index] : 0;
return {
...row,
transactions_count,
messages_count,
};
});
cached_blocks = blocks_all;
}
async function updateTransactionsCache() {
const start = new Date().getTime();
const btc_transactions_latest = await Queries.getTransactionsLatest(db);
const end = new Date().getTime();
cached_transactions = btc_transactions_latest;
cached_transactions_timems = end - start;
}
// cached responses
let cached_mempool = [];
let cached_mempool_timems = null;
let cached_blocks = [];
let cached_blocks_query1_timems = null;
let cached_blocks_query2_timems = null;
let cached_blocks_query3_timems = null;
let cached_transactions = [];
let cached_transactions_timems = null;
async function updateMinuteCache() {
if (db) {
await updateMempoolCache();
await updateBlocksCache();
await updateTransactionsCache();
}
}
///////////////////////////////////////
///////////////////////////////////////
apiRouter.get('/mempool', async (req, res) => {
if (!db) return serviceUnavailable(res);
res.status(200).json({
mempool: cached_mempool,
query_timems: cached_mempool_timems,
});
});
apiRouter.get('/blocks/:blockIndex', async (req, res) => {
// app.get('/blocks/:blockTime', async (req, res) => { // would be cool but is not indexed... (also good for clear difference to /block/blockIndex)
if (!db) return serviceUnavailable(res);
let start;
let end;
let block_index;
try {
block_index = Number(req.params.blockIndex);
if (Number.isNaN(block_index) || block_index < 0) throw Error();
}
catch (err) {
res.status(400).json({
error: '400 Bad Request',
});
return;
}
// const block_index = Number(req.params.blockIndex);
const to_index = Number(block_index) + 99;
start = new Date().getTime();
let blocks_all = await Queries.getBlocksInRange(db, block_index, to_index);
end = new Date().getTime();
const query1_timems = end - start;
// get counts
start = new Date().getTime();
const transactions_per_block = await Queries.getTransactionsCountFromBlockRange(db, block_index, to_index);
end = new Date().getTime();
const query2_timems = end - start;
start = new Date().getTime();
const messages_per_block = await Queries.getMessagesCountFromBlockRange(db, block_index, to_index);
end = new Date().getTime();
const query3_timems = end - start;
const block_transactions_dict = {};
for (const block of transactions_per_block) {
block_transactions_dict[block.block_index] = block.transactions;
}
const block_messages_dict = {};
for (const block of messages_per_block) {
block_messages_dict[block.block_index] = block.messages;
}
blocks_all = blocks_all.map((row) => {
let transactions_count = block_transactions_dict[row.block_index] ? block_transactions_dict[row.block_index] : 0;
let messages_count = block_messages_dict[row.block_index] ? block_messages_dict[row.block_index] : 0;
return {
...row,
transactions_count,
messages_count,
};
});
res.status(200).json({
node: {
BITCOIN_VERSION,
COUNTERPARTY_VERSION,
},
from_index: block_index,
to_index,
blocks: blocks_all,
query1_timems,
query2_timems,
query3_timems,
});
});
apiRouter.get('/blocks', async (req, res) => {
if (!db) return serviceUnavailable(res);
res.status(200).json({
blocks: cached_blocks,
query1_timems: cached_blocks_query1_timems,
query2_timems: cached_blocks_query2_timems,
query3_timems: cached_blocks_query3_timems,
});
});
apiRouter.get('/block/:blockIndex/transactions', async (req, res) => {
if (!db) return serviceUnavailable(res);
const block_index = req.params.blockIndex;
const start = new Date().getTime();
const transactions = await Queries.getTransactionsRowsByBlock(db, block_index);
const end = new Date().getTime();
res.status(200).json({
transactions,
query_timems: end - start,
});
});
apiRouter.get('/block/:blockIndex/messages', async (req, res) => {
if (!db) return serviceUnavailable(res);
const block_index = req.params.blockIndex;
const start = new Date().getTime();
const messages = await Queries.getMessagesRowsByBlock(db, block_index);
const end = new Date().getTime();
res.status(200).json({
messages,
query_timems: end - start,
});
});
apiRouter.get('/block/:blockIndex', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
let block_index;
try {
block_index = Number(req.params.blockIndex);
if (Number.isNaN(block_index) || block_index < 0) throw Error();
}
catch (err) {
res.status(400).json({
error: '400 Bad Request',
});
return;
}
const BLOCK_FIRST_MAINNET = 278270;
if (block_index < BLOCK_FIRST_MAINNET) {
start = new Date().getTime();
const command1 = await client.command([
{ method: 'getblockhash', parameters: [block_index] },
]);
const command2 = await client.command([
{ method: 'getblock', parameters: [command1[0], 1] },
]);
end = new Date().getTime();
const bitcoind_block = command2[0];
// simulating a "row"
const block_row = {
block_index,
block_hash: bitcoind_block.hash,
block_time: bitcoind_block.time,
previous_block_hash: bitcoind_block.previousblockhash,
difficulty: bitcoind_block.difficulty,
ledger_hash: "",
txlist_hash: "",
messages_hash: ""
};
res.status(200).json({
block_row,
timems: end - start,
});
return;
}
start = new Date().getTime();
const block_row = await Queries.getBlocksRow(db, block_index);
end = new Date().getTime();
if (!block_row) {
res.status(404).json({
error: '404 Not Found'
});
}
else {
res.status(200).json({
block_row,
query_timems: end - start,
});
}
});
apiRouter.get('/blockhash/:blockHash', async (req, res) => {
if (!db) return serviceUnavailable(res);
const block_hash = req.params.blockHash;
const start = new Date().getTime();
const block_row = await Queries.getBlocksRowByBlockHash(db, block_hash);
const end = new Date().getTime();
if (!block_row) {
res.status(404).json({
error: '404 Not Found'
});
}
else {
res.status(200).json({
block_row,
query_timems: end - start,
});
}
});
apiRouter.get('/transactions/dispensers/:txHash', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const tx_hash = req.params.txHash;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
start = new Date().getTime();
const dispensers_row = await Queries.getDispensersRow(db, tx_hash);
end = new Date().getTime();
const dispensers_row_timems = end - start;
if (!dispensers_row) {
res.status(404).json({
error: '404 Not Found'
});
}
else {
const asset_metadata_obj = await getAssetMetadataMaybeQuery(db, dispensers_row.asset);
const asset_metadata_obj_query1_timems = asset_metadata_obj.query1_timems;
const asset_metadata_obj_query2_timems = asset_metadata_obj.query2_timems;
start = new Date().getTime();
const dispenses_rows = await Queries.getDispensesRows(db, tx_hash);
end = new Date().getTime();
const dispenses_rows_timems = end - start;
res.status(200).json({
tip_blocks_row,
query1_timems: tip_blocks_row_timems,
dispensers_row,
query2_timems: dispensers_row_timems,
// TODO:
// - should be renamed BTC/XCP are not issuances...
// - row/rows needs to be consistent!
issuances_row: [asset_metadata_obj.asset_metadata],
query3_timems: asset_metadata_obj_query1_timems,
query4_timems: asset_metadata_obj_query2_timems,
// issuances_row,
dispenses_rows,
query5_timems: dispenses_rows_timems,
});
}
});
apiRouter.get('/transactions/orders/:txHash', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const tx_hash = req.params.txHash;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
start = new Date().getTime();
const orders_row = await Queries.getOrdersRow(db, tx_hash);
end = new Date().getTime();
const orders_row_timems = end - start;
const get_asset_metadata_obj = await getAssetMetadataMaybeQuery(db, orders_row.get_asset);
const get_asset_metadata_obj_query1_timems = get_asset_metadata_obj.query1_timems;
const get_asset_metadata_obj_query2_timems = get_asset_metadata_obj.query2_timems;
const give_asset_metadata_obj = await getAssetMetadataMaybeQuery(db, orders_row.give_asset);
const give_asset_metadata_obj_query1_timems = give_asset_metadata_obj.query1_timems;
const give_asset_metadata_obj_query2_timems = give_asset_metadata_obj.query2_timems;
start = new Date().getTime();
const order_matches_rows = await Queries.getOrderMatchesRows(db, tx_hash);
end = new Date().getTime();
const order_matches_rows_timems = end - start;
let btcpays_rows = [];
let btcpays_rows_timems = null;
if (
orders_row.get_asset === 'BTC' ||
orders_row.give_asset === 'BTC'
) {
start = new Date().getTime();
btcpays_rows = await Queries.getOrderMatchesBtcpaysRows(db, tx_hash);
end = new Date().getTime();
btcpays_rows_timems = end - start;
}
if (!orders_row) {
res.status(404).json({
error: '404 Not Found'
});
}
else {
res.status(200).json({
tip_blocks_row,
query1_timems: tip_blocks_row_timems,
orders_row,
query2_timems: orders_row_timems,
// TODO:
// - should be renamed BTC/XCP are not issuances...
// - row/rows needs to be consistent!
get_issuances_row: [get_asset_metadata_obj.asset_metadata],
query3_timems: get_asset_metadata_obj_query1_timems,
query4_timems: get_asset_metadata_obj_query2_timems,
// get_issuances_row,
give_issuances_row: [give_asset_metadata_obj.asset_metadata],
query5_timems: give_asset_metadata_obj_query1_timems,
query6_timems: give_asset_metadata_obj_query2_timems,
// give_issuances_row,
order_matches_rows,
query7_timems: order_matches_rows_timems,
btcpays_rows,
query8_timems: btcpays_rows_timems,
});
}
});
apiRouter.get('/transactions/:txIndex', async (req, res) => {
if (!db) return serviceUnavailable(res);
let tx_index;
try {
tx_index = Number(req.params.txIndex);
if (Number.isNaN(tx_index) || tx_index < 0) throw Error();
}
catch (err) {
res.status(400).json({
error: '400 Bad Request',
});
return;
}
// const tx_index = Number(req.params.txIndex);
// get the transactions including the tx and the next 100 transactions
const to_index = Number(tx_index) + 99;
// const to_index = Number(tx_index) + 999;
const start = new Date().getTime();
const transactions = await Queries.getTransactionsFromTxIndexToTxIndex(db, tx_index, to_index);
const end = new Date().getTime();
res.status(200).json({
node: {
BITCOIN_VERSION,
COUNTERPARTY_VERSION,
},
from_index: tx_index,
to_index,
transactions,
query_timems: end - start,
});
});
apiRouter.get('/transactions', async (req, res) => {
if (!db) return serviceUnavailable(res);
res.status(200).json({
transactions: cached_transactions,
query_timems: cached_transactions_timems,
});
});
apiRouter.get('/txindex/:txIndex', async (req, res) => {
if (!db) return serviceUnavailable(res);
const tx_index = req.params.txIndex;
const start = new Date().getTime();
const transaction_row = await Queries.getTransactionsRowByTxIndex(db, tx_index);
const end = new Date().getTime();
if (!transaction_row) {
res.status(404).json({
error: '404 Not Found'
});
}
else {
res.status(200).json({
transaction_row,
query_timems: end - start,
});
}
});
apiRouter.get('/tx/:txHash', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
let transaction_timems;
let mempool_timems = null;
const tx_hash = req.params.txHash;
// transaction could be in the mempool
// but first try get direct from table
let mempool = [];
start = new Date().getTime();
const transaction = await Queries.getTransactionsRow(db, tx_hash);
end = new Date().getTime();
transaction_timems = end - start;
if (!transaction) { // try if is in mempool
start = new Date().getTime();
mempool = await Queries.getMempoolRowsByTxHash(db, tx_hash);
end = new Date().getTime();
mempool_timems = end - start;
}
if (!transaction && !mempool.length) {
res.status(404).json({
error: '404 Not Found'
});
}
else {
res.status(200).json({
transaction,
query1_timems: transaction_timems,
mempool,
query2_timems: mempool_timems,
});
}
});
apiRouter.get('/address/:address/dispensers/open', async (req, res) => {
if (!db) return serviceUnavailable(res);
const address = req.params.address;
const start = new Date().getTime();
const dispensers_open = await Queries.getOpenDispensersRowsByAddress(db, address);
const end = new Date().getTime();
res.status(200).json({
dispensers_open,
query_timems: end - start,
});
});
apiRouter.get('/address/:address/dispensers/closed', async (req, res) => {
if (!db) return serviceUnavailable(res);
const address = req.params.address;
const start = new Date().getTime();
const dispensers_closed = await Queries.getClosedDispensersRowsByAddress(db, address);
const end = new Date().getTime();
res.status(200).json({
dispensers_closed,
query_timems: end - start,
});
});
apiRouter.get('/address/:address/broadcasts', async (req, res) => {
if (!db) return serviceUnavailable(res);
const address = req.params.address;
const start = new Date().getTime();
const broadcasts = await Queries.getBroadcastsRowsByAddress(db, address);
const end = new Date().getTime();
res.status(200).json({
broadcasts,
query_timems: end - start,
});
});
apiRouter.get('/address/:address/issuances', async (req, res) => {
if (!db) return serviceUnavailable(res);
const address = req.params.address;
const start = new Date().getTime();
const issuances = await Queries.getIssuancesRowsByAssetsByIssuer(db, address);
const end = new Date().getTime();
res.status(200).json({
issuances,
query_timems: end - start,
});
});
apiRouter.get('/address/:address/balances', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
let query1_timems;
let query2_timems;
let query3_timems = null;
const address = req.params.address;
start = new Date().getTime();
let query1 = await Queries.getBalancesRowsByAddressWithoutXcp(db, address);
end = new Date().getTime();
query1_timems = end - start;
start = new Date().getTime();
const query2 = await Queries.getBalancesRowsByAddressXcp(db, address);
end = new Date().getTime();
query2_timems = end - start;
// depends on COUNTERPARTY_VERSION
// detecting reset assets (this project started from 9.59.6 and then 9.60 added reset)
if (!COUNTERPARTY_VERSION.startsWith('9.59')) {
start = new Date().getTime();
const query3 = await Queries.getBalancesResetsCheck(db, address);
end = new Date().getTime();
query3_timems = end - start;
// making the above query already affects EVERYONE (in the latest COUNTERPARTY_VERSION), but the next only affects people that ACTUALLY have/had reset assets
if (query3.length) {
const reset_dict = {};
for (const reset_row of query3) {
if (reset_dict[reset_row.asset]) {
reset_dict[reset_row.asset].push(reset_row);
}
else {
reset_dict[reset_row.asset] = [reset_row];
}
}
query1 = query1.map(row => {
if (reset_dict[row.asset]) {
row.resets = reset_dict[row.asset];
}
return row;
});
}
}
const balances = [
...query1,
...query2.map(row => {
return {
...row,
asset_longname: null,
divisible: true,
}
}
),
];
res.status(200).json({
balances,
query1_timems,
query2_timems,
query3_timems,
});
});
apiRouter.get('/assets/:fromString', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const from_string = req.params.fromString;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
start = new Date().getTime();
const assets = await Queries.getAssetsRange(db, from_string);
end = new Date().getTime();
const from_asset_adjusted = assets.length ? assets[0].asset_name : from_string;
const to_asset = assets.length ? assets[assets.length - 1].asset_name : from_string;
res.status(200).json({
from_asset: from_asset_adjusted,
to_asset,
tip_blocks_row,
query1_timems: tip_blocks_row_timems,
assets,
query2_timems: end - start,
});
});
// app.get('/asset/:assetName/dispensers/open', async (req, res) => {
apiRouter.get('/asset/:assetName/escrows/dispensers', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const asset_name = req.params.assetName;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
start = new Date().getTime();
const dispensers_open = await Queries.getDispensersRowsByAssetName(db, asset_name);
end = new Date().getTime();
const dispensers_open_timems = end - start;
res.status(200).json({
tip_blocks_row,
query1_timems: tip_blocks_row_timems,
dispensers_open,
query2_timems: dispensers_open_timems,
});
});
// app.get('/asset/:assetName/orders/give', async (req, res) => {
apiRouter.get('/asset/:assetName/escrows/orders', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const asset_name = req.params.assetName;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
start = new Date().getTime();
const orders_give_open = await Queries.getOrdersRowsGiveAssetByAssetName(db, asset_name);
end = new Date().getTime();
const orders_give_open_timems = end - start;
res.status(200).json({
tip_blocks_row,
query1_timems: tip_blocks_row_timems,
orders_give_open,
query2_timems: orders_give_open_timems,
});
});
// app.get('/asset/:assetName/orders/get', async (req, res) => {
apiRouter.get('/asset/:assetName/exchanges', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const asset_name = req.params.assetName;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
start = new Date().getTime();
const orders_get_open = await Queries.getOrdersRowsGetAssetByAssetName(db, asset_name);
end = new Date().getTime();
const orders_get_open_timems = end - start;
res.status(200).json({
tip_blocks_row,
query1_timems: tip_blocks_row_timems,
orders_get_open,
query2_timems: orders_get_open_timems,
});
});
apiRouter.get('/asset/:assetName/issuances', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const asset_name = req.params.assetName;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
start = new Date().getTime();
const issuances = await Queries.getIssuancesRowsByAssetName(db, asset_name);
end = new Date().getTime();
const issuances_timems = end - start;
res.status(200).json({
tip_blocks_row, // included in all asset page calls for client side verification (but still not perfect)
query1_timems: tip_blocks_row_timems,
issuances,
query2_timems: issuances_timems,
});
});
apiRouter.get('/asset/:assetName/destructions', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const asset_name = req.params.assetName;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
start = new Date().getTime();
const destructions = await Queries.getDestructionsRowsByAssetName(db, asset_name);
end = new Date().getTime();
const destructions_timems = end - start;
res.status(200).json({
tip_blocks_row,
query1_timems: tip_blocks_row_timems,
destructions,
query2_timems: destructions_timems,
});
});
apiRouter.get('/asset/:assetName/balances', async (req, res) => {
if (!db) return serviceUnavailable(res);
let start;
let end;
const asset_name = req.params.assetName;
start = new Date().getTime();
const tip_blocks_row = await Queries.getBlocksRowTip(db);
end = new Date().getTime();
const tip_blocks_row_timems = end - start;
const asset_metadata_obj = await getAssetMetadataMaybeQuery(db, asset_name);
const asset_metadata_obj_query1_timems = asset_metadata_obj.query1_timems;
const asset_metadata_obj_query2_timems = asset_metadata_obj.query2_timems;
start = new Date().getTime();
const balances_rows = await Queries.getBalancesRowsByAssetName(db, asset_name);
end = new Date().getTime();
const balances_rows_timems = end - start;
res.status(200).json({
tip_blocks_row,
query1_timems: tip_blocks_row_timems,
asset_metadata: asset_metadata_obj,
query2_timems: asset_metadata_obj_query1_timems,
query3_timems: asset_metadata_obj_query2_timems,
balances_rows,
query4_timems: balances_rows_timems,
});
});
apiRouter.get('/asset/:assetName/subassets', async (req, res) => {
if (!db) return serviceUnavailable(res);
const asset_name = req.params.assetName;
const start = new Date().getTime();
const assets = await Queries.getAssetsRowsForAssetLongname(db, asset_name);
const end = new Date().getTime();
res.status(200).json({
assets,
query_timems: end - start,
});
});
apiRouter.get('/asset/:assetName', async (req, res) => {
if (!db) return serviceUnavailable(res);
const asset_name = req.params.assetName;
const start = new Date().getTime();
const asset_row = await Queries.getAssetsRowByAssetName(db, asset_name);
const end = new Date().getTime();
if (!asset_row) {
res.status(404).json({
error: '404 Not Found'
});
}
else {
res.status(200).json({
asset_row,
query_timems: end - start,
});
}
});
apiRouter.get('/subasset/:assetLongname', async (req, res) => {
if (!db) return serviceUnavailable(res);
const asset_longname = req.params.assetLongname;
const start = new Date().getTime();