rocketh 0.9.1 → 0.10.0

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.
package/dist/index.cjs CHANGED
@@ -30,13 +30,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
+ chainById: () => chainById,
33
34
  execute: () => execute,
34
35
  executeDeployScripts: () => executeDeployScripts,
35
36
  extendEnvironment: () => extendEnvironment,
37
+ getChain: () => getChain,
36
38
  handleSignerProtocol: () => handleSignerProtocol,
37
39
  loadAndExecuteDeployments: () => loadAndExecuteDeployments,
38
40
  loadDeployments: () => loadDeployments,
39
41
  loadEnvironment: () => loadEnvironment,
42
+ mergeArtifacts: () => mergeArtifacts,
40
43
  readAndResolveConfig: () => readAndResolveConfig,
41
44
  readConfig: () => readConfig,
42
45
  resolveConfig: () => resolveConfig
@@ -112,9 +115,9 @@ function stringToJSON(str) {
112
115
  // src/environment/deployments.ts
113
116
  var import_node_path2 = __toESM(require("path"), 1);
114
117
  var import_node_fs2 = __toESM(require("fs"), 1);
115
- function loadDeployments(deploymentsPath, subPath, onlyABIAndAddress, expectedChain) {
118
+ function loadDeployments(deploymentsPath, networkName, onlyABIAndAddress, expectedChain) {
116
119
  const deploymentsFound = {};
117
- const deployPath = import_node_path2.default.join(deploymentsPath, subPath);
120
+ const deployPath = import_node_path2.default.join(deploymentsPath, networkName);
118
121
  let filesStats;
119
122
  try {
120
123
  filesStats = traverse(deployPath, void 0, void 0, (name) => !name.startsWith(".") && name !== "solcInputs");
@@ -136,7 +139,7 @@ function loadDeployments(deploymentsPath, subPath, onlyABIAndAddress, expectedCh
136
139
  genesisHash = chainData.genesisHash;
137
140
  } else {
138
141
  throw new Error(
139
- `A '.chain' or '.chainId' file is expected to be present in the deployment folder for network ${subPath}`
142
+ `A '.chain' or '.chainId' file is expected to be present in the deployment folder for network ${networkName}`
140
143
  );
141
144
  }
142
145
  }
@@ -283,6 +286,124 @@ function getChain(id) {
283
286
  return chain;
284
287
  }
285
288
 
289
+ // src/environment/utils/artifacts.ts
290
+ var import_ethers = require("ethers");
291
+ function deepEqual(obj1, obj2) {
292
+ function isObject(obj) {
293
+ if (typeof obj === "object" && obj != null) {
294
+ return true;
295
+ } else {
296
+ return false;
297
+ }
298
+ }
299
+ if (obj1 === obj2) {
300
+ return true;
301
+ } else if (isObject(obj1) && isObject(obj2)) {
302
+ if (Object.keys(obj1).length !== Object.keys(obj2).length) {
303
+ return false;
304
+ }
305
+ for (var prop in obj1) {
306
+ if (!deepEqual(obj1[prop], obj2[prop])) {
307
+ return false;
308
+ }
309
+ }
310
+ return true;
311
+ }
312
+ return false;
313
+ }
314
+ function mergeDoc(values, mergedDevDocs, field) {
315
+ if (values[field]) {
316
+ const mergedEventDocs = mergedDevDocs[field] = mergedDevDocs[field] || {};
317
+ for (const signature of Object.keys(values[field])) {
318
+ if (mergedEventDocs[signature] && !deepEqual(mergedEventDocs[signature], values[field][signature])) {
319
+ throw new Error(`Doc ${field} conflict: "${signature}" `);
320
+ }
321
+ mergedEventDocs[signature] = values[field][signature];
322
+ }
323
+ }
324
+ }
325
+ function mergeArtifacts(list) {
326
+ const mergedABI = [];
327
+ const added = /* @__PURE__ */ new Map();
328
+ const mergedDevDocs = { kind: "dev", version: 1, methods: {} };
329
+ const mergedUserDocs = { kind: "user", version: 1, methods: {} };
330
+ const sigJSMap = /* @__PURE__ */ new Map();
331
+ for (let i = 0; i < list.length; i++) {
332
+ const listElem = list[i];
333
+ for (const element of listElem.artifact.abi) {
334
+ if (element.type === "function") {
335
+ const selector = import_ethers.FunctionFragment.from(element).selector;
336
+ if (sigJSMap.has(selector)) {
337
+ const existing = sigJSMap.get(selector);
338
+ throw new Error(
339
+ `ABI conflict: ${existing.routeName} has function "${existing.functionName}" which conflict with ${listElem.name}'s "${element.name}" (selector: "${selector}") `
340
+ );
341
+ }
342
+ sigJSMap.set(selector, { index: i, routeName: listElem.name, functionName: element.name });
343
+ const exists = added.has(element.name);
344
+ if (exists) {
345
+ } else {
346
+ added.set(element.name, element);
347
+ mergedABI.push(element);
348
+ }
349
+ } else if (element.type === "constructor") {
350
+ } else if (element.type === "error") {
351
+ const exists = added.has(element.name);
352
+ if (exists) {
353
+ } else {
354
+ added.set(element.name, element);
355
+ mergedABI.push(element);
356
+ }
357
+ } else if (element.type === "event") {
358
+ const exists = added.has(element.name);
359
+ if (exists) {
360
+ } else {
361
+ added.set(element.name, element);
362
+ mergedABI.push(element);
363
+ }
364
+ } else if (element.type === "fallback") {
365
+ } else if (element.type === "receive") {
366
+ } else {
367
+ }
368
+ }
369
+ const devdoc = listElem.artifact.devdoc;
370
+ if (devdoc) {
371
+ mergeDoc(devdoc, mergedDevDocs, "events");
372
+ mergeDoc(devdoc, mergedDevDocs, "errors");
373
+ mergeDoc(devdoc, mergedDevDocs, "methods");
374
+ if (devdoc.author) {
375
+ if (mergedDevDocs.author && mergedDevDocs.author != devdoc.author) {
376
+ throw new Error(`DevDoc author conflict `);
377
+ }
378
+ mergedDevDocs.author = devdoc.author;
379
+ if (mergedDevDocs.title && mergedDevDocs.title != devdoc.title) {
380
+ throw new Error(`DevDoc title conflict `);
381
+ }
382
+ mergedDevDocs.title = devdoc.title;
383
+ }
384
+ }
385
+ const userdoc = listElem.artifact.userdoc;
386
+ if (userdoc) {
387
+ mergeDoc(userdoc, mergedUserDocs, "events");
388
+ mergeDoc(userdoc, mergedUserDocs, "errors");
389
+ mergeDoc(userdoc, mergedUserDocs, "methods");
390
+ if (userdoc.notice) {
391
+ if (mergedUserDocs.notice && mergedUserDocs.notice != userdoc.notice) {
392
+ throw new Error(`UserDoc notice conflict `);
393
+ }
394
+ mergedUserDocs.notice = userdoc.notice;
395
+ }
396
+ }
397
+ }
398
+ return {
399
+ mergedABI,
400
+ added,
401
+ mergedDevDocs,
402
+ mergedUserDocs,
403
+ sigJSMap
404
+ };
405
+ }
406
+
286
407
  // src/environment/index.ts
287
408
  globalThis.extensions = [];
288
409
  function extendEnvironment(extension) {
@@ -309,7 +430,7 @@ function displayTransaction(transaction) {
309
430
  }
310
431
  }
311
432
  async function createEnvironment(config, providedContext) {
312
- const provider = "provider" in config ? config.provider : new import_eip_1193_jsonrpc_provider.JSONRPCHTTPProvider(config.nodeUrl);
433
+ const provider = "provider" in config.network ? config.network.provider : new import_eip_1193_jsonrpc_provider.JSONRPCHTTPProvider(config.network.nodeUrl);
313
434
  const transport = (0, import_viem.custom)(provider);
314
435
  const viemClient = (0, import_viem.createPublicClient)({ transport });
315
436
  const chainId = (await viemClient.getChainId()).toString();
@@ -321,23 +442,29 @@ async function createEnvironment(config, providedContext) {
321
442
  }
322
443
  let networkName;
323
444
  let saveDeployments;
324
- let tags = {};
445
+ let networkTags = {};
446
+ for (const networkTag of config.network.tags) {
447
+ networkTags[networkTag] = true;
448
+ }
325
449
  if ("nodeUrl" in config) {
326
- networkName = config.networkName;
450
+ networkName = config.network.name;
327
451
  saveDeployments = true;
328
452
  } else {
329
- if (config.networkName) {
330
- networkName = config.networkName;
453
+ if (config.network.name) {
454
+ networkName = config.network.name;
331
455
  } else {
332
456
  networkName = "memory";
333
457
  }
334
458
  if (networkName === "memory" || networkName === "hardhat") {
335
- tags["memory"] = true;
459
+ networkTags["memory"] = true;
336
460
  saveDeployments = false;
337
461
  } else {
338
462
  saveDeployments = true;
339
463
  }
340
464
  }
465
+ if (config.saveDeployments !== void 0) {
466
+ saveDeployments = config.saveDeployments;
467
+ }
341
468
  const resolvedAccounts = {};
342
469
  const accountCache = {};
343
470
  async function getAccount(name, accounts, accountDef) {
@@ -417,15 +544,21 @@ async function createEnvironment(config, providedContext) {
417
544
  artifacts: providedContext.artifacts,
418
545
  network: {
419
546
  name: networkName,
547
+ fork: config.network.fork,
420
548
  saveDeployments,
421
- tags
549
+ tags: networkTags
422
550
  }
423
551
  };
424
- const { deployments } = loadDeployments(config.deployments, context.network.name, false, {
425
- chainId,
426
- genesisHash,
427
- deleteDeploymentsIfDifferentGenesisHash: true
428
- });
552
+ const { deployments } = loadDeployments(
553
+ config.deployments,
554
+ context.network.name,
555
+ false,
556
+ context.network.fork ? void 0 : {
557
+ chainId,
558
+ genesisHash,
559
+ deleteDeploymentsIfDifferentGenesisHash: true
560
+ }
561
+ );
429
562
  const namedAccounts = {};
430
563
  const namedSigners = {};
431
564
  const addressSigners = {};
@@ -467,7 +600,31 @@ async function createEnvironment(config, providedContext) {
467
600
  return deployment;
468
601
  }
469
602
  function getOrNull(name) {
470
- return deployments[name];
603
+ return deployments[name] || null;
604
+ }
605
+ function fromAddressToNamedABIOrNull(address) {
606
+ let list = [];
607
+ for (const name of Object.keys(deployments)) {
608
+ const deployment = deployments[name];
609
+ if (deployment.address.toLowerCase() == address.toLowerCase()) {
610
+ list.push({ name, artifact: deployment });
611
+ }
612
+ }
613
+ if (list.length === 0) {
614
+ return null;
615
+ }
616
+ const { mergedABI } = mergeArtifacts(list);
617
+ return {
618
+ mergedABI,
619
+ names: list.map((v) => v.name)
620
+ };
621
+ }
622
+ function fromAddressToNamedABI(address) {
623
+ const n = fromAddressToNamedABIOrNull(address);
624
+ if (!n) {
625
+ throw new Error(`could not find artifact for address ${address}`);
626
+ }
627
+ return n;
471
628
  }
472
629
  async function save(name, deployment) {
473
630
  deployments[name] = deployment;
@@ -736,6 +893,8 @@ async function createEnvironment(config, providedContext) {
736
893
  savePendingExecution,
737
894
  get,
738
895
  getOrNull,
896
+ fromAddressToNamedABI,
897
+ fromAddressToNamedABIOrNull,
739
898
  showMessage,
740
899
  showProgress
741
900
  };
@@ -762,24 +921,55 @@ function execute(context, callback, options) {
762
921
  scriptModule.dependencies = options.dependencies;
763
922
  return scriptModule;
764
923
  }
765
- function readConfig(options, extra) {
924
+ function readConfig(options) {
766
925
  let configFile;
767
926
  try {
768
927
  const configString = import_node_fs4.default.readFileSync("./rocketh.json", "utf-8");
769
928
  configFile = JSON.parse(configString);
770
929
  } catch {
771
930
  }
772
- let nodeUrl;
931
+ if (configFile) {
932
+ if (!options.deployments && configFile.deployments) {
933
+ options.deployments = configFile.deployments;
934
+ }
935
+ if (!options.scripts && configFile.scripts) {
936
+ options.scripts = configFile.scripts;
937
+ }
938
+ }
773
939
  const fromEnv = process.env["ETH_NODE_URI_" + options.network];
774
- if (typeof fromEnv === "string") {
775
- nodeUrl = fromEnv;
776
- } else {
777
- if (configFile) {
778
- const network = configFile.networks && configFile.networks[options.network];
779
- if (network) {
780
- nodeUrl = network.rpcUrl;
940
+ const fork = typeof options.network !== "string";
941
+ let networkName = "memory";
942
+ if (options.network) {
943
+ if (typeof options.network === "string") {
944
+ networkName = options.network;
945
+ } else if ("fork" in options.network) {
946
+ networkName = options.network.fork;
947
+ }
948
+ }
949
+ let networkTags = configFile?.networks && configFile?.networks[networkName]?.tags || [];
950
+ if (!options.provider) {
951
+ let nodeUrl;
952
+ if (typeof fromEnv === "string") {
953
+ nodeUrl = fromEnv;
954
+ } else {
955
+ if (configFile) {
956
+ const network = configFile.networks && configFile.networks[networkName];
957
+ if (network && network.rpcUrl) {
958
+ nodeUrl = network.rpcUrl;
959
+ } else {
960
+ if (options?.ignoreMissingRPC) {
961
+ nodeUrl = "";
962
+ } else {
963
+ if (options.network === "localhost") {
964
+ nodeUrl = "http://127.0.0.1:8545";
965
+ } else {
966
+ logger.error(`network "${options.network}" is not configured. Please add it to the rocketh.json file`);
967
+ process.exit(1);
968
+ }
969
+ }
970
+ }
781
971
  } else {
782
- if (extra?.ignoreMissingRPC) {
972
+ if (options?.ignoreMissingRPC) {
783
973
  nodeUrl = "";
784
974
  } else {
785
975
  if (options.network === "localhost") {
@@ -790,47 +980,59 @@ function readConfig(options, extra) {
790
980
  }
791
981
  }
792
982
  }
793
- } else {
794
- if (extra?.ignoreMissingRPC) {
795
- nodeUrl = "";
796
- } else {
797
- if (options.network === "localhost") {
798
- nodeUrl = "http://127.0.0.1:8545";
799
- } else {
800
- logger.error(`network "${options.network}" is not configured. Please add it to the rocketh.json file`);
801
- process.exit(1);
802
- }
803
- }
804
983
  }
984
+ return {
985
+ network: {
986
+ nodeUrl,
987
+ name: networkName,
988
+ tags: networkTags,
989
+ fork
990
+ },
991
+ deployments: options.deployments,
992
+ saveDeployments: options.saveDeployments,
993
+ scripts: options.scripts,
994
+ tags: typeof options.tags === "undefined" ? void 0 : options.tags.split(","),
995
+ logLevel: options.logLevel
996
+ };
997
+ } else {
998
+ return {
999
+ network: {
1000
+ provider: options.provider,
1001
+ name: networkName,
1002
+ tags: networkTags,
1003
+ fork
1004
+ },
1005
+ deployments: options.deployments,
1006
+ saveDeployments: options.saveDeployments,
1007
+ scripts: options.scripts,
1008
+ tags: typeof options.tags === "undefined" ? void 0 : options.tags.split(","),
1009
+ logLevel: options.logLevel
1010
+ };
805
1011
  }
806
- return {
807
- nodeUrl,
808
- networkName: options.network,
809
- deployments: options.deployments,
810
- scripts: options.scripts,
811
- tags: typeof options.tags === "undefined" ? void 0 : options.tags.split(",")
812
- };
813
1012
  }
814
- function readAndResolveConfig(options, extra) {
815
- return resolveConfig(readConfig(options, extra));
1013
+ function readAndResolveConfig(options) {
1014
+ return resolveConfig(readConfig(options));
816
1015
  }
817
1016
  function resolveConfig(config) {
818
1017
  const resolvedConfig = {
819
1018
  ...config,
820
- networkName: config.networkName || "memory",
1019
+ network: config.network,
1020
+ // TODO default to || {name: 'memory'....}
821
1021
  deployments: config.deployments || "deployments",
822
1022
  scripts: config.scripts || "deploy",
823
- tags: config.tags || []
1023
+ tags: config.tags || [],
1024
+ networkTags: config.networkTags || [],
1025
+ saveDeployments: config.saveDeployments
824
1026
  };
825
1027
  return resolvedConfig;
826
1028
  }
827
- async function loadEnvironment(config, context) {
828
- const resolvedConfig = resolveConfig(config);
1029
+ async function loadEnvironment(options, context) {
1030
+ const resolvedConfig = readAndResolveConfig(options);
829
1031
  const { external, internal } = await createEnvironment(resolvedConfig, context);
830
1032
  return external;
831
1033
  }
832
- async function loadAndExecuteDeployments(config, args) {
833
- const resolvedConfig = resolveConfig(config);
1034
+ async function loadAndExecuteDeployments(options, args) {
1035
+ const resolvedConfig = readAndResolveConfig(options);
834
1036
  return executeDeployScripts(resolvedConfig, args);
835
1037
  }
836
1038
  async function executeDeployScripts(config, args) {
@@ -983,13 +1185,16 @@ async function executeDeployScripts(config, args) {
983
1185
  }
984
1186
  // Annotate the CommonJS export names for ESM import in node:
985
1187
  0 && (module.exports = {
1188
+ chainById,
986
1189
  execute,
987
1190
  executeDeployScripts,
988
1191
  extendEnvironment,
1192
+ getChain,
989
1193
  handleSignerProtocol,
990
1194
  loadAndExecuteDeployments,
991
1195
  loadDeployments,
992
1196
  loadEnvironment,
1197
+ mergeArtifacts,
993
1198
  readAndResolveConfig,
994
1199
  readConfig,
995
1200
  resolveConfig