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,446 @@
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
+ const
42
+ { ethers } =
43
+ require(
44
+ "ethers");
45
+ _ethers =
46
+ ethers;
47
+ _wei_to_ether =
48
+ _ethers.formatEther;
49
+ const
50
+ _transaction_get_module =
51
+ require(
52
+ '../evm-transactions-tools/transaction-get');
53
+ _transaction_get =
54
+ _transaction_get_module._transaction_get;
55
+ const
56
+ _transaction_receipt_get_module =
57
+ require(
58
+ '../evm-transactions-tools/transaction-receipt-get');
59
+ _transaction_receipt_get =
60
+ _transaction_receipt_get_module._transaction_receipt_get;
61
+
62
+ function
63
+ _global_variables() {
64
+ app_name =
65
+ "deployer-get";
66
+ wallet_seed =
67
+ "";
68
+ retries_max =
69
+ "";
70
+ api_key_path =
71
+ "";
72
+ chain_id =
73
+ "";
74
+ chain_name =
75
+ "";
76
+ target_network =
77
+ "";
78
+ contract_address =
79
+ "";
80
+ transaction_deployment =
81
+ "";
82
+ quiet =
83
+ "";
84
+ }
85
+
86
+ function
87
+ _delay(
88
+ _time) {
89
+ return new Promise(resolve => setTimeout(resolve, _time));
90
+ }
91
+
92
+ async function
93
+ _deployer_get(
94
+ _wallet_seed,
95
+ _retries_max,
96
+ _api_key_path,
97
+ _chain_id,
98
+ _chain_name,
99
+ _target_network,
100
+ _contract_address,
101
+ _transaction_deployment) {
102
+ let
103
+ _attempt,
104
+ _candidate,
105
+ _deployer,
106
+ _receipt,
107
+ _recipient,
108
+ _transaction_get_opts,
109
+ _transaction_receipt_get_opts,
110
+ _transaction_contract,
111
+ _tx,
112
+ _wallet_password;
113
+ _contract_address =
114
+ _contract_address.toLowerCase();
115
+ _attempt =
116
+ 1;
117
+ _wallet_password =
118
+ "";
119
+ _output_path =
120
+ "";
121
+ _transaction_get_opts = [
122
+ _retries_max,
123
+ _wallet_seed,
124
+ _wallet_password,
125
+ _chain_id,
126
+ _chain_name,
127
+ _target_network,
128
+ _api_key_path,
129
+ _output_path,
130
+ _transaction_deployment
131
+ ];
132
+ _transaction_receipt_get_opts = [
133
+ _retries_max,
134
+ _wallet_seed,
135
+ _wallet_password,
136
+ _chain_id,
137
+ _chain_name,
138
+ _target_network,
139
+ _api_key_path,
140
+ _output_path,
141
+ _transaction_deployment
142
+ ];
143
+ while ( _attempt <= _retries_max ) {
144
+ try {
145
+ _tx =
146
+ await _transaction_get.apply(
147
+ null,
148
+ _transaction_get_opts);
149
+ if ( _tx == undefined ) {
150
+ throw { "error": {
151
+ "message":
152
+ "Undefined transaction." } };
153
+ }
154
+ else if ( _tx != undefined ) {
155
+ _candidate =
156
+ _tx.from;
157
+ _recipient =
158
+ _tx.to;
159
+ }
160
+ if ( _recipient != null ) {
161
+ throw { "error": {
162
+ "message":
163
+ "The transaction is not a deployment."} };
164
+ }
165
+ _receipt =
166
+ await _transaction_receipt_get.apply(
167
+ null,
168
+ _transaction_receipt_get_opts);
169
+ if ( _receipt == undefined ) {
170
+ throw { "error": {
171
+ "message":
172
+ "Undefined transaction receipt."}};
173
+ }
174
+ else if ( _receipt != undefined ) {
175
+ _transaction_contract =
176
+ _receipt.contractAddress.toLowerCase();
177
+ }
178
+ if ( _transaction_contract != _contract_address ) {
179
+ console.error(
180
+ _receipt);
181
+ console.error(
182
+ "Contract address:",
183
+ _contract_address);
184
+ console.error(
185
+ "Transaction contract:",
186
+ _transaction_contract);
187
+ throw { "error": {
188
+ "message":
189
+ "The transaction does not deploy input contract."}};
190
+ }
191
+ _deployer =
192
+ _candidate;
193
+ break
194
+ } catch (
195
+ _error) {
196
+ _error_display(
197
+ _error);
198
+ // TODO: Do not break the loop
199
+ // only on network errors.
200
+ _attempt =
201
+ _attempt + 1;
202
+ _msg_error(
203
+ "Increasing retry and waiting 3 seconds.",
204
+ 0);
205
+ await _delay(
206
+ 3000);
207
+ }
208
+ }
209
+ if ( _attempt > _retries_max ) {
210
+ throw "Maximum number of retries reached.";
211
+ }
212
+ return _deployer;
213
+ }
214
+
215
+ function
216
+ _config_show() {
217
+ let
218
+ _line,
219
+ _method_args,
220
+ _text;
221
+ _text =
222
+ [];
223
+ _text.push(
224
+ "deployer-get configuration:");
225
+ _text.push(
226
+ " Wallet seed: {wallet_seed}".replace(
227
+ "{wallet_seed}",
228
+ wallet_seed));
229
+ _text.push(
230
+ " Max retries: {retries_max}".replace(
231
+ "{retries_max}",
232
+ retries_max));
233
+ _text.push(
234
+ " API Key path: {api_key_path}".replace(
235
+ "{api_key_path}",
236
+ api_key_path));
237
+ _text.push(
238
+ " Chain ID: {chain_id}".replace(
239
+ "{chain_id}",
240
+ chain_id));
241
+ _text.push(
242
+ " Chain Name: {chain_name}".replace(
243
+ "{chain_name}",
244
+ chain_name));
245
+ _text.push(
246
+ " Target network: {target_network}".replace(
247
+ "{target_network}",
248
+ target_network));
249
+ _text.push(
250
+ " Contract address: {contract_address}".replace(
251
+ "{contract_address}",
252
+ contract_address));
253
+ _text.push(
254
+ " Deployment transaction: {transaction_deployment}".replace(
255
+ "{transaction_deployment}",
256
+ transaction_deployment));
257
+ for (_line of _text) {
258
+ _msg_info(
259
+ _line);
260
+ }
261
+ }
262
+
263
+ function
264
+ _usage(
265
+ _exit) {
266
+ let
267
+ _display_fun,
268
+ _line,
269
+ _text;
270
+ _text = [
271
+ "Returns the deployer address for a contract.",
272
+ "",
273
+ "Usage:",
274
+ " deployer-get",
275
+ " <quiet>",
276
+ " <wallet_seed>",
277
+ " <retries_max>",
278
+ " <api_key_path>",
279
+ " <chain_id>",
280
+ " <chain_name>",
281
+ " <target_network>",
282
+ " <contract_address>",
283
+ " <transaction_deployment>",
284
+ "",
285
+ "Args:",
286
+ " <quiet> Can be 'y' or 'n'",
287
+ " Default: y",
288
+ " <wallet_seed> Path of the file containing",
289
+ " the seed phrase.",
290
+ " <retries_max> Maximum number of retries before",
291
+ " failing.",
292
+ " <api_key_path> Path of the API key for the",
293
+ " contract ABI provider service",
294
+ " <chain_id> Chain ID for the network.",
295
+ " <chain_name> Name for the network.",
296
+ " <target_network> Network on which the contract",
297
+ " resides.",
298
+ " <contract_address> Address of the contract",
299
+ " <transaction_deployment> Contract deployment transaction.",
300
+ "",
301
+ " Options:",
302
+ " -h This help."
303
+ ];
304
+ for (_line of _text) {
305
+ _msg_info(
306
+ _line);
307
+ }
308
+ process.exit(
309
+ _exit);
310
+ }
311
+
312
+ function
313
+ _overrides_set() {
314
+ if ( retries_max == "" ) {
315
+ retries_max =
316
+ 10;
317
+ }
318
+ }
319
+
320
+ function
321
+ _cmdline_parse() {
322
+ quiet =
323
+ "y";
324
+ process.argv.forEach(
325
+ function (
326
+ _value,
327
+ _index,
328
+ _array) {
329
+ if ( _index == 2 ) {
330
+ quiet =
331
+ _value;
332
+ }
333
+ if ( _index == 3 ) {
334
+ wallet_seed =
335
+ _value;
336
+ }
337
+ if ( _index == 4 ) {
338
+ retries_max =
339
+ _value;
340
+ }
341
+ if ( _index == 5 ) {
342
+ api_key_path =
343
+ _value;
344
+ }
345
+ if ( _index == 6 ) {
346
+ chain_id =
347
+ _value;
348
+ }
349
+ if ( _index == 7 ) {
350
+ chain_name =
351
+ _value;
352
+ }
353
+ if ( _index == 8 ) {
354
+ target_network =
355
+ _value;
356
+ }
357
+ if ( _index == 9 ) {
358
+ contract_address =
359
+ _value;
360
+ }
361
+ if ( _index == 10 ) {
362
+ transaction_deployment =
363
+ _value;
364
+ }
365
+ if ( _value == "-h" ||
366
+ _value == "--help" ) {
367
+ quiet =
368
+ "n";
369
+ _usage(
370
+ 0);
371
+ }
372
+ }
373
+ );
374
+ if ( contract_address == "" ) {
375
+ _msg_error(
376
+ "No contract address specified.",
377
+ 0);
378
+ quiet =
379
+ "n";
380
+ _usage(
381
+ 1);
382
+ }
383
+ if ( transaction_deployment == "" ) {
384
+ _msg_error(
385
+ "No deployment transaction specified.",
386
+ 0);
387
+ quiet =
388
+ "n";
389
+ _usage(
390
+ 1);
391
+ }
392
+ }
393
+
394
+ async function
395
+ _deployer_get_cmdline(
396
+ _app_opts) {
397
+ let
398
+ _deployer,
399
+ _msg;
400
+ try {
401
+ _deployer =
402
+ await _deployer_get.apply(
403
+ null,
404
+ _app_opts);
405
+ if ( _deployer != undefined ) {
406
+ _printf(
407
+ _deployer.toString());
408
+ }
409
+ else {
410
+ _msg =
411
+ "It has not been possible to determine " +
412
+ "the deployer from the transaction.";
413
+ throw _msg;
414
+ }
415
+ } catch (
416
+ _error) {
417
+ _msg_error(
418
+ _error,
419
+ 1);
420
+ }
421
+ }
422
+
423
+ if ( _cmdline_check(
424
+ "deployer-get") ) {
425
+ _global_variables();
426
+ _cmdline_parse();
427
+ _overrides_set();
428
+ _config_show();
429
+ app_opts = [
430
+ wallet_seed,
431
+ retries_max,
432
+ api_key_path,
433
+ chain_id,
434
+ chain_name,
435
+ target_network,
436
+ contract_address,
437
+ transaction_deployment
438
+ ];
439
+ _deployer_get_cmdline(
440
+ app_opts);
441
+ }
442
+
443
+ module.exports = {
444
+ _deployer_get:
445
+ _deployer_get
446
+ };