evm-contracts-tools 0.0.1

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,456 @@
1
+ #!/usr/bin/env node
2
+
3
+ // SPDX-License-Identifier: AGPL-3.0-or-later
4
+
5
+ /** ----------------------------------------------------------------------
6
+ * Copyright ©
7
+ * Pellegrino Prevete
8
+ * 2024, 2025, 2026
9
+ *
10
+ * All rights reserved
11
+ * ----------------------------------------------------------------------
12
+ *
13
+ * This program is free software: you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation, either version 3 of the License, or
16
+ * (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
25
+ */
26
+
27
+ const
28
+ _libcrash =
29
+ require(
30
+ '../libcrash-js/crash-js');
31
+ _cmdline_check =
32
+ _libcrash._cmdline_check;
33
+ _error_display =
34
+ _libcrash._error_display;
35
+ _msg_info =
36
+ _libcrash._msg_info;
37
+ _msg_error =
38
+ _libcrash._msg_error;
39
+ _printf =
40
+ _libcrash._printf;
41
+ _sleep =
42
+ _libcrash._sleep;
43
+ const
44
+ _contract_get_module =
45
+ require(
46
+ './contract-get');
47
+ _contract_get =
48
+ _contract_get_module._contract_get
49
+
50
+
51
+ function
52
+ _global_variables() {
53
+ app_name =
54
+ "evm-contract-call-static";
55
+ wallet_seed =
56
+ '';
57
+ retries_max =
58
+ '';
59
+ api_key_path =
60
+ '';
61
+ target_network =
62
+ '';
63
+ contract_address =
64
+ '';
65
+ contract_abi_path =
66
+ '';
67
+ contract_bytecode_path =
68
+ '';
69
+ contract_compiler_output_path =
70
+ '';
71
+ method_object =
72
+ '';
73
+ contract_method =
74
+ '';
75
+ method_args =
76
+ [];
77
+ quiet =
78
+ '';
79
+ }
80
+
81
+ function
82
+ _msg_info_contract_obtained(
83
+ _contract) {
84
+ let
85
+ _msg,
86
+ _template;
87
+ _template =
88
+ "Contract '{_contract}' obtained.";
89
+ _msg =
90
+ _template.replaceAll(
91
+ "{_contract}",
92
+ _contract);
93
+ _msg_info(
94
+ _msg);
95
+ }
96
+
97
+ function
98
+ _msg_info_method_obtained(
99
+ _method) {
100
+ let
101
+ _msg;
102
+ _msg =
103
+ "Method obtained.";
104
+ _msg_info(
105
+ _msg);
106
+ }
107
+
108
+ async function
109
+ _contract_call_static(
110
+ _wallet_seed,
111
+ _retries_max,
112
+ _api_key_path,
113
+ _target_network,
114
+ _contract_abi_path,
115
+ _contract_bytecode_path,
116
+ _contract_compiler_output_path,
117
+ _method_object,
118
+ _contract_address,
119
+ _contract_method,
120
+ _method_args) {
121
+ let
122
+ _contract,
123
+ _contract_get_opts,
124
+ _method,
125
+ _passed,
126
+ _value,
127
+ _wallet_password;
128
+ if ( _method_object == undefined ) {
129
+ _method_object =
130
+ "";
131
+ }
132
+ _retry =
133
+ 1;
134
+ _wallet_password =
135
+ "";
136
+ _contract_get_opts = [
137
+ _wallet_seed,
138
+ _wallet_password,
139
+ _contract_address,
140
+ _contract_abi_path,
141
+ _contract_bytecode_path,
142
+ _contract_compiler_output_path,
143
+ _target_network,
144
+ _api_key_path,
145
+ ];
146
+ _passed =
147
+ false;
148
+ while ( _retry <= _retries_max &&
149
+ _passed == false ) {
150
+ try {
151
+ if ( _method_object == "" ||
152
+ _method_object == undefined ) {
153
+ _contract =
154
+ await _contract_get.apply(
155
+ null,
156
+ _contract_get_opts);
157
+ _msg_info_contract_obtained(
158
+ _contract);
159
+ _method =
160
+ await _contract.getFunction(
161
+ _contract_method);
162
+ _msg_info_method_obtained(
163
+ _method);
164
+ }
165
+ else if ( _method_object != "" ||
166
+ _method_object != undefined ) {
167
+ _msg_info(
168
+ "Re-using contract method.");
169
+ _method =
170
+ _method_object;
171
+ }
172
+ _value =
173
+ await _method.staticCall.apply(
174
+ null,
175
+ _method_args);
176
+ if ( _value != undefined ) {
177
+ _passed =
178
+ true;
179
+ }
180
+ else if ( _value == undefined ) {
181
+ throw { "error": {
182
+ "message":
183
+ "Value undefined, static call error" } };
184
+ }
185
+ } catch (
186
+ _error) {
187
+ _error_display(
188
+ _error);
189
+ _retry =
190
+ _retry + 1;
191
+ _msg_error(
192
+ "Increasing retry and waiting 3 seconds.",
193
+ 0);
194
+ await _sleep(
195
+ 3000);
196
+ }
197
+ }
198
+ if ( _retry > _retries_max ) {
199
+ throw { "error": {
200
+ "message":
201
+ "Maximum number of retries reached." } };
202
+ }
203
+ return [
204
+ _value,
205
+ _method
206
+ ];
207
+ }
208
+
209
+ function
210
+ _config_show() {
211
+ let
212
+ _line,
213
+ _method_args,
214
+ _text;
215
+ _text =
216
+ [];
217
+ _text.push(
218
+ "Static call configuration:");
219
+ _text.push(
220
+ " evm-contract-call-static configuration:");
221
+ _text.push(
222
+ " Wallet seed: {wallet_seed}".replace(
223
+ "{wallet_seed}",
224
+ wallet_seed));
225
+ _text.push(
226
+ " Max retries: {retries_max}".replace(
227
+ "{retries_max}",
228
+ retries_max));
229
+ _text.push(
230
+ " API Key path: {api_key_path}".replace(
231
+ "{api_key_path}",
232
+ api_key_path));
233
+ _text.push(
234
+ " Target network: {target_network}".replace(
235
+ "{target_network}",
236
+ target_network));
237
+ _text.push(
238
+ " Contract ABI: {contract_abi_path}".replace(
239
+ "{contract_abi_path}",
240
+ contract_abi_path));
241
+ _text.push(
242
+ " Contract bytecode: {contract_bytecode_path}".replace(
243
+ "{contract_bytecode_path}",
244
+ contract_bytecode_path));
245
+ _text.push(
246
+ "Contract compiler output: {contract_compiler_output_path}".replace(
247
+ "{contract_compiler_output_path}",
248
+ contract_compiler_output_path));
249
+ _text.push(
250
+ " Contract address: {contract_address}".replace(
251
+ "{contract_address}",
252
+ contract_address));
253
+ _text.push(
254
+ " Contract method: {contract_method}".replace(
255
+ "{contract_method}",
256
+ contract_method));
257
+ _method_args =
258
+ method_args.join(
259
+ " ");
260
+ _text.push(
261
+ " Method args: {method_args}".replace(
262
+ "{method_args}",
263
+ _method_args));
264
+ for (_line of _text) {
265
+ _msg_info(
266
+ _line);
267
+ }
268
+ }
269
+
270
+ function
271
+ _usage(
272
+ _exit) {
273
+ let
274
+ _line,
275
+ _text;
276
+ _text = [
277
+ "Execute a static call to an EVM network's contract method",
278
+ "",
279
+ "Usage:",
280
+ " evm-contract-call-static",
281
+ " <quiet>",
282
+ " <wallet_seed>",
283
+ " <retries_max>",
284
+ " <api_key_path>",
285
+ " <target_network>",
286
+ " <contract_abi_path>",
287
+ " <contract_bytecode_path>",
288
+ " <contract_compiler_output_path>",
289
+ " <contract_address>",
290
+ " <contract_method>",
291
+ " <method_args>",
292
+ "",
293
+ "Args:",
294
+ " <quiet> Can be 'y' or 'n'",
295
+ " Default: y",
296
+ " <wallet_seed> Path of the file containing",
297
+ " the seed phrase.",
298
+ " <retries_max> Maximum number of retries before",
299
+ " failing.",
300
+ " <api_key_path> Path of the API key for the",
301
+ " contract ABI provider service",
302
+ " <target_network> Network on which the contract",
303
+ " resides.",
304
+ " <contract_abi_path> Path of the contract ABI",
305
+ " <contract_bytecode_path> Path for the contract bytecode",
306
+ " <contract_compiler_output_path> Path for the contract compiler",
307
+ " output path (the hardhat artifact).",
308
+ " <contract_address> Address of the contract",
309
+ " <contract_method> Contract method to execute",
310
+ " [method_args] Arguments to pass to the method",
311
+ "",
312
+ " Options:",
313
+ " -h This help."
314
+ ];
315
+ for (_line of _text) {
316
+ _msg_info(
317
+ _line);
318
+ }
319
+ process.exit(
320
+ _exit);
321
+ }
322
+
323
+ function
324
+ _overrides_set() {
325
+ if ( retries_max == "" ) {
326
+ retries_max =
327
+ 100;
328
+ }
329
+ }
330
+
331
+ function
332
+ _cmdline_parse() {
333
+ quiet =
334
+ "y";
335
+ process.argv.forEach(
336
+ function (
337
+ _value,
338
+ _index,
339
+ _array) {
340
+ if ( _index == 2 ) {
341
+ quiet =
342
+ _value;
343
+ }
344
+ if ( _index == 3 ) {
345
+ wallet_seed =
346
+ _value;
347
+ }
348
+ if ( _index == 4 ) {
349
+ retries_max =
350
+ _value;
351
+ }
352
+ if ( _index == 5 ) {
353
+ api_key_path =
354
+ _value;
355
+ }
356
+ if ( _index == 6 ) {
357
+ target_network =
358
+ _value;
359
+ }
360
+ if ( _index == 7 ) {
361
+ contract_abi_path =
362
+ _value;
363
+ }
364
+ if ( _index == 8 ) {
365
+ contract_bytecode_path =
366
+ _value;
367
+ }
368
+ if ( _index == 9 ) {
369
+ contract_compiler_output_path =
370
+ _value;
371
+ }
372
+ if ( _index == 10 ) {
373
+ contract_address =
374
+ _value;
375
+ }
376
+ if ( _index == 11 ) {
377
+ contract_method =
378
+ _value;
379
+ }
380
+ if ( 11 < _index ) {
381
+ method_args.push(
382
+ _value);
383
+ }
384
+ if ( _value == "-h" ||
385
+ _value == "--help" ) {
386
+ _usage(
387
+ 0);
388
+ }
389
+ }
390
+ );
391
+ if ( contract_address == "" ) {
392
+ _usage(
393
+ 1);
394
+ }
395
+ }
396
+
397
+ async function
398
+ _contract_call_static_cmdline(
399
+ _app_opts) {
400
+ let
401
+ _method_object,
402
+ _value;
403
+ try {
404
+ [_value,
405
+ _method_object] =
406
+ await _contract_call_static.apply(
407
+ null,
408
+ _app_opts);
409
+ if ( _value != undefined ) {
410
+ _printf(
411
+ _value.toString());
412
+ }
413
+ else {
414
+ throw { "error": {
415
+ "message":
416
+ "Static contract call returned undefined value." } };
417
+ }
418
+ } catch (
419
+ _error) {
420
+ _error_display(
421
+ _error);
422
+ _msg_error(
423
+ "Error while running static call.",
424
+ 1);
425
+ }
426
+ }
427
+
428
+
429
+ if ( _cmdline_check(
430
+ "evm-contract-call-static") ) {
431
+ _global_variables();
432
+ _cmdline_parse();
433
+ _overrides_set();
434
+ _config_show();
435
+ app_opts = [
436
+ wallet_seed,
437
+ retries_max,
438
+ api_key_path,
439
+ target_network,
440
+ contract_abi_path,
441
+ contract_bytecode_path,
442
+ contract_compiler_output_path,
443
+ method_object,
444
+ contract_address,
445
+ contract_method,
446
+ method_args
447
+ ];
448
+ _contract_call_static_cmdline(
449
+ app_opts);
450
+ }
451
+
452
+ module.exports = {
453
+ _contract_call_static:
454
+ _contract_call_static
455
+ };
456
+
@@ -0,0 +1,206 @@
1
+ #!/usr/bin/env node
2
+
3
+
4
+ // SPDX-License-Identifier: AGPL-3.0-or-later
5
+
6
+ /** ----------------------------------------------------------------
7
+ * libevm-chains-info
8
+ * ----------------------------------------------------------------
9
+ * Copyright ©
10
+ * Pellegrino Prevete
11
+ * 2025, 2026
12
+ *
13
+ * All rights reserved
14
+ * ----------------------------------------------------------------
15
+ *
16
+ * This program is free software: you can redistribute it and/or
17
+ * modify it under the terms of the GNU General Public License as
18
+ * published by the Free Software Foundation, either version 3 of
19
+ * the License, or (at your option) any later version.
20
+ *
21
+ * This program is distributed in the hope that it will be useful,
22
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ * GNU General Public License for more details.
25
+ *
26
+ * You should have received a copy of the
27
+ * GNU General Public License
28
+ * along with this program.
29
+ * If not, see <https://www.gnu.org/licenses/>.
30
+ */
31
+
32
+ // const
33
+ // _libcrash =
34
+ // require(
35
+ // '../libcrash-js/crash-js');
36
+ const
37
+ _libcrash_module =
38
+ require(
39
+ "crash-js");
40
+ _file_exists =
41
+ _libcrash_module._file_exists;
42
+ _json_read =
43
+ _libcrash_module._json_read;
44
+ _path_join =
45
+ _libcrash_module._path_join;
46
+
47
+ function
48
+ _evm_chains_db_path_get(
49
+ _user) {
50
+ let
51
+ _db_exists,
52
+ _path;
53
+ if ( _user == undefined ) {
54
+ _user =
55
+ false;
56
+ }
57
+ if ( _user == true ) {
58
+ _path =
59
+ _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);
68
+ if ( _db_exists == false ) {
69
+ _user =
70
+ false;
71
+ }
72
+ }
73
+ if ( _user == false ) {
74
+ _path =
75
+ _path_join(
76
+ [process.env.NODE_PATH,
77
+ "..",
78
+ "evm-chains",
79
+ "chains.json"])
80
+ }
81
+ else {
82
+ _msg_error(
83
+ "Input variable '_user' must be a boolean",
84
+ 0);
85
+ }
86
+ return _path;
87
+ }
88
+
89
+ function
90
+ _evm_chain_info_get(
91
+ _chain_id,
92
+ _chain_info,
93
+ _db_path) {
94
+ let
95
+ _db,
96
+ _db_exists;
97
+ if ( _db_path == undefined ) {
98
+ _db_path =
99
+ _evm_chains_db_path_get(
100
+ false);
101
+ }
102
+ _db_exists =
103
+ _file_exists(
104
+ _db_path);
105
+ if ( _db_exists == false ) {
106
+ _msg_error(
107
+ "Database file does not exists.",
108
+ 1);
109
+ }
110
+ _db =
111
+ _json_read(
112
+ _db_path);
113
+ for ( _chain_info of _db) {
114
+ if ( _chain_info["chainId"] == _chain_id ) {
115
+ return _chain_info;
116
+ }
117
+ }
118
+ _msg_error(
119
+ `Network '${_chain_id}' not found.`,
120
+ 1);
121
+ }
122
+
123
+ function
124
+ _rpc_backend_get(
125
+ _network_chain_id,
126
+ _selection_method,
127
+ _db_path) {
128
+ let
129
+ _chain_id,
130
+ _chain_info;
131
+ if ( _network_chain_id == undefined ) {
132
+ _msg_error(
133
+ "Network's ChainID not specified.",
134
+ 1);
135
+ }
136
+ if ( _selection_method == undefined ) {
137
+ _selection_method =
138
+ "kirsh";
139
+ }
140
+ if ( _db_path == undefined ) {
141
+ _db_path =
142
+ _evm_chains_db_path_get(
143
+ false);
144
+ }
145
+ _chain_id =
146
+ Number(
147
+ _network_chain_id);
148
+ _chain_info =
149
+ _evm_chain_info_get(
150
+ _chain_id,
151
+ _db_path);
152
+ if ( _selection_method == "kirsh" ) {
153
+ return _chain_info["rpc"][0];
154
+ }
155
+ else {
156
+ _msg_error(
157
+ "Unknown selection method.",
158
+ 1);
159
+ }
160
+ }
161
+
162
+ function _rpc_backends_get(
163
+ _networks_chain_ids,
164
+ _selection_method,
165
+ _db_path) {
166
+ let
167
+ _chain_id,
168
+ _rpc_backend_get_opts,
169
+ _rpcs;
170
+ if ( _selection_method == undefined ) {
171
+ _selection_method =
172
+ "kirsh";
173
+ }
174
+ if ( _db_path == undefined ) {
175
+ _db_path =
176
+ _evm_chains_db_path_get(
177
+ false);
178
+ }
179
+ _rpcs =
180
+ [];
181
+ for ( _chain_id of _networks_chain_ids ) {
182
+ _rpc_backend_get_opts = [
183
+ _chain_id,
184
+ _selection_method,
185
+ _db_path
186
+ ];
187
+ _rpc_backend =
188
+ _rpc_backend_get.apply(
189
+ null,
190
+ _rpc_backend_get_opts);
191
+ _rpcs.append(
192
+ _rpc_backend);
193
+ }
194
+ return _rpcs;
195
+ }
196
+
197
+ module.exports = {
198
+ _evm_chains_db_path_get:
199
+ _evm_chains_db_path_get,
200
+ _evm_chain_info_get:
201
+ _evm_chain_info_get,
202
+ _rpc_backend_get:
203
+ _rpc_backend_get,
204
+ _rpc_backends_get:
205
+ _rpc_backends_get
206
+ };