evm-chains-info 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
 
3
-
4
3
  // SPDX-License-Identifier: AGPL-3.0-or-later
5
4
 
6
5
  /** ----------------------------------------------------------------
@@ -29,87 +28,198 @@
29
28
  * If not, see <https://www.gnu.org/licenses/>.
30
29
  */
31
30
 
32
- // const
33
- // _libcrash =
34
- // require(
35
- // '../libcrash-js/crash-js');
31
+ /* eslint-disable
32
+ no-unused-vars */
33
+
34
+ /* global
35
+ chain_id */
36
+ /* global
37
+ db_paths */
38
+ /* global
39
+ input_type:
40
+ writable */
41
+ /* global
42
+ user_level */
43
+ /* global
44
+ target_chain */
45
+
36
46
  const
37
47
  _libcrash_module =
38
48
  require(
39
49
  "crash-js");
50
+ /* global
51
+ _homedir_get */
52
+ /* eslint-disable-next-line
53
+ no-global-assign */
54
+ _homedir_get =
55
+ _libcrash_module._homedir_get;
56
+ /* global
57
+ _file_exists */
58
+ /* eslint-disable-next-line
59
+ no-global-assign */
40
60
  _file_exists =
41
61
  _libcrash_module._file_exists;
62
+ /* global
63
+ _json_read */
64
+ /* eslint-disable-next-line
65
+ no-global-assign */
42
66
  _json_read =
43
67
  _libcrash_module._json_read;
68
+ /* global
69
+ _msg_info */
70
+ /* eslint-disable-next-line
71
+ no-global-assign */
72
+ _msg_info =
73
+ _libcrash_module._msg_info;
74
+ /* global
75
+ _msg_error */
76
+ /* eslint-disable-next-line
77
+ no-global-assign */
78
+ _msg_error =
79
+ _libcrash_module._msg_error;
80
+ /* global
81
+ _path_join */
82
+ /* eslint-disable-next-line
83
+ no-global-assign */
44
84
  _path_join =
45
85
  _libcrash_module._path_join;
46
86
 
87
+ const
88
+ _ethereum_data_module =
89
+ require(
90
+ "ethereum-data");
91
+ /* global
92
+ _data_get */
93
+ const
94
+ _data_get =
95
+ _ethereum_data_module._data_get;
96
+
47
97
  function
48
98
  _evm_chains_db_path_get(
49
- _user) {
99
+ _user,
100
+ _chain_id,
101
+ _format) {
50
102
  let
51
- _db_exists,
52
- _path;
53
- if ( _user == undefined ) {
103
+ _db_name,
104
+ _db_path;
105
+ if ( typeof(
106
+ _user) == "undefined" ) {
54
107
  _user =
55
108
  false;
56
109
  }
110
+ if ( typeof(
111
+ _chain_id) == "undefined" ) {
112
+ _chain_id =
113
+ "";
114
+ }
115
+ if ( typeof(
116
+ _format) == "undefined" ) {
117
+ if ( _chain_id != "" ) {
118
+ _format =
119
+ "split";
120
+ }
121
+ else if ( _chain_id == "" ) {
122
+ _format =
123
+ "unified";
124
+ }
125
+ }
126
+ if ( _format == "unified" ) {
127
+ _db_name =
128
+ "chains.json";
129
+ }
130
+ else if ( _format == "split" ) {
131
+ _db_name =
132
+ `${_chain_id}.json`;
133
+ }
57
134
  if ( _user == true ) {
58
- _path =
135
+ _db_path =
59
136
  _path_join(
60
- [process.env.HOME,
61
- ".local",
62
- "share",
63
- "evm-chains",
64
- "chains.json"])
65
- _db_exists =
66
- _file_exists(
67
- _db_path);
137
+ [ _homedir_get(),
138
+ ".config",
139
+ "evm-chains",
140
+ _db_name ]);
141
+ const
142
+ _db_exists =
143
+ _file_exists(
144
+ _db_path);
68
145
  if ( _db_exists == false ) {
69
146
  _user =
70
147
  false;
71
148
  }
72
149
  }
73
150
  if ( _user == false ) {
74
- _path =
151
+ _db_path =
75
152
  _path_join(
76
- [process.env.NODE_PATH,
77
- "..",
78
- "evm-chains",
79
- "chains.json"])
153
+ [ process.env.NODE_PATH,
154
+ "..",
155
+ "evm-chains",
156
+ "chains.json" ]);
80
157
  }
81
158
  else {
159
+ const
160
+ _msg =
161
+ "Input variable '_user' must be a boolean.";
82
162
  _msg_error(
83
- "Input variable '_user' must be a boolean",
163
+ _msg,
84
164
  0);
85
165
  }
86
- return _path;
166
+ return _db_path;
87
167
  }
88
168
 
89
169
  function
90
170
  _evm_chain_info_get(
91
171
  _chain_id,
92
172
  _chain_info,
93
- _db_path) {
173
+ _db_path,
174
+ _db_types) {
94
175
  let
95
- _db,
96
- _db_exists;
97
- if ( _db_path == undefined ) {
176
+ _data,
177
+ _msg;
178
+ if ( typeof(
179
+ _db_path) == "undefined" ) {
98
180
  _db_path =
99
181
  _evm_chains_db_path_get(
100
182
  false);
101
183
  }
102
- _db_exists =
103
- _file_exists(
104
- _db_path);
184
+ const
185
+ _db_exists =
186
+ _file_exists(
187
+ _db_path);
105
188
  if ( _db_exists == false ) {
106
- _msg_error(
107
- "Database file does not exists.",
108
- 1);
189
+ if ( typeof window === 'undefined' &&
190
+ ( typeof global !== 'undefined' && global.global === global ) &&
191
+ typeof __webpack_require__ !== 'function' ) {
192
+ _data =
193
+ _data_get(
194
+ _chain_id);
195
+ _msg =
196
+ `Displaying data for ` +
197
+ `chain '${_chain_id}' `+
198
+ `of type '${_db_types}'.`;
199
+ _msg_info(
200
+ _msg);
201
+ console.log(
202
+ _data);
203
+ }
204
+ // _msg_error(
205
+ // "Database file does not exists.",
206
+ // 1);
109
207
  }
110
- _db =
111
- _json_read(
112
- _db_path);
208
+ const
209
+ _db =
210
+ _json_read(
211
+ _db_path);
212
+ // _data =
213
+ // _data_get(
214
+ // _chain_id);
215
+ _msg =
216
+ `Displaying data for ` +
217
+ `chain '${_chain_id}' `+
218
+ `of type '${_db_types}'.`;
219
+ _msg_info(
220
+ _msg);
221
+ console.log(
222
+ _db);
113
223
  for ( _chain_info of _db) {
114
224
  if ( _chain_info["chainId"] == _chain_id ) {
115
225
  return _chain_info;
@@ -124,13 +234,14 @@ function
124
234
  _rpc_backend_get(
125
235
  _network_chain_id,
126
236
  _selection_method,
127
- _db_path) {
128
- let
129
- _chain_id,
130
- _chain_info;
237
+ _db_path,
238
+ _db_types) {
131
239
  if ( _network_chain_id == undefined ) {
240
+ const
241
+ _msg =
242
+ "Network's ChainID not specified.";
132
243
  _msg_error(
133
- "Network's ChainID not specified.",
244
+ _msg,
134
245
  1);
135
246
  }
136
247
  if ( _selection_method == undefined ) {
@@ -142,13 +253,20 @@ function
142
253
  _evm_chains_db_path_get(
143
254
  false);
144
255
  }
145
- _chain_id =
146
- Number(
147
- _network_chain_id);
148
- _chain_info =
149
- _evm_chain_info_get(
150
- _chain_id,
151
- _db_path);
256
+ if ( _db_types == undefined ) {
257
+ _db_types.push(
258
+ "system-db");
259
+ }
260
+ const
261
+ _chain_id =
262
+ Number(
263
+ _network_chain_id);
264
+ const
265
+ _chain_info =
266
+ _evm_chain_info_get(
267
+ _chain_id,
268
+ _db_path,
269
+ _db_types);
152
270
  if ( _selection_method == "kirsh" ) {
153
271
  return _chain_info["rpc"][0];
154
272
  }
@@ -166,7 +284,7 @@ function _rpc_backends_get(
166
284
  let
167
285
  _chain_id,
168
286
  _rpc_backend_get_opts,
169
- _rpcs;
287
+ _rpc_backend;
170
288
  if ( _selection_method == undefined ) {
171
289
  _selection_method =
172
290
  "kirsh";
@@ -176,8 +294,9 @@ function _rpc_backends_get(
176
294
  _evm_chains_db_path_get(
177
295
  false);
178
296
  }
179
- _rpcs =
180
- [];
297
+ const
298
+ _rpcs =
299
+ [];
181
300
  for ( _chain_id of _networks_chain_ids ) {
182
301
  _rpc_backend_get_opts = [
183
302
  _chain_id,
@@ -188,17 +307,354 @@ function _rpc_backends_get(
188
307
  _rpc_backend_get.apply(
189
308
  null,
190
309
  _rpc_backend_get_opts);
191
- _rpcs.append(
310
+ _rpcs.push(
192
311
  _rpc_backend);
193
312
  }
194
313
  return _rpcs;
195
314
  }
196
315
 
316
+ function
317
+ _db_path_check(
318
+ _db_path) {
319
+ if ( ! _file_exists(
320
+ _db_path) ) {
321
+ const
322
+ _msg =
323
+ `Database at ` +
324
+ `'${_db_path}' does not exist.`;
325
+ _msg_error(
326
+ _msg,
327
+ 1);
328
+ }
329
+ }
330
+
331
+ function
332
+ _all_get(
333
+ _filter_output_type) {
334
+ console.log(
335
+ _filter_output_type);
336
+ }
337
+
338
+ function
339
+ _db_get(
340
+ _db_path,
341
+ _input_type,
342
+ _output_type,
343
+ _selection_method,
344
+ _api_key,
345
+ _networks_all,
346
+ _target_chain) {
347
+ let
348
+ _filter,
349
+ _filter_key,
350
+ _filter_output_type,
351
+ _results,
352
+ _msg;
353
+ _filter_output_type =
354
+ _output_type;
355
+ if ( _input_type == "id" ) {
356
+ /* eslint-disable-next-line
357
+ no-useless-assignment */
358
+ _filter_key =
359
+ "chainId";
360
+ _filter_key =
361
+ _target_chain;
362
+ _msg =
363
+ `Filter key: ${_filter_key}`;
364
+ _msg_info(
365
+ _msg);
366
+ }
367
+ else if ( _input_type == "testnet" ) {
368
+ _msg =
369
+ "Like I have to agg a tag.";
370
+ _msg_info(
371
+ _msg);
372
+ }
373
+ if ( _output_type == "id" ) {
374
+ _filter_output_type =
375
+ "chainId";
376
+ }
377
+ if ( _networks_all == "y" ) {
378
+ _filter =
379
+ _all_get(
380
+ _filter_output_type);
381
+ console.log(
382
+ _filter);
383
+ _results =
384
+ [];
385
+ console.log(
386
+ _results);
387
+ }
388
+ else if ( _networks_all == "n" ) {
389
+ // this doesnt work the same in
390
+ // javascript as in bash i suppose.
391
+ // _filter =
392
+ // _filter_get(
393
+ // _filter_key,
394
+ // _filter_value,
395
+ // _filter_output_type);
396
+ const
397
+ _db =
398
+ _json_read(
399
+ _db_path);
400
+ console.log(
401
+ _db);
402
+ _results =
403
+ [];
404
+ console.log(
405
+ _results);
406
+ }
407
+ }
408
+
409
+ function
410
+ _input_type_auto_detect() {
411
+ if ( input_type == "" ) {
412
+ if ( isNaN(target_chain) ) {
413
+ input_type =
414
+ "name";
415
+ }
416
+ else {
417
+ input_type =
418
+ "id";
419
+ }
420
+ }
421
+ }
422
+
423
+ function
424
+ _db_path_auto_detect() {
425
+ let
426
+ _type_primary,
427
+ _type_fallback,
428
+ _msg;
429
+ if ( user_level == "y" ) {
430
+ _type_primary =
431
+ "user";
432
+ _type_fallback =
433
+ "system";
434
+ }
435
+ else if ( user_level == "n" ) {
436
+ _type_primary =
437
+ "system";
438
+ _type_fallback =
439
+ "user";
440
+ }
441
+ const
442
+ _db =
443
+ _evm_chains_db_path_get(
444
+ _type_primary,
445
+ "",
446
+ "unified");
447
+ const
448
+ _db_split =
449
+ _evm_chains_db_path_get(
450
+ _type_primary,
451
+ chain_id,
452
+ "split");
453
+ const
454
+ _db_fallback =
455
+ _evm_chains_db_path_get(
456
+ _type_fallback,
457
+ "",
458
+ "unified");
459
+ const
460
+ _db_fallback_split =
461
+ _evm_chains_db_path_get(
462
+ _type_fallback,
463
+ chain_id,
464
+ "split");
465
+ if ( _file_exists(
466
+ _db_split) ) {
467
+ db_paths.push(
468
+ _db_split);
469
+ }
470
+ else {
471
+ _msg =
472
+ `Default per-network database ` +
473
+ `'${_db_split}' ` +
474
+ `not found, trying '${_db}'.`;
475
+ _msg_info(
476
+ _msg);
477
+ }
478
+ if ( _file_exists(
479
+ _db) ) {
480
+ db_paths.push(
481
+ _db);
482
+ }
483
+ else {
484
+ _msg =
485
+ `Default unified database ` +
486
+ `'${_db_split}' ` +
487
+ `not found, trying '${_db}'.`;
488
+ _msg_info(
489
+ _msg);
490
+ }
491
+ if ( _file_exists(
492
+ _db_fallback_split) ) {
493
+ db_paths.push(
494
+ _db_fallback_split);
495
+ }
496
+ else {
497
+ _msg =
498
+ `Fallback per-network database ` +
499
+ `'${_db_fallback_split}' ` +
500
+ `not found, trying '${_db_fallback}'.`;
501
+ _msg_info(
502
+ _msg);
503
+ }
504
+ if ( _file_exists(
505
+ _db_fallback) ) {
506
+ db_paths.push(
507
+ _db_fallback);
508
+ }
509
+ else {
510
+ _msg =
511
+ `Fallback database ` +
512
+ `'${_db_fallback_split}' ` +
513
+ `not found too!`;
514
+ _msg_error(
515
+ _msg,
516
+ 1);
517
+ }
518
+ }
519
+
520
+ function
521
+ _evm_chains_info_get(
522
+ _target_chain,
523
+ _retrieval_mode,
524
+ _input_type,
525
+ _output_type,
526
+ _selection_method,
527
+ _api_key,
528
+ _networks_all,
529
+ _db_paths) {
530
+ let
531
+ _db,
532
+ _db_path,
533
+ _msg,
534
+ _networks_total,
535
+ _result,
536
+ _results;
537
+ _networks_total =
538
+ 0;
539
+ if ( _retrieval_mode == "offline" ) {
540
+ for ( _db_path of _db_paths ) {
541
+ _msg =
542
+ `Looking in '${_db_path}'.`;
543
+ _msg_info(
544
+ _msg);
545
+ _db_path_check(
546
+ _db_path);
547
+ if ( _input_type != "amount" ) {
548
+ _results =
549
+ _db_get(
550
+ _db_path,
551
+ _input_type,
552
+ _output_type,
553
+ _selection_method,
554
+ _api_key,
555
+ _networks_all,
556
+ _target_chain);
557
+ }
558
+ else if ( _input_type == "amount" ) {
559
+ _db =
560
+ _json_read(
561
+ _db_path);
562
+ _networks_total =
563
+ _db.length;
564
+ }
565
+ }
566
+ if ( _input_type != "amount" ) {
567
+ if ( _results.length == 0 ) {
568
+ _msg =
569
+ `No results found for network ` +
570
+ `'${_target_chain}'.`;
571
+ _msg_error(
572
+ _msg,
573
+ 0);
574
+ }
575
+ for ( _result of _results ) {
576
+ console.log(
577
+ _result);
578
+ if ( _selection_method != "all" ) {
579
+ break;
580
+ }
581
+ }
582
+ }
583
+ else if ( _input_type == "amount" ) {
584
+ console.log(
585
+ _networks_total);
586
+ }
587
+ }
588
+ else if ( _retrieval_mode == "online" ) {
589
+ _msg =
590
+ `About to implement it probably.`;
591
+ _msg_error(
592
+ _msg,
593
+ 1);
594
+ }
595
+ }
596
+
597
+ function
598
+ _evm_chains_info_set(
599
+ _input_type) {
600
+ if ( _input_type == "id" ) {
601
+ console.log(
602
+ _input_type);
603
+ }
604
+ }
605
+
606
+ function
607
+ _evm_chains_info(
608
+ _target_command,
609
+ _input_type,
610
+ _output_type,
611
+ _retrieval_mode,
612
+ _selection_method,
613
+ _api_key,
614
+ _networks_all,
615
+ _target_chain,
616
+ _db_paths) {
617
+ if ( _target_command == "get" ) {
618
+ _evm_chains_info_get(
619
+ _target_chain,
620
+ _retrieval_mode,
621
+ _input_type,
622
+ _output_type,
623
+ _selection_method,
624
+ _networks_all,
625
+ _api_key,
626
+ _db_paths);
627
+ }
628
+ else if ( _target_command == "set" ) {
629
+ _evm_chains_info_set(
630
+ _input_type);
631
+ }
632
+ else {
633
+ const
634
+ _msg =
635
+ `Unknown command ` +
636
+ `'${_target_command}'.`;
637
+ _msg_error(
638
+ _msg,
639
+ 1);
640
+ }
641
+ }
642
+
197
643
  module.exports = {
644
+ _data_get:
645
+ _data_get,
646
+ _db_path_auto_detect:
647
+ _db_path_auto_detect,
198
648
  _evm_chains_db_path_get:
199
649
  _evm_chains_db_path_get,
200
- _evm_chain_info_get:
201
- _evm_chain_info_get,
650
+ _evm_chains_info:
651
+ _evm_chains_info,
652
+ _evm_chains_info_get:
653
+ _evm_chains_info_get,
654
+ _evm_chains_info_set:
655
+ _evm_chains_info_set,
656
+ _input_type_auto_detect:
657
+ _input_type_auto_detect,
202
658
  _rpc_backend_get:
203
659
  _rpc_backend_get,
204
660
  _rpc_backends_get: