evm-contracts-tools 0.0.1 → 0.0.2

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.
@@ -0,0 +1,862 @@
1
+ #!/usr/bin/env node
2
+
3
+ // SPDX-License-Identifier: AGPL-3.0-or-later
4
+
5
+ /** ----------------------------------------------------------------
6
+ * libevm-chains-info
7
+ * ----------------------------------------------------------------
8
+ * Copyright ©
9
+ * Pellegrino Prevete
10
+ * 2025, 2026
11
+ *
12
+ * All rights reserved
13
+ * ----------------------------------------------------------------
14
+ *
15
+ * This program is free software: you can redistribute it and/or
16
+ * modify it under the terms of the GNU General Public License as
17
+ * published by the Free Software Foundation, either version 3 of
18
+ * the License, or (at your option) any later version.
19
+ *
20
+ * This program is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the
26
+ * GNU General Public License
27
+ * along with this program.
28
+ * If not, see <https://www.gnu.org/licenses/>.
29
+ */
30
+
31
+ /* eslint-disable
32
+ no-unused-vars */
33
+ /* eslint-disable
34
+ no-useless-assignment */
35
+
36
+ /* global
37
+ db_paths */
38
+ /* global
39
+ input_type:
40
+ writable */
41
+ /* global
42
+ user_level */
43
+ /* global
44
+ target_chain */
45
+
46
+ const
47
+ _libcrash_module =
48
+ require(
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 */
60
+ _file_exists =
61
+ _libcrash_module._file_exists;
62
+ /* global
63
+ _json_read */
64
+ /* eslint-disable-next-line
65
+ no-global-assign */
66
+ _json_read =
67
+ _libcrash_module._json_read;
68
+ /* global
69
+ _lib_get */
70
+ /* eslint-disable-next-line
71
+ no-global-assign */
72
+ _lib_get =
73
+ _libcrash_module._lib_get;
74
+ /* global
75
+ _msg_info */
76
+ /* eslint-disable-next-line
77
+ no-global-assign */
78
+ _msg_info =
79
+ _libcrash_module._msg_info;
80
+ /* global
81
+ _msg_error */
82
+ /* eslint-disable-next-line
83
+ no-global-assign */
84
+ _msg_error =
85
+ _libcrash_module._msg_error;
86
+ /* global
87
+ _path_join */
88
+ /* eslint-disable-next-line
89
+ no-global-assign */
90
+ _path_join =
91
+ _libcrash_module._path_join;
92
+ /* global
93
+ _range */
94
+ /* eslint-disable-next-line
95
+ no-global-assign */
96
+ _range =
97
+ _libcrash_module._range;
98
+
99
+ const
100
+ _ethereum_data_module =
101
+ require(
102
+ "ethereum-data");
103
+ /* global
104
+ _data_get */
105
+ const
106
+ _data_get =
107
+ _ethereum_data_module._data_get;
108
+
109
+ function
110
+ _evm_chains_db_path_get(
111
+ _user,
112
+ _chain_id,
113
+ _format) {
114
+ let
115
+ _db_name,
116
+ _db_path,
117
+ _msg;
118
+ if ( typeof(
119
+ _user) == "undefined" ||
120
+ _user == "" ) {
121
+ _user =
122
+ "n";
123
+ }
124
+ if ( typeof(
125
+ _chain_id) == "undefined" ||
126
+ _chain_id == "" ) {
127
+ _chain_id =
128
+ "";
129
+ }
130
+ if ( typeof(
131
+ _format) == "undefined" ||
132
+ _format == "" ) {
133
+ if ( _chain_id != "" ) {
134
+ _format =
135
+ "split";
136
+ }
137
+ else if ( _chain_id == "" ) {
138
+ _format =
139
+ "unified";
140
+ }
141
+ }
142
+ _msg =
143
+ `Getting path for chain ` +
144
+ `with chain id '${_chain_id}' ` +
145
+ `with privilege user set to ` +
146
+ `'${_user}' in format '${_format}'.`;
147
+ _msg_info(
148
+ _msg);
149
+ if ( _format == "unified" ) {
150
+ _db_name =
151
+ "chains.json";
152
+ }
153
+ else if ( _format == "split" ) {
154
+ _db_name =
155
+ `${_chain_id}.json`;
156
+ }
157
+ if ( _user == "y" ) {
158
+ const
159
+ _home_dir =
160
+ _homedir_get();
161
+ _msg =
162
+ `Home directory: '${_home_dir}'`;
163
+ _msg_info(
164
+ _msg);
165
+ _db_path =
166
+ _path_join(
167
+ [ _home_dir,
168
+ ".config",
169
+ "evm-chains",
170
+ _db_name ]);
171
+ const
172
+ _db_exists =
173
+ _file_exists(
174
+ _db_path);
175
+ if ( _db_exists == true ) {
176
+ _msg =
177
+ `Found database '${_db_path}'.`;
178
+ _msg_info(
179
+ _msg);
180
+ }
181
+ else if ( _db_exists == false ) {
182
+ _msg =
183
+ `Database '${_db_path}' ` +
184
+ "not found";
185
+ _msg_info(
186
+ _msg);
187
+ // _user =
188
+ // "n";
189
+ }
190
+ else {
191
+ _msg =
192
+ "wat";
193
+ _msg_error(
194
+ _msg);
195
+ }
196
+ }
197
+ else if ( _user == "n" ) {
198
+ const
199
+ _lib =
200
+ _lib_get();
201
+ _db_path =
202
+ _path_join(
203
+ [ _lib,
204
+ "evm-chains",
205
+ _db_name ]);
206
+ }
207
+ else {
208
+ _msg =
209
+ "Input variable '_user' must be a boolean. " +
210
+ `_user value is '${_user}'.`;
211
+ _msg_error(
212
+ _msg,
213
+ 0);
214
+ }
215
+ return _db_path;
216
+ }
217
+
218
+ function
219
+ _evm_chain_info_get(
220
+ _chain_id,
221
+ _db_path,
222
+ _user_level,
223
+ _format,
224
+ _db_types) {
225
+ let
226
+ _chain_info,
227
+ _data,
228
+ _msg;
229
+ if ( typeof(
230
+ _user_level) == "undefined" ) {
231
+ _user_level =
232
+ false;
233
+ }
234
+ if ( typeof(
235
+ _format) == "undefined" ) {
236
+ _format =
237
+ "split";
238
+ }
239
+ if ( typeof(
240
+ _db_path) == "undefined" ) {
241
+ _db_path =
242
+ _evm_chains_db_path_get(
243
+ _user_level,
244
+ _chain_id,
245
+ _format);
246
+ }
247
+ const
248
+ _db_exists =
249
+ _file_exists(
250
+ _db_path);
251
+ if ( _db_exists == false ) {
252
+ if ( typeof window === 'undefined' &&
253
+ ( typeof global !== 'undefined' && global.global === global ) &&
254
+ typeof __webpack_require__ !== 'function' ) {
255
+ _data =
256
+ _data_get(
257
+ _chain_id);
258
+ _msg =
259
+ `Displaying data for ` +
260
+ `chain '${_chain_id}' `+
261
+ `of type '${_db_types}'.`;
262
+ _msg_info(
263
+ _msg);
264
+ console.log(
265
+ _data);
266
+ }
267
+ // _msg_error(
268
+ // "Database file does not exists.",
269
+ // 1);
270
+ }
271
+ const
272
+ _db =
273
+ _json_read(
274
+ _db_path);
275
+ // _data =
276
+ // _data_get(
277
+ // _chain_id);
278
+ _msg =
279
+ `Displaying data for ` +
280
+ `chain '${_chain_id}' `+
281
+ `of type '${_db_types}'.`;
282
+ _msg_info(
283
+ _msg);
284
+ // console.log(
285
+ // _db);
286
+ for ( _chain_info of _db) {
287
+ if ( _chain_info["chainId"] == _chain_id ) {
288
+ return _chain_info;
289
+ }
290
+ }
291
+ _msg_error(
292
+ `Network '${_chain_id}' not found.`,
293
+ 1);
294
+ }
295
+
296
+ function
297
+ _rpc_backend_get(
298
+ _network_chain_id,
299
+ _selection_method,
300
+ _db_path,
301
+ _user_level,
302
+ _db_types) {
303
+ if ( typeof(
304
+ _network_chain_id) == "undefined" ) {
305
+ const
306
+ _msg =
307
+ "Network's ChainID not specified.";
308
+ _msg_error(
309
+ _msg,
310
+ 1);
311
+ }
312
+ if ( typeof(
313
+ _selection_method) == "undefined" ) {
314
+ _selection_method =
315
+ "kirsh";
316
+ }
317
+ if ( typeof(
318
+ _db_path) == "undefined" ) {
319
+ _db_path =
320
+ _evm_chains_db_path_get(
321
+ false);
322
+ }
323
+ if ( typeof(
324
+ _db_types) == "undefined" ) {
325
+ _db_types = [
326
+ "node",
327
+ "system"
328
+ ];
329
+ }
330
+ if ( _user_level == undefined ) {
331
+ _user_level =
332
+ true;
333
+ }
334
+ const
335
+ _chain_id =
336
+ Number(
337
+ _network_chain_id);
338
+ const
339
+ _chain_info =
340
+ _evm_chain_info_get(
341
+ _chain_id,
342
+ _db_path,
343
+ _user_level,
344
+ _db_types);
345
+ if ( _selection_method == "kirsh" ) {
346
+ return _chain_info["rpc"][0];
347
+ }
348
+ else {
349
+ _msg_error(
350
+ "Unknown selection method.",
351
+ 1);
352
+ }
353
+ }
354
+
355
+ function _rpc_backends_get(
356
+ _networks_chain_ids,
357
+ _selection_method,
358
+ _db_path) {
359
+ let
360
+ _chain_id,
361
+ _rpc_backend_get_opts,
362
+ _rpc_backend;
363
+ if ( _selection_method == undefined ) {
364
+ _selection_method =
365
+ "kirsh";
366
+ }
367
+ if ( _db_path == undefined ) {
368
+ _db_path =
369
+ _evm_chains_db_path_get(
370
+ false);
371
+ }
372
+ const
373
+ _rpcs =
374
+ [];
375
+ for ( _chain_id of _networks_chain_ids ) {
376
+ _rpc_backend_get_opts = [
377
+ _chain_id,
378
+ _selection_method,
379
+ _db_path
380
+ ];
381
+ _rpc_backend =
382
+ _rpc_backend_get.apply(
383
+ null,
384
+ _rpc_backend_get_opts);
385
+ _rpcs.push(
386
+ _rpc_backend);
387
+ }
388
+ return _rpcs;
389
+ }
390
+
391
+ function
392
+ _db_path_check(
393
+ _db_path) {
394
+ if ( ! _file_exists(
395
+ _db_path) ) {
396
+ const
397
+ _msg =
398
+ `Database at ` +
399
+ `'${_db_path}' does not exist.`;
400
+ _msg_error(
401
+ _msg,
402
+ 1);
403
+ }
404
+ }
405
+
406
+ function
407
+ _all_get(
408
+ _filter_output_type) {
409
+ console.log(
410
+ _filter_output_type);
411
+ }
412
+
413
+ function
414
+ _db_get(
415
+ _db_path,
416
+ _input_type,
417
+ _output_type,
418
+ _selection_method,
419
+ _api_key,
420
+ _networks_all,
421
+ _target_chain) {
422
+ let
423
+ _db,
424
+ _entry,
425
+ _filter,
426
+ _filter_key,
427
+ _filter_output_type,
428
+ _filter_value,
429
+ _results,
430
+ _msg;
431
+ _filter_output_type =
432
+ _output_type;
433
+ if ( _input_type == "id" ) {
434
+ /* eslint-disable-next-line
435
+ no-useless-assignment */
436
+ _filter_key =
437
+ "chainId";
438
+ _filter_value =
439
+ _target_chain;
440
+ _msg =
441
+ `Getting data for filter ` +
442
+ `key: '${_filter_key}' and value ` +
443
+ `'${_filter_value}'.`;
444
+ _msg_info(
445
+ _msg);
446
+ }
447
+ else if ( _input_type == "testnet" ) {
448
+ _msg =
449
+ "Like I have to agg a tag.";
450
+ _msg_info(
451
+ _msg);
452
+ }
453
+ if ( _output_type == "id" ) {
454
+ _filter_output_type =
455
+ "chainId";
456
+ }
457
+ else if ( _output_type == "rpc" ) {
458
+ _filter_output_type =
459
+ _output_type;
460
+ }
461
+ if ( _networks_all == "y" ) {
462
+ _filter =
463
+ _all_get(
464
+ _filter_output_type);
465
+ _msg =
466
+ "Showing results for all networks " +
467
+ `with filter '${_filter}'.`;
468
+ _msg_info(
469
+ _msg);
470
+ _db =
471
+ _json_read(
472
+ _db_path);
473
+ _msg =
474
+ "Showing results for all networks " +
475
+ `with filter '${_filter}'.`;
476
+ _msg_info(
477
+ _msg);
478
+ return _db;
479
+ }
480
+ else if ( _networks_all == "n" ) {
481
+ _results =
482
+ [];
483
+ _msg =
484
+ `Showing results for chain '${_target_chain}' in ` +
485
+ `database '${_db_path}' with filter '${_filter_output_type}'.`;
486
+ _msg_info(
487
+ _msg);
488
+ // this doesnt work the same in
489
+ // javascript as in bash i suppose.
490
+ // _filter =
491
+ // _filter_get(
492
+ // _filter_key,
493
+ // _filter_value,
494
+ // _filter_output_type);
495
+ // _json_read(
496
+ // _db_path);
497
+ _db =
498
+ _json_read(
499
+ _db_path);
500
+ _msg =
501
+ `Database '${_db_path}' ` +
502
+ `contains '${_db.length}' items.`;
503
+ _msg_info(
504
+ _msg);
505
+ for ( _entry of _db ) {
506
+ if ( _entry[
507
+ _filter_key] == _target_chain ) {
508
+ _results.push(
509
+ _entry);
510
+ if ( _selection_method == "kirsh" ) {
511
+ break;
512
+ }
513
+ }
514
+ }
515
+ _msg =
516
+ `Total results returned for '${_db_path}' ` +
517
+ `are '${_results.length}'.`;
518
+ _msg_info(
519
+ _msg);
520
+ return _results;
521
+ }
522
+ }
523
+
524
+ function
525
+ _input_type_auto_detect() {
526
+ if ( input_type == "" ) {
527
+ if ( isNaN(target_chain) ) {
528
+ input_type =
529
+ "name";
530
+ }
531
+ else {
532
+ input_type =
533
+ "id";
534
+ }
535
+ }
536
+ }
537
+
538
+ function
539
+ _db_paths_auto_detect(
540
+ _chain_id) {
541
+ let
542
+ _type_primary,
543
+ _type_fallback,
544
+ _msg;
545
+ if ( user_level == "y" ) {
546
+ _type_primary =
547
+ true;
548
+ _type_fallback =
549
+ false;
550
+ }
551
+ else if ( user_level == "n" ) {
552
+ _type_primary =
553
+ true;
554
+ _type_fallback =
555
+ false;
556
+ }
557
+ const
558
+ _db =
559
+ _evm_chains_db_path_get(
560
+ _type_primary,
561
+ "",
562
+ "unified");
563
+ const
564
+ _db_split =
565
+ _evm_chains_db_path_get(
566
+ _type_primary,
567
+ _chain_id,
568
+ "split");
569
+ const
570
+ _db_fallback =
571
+ _evm_chains_db_path_get(
572
+ _type_fallback,
573
+ "",
574
+ "unified");
575
+ const
576
+ _db_fallback_split =
577
+ _evm_chains_db_path_get(
578
+ _type_fallback,
579
+ _chain_id,
580
+ "split");
581
+ if ( _file_exists(
582
+ _db_split) ) {
583
+ db_paths.push(
584
+ _db_split);
585
+ _msg =
586
+ `Found database ` +
587
+ `'${_db_split}', ` +
588
+ `adding.`;
589
+ _msg_info(
590
+ _msg);
591
+ }
592
+ else if ( ! _file_exists(
593
+ _db_split) ) {
594
+ _msg =
595
+ `Default per-network database ` +
596
+ `'${_db_split}' ` +
597
+ `not found, trying '${_db}'.`;
598
+ _msg_info(
599
+ _msg);
600
+ }
601
+ else {
602
+ _msg =
603
+ `wat`;
604
+ _msg_error(
605
+ _msg,
606
+ 1);
607
+ }
608
+ if ( _file_exists(
609
+ _db) ) {
610
+ db_paths.push(
611
+ _db);
612
+ }
613
+ else {
614
+ _msg =
615
+ `Default unified database ` +
616
+ `'${_db_split}' ` +
617
+ `not found, trying '${_db}'.`;
618
+ _msg_info(
619
+ _msg);
620
+ }
621
+ if ( _file_exists(
622
+ _db_fallback_split) ) {
623
+ db_paths.push(
624
+ _db_fallback_split);
625
+ }
626
+ else {
627
+ _msg =
628
+ `Fallback per-network database ` +
629
+ `'${_db_fallback_split}' ` +
630
+ `not found, trying '${_db_fallback}'.`;
631
+ _msg_info(
632
+ _msg);
633
+ }
634
+ if ( _file_exists(
635
+ _db_fallback) ) {
636
+ db_paths.push(
637
+ _db_fallback);
638
+ }
639
+ else {
640
+ _msg =
641
+ `Fallback database ` +
642
+ `'${_db_fallback_split}' ` +
643
+ `not found too!`;
644
+ _msg_error(
645
+ _msg,
646
+ 1);
647
+ }
648
+ }
649
+
650
+ function
651
+ _evm_chains_info_get(
652
+ _target_chain,
653
+ _retrieval_mode,
654
+ _input_type,
655
+ _output_type,
656
+ _selection_method,
657
+ _api_key,
658
+ _networks_all,
659
+ _db_paths) {
660
+ let
661
+ _db,
662
+ _db_path,
663
+ _entry,
664
+ _msg,
665
+ _networks_total,
666
+ _results;
667
+ _networks_total =
668
+ 0;
669
+ if ( _retrieval_mode == "offline" ) {
670
+ _msg =
671
+ `Getting data from databases ` +
672
+ `'${_db_paths}'`;
673
+ _msg_info(
674
+ _msg);
675
+ for ( _db_path of _db_paths ) {
676
+ _msg =
677
+ `Looking in '${_db_path}'.`;
678
+ _msg_info(
679
+ _msg);
680
+ _db_path_check(
681
+ _db_path);
682
+ if ( _input_type != "amount" ) {
683
+ _db =
684
+ _db_get(
685
+ _db_path,
686
+ _input_type,
687
+ _output_type,
688
+ _selection_method,
689
+ _api_key,
690
+ _networks_all,
691
+ _target_chain);
692
+ if ( _selection_method == "kirsh" ) {
693
+ break;
694
+ }
695
+ }
696
+ else if ( _input_type == "amount" ) {
697
+ _db =
698
+ _json_read(
699
+ _db_path);
700
+ _networks_total =
701
+ _db.length;
702
+ }
703
+ }
704
+ if ( _input_type != "amount" ) {
705
+ _networks_total =
706
+ _db.length;
707
+ if ( _db.length == 0 ) {
708
+ _msg =
709
+ `No results found for network ` +
710
+ `'${_target_chain}'.`;
711
+ _msg_error(
712
+ _msg,
713
+ 0);
714
+ }
715
+ const
716
+ _results =
717
+ [];
718
+ for ( _entry of _db ) {
719
+ if ( _entry['chainId'] == _target_chain ) {
720
+ _results.push(
721
+ _entry[
722
+ _output_type]);
723
+ if ( _selection_method != "all" ) {
724
+ break;
725
+ }
726
+ }
727
+ }
728
+ _db =
729
+ _results;
730
+ _msg =
731
+ `Found '${_networks_total}' ` +
732
+ `networks in database '${_db_path}'.`;
733
+ _msg_info(
734
+ _msg);
735
+ }
736
+ else if ( _input_type == "amount" ) {
737
+ _msg =
738
+ `Found '${_networks_total}' networks.`;
739
+ _msg_info(
740
+ _msg);
741
+ return _networks_total;
742
+ }
743
+ }
744
+ else if ( _retrieval_mode == "online" ) {
745
+ _msg =
746
+ `About to implement it probably.`;
747
+ _msg_error(
748
+ _msg,
749
+ 1);
750
+ }
751
+ return _db;
752
+ }
753
+
754
+ function
755
+ _evm_chains_info_set(
756
+ _input_type) {
757
+ const
758
+ _msg =
759
+ `Setting value for type '${_input_type}'.`;
760
+ if ( _input_type == "id" ) {
761
+ _msg_info(
762
+ _msg);
763
+ }
764
+ }
765
+
766
+ function
767
+ _evm_chains_info(
768
+ _target_command,
769
+ _input_type,
770
+ _output_type,
771
+ _retrieval_mode,
772
+ _selection_method,
773
+ _api_key,
774
+ _networks_all,
775
+ _target_chain,
776
+ _db_paths) {
777
+ let
778
+ _explorer,
779
+ _result,
780
+ _rpc;
781
+ if ( _target_command == "get" ) {
782
+ const
783
+ _infos =
784
+ [];
785
+ const
786
+ _results =
787
+ _evm_chains_info_get(
788
+ _target_chain,
789
+ _retrieval_mode,
790
+ _input_type,
791
+ _output_type,
792
+ _selection_method,
793
+ _networks_all,
794
+ _api_key,
795
+ _db_paths);
796
+ if ( _output_type == "rpc" ) {
797
+ for ( _result of _results ) {
798
+ for ( _rpc of _result) {
799
+ _infos.push(
800
+ _rpc);
801
+ if ( _selection_method == "kirsh" ) {
802
+ break;
803
+ }
804
+ }
805
+ }
806
+ return _infos;
807
+ }
808
+ else if ( _output_type == "name" ) {
809
+ return _results;
810
+ }
811
+ else if ( _output_type == "id" ) {
812
+ return _results;
813
+ }
814
+ else if ( _output_type == "explorers" ) {
815
+ for ( _result of _results ) {
816
+ for ( _explorer of _result ) {
817
+ _infos.push(
818
+ _explorer[
819
+ "url"]);
820
+ if ( _selection_method == "kirsh" ) {
821
+ break;
822
+ }
823
+ }
824
+ }
825
+ return _infos;
826
+ }
827
+ }
828
+ else if ( _target_command == "set" ) {
829
+ _evm_chains_info_set(
830
+ _input_type);
831
+ }
832
+ else {
833
+ const
834
+ _msg =
835
+ `Unknown command ` +
836
+ `'${_target_command}'.`;
837
+ _msg_error(
838
+ _msg,
839
+ 1);
840
+ }
841
+ }
842
+
843
+ module.exports = {
844
+ _data_get:
845
+ _data_get,
846
+ _db_paths_auto_detect:
847
+ _db_paths_auto_detect,
848
+ _evm_chains_db_path_get:
849
+ _evm_chains_db_path_get,
850
+ _evm_chains_info:
851
+ _evm_chains_info,
852
+ _evm_chains_info_get:
853
+ _evm_chains_info_get,
854
+ _evm_chains_info_set:
855
+ _evm_chains_info_set,
856
+ _input_type_auto_detect:
857
+ _input_type_auto_detect,
858
+ _rpc_backend_get:
859
+ _rpc_backend_get,
860
+ _rpc_backends_get:
861
+ _rpc_backends_get
862
+ };