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,485 @@
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
+ { ethers } =
29
+ require(
30
+ "ethers");
31
+ _ethers =
32
+ ethers;
33
+ _contract_factory =
34
+ _ethers.ContractFactory;
35
+ _base_contract =
36
+ _ethers.BaseContract;
37
+ const
38
+ _libcrash =
39
+ require(
40
+ '../libcrash-js/crash-js');
41
+ _cmdline_check =
42
+ _libcrash._cmdline_check;
43
+ _dirname =
44
+ _libcrash._dirname;
45
+ _file_read =
46
+ _libcrash._file_read;
47
+ _json_read =
48
+ _libcrash._json_read;
49
+ _path_join =
50
+ _libcrash._path_join;
51
+ _msg_info =
52
+ _libcrash._msg_info;
53
+ _msg_error =
54
+ _libcrash._msg_error;
55
+ const
56
+ _evm_wallet_get =
57
+ require(
58
+ '../evm-wallet/wallet-get');
59
+ _wallet_get =
60
+ _evm_wallet_get._wallet_get;
61
+ const
62
+ _network_provider_module =
63
+ require(
64
+ '../evm-wallet/network-provider');
65
+ _provider_get =
66
+ _network_provider_module._provider_get;
67
+
68
+
69
+ function
70
+ _global_variables() {
71
+ app_name =
72
+ "contract-get"
73
+ wallet_seed =
74
+ "";
75
+ wallet_password_path =
76
+ "";
77
+ contract_address =
78
+ "";
79
+ contract_abi_path =
80
+ "";
81
+ contract_bytecode_path =
82
+ "";
83
+ contract_compiler_output_path =
84
+ "";
85
+ target_network =
86
+ "";
87
+ api_key_path =
88
+ "";
89
+ quiet =
90
+ "";
91
+ }
92
+
93
+ function
94
+ _msg_error_no_input_data() {
95
+ let
96
+ _msg,
97
+ _text;
98
+ _text = [
99
+ "You need to provide either",
100
+ "the bytecode or the data"
101
+ ];
102
+ _msg =
103
+ _text.join(
104
+ " ");
105
+ _msg_error(
106
+ _msg,
107
+ 1);
108
+ }
109
+
110
+ function
111
+ _msg_info_wallet_selected(
112
+ _wallet) {
113
+ let
114
+ _msg,
115
+ _template;
116
+ _template =
117
+ "Wallet is '{_wallet}'.";
118
+ _msg =
119
+ _template.replaceAll(
120
+ "{_wallet}",
121
+ _wallet);
122
+ _msg_info(
123
+ _msg);
124
+ }
125
+
126
+ async function
127
+ _contract_get_abi(
128
+ _wallet,
129
+ _contract_address,
130
+ _contract_abi_path) {
131
+ let
132
+ _contract,
133
+ _contract_abi;
134
+ _contract_abi =
135
+ _json_read(
136
+ _contract_abi_path);
137
+ _contract = new
138
+ _base_contract(
139
+ _contract_address,
140
+ _contract_abi,
141
+ runner =
142
+ _wallet);
143
+ return _contract;
144
+ }
145
+
146
+ async function
147
+ _contract_get_bytecode(
148
+ _wallet,
149
+ _contract_address,
150
+ _contract_abi_path,
151
+ _contract_bytecode_path) {
152
+ let
153
+ _contract,
154
+ _contract_abi,
155
+ _contract_bytecode,
156
+ _factory;
157
+ if ( _contract_abi_path == "" ) {
158
+ _contract_abi = [];
159
+ }
160
+ else if ( _contract_abi_path != "" ) {
161
+ _contract_abi =
162
+ _json_read(
163
+ _contract_abi_path);
164
+ }
165
+ if ( _contract_bytecode_path != "" ) {
166
+ _contract_bytecode =
167
+ _file_read(
168
+ _contract_bytecode_path);
169
+ }
170
+ _factory =
171
+ new _contract_factory(
172
+ _contract_abi,
173
+ _contract_bytecode,
174
+ _wallet);
175
+ if ( _contract_address != "" ) {
176
+ _contract =
177
+ _factory.attach(
178
+ _contract_address);
179
+ }
180
+ else if ( _contract_address == "" ) {
181
+ try {
182
+ _contract =
183
+ _factory.getDeployTransaction();
184
+ } catch (e) {
185
+ console.error(
186
+ e);
187
+ }
188
+ }
189
+ return _contract;
190
+ }
191
+
192
+ async function
193
+ _contract_get_compiler_output(
194
+ _wallet,
195
+ _contract_address,
196
+ _contract_compiler_output_path) {
197
+ let
198
+ _contract,
199
+ _contract_compiler_output,
200
+ _factory;
201
+ _contract_compiler_output =
202
+ _file_read(
203
+ _contract_compiler_output_path);
204
+ _factory =
205
+ _contract_factory.fromSolidity(
206
+ _contract_compiler_output,
207
+ _wallet);
208
+ if ( _contract_address != "" ) {
209
+ _contract =
210
+ _factory.attach(
211
+ _contract_address);
212
+ }
213
+ else if ( _contract_address == "" ) {
214
+ _contract = await
215
+ _factory.getDeployTransaction();
216
+ }
217
+ return _contract;
218
+ }
219
+
220
+
221
+ async function
222
+ _contract_get(
223
+ _wallet_seed,
224
+ _wallet_password_path,
225
+ _contract_address,
226
+ _contract_abi_path,
227
+ _contract_bytecode_path,
228
+ _contract_compiler_output_path,
229
+ _target_network,
230
+ _api_key_path) {
231
+ let
232
+ _api_key,
233
+ _contract,
234
+ _contract_get_opts,
235
+ _network_provider,
236
+ _wallet,
237
+ _wallet_get_opts,
238
+ _wallet_path_list,
239
+ _wallet_path;
240
+ if ( _api_key_path != "" ) {
241
+ _api_key =
242
+ _file_read(
243
+ _api_key_path);
244
+ }
245
+ _network_provider =
246
+ _provider_get(
247
+ _target_network,
248
+ _api_key);
249
+ _contract_get_opts =
250
+ [];
251
+ if ( _wallet_seed != "" ) {
252
+ _wallet_path_list = [
253
+ _dirname(
254
+ _wallet_seed),
255
+ "wallet.dat"
256
+ ];
257
+ _wallet_path =
258
+ _path_join(
259
+ _wallet_path_list);
260
+ _wallet_get_opts = [
261
+ _wallet_path,
262
+ _wallet_password_path,
263
+ _wallet_seed,
264
+ _network_provider
265
+ ];
266
+ _wallet =
267
+ _wallet_get.apply(
268
+ null,
269
+ _wallet_get_opts);
270
+ _msg_info_wallet_selected(
271
+ _wallet);
272
+ _contract_get_opts.push(
273
+ _wallet);
274
+ }
275
+ else {
276
+ _contract_get_opts.push(
277
+ _network_provider);
278
+ }
279
+ _contract_get_opts.push(
280
+ _contract_address);
281
+ if ( _contract_bytecode_path != "" ) {
282
+ _contract_get_opts.push(
283
+ _contract_abi_path,
284
+ _contract_bytecode_path);
285
+ _contract_get_fun =
286
+ _contract_get_bytecode;
287
+ }
288
+ else if ( _contract_compiler_output_path != "" ) {
289
+ _contract_get_opts.push(
290
+ _contract_compiler_output_path);
291
+ _contract_get_fun =
292
+ _contract_get_compiler_output;
293
+ }
294
+ else if ( _contract_bytecode_path == "" &&
295
+ _contract_abi_path != "" ) {
296
+ _contract_get_opts.push(
297
+ _contract_abi_path);
298
+ _contract_get_fun =
299
+ _contract_get_abi;
300
+ }
301
+ else if ( _contract_bytecode_path == "" &&
302
+ _contract_abi_path == "" &&
303
+ _contract_compiler_output_path == "" ) {
304
+ _msg_error_no_input_data();
305
+ }
306
+ try {
307
+ _contract = await
308
+ _contract_get_fun.apply(
309
+ null,
310
+ _contract_get_opts);
311
+ } catch (e) {
312
+ console.error(
313
+ e);
314
+ }
315
+ return _contract;
316
+ }
317
+
318
+ function
319
+ _config_show() {
320
+ let
321
+ _line,
322
+ _text;
323
+ _text = [];
324
+ _text.push(
325
+ " Wallet seed: {wallet_seed}".replace(
326
+ "{wallet_seed}",
327
+ wallet_seed));
328
+ _text.push(
329
+ " Wallet password: {wallet_password_path}".replace(
330
+ "{wallet_password_path}",
331
+ wallet_password_path));
332
+ _text.push(
333
+ " Contract address: {contract_address}".replace(
334
+ "{contract_address}",
335
+ contract_address));
336
+ _text.push(
337
+ " Contract ABI path: {contract_abi_path}".replace(
338
+ "{contract_abi_path}",
339
+ contract_abi_path));
340
+ _text.push(
341
+ " Contract bytecode: {contract_bytecode_path}".replace(
342
+ "{contract_bytecode_path}",
343
+ contract_bytecode_path));
344
+ _text.push(
345
+ "Contract compiler output: {contract_compiler_output_path}".replace(
346
+ "{contract_compiler_output_path}",
347
+ contract_compiler_output_path));
348
+ _text.push(
349
+ " Target network: {target_network}".replace(
350
+ "{target_network}",
351
+ target_network));
352
+ _text.push(
353
+ " API Key: {api_key_path}".replace(
354
+ "{api_key_path}",
355
+ api_key_path));
356
+ for ( _line of _text ) {
357
+ _msg_info(
358
+ _line);
359
+ }
360
+ }
361
+
362
+ function
363
+ _usage() {
364
+ let
365
+ _line,
366
+ _text;
367
+ _text = [
368
+ "Usage:",
369
+ " contract-get",
370
+ " <quiet>",
371
+ " <wallet_seed>",
372
+ " <wallet_password_path>",
373
+ " <contract_address>",
374
+ " <contract_abi_path>",
375
+ " <contract_bytecode_path>",
376
+ " <contract_compiler_output_path>",
377
+ " <target_network>",
378
+ " (<api_key_path>)",
379
+ "",
380
+ "Args:",
381
+ " <quiet> Can be 'y' or 'n'",
382
+ " Default: y",
383
+ " <wallet_seed> Path of the seed phrase.",
384
+ " <wallet_password_path> Wallet password path.",
385
+ " <contract_address> Contract of the address one is",
386
+ " getting the contract of.",
387
+ " <contract_abi_path> Path of the contract ABI.",
388
+ " <contract_bytecode_path> Path for the contract bytecode.",
389
+ " <contract_compiler_output_path> Path for the contract compiler",
390
+ " output path (the hardhat artifact).",
391
+ " <target_network> Network on which the contract",
392
+ " resides.",
393
+ " <api_key_path> Path of the API key for the",
394
+ " contract ABI provider service.",
395
+ "",
396
+ " Options:",
397
+ " -h This help."
398
+ ];
399
+ for ( _line of _text ) {
400
+ _msg_info(
401
+ _line);
402
+ }
403
+ process.exit();
404
+ }
405
+
406
+ function _cmdline_parse() {
407
+ quiet = "y";
408
+ process.argv.forEach(
409
+ function (
410
+ _value,
411
+ _index,
412
+ _array) {
413
+ if ( _index == 2 ) {
414
+ quiet =
415
+ _value;
416
+ }
417
+ if ( _index == 3 ) {
418
+ wallet_seed =
419
+ _value;
420
+ }
421
+ if ( _index == 4 ) {
422
+ wallet_password_path =
423
+ _value;
424
+ }
425
+ if ( _index == 5 ) {
426
+ contract_address =
427
+ _value;
428
+ }
429
+ if ( _index == 6 ) {
430
+ contract_abi_path =
431
+ _value;
432
+ }
433
+ if ( _index == 7 ) {
434
+ contract_bytecode_path =
435
+ _value;
436
+ }
437
+ if ( _index == 8 ) {
438
+ contract_compiler_output_path =
439
+ _value;
440
+ }
441
+ if ( _index == 9 ) {
442
+ target_network =
443
+ _value;
444
+ }
445
+ if ( _index == 10 ) {
446
+ api_key_path =
447
+ _value;
448
+ }
449
+ if ( _value == "-h" ||
450
+ _value == "--help" ) {
451
+ _usage();
452
+ }
453
+ }
454
+ );
455
+ if ( contract_abi_path == "" &&
456
+ contract_bytecode_path == "" &&
457
+ contract_compiler_output_path == "" ) {
458
+ _usage();
459
+ }
460
+ }
461
+
462
+ if ( _cmdline_check(
463
+ "contract-get") ) {
464
+ _global_variables();
465
+ _cmdline_parse();
466
+ _config_show();
467
+ app_opts = [
468
+ wallet_seed,
469
+ wallet_password_path,
470
+ contract_address,
471
+ contract_abi_path,
472
+ contract_bytecode_path,
473
+ contract_compiler_output_path,
474
+ target_network,
475
+ api_key_path,
476
+ ];
477
+ _contract_get.apply(
478
+ null,
479
+ app_opts);
480
+ }
481
+
482
+ module.exports = {
483
+ _contract_get:
484
+ _contract_get
485
+ };