evm-contracts-tools 0.0.55 → 0.0.62

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.
@@ -65,11 +65,21 @@ const
65
65
  _libcrash_module =
66
66
  await import(
67
67
  "libcrash-js");
68
+ /* global
69
+ _dir_exists */
70
+ const
71
+ _dir_exists =
72
+ _libcrash_module._dir_exists;
68
73
  /* global
69
74
  _dirname */
70
75
  const
71
76
  _dirname =
72
77
  _libcrash_module._dirname;
78
+ /* global
79
+ _echo */
80
+ const
81
+ _echo =
82
+ _libcrash_module._echo;
73
83
  /* global
74
84
  _file_exists */
75
85
  const
@@ -95,6 +105,11 @@ const
95
105
  const
96
106
  _lib_get =
97
107
  _libcrash_module._lib_get;
108
+ /* global
109
+ _ls */
110
+ const
111
+ _ls =
112
+ _libcrash_module._ls;
98
113
  /* global
99
114
  _msg_info */
100
115
  const
@@ -139,6 +154,24 @@ const
139
154
  const
140
155
  _address_check =
141
156
  _evm_contracts_tools_module._address_check;
157
+ const
158
+ _evm_contract_deployment_address_module =
159
+ await import(
160
+ "./libevm-contract-deployment-address");
161
+ /* global
162
+ _evm_contract_deployment_address */
163
+ const
164
+ _evm_contract_deployment_address =
165
+ _evm_contract_deployment_address_module._evm_contract_deployment_address;
166
+ const
167
+ _evm_contract_deployments_dir_module =
168
+ await import(
169
+ "./libevm-contract-deployments-dir");
170
+ /* global
171
+ _evm_contract_deployments_dir */
172
+ const
173
+ _evm_contract_deployments_dir =
174
+ _evm_contract_deployments_dir_module._evm_contract_deployments_dir;
142
175
  const
143
176
  _ethereum_data_module =
144
177
  await _require(
@@ -207,9 +240,33 @@ function
207
240
 
208
241
  function
209
242
  _default_artifact_path_get(
210
- _type) {
243
+ _type,
244
+ _target_network,
245
+ _contract_address) {
211
246
  let
212
247
  _file_name;
248
+ const
249
+ _network_type =
250
+ typeof(
251
+ _target_network);
252
+ if ( _network_type === "undefined" ||
253
+ _target_network === "" ) {
254
+ _target_network =
255
+ _get(
256
+ "target",
257
+ "network");
258
+ }
259
+ const
260
+ _contract_address_type =
261
+ typeof(
262
+ _contract_address);
263
+ if ( _contract_address_type === "undefined" ||
264
+ _contract_address === "" ) {
265
+ _contract_address =
266
+ _get(
267
+ "contract",
268
+ "address");
269
+ }
213
270
  const
214
271
  _home =
215
272
  _homedir_get();
@@ -222,15 +279,15 @@ function
222
279
  "evm-contract-call" ]);
223
280
  if ( _type == "abi" ) {
224
281
  _file_name =
225
- `${contract_address}.abi.json`;
282
+ `${_contract_address}.abi.json`;
226
283
  }
227
284
  else if ( _type == "bytecode" ) {
228
285
  _file_name =
229
- `${contract_address}.bin`;
286
+ `${_contract_address}.bin`;
230
287
  }
231
288
  else if ( _type == "compiler_output" ) {
232
289
  _file_name =
233
- `${contract_address}.json`;
290
+ `${_contract_address}.json`;
234
291
  }
235
292
  else {
236
293
  const
@@ -238,13 +295,14 @@ function
238
295
  `Unknown artifact type '${_type}'.`;
239
296
  _msg_error(
240
297
  _msg,
241
- 1);
298
+ 0);
299
+ throw _msg;
242
300
  }
243
301
  const
244
302
  _artifact =
245
303
  _path_join( [
246
304
  _data_dir,
247
- target_network,
305
+ _target_network,
248
306
  _file_name ]);
249
307
  return _artifact;
250
308
  }
@@ -316,8 +374,8 @@ function
316
374
  _abi_target =
317
375
  _abi;
318
376
  if ( typeof(
319
- _abi_target) == "undefined" ||
320
- _abi_target == "" ) {
377
+ _abi_target) === "undefined" ||
378
+ _abi_target === "" ) {
321
379
  _abi_target =
322
380
  _default_artifact_path_get(
323
381
  "abi");
@@ -457,33 +515,69 @@ async function
457
515
  _contract_compiler_output,
458
516
  _contract_method,
459
517
  _method_args) {
460
- let
461
- _explorer,
462
- _out,
463
- _result,
464
- _rpc;
465
- if ( _call_type == "static" ) {
466
- const
467
- _infos =
468
- [];
469
- _out =
470
- "";
518
+ // let
519
+ // _msg,
520
+ // _out;
521
+ const
522
+ _call_type_type =
523
+ typeof(
524
+ _call_type);
525
+ if ( _call_type_type == "undefined" ) {
526
+ _call_type =
527
+ "static";
471
528
  }
472
- else if ( _call_type == "dynamic" ) {
473
- _out =
474
- "";
529
+ else if ( _call_type_type == "string" ) {
530
+ if ( _call_type.length < 1 ) {
531
+ // Properly
532
+ _call_type =
533
+ "static";
534
+ }
535
+ else {
536
+ _echo(
537
+ _call_type);
538
+ }
539
+ }
540
+ else if ( _call_type_type == "object" ) {
541
+ _echo(
542
+ _call_type);
475
543
  }
476
544
  else {
477
- const
478
- _msg =
479
- `Unknown call type ` +
480
- `'${_call_type}'.`;
481
- _msg_error(
482
- _msg,
483
- 0);
484
- throw _msg;
545
+ _echo(
546
+ _call_type);
485
547
  }
486
- return _out;
548
+ return [ _call_type ];
549
+ // _msg =
550
+ // "Running EVM network call " +
551
+ // `of type '${_call_type}'.`;
552
+ // _msg_info(
553
+ // _msg);
554
+ // if ( _call_type == "static" ) {
555
+ // _out =
556
+ // "static";
557
+ // _echo(
558
+ // "static");
559
+ // // const
560
+ // // _infos =
561
+ // // [];
562
+ // // _out =
563
+ // // "";
564
+ // }
565
+ // else if ( _call_type == "dynamic" ) {
566
+ // _echo(
567
+ // "dynamic");
568
+ // _out =
569
+ // "static";
570
+ // }
571
+ // else {
572
+ // _msg =
573
+ // `Unknown call type ` +
574
+ // `'${_call_type}'.`;
575
+ // _msg_error(
576
+ // _msg,
577
+ // 0);
578
+ // throw _msg;
579
+ // }
580
+ // return _out;
487
581
  }
488
582
 
489
583
  function
@@ -513,17 +607,474 @@ function
513
607
  }
514
608
  }
515
609
 
610
+ async function
611
+ _target_app_get(
612
+ _contract_address) {
613
+ let
614
+ _target_app;
615
+ _target_app =
616
+ "";
617
+ const
618
+ _contract_address_type =
619
+ typeof(
620
+ _contract_address);
621
+ if ( _contract_address_type != "undefined" &&
622
+ _contract_address != "" ) {
623
+ const
624
+ _address_valid =
625
+ _address_check(
626
+ _contract_address);
627
+ if ( ! _address_valid ) {
628
+ _target_app =
629
+ await _app_lookup_contract(
630
+ _contract_address);
631
+ }
632
+ }
633
+ return _target_app;
634
+ }
635
+
636
+ async function
637
+ _contract_lookup_address(
638
+ _target_app,
639
+ _address,
640
+ _target_network) {
641
+ let
642
+ _contract,
643
+ _contract_address,
644
+ _contract_dir,
645
+ _contract_networks,
646
+ _evm_contract_deployment_address_args,
647
+ _exception,
648
+ _msg,
649
+ _network;
650
+ const
651
+ _target_contracts =
652
+ {};
653
+ const
654
+ _lib =
655
+ await _lib_get();
656
+ const
657
+ _deployments_dir =
658
+ _path_join([
659
+ _lib,
660
+ _target_app,
661
+ "deployments"
662
+ ]);
663
+ const
664
+ _contracts =
665
+ await _ls(
666
+ _deployments_dir);
667
+ for ( _contract of _contracts ) {
668
+ _contract_dir =
669
+ _path_join([
670
+ _deployments_dir,
671
+ _contract
672
+ ]);
673
+ _contract_networks =
674
+ await _ls(
675
+ _contract_dir);
676
+ for ( _network in _contract_networks ) {
677
+ _evm_contract_deployment_address_args = [
678
+ [ _target_network ], // target networks
679
+ [ _target_network ], // target chain ids
680
+ "n", // all networks
681
+ _target_app,
682
+ _contract,
683
+ "", // contract version
684
+ "n", // user-level
685
+ "", // configuration format
686
+ _deployments_dir
687
+ ];
688
+ _contract_address =
689
+ await _evm_contract_deployment_address.apply(
690
+ null,
691
+ _evm_contract_deployment_address_args);
692
+ if ( _address == _contract_address ) {
693
+ if ( _target_network == "" ) {
694
+ _target_contracts[
695
+ _contract] =
696
+ 1;
697
+ }
698
+ else {
699
+ if ( _network == _target_network ) {
700
+ _target_contracts[
701
+ _contract] =
702
+ 1;
703
+ }
704
+ }
705
+ }
706
+ }
707
+ }
708
+ const
709
+ _target_contracts_set =
710
+ Object.keys(
711
+ _target_contracts);
712
+ const
713
+ _target_contracts_amount =
714
+ _target_contracts_set.length;
715
+ if ( _target_contracts_amount < 1 ) {
716
+ _msg =
717
+ "No contract found for " +
718
+ `address '${_address}' ` +
719
+ `on network '${_network}' in '${_lib}'.`;
720
+ _msg_error(
721
+ _msg,
722
+ 0);
723
+ _exception = {
724
+ "msg":
725
+ _msg
726
+ };
727
+ throw _exception;
728
+ }
729
+ if ( 1 < _target_contracts_amount ) {
730
+ _msg =
731
+ "Found more than one contract for " +
732
+ `contract '${_contract}' ` +
733
+ `in '${_lib}':\n`;
734
+ for ( _contract of _target_contracts_set ) {
735
+ _msg =
736
+ _msg + `${_contract}\n`;
737
+ }
738
+ _msg_error(
739
+ _msg,
740
+ 0);
741
+ _exception = {
742
+ "msg":
743
+ _msg
744
+ };
745
+ throw _exception;
746
+ }
747
+ else {
748
+ return _target_contracts_set;
749
+ }
750
+ }
751
+
752
+ async function
753
+ _app_lookup_contract(
754
+ _contract_name) {
755
+ let
756
+ _app,
757
+ _contract_dir,
758
+ _is_dir,
759
+ _msg;
760
+ const
761
+ _lib =
762
+ await _lib_get();
763
+ const
764
+ _target_apps =
765
+ {};
766
+ const
767
+ _apps =
768
+ await _ls(
769
+ _lib);
770
+ for ( _app of _apps ) {
771
+ _contract_dir =
772
+ _path_join([
773
+ _lib,
774
+ _app,
775
+ "deployments",
776
+ _contract_name
777
+ ]);
778
+ // _echo(_contract_dir);
779
+ _is_dir =
780
+ await _dir_exists(
781
+ _contract_dir);
782
+ // _echo(_is_dir);
783
+ if ( _is_dir ) {
784
+ // _echo(
785
+ // _contract_dir);
786
+ _target_apps[
787
+ _app] =
788
+ 1;
789
+ }
790
+ }
791
+ const
792
+ _target_apps_set =
793
+ Object.keys(
794
+ _target_apps);
795
+ const
796
+ _target_apps_amount =
797
+ _target_apps_set.length;
798
+ if ( _target_apps_amount < 1 ) {
799
+ _msg =
800
+ "No apps found for " +
801
+ `contract '${_contract_name}' ` +
802
+ `in '${_lib}'.`;
803
+ _msg_error(
804
+ _msg,
805
+ 0);
806
+ const
807
+ _exception = {
808
+ "msg":
809
+ _msg
810
+ };
811
+ throw _exception;
812
+ }
813
+ else if ( 1 < _target_apps_amount ) {
814
+ _msg =
815
+ "Found more than one app for " +
816
+ `contract '${_contract_name}' ` +
817
+ `in '${_lib}': '${_target_apps_set}'.`;
818
+ _msg_error(
819
+ _msg,
820
+ 0);
821
+ const
822
+ _exception = {
823
+ "msg":
824
+ _msg
825
+ };
826
+ throw _exception;
827
+ }
828
+ return _target_apps_set[
829
+ 0];
830
+ }
831
+
832
+ async function
833
+ _target_app_auto_detect() {
834
+ let
835
+ _target_app;
836
+ _target_app =
837
+ "";
838
+ const
839
+ _contract_address =
840
+ _get(
841
+ "contract",
842
+ "address");
843
+ const
844
+ _contract_address_type =
845
+ typeof(
846
+ _contract_address);
847
+ if ( _contract_address_type != "undefined" &&
848
+ _contract_address != "" ) {
849
+ _target_app =
850
+ await _target_app_get(
851
+ _contract_address);
852
+ }
853
+ _override_set(
854
+ "target",
855
+ "app",
856
+ _target_app);
857
+ }
858
+
859
+ async function
860
+ _target_contract_auto_detect() {
861
+ let
862
+ _target_contract;
863
+ _target_contract =
864
+ "";
865
+ const
866
+ _contract_address =
867
+ _get(
868
+ "contract",
869
+ "address");
870
+ const
871
+ _address_valid =
872
+ await _address_check(
873
+ _contract_address);
874
+ if ( ! _address_valid ) {
875
+ _target_contract =
876
+ _contract_address;
877
+ }
878
+ _override_set(
879
+ "target",
880
+ "contract",
881
+ _target_contract);
882
+ }
883
+
884
+ async function
885
+ _contract_address_auto_detect() {
886
+ let
887
+ _contract_address,
888
+ _contract_addresses,
889
+ _evm_contract_deployment_address_args,
890
+ _msg,
891
+ _network;
892
+ const
893
+ _target_networks_index =
894
+ {};
895
+ _contract_address =
896
+ _get(
897
+ "contract",
898
+ "address");
899
+ const
900
+ _target_app =
901
+ _get(
902
+ "target",
903
+ "app");
904
+ const
905
+ _target_networks =
906
+ _get(
907
+ "target",
908
+ "networks");
909
+ const
910
+ _target_contract =
911
+ _get(
912
+ "target",
913
+ "contract");
914
+ const
915
+ _deployments_dir =
916
+ _get(
917
+ "deployments",
918
+ "dir");
919
+ for ( _network of _target_networks ) {
920
+ _target_networks_index[
921
+ _network] =
922
+ 1;
923
+ }
924
+ const
925
+ _target_networks_set =
926
+ Object.keys(
927
+ _target_networks_index);
928
+ _msg =
929
+ "Auto-detecting address " +
930
+ `for contract '${_contract_address}' ` +
931
+ `on network '${_target_networks_set}' ` +
932
+ `for application '${_target_app}'.`;
933
+ _msg_info(
934
+ _msg);
935
+ if ( _contract_address != "" ) {
936
+ const
937
+ _address_valid =
938
+ await _address_check(
939
+ _contract_address);
940
+ if ( _address_valid ) {
941
+ _msg =
942
+ `Address '${_contract_address}' ` +
943
+ `is valid.`;
944
+ _msg_info(
945
+ _msg);
946
+ }
947
+ else {
948
+ if ( _target_app != "" &&
949
+ 0 < _target_networks.length &&
950
+ _target_contract != "" ) {
951
+ _msg =
952
+ "Looking for address " +
953
+ "corresponding to contract " +
954
+ `'${_target_contract}' for the ` +
955
+ `'${_target_app}' ` +
956
+ "libEVM application on network " +
957
+ `'${_target_networks}' at deployments ` +
958
+ `directory '${_deployments_dir}'.`;
959
+ _msg_info(
960
+ _msg);
961
+ _evm_contract_deployment_address_args = [
962
+ _target_networks_set, // target networks
963
+ _target_networks_set, // target chain ids
964
+ "n", // all networks
965
+ _target_app,
966
+ _target_contract,
967
+ "", // contract version
968
+ "n", // user-level
969
+ "", // configuration format
970
+ _deployments_dir
971
+ ];
972
+ try {
973
+ _contract_addresses =
974
+ await _evm_contract_deployment_address.apply(
975
+ null,
976
+ _evm_contract_deployment_address_args);
977
+ _contract_address =
978
+ _contract_addresses[
979
+ 0];
980
+ }
981
+ catch(
982
+ _error) {
983
+ _msg =
984
+ "Error while looking for contract " +
985
+ "address from local libEVM applications " +
986
+ "deployments directories.";
987
+ _msg_error(
988
+ _msg,
989
+ 0);
990
+ _msg_error(
991
+ _error,
992
+ 0);
993
+ throw _error;
994
+ }
995
+ }
996
+ else {
997
+ _msg =
998
+ "One between the " +
999
+ `application '${_target_app}', ` +
1000
+ `the network '${_target_networks_set}' ` +
1001
+ `or the contract '${_target_contract}' ` +
1002
+ `has not been specified.`;
1003
+ _msg_info(
1004
+ _msg);
1005
+ }
1006
+ }
1007
+ }
1008
+ _set(
1009
+ "contract",
1010
+ "address",
1011
+ _contract_address);
1012
+ }
1013
+
1014
+ function
1015
+ _data_dir_auto_detect() {
1016
+ const
1017
+ _homedir =
1018
+ _homedir_get();
1019
+ const
1020
+ _app_name =
1021
+ _get(
1022
+ "app",
1023
+ "name");
1024
+ const
1025
+ _data_dir =
1026
+ _path_join([
1027
+ _homedir,
1028
+ ".local",
1029
+ "share",
1030
+ _app_name
1031
+ ]);
1032
+ _override_set(
1033
+ "data",
1034
+ "dir",
1035
+ _data_dir);
1036
+ }
1037
+
1038
+ function
1039
+ _cache_dir_auto_detect() {
1040
+ const
1041
+ _homedir =
1042
+ _homedir_get();
1043
+ const
1044
+ _app_name =
1045
+ _get(
1046
+ "app",
1047
+ "name");
1048
+ const
1049
+ _cache_dir =
1050
+ _path_join([
1051
+ _homedir,
1052
+ ".cache",
1053
+ _app_name
1054
+ ]);
1055
+ _override_set(
1056
+ "cache",
1057
+ "dir",
1058
+ _cache_dir);
1059
+ }
1060
+
516
1061
  // ES6 module exports
517
1062
 
518
1063
  export {
519
1064
  _address_check,
1065
+ _app_lookup_contract,
520
1066
  _artifact_local_auto_detect,
1067
+ _cache_dir_auto_detect,
521
1068
  _call_auth_auto_detect,
522
1069
  _call_auth_get,
523
1070
  _contract_abi_auto_detect,
524
1071
  _contract_abi_get,
1072
+ _contract_address_auto_detect,
1073
+ _data_dir_auto_detect,
525
1074
  _default_artifact_path_get,
526
1075
  _evm_contract_call,
1076
+ _target_app_auto_detect,
1077
+ _target_contract_auto_detect,
527
1078
  _target_networks_auto_detect,
528
1079
  _target_networks_get
529
1080
  };
@@ -533,8 +1084,12 @@ export {
533
1084
  // module.exports = {
534
1085
  // _address_check:
535
1086
  // _address_check,
1087
+ // _app_lookup_contract:
1088
+ // _app_lookup_contract,
536
1089
  // _artifact_local_auto_detect:
537
1090
  // _artifact_local_auto_detect,
1091
+ // _cache_dir_auto_detect:
1092
+ // _cache_dir_auto_detect,
538
1093
  // _call_auth_auto_detect:
539
1094
  // _call_auth_auto_detect,
540
1095
  // _call_auth_get:
@@ -543,12 +1098,20 @@ export {
543
1098
  // _contract_abi_auto_detect,
544
1099
  // _contract_abi_get:
545
1100
  // _contract_abi_get,
1101
+ // _contract_address_auto_detect:
1102
+ // _contract_address_auto_detect,
1103
+ // _data_dir_auto_detect:
1104
+ // _data_dir_auto_detect,
546
1105
  // _default_artifact_path_get:
547
1106
  // _default_artifact_path_get,
548
1107
  // _evm_contract_call:
549
1108
  // _evm_contract_call,
1109
+ // _target_app_auto_detect:
1110
+ // _target_app_auto_detect,
550
1111
  // _target_networks_auto_detect:
551
1112
  // _target_networks_auto_detect,
1113
+ // _target_contract_auto_detect:
1114
+ // _target_contract_auto_detect,
552
1115
  // _target_networks_get:
553
1116
  // _target_networks_get
554
1117
  // };