@latticexyz/cli 2.0.0-next.1 → 2.0.0-next.11

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.
Files changed (52) hide show
  1. package/dist/chunk-TW3YGZ4D.js +11 -0
  2. package/dist/chunk-TW3YGZ4D.js.map +1 -0
  3. package/dist/index.js +1 -1
  4. package/dist/mud.js +11 -7
  5. package/dist/mud.js.map +1 -1
  6. package/package.json +17 -15
  7. package/src/commands/deploy.ts +1 -1
  8. package/src/commands/dev-contracts.ts +17 -16
  9. package/src/commands/index.ts +2 -2
  10. package/src/commands/set-version.ts +19 -60
  11. package/src/commands/tablegen.ts +3 -2
  12. package/src/commands/test.ts +3 -2
  13. package/src/commands/trace.ts +17 -9
  14. package/src/commands/worldgen.ts +1 -1
  15. package/src/common.ts +1 -0
  16. package/src/index.ts +0 -1
  17. package/src/mud.ts +5 -2
  18. package/src/mudPackages.ts +24 -0
  19. package/src/utils/deploy.ts +189 -554
  20. package/src/utils/deployHandler.ts +9 -3
  21. package/src/utils/modules/constants.ts +23 -0
  22. package/src/utils/modules/getInstallModuleCallData.ts +27 -0
  23. package/src/utils/modules/getUserModules.ts +5 -0
  24. package/src/utils/modules/types.ts +14 -0
  25. package/src/utils/systems/getGrantAccessCallData.ts +29 -0
  26. package/src/utils/systems/getRegisterFunctionSelectorsCallData.ts +57 -0
  27. package/src/utils/systems/getRegisterSystemCallData.ts +17 -0
  28. package/src/utils/systems/types.ts +9 -0
  29. package/src/utils/systems/utils.ts +42 -0
  30. package/src/utils/tables/getRegisterTableCallData.ts +49 -0
  31. package/src/utils/tables/getTableIds.ts +18 -0
  32. package/src/utils/tables/types.ts +12 -0
  33. package/src/utils/utils/confirmNonce.ts +24 -0
  34. package/src/utils/utils/deployContract.ts +33 -0
  35. package/src/utils/utils/fastTxExecute.ts +56 -0
  36. package/src/utils/utils/getContractData.ts +29 -0
  37. package/src/utils/utils/postDeploy.ts +25 -0
  38. package/src/utils/utils/setInternalFeePerGas.ts +49 -0
  39. package/src/utils/utils/types.ts +21 -0
  40. package/src/utils/world.ts +28 -0
  41. package/dist/chunk-P7JIR52V.js +0 -32
  42. package/dist/chunk-P7JIR52V.js.map +0 -1
  43. package/src/commands/tsgen.ts +0 -34
  44. package/src/render-ts/index.ts +0 -5
  45. package/src/render-ts/recsV1TableOptions.ts +0 -57
  46. package/src/render-ts/renderRecsV1Tables.ts +0 -35
  47. package/src/render-ts/schemaTypesToRecsTypeStrings.ts +0 -202
  48. package/src/render-ts/tsgen.ts +0 -12
  49. package/src/render-ts/types.ts +0 -17
  50. package/src/utils/index.ts +0 -7
  51. package/src/utils/worldtypes.ts +0 -21
  52. /package/src/utils/{getChainId.ts → utils/getChainId.ts} +0 -0
@@ -1,11 +1,12 @@
1
1
  import chalk from "chalk";
2
- import { existsSync, readFileSync, rmSync, writeFileSync } from "fs";
2
+ import { readFileSync, writeFileSync } from "fs";
3
3
  import path from "path";
4
4
  import type { CommandModule } from "yargs";
5
5
  import { MUDError } from "@latticexyz/common/errors";
6
6
  import { logError } from "../utils/errors";
7
7
  import localPackageJson from "../../package.json" assert { type: "json" };
8
8
  import glob from "glob";
9
+ import { mudPackages } from "../mudPackages";
9
10
 
10
11
  type Options = {
11
12
  backup?: boolean;
@@ -17,9 +18,6 @@ type Options = {
17
18
  link?: string;
18
19
  };
19
20
 
20
- const BACKUP_FILE = ".mudbackup";
21
- const MUD_PREFIX = "@latticexyz";
22
-
23
21
  const commandModule: CommandModule<Options, Options> = {
24
22
  command: "set-version",
25
23
 
@@ -27,12 +25,6 @@ const commandModule: CommandModule<Options, Options> = {
27
25
 
28
26
  builder(yargs) {
29
27
  return yargs.options({
30
- backup: { type: "boolean", description: `Back up the current MUD versions to "${BACKUP_FILE}"` },
31
- force: {
32
- type: "boolean",
33
- description: `Backup fails if a "${BACKUP_FILE}" file is found, unless --force is provided`,
34
- },
35
- restore: { type: "boolean", description: `Restore the previous MUD versions from "${BACKUP_FILE}"` },
36
28
  mudVersion: { alias: "v", type: "string", description: "Set MUD to the given version" },
37
29
  tag: {
38
30
  alias: "t",
@@ -117,63 +109,39 @@ async function resolveVersion(options: Options) {
117
109
  }
118
110
 
119
111
  function updatePackageJson(filePath: string, options: Options): { workspaces?: string[] } {
120
- const { restore, force, link } = options;
121
- let { backup, mudVersion } = options;
122
-
123
- const backupFilePath = path.join(path.dirname(filePath), BACKUP_FILE);
124
- const backupFileExists = existsSync(backupFilePath);
125
-
126
- // Create a backup file for previous MUD versions by default if linking to local MUD
127
- if (link && !backupFileExists) backup = true;
128
-
129
- // If `backup` is true and force not set, check if a backup file already exists and throw an error if it does
130
- if (backup && !force && backupFileExists) {
131
- throw new MUDError(
132
- `A backup file already exists at ${backupFilePath}.\nUse --force to overwrite it or --restore to restore it.`
133
- );
134
- }
112
+ const { link } = options;
113
+ let { mudVersion } = options;
135
114
 
136
115
  const packageJson = readPackageJson(filePath);
137
-
138
- // Load .mudbackup if `restore` is true
139
- const backupJson = restore ? readPackageJson(backupFilePath) : undefined;
116
+ const mudPackageNames = Object.keys(mudPackages);
140
117
 
141
118
  // Find all MUD dependencies
142
119
  const mudDependencies: Record<string, string> = {};
143
- for (const key in packageJson.dependencies) {
144
- if (key.startsWith(MUD_PREFIX)) {
145
- mudDependencies[key] = packageJson.dependencies[key];
120
+ for (const packageName in packageJson.dependencies) {
121
+ if (mudPackageNames.includes(packageName)) {
122
+ mudDependencies[packageName] = packageJson.dependencies[packageName];
146
123
  }
147
124
  }
148
125
 
149
126
  // Find all MUD devDependencies
150
127
  const mudDevDependencies: Record<string, string> = {};
151
- for (const key in packageJson.devDependencies) {
152
- if (key.startsWith(MUD_PREFIX)) {
153
- mudDevDependencies[key] = packageJson.devDependencies[key];
128
+ for (const packageName in packageJson.devDependencies) {
129
+ if (mudPackageNames.includes(packageName)) {
130
+ mudDevDependencies[packageName] = packageJson.devDependencies[packageName];
154
131
  }
155
132
  }
156
133
 
157
- // Back up the current dependencies if `backup` is true
158
- if (backup) {
159
- writeFileSync(
160
- backupFilePath,
161
- JSON.stringify({ dependencies: mudDependencies, devDependencies: mudDevDependencies }, null, 2)
162
- );
163
- console.log(chalk.green(`Backed up MUD dependencies from ${filePath} to ${backupFilePath}`));
164
- }
165
-
166
134
  // Update the dependencies
167
- for (const key in packageJson.dependencies) {
168
- if (key.startsWith(MUD_PREFIX)) {
169
- packageJson.dependencies[key] = resolveMudVersion(key, "dependencies");
135
+ for (const packageName in packageJson.dependencies) {
136
+ if (mudPackageNames.includes(packageName)) {
137
+ packageJson.dependencies[packageName] = resolveMudVersion(packageName, "dependencies");
170
138
  }
171
139
  }
172
140
 
173
141
  // Update the devDependencies
174
- for (const key in packageJson.devDependencies) {
175
- if (key.startsWith(MUD_PREFIX)) {
176
- packageJson.devDependencies[key] = resolveMudVersion(key, "devDependencies");
142
+ for (const packageName in packageJson.devDependencies) {
143
+ if (mudPackageNames.includes(packageName)) {
144
+ packageJson.devDependencies[packageName] = resolveMudVersion(packageName, "devDependencies");
177
145
  }
178
146
  }
179
147
 
@@ -184,17 +152,9 @@ function updatePackageJson(filePath: string, options: Options): { workspaces?: s
184
152
  logComparison(mudDependencies, packageJson.dependencies);
185
153
  logComparison(mudDevDependencies, packageJson.devDependencies);
186
154
 
187
- // Remove the backup file if `restore` is true and `backup` is false
188
- // because the old backup file is no longer needed
189
- if (restore && !backup) {
190
- rmSync(backupFilePath);
191
- console.log(chalk.green(`Cleaned up ${backupFilePath}`));
192
- }
193
-
194
155
  return packageJson;
195
156
 
196
157
  function resolveMudVersion(key: string, type: "dependencies" | "devDependencies") {
197
- if (restore && backupJson) return backupJson[type][key];
198
158
  if (link) mudVersion = resolveLinkPath(filePath, link, key);
199
159
  if (!mudVersion) return packageJson[type][key];
200
160
  return mudVersion;
@@ -225,10 +185,9 @@ function logComparison(prev: Record<string, string>, curr: Record<string, string
225
185
  /**
226
186
  * Returns path of the package to link, given a path to a local MUD clone and a package
227
187
  */
228
- function resolveLinkPath(packageJsonPath: string, mudLinkPath: string, pkg: string) {
229
- const pkgName = pkg.replace(MUD_PREFIX, "");
188
+ function resolveLinkPath(packageJsonPath: string, mudLinkPath: string, packageName: string) {
230
189
  const packageJsonToRootPath = path.relative(path.dirname(packageJsonPath), process.cwd());
231
- const linkPath = path.join(packageJsonToRootPath, mudLinkPath, "packages", pkgName);
190
+ const linkPath = path.join(packageJsonToRootPath, mudLinkPath, mudPackages[packageName].localPath);
232
191
  return "link:" + linkPath;
233
192
  }
234
193
 
@@ -3,7 +3,7 @@ import type { CommandModule } from "yargs";
3
3
  import { loadConfig } from "@latticexyz/config/node";
4
4
  import { StoreConfig } from "@latticexyz/store";
5
5
  import { tablegen } from "@latticexyz/store/codegen";
6
- import { getSrcDirectory } from "@latticexyz/common/foundry";
6
+ import { getRemappings, getSrcDirectory } from "@latticexyz/common/foundry";
7
7
 
8
8
  type Options = {
9
9
  configPath?: string;
@@ -23,8 +23,9 @@ const commandModule: CommandModule<Options, Options> = {
23
23
  async handler({ configPath }) {
24
24
  const config = (await loadConfig(configPath)) as StoreConfig;
25
25
  const srcDir = await getSrcDirectory();
26
+ const remappings = await getRemappings();
26
27
 
27
- await tablegen(config, path.join(srcDir, config.codegenDirectory));
28
+ await tablegen(config, path.join(srcDir, config.codegenDirectory), remappings);
28
29
 
29
30
  process.exit(0);
30
31
  },
@@ -3,7 +3,7 @@ import { anvil, forge, getRpcUrl } from "@latticexyz/common/foundry";
3
3
  import chalk from "chalk";
4
4
  import { rmSync, writeFileSync } from "fs";
5
5
  import { yDeployOptions } from "./deploy";
6
- import { deployHandler, DeployOptions } from "../utils";
6
+ import { DeployOptions, deployHandler } from "../utils/deployHandler";
7
7
 
8
8
  type Options = DeployOptions & { port?: number; worldAddress?: string; forgeOptions?: string };
9
9
 
@@ -59,10 +59,11 @@ const commandModule: CommandModule<Options, Options> = {
59
59
  console.log(testResult);
60
60
  } catch (e) {
61
61
  console.error(e);
62
+ rmSync(WORLD_ADDRESS_FILE);
63
+ process.exit(1);
62
64
  }
63
65
 
64
66
  rmSync(WORLD_ADDRESS_FILE);
65
-
66
67
  process.exit(0);
67
68
  },
68
69
  };
@@ -5,14 +5,20 @@ import { ethers } from "ethers";
5
5
  import { loadConfig } from "@latticexyz/config/node";
6
6
  import { MUDError } from "@latticexyz/common/errors";
7
7
  import { cast, getRpcUrl, getSrcDirectory } from "@latticexyz/common/foundry";
8
- import { TableId } from "@latticexyz/common/deprecated";
9
8
  import { StoreConfig } from "@latticexyz/store";
10
9
  import { resolveWorldConfig, WorldConfig } from "@latticexyz/world";
11
- import { IBaseWorld } from "@latticexyz/world/types/ethers-contracts/IBaseWorld";
12
- import IBaseWorldData from "@latticexyz/world/abi/IBaseWorld.sol/IBaseWorld.json" assert { type: "json" };
13
- import { getChainId, getExistingContracts } from "../utils";
14
-
15
- const systemsTableId = new TableId("", "Systems");
10
+ import IBaseWorldAbi from "@latticexyz/world/out/IBaseWorld.sol/IBaseWorld.abi.json" assert { type: "json" };
11
+ import worldConfig from "@latticexyz/world/mud.config.js";
12
+ import { resourceIdToHex } from "@latticexyz/common";
13
+ import { getExistingContracts } from "../utils/getExistingContracts";
14
+ import { getChainId } from "../utils/utils/getChainId";
15
+
16
+ // TODO account for multiple namespaces (https://github.com/latticexyz/mud/issues/994)
17
+ const systemsTableId = resourceIdToHex({
18
+ type: "system",
19
+ namespace: worldConfig.namespace,
20
+ name: worldConfig.tables.Systems.name,
21
+ });
16
22
 
17
23
  type Options = {
18
24
  tx: string;
@@ -64,17 +70,19 @@ const commandModule: CommandModule<Options, Options> = {
64
70
 
65
71
  // Create World contract instance from deployed address
66
72
  const provider = new ethers.providers.StaticJsonRpcProvider(rpc);
67
- const WorldContract = new ethers.Contract(worldAddress, IBaseWorldData.abi, provider) as IBaseWorld;
73
+ const WorldContract = new ethers.Contract(worldAddress, IBaseWorldAbi, provider);
68
74
 
69
75
  // TODO account for multiple namespaces (https://github.com/latticexyz/mud/issues/994)
70
76
  const namespace = mudConfig.namespace;
71
77
  const names = Object.values(resolvedConfig.systems).map(({ name }) => name);
72
78
 
79
+ // Fetch system table field layout from chain
80
+ const systemTableFieldLayout = await WorldContract.getFieldLayout(systemsTableId);
73
81
  const labels: { name: string; address: string }[] = [];
74
82
  for (const name of names) {
75
- const systemSelector = new TableId(namespace, name);
83
+ const systemSelector = resourceIdToHex({ type: "system", namespace, name });
76
84
  // Get the first field of `Systems` table (the table maps system name to its address and other data)
77
- const address = await WorldContract.getField(systemsTableId.toHex(), [systemSelector.toHex()], 0);
85
+ const address = await WorldContract.getField(systemsTableId, [systemSelector], 0, systemTableFieldLayout);
78
86
  labels.push({ name, address });
79
87
  }
80
88
 
@@ -6,7 +6,7 @@ import { worldgen } from "@latticexyz/world/node";
6
6
  import { getSrcDirectory } from "@latticexyz/common/foundry";
7
7
  import path from "path";
8
8
  import { rmSync } from "fs";
9
- import { getExistingContracts } from "../utils";
9
+ import { getExistingContracts } from "../utils/getExistingContracts";
10
10
 
11
11
  type Options = {
12
12
  configPath?: string;
package/src/common.ts ADDED
@@ -0,0 +1 @@
1
+ export type MudPackages = Record<string, { localPath: string }>;
package/src/index.ts CHANGED
@@ -1,2 +1 @@
1
- export * from "./render-ts";
2
1
  export * from "./utils/deployHandler";
package/src/mud.ts CHANGED
@@ -27,8 +27,11 @@ yargs(hideBin(process.argv))
27
27
  );
28
28
  }
29
29
  console.log("");
30
- logError(err);
31
- console.log("");
30
+ // Even though `.fail` type says we should get an `Error`, this can sometimes be undefined
31
+ if (err != null) {
32
+ logError(err);
33
+ console.log("");
34
+ }
32
35
 
33
36
  process.exit(1);
34
37
  })
@@ -0,0 +1,24 @@
1
+ import { ZodError, z } from "zod";
2
+ import { MudPackages } from "./common";
3
+
4
+ const envSchema = z.object({
5
+ MUD_PACKAGES: z.string().transform((value) => JSON.parse(value) as MudPackages),
6
+ });
7
+
8
+ function parseEnv(): z.infer<typeof envSchema> {
9
+ try {
10
+ return envSchema.parse({
11
+ // tsup replaces the env vars with their values at compile time
12
+ MUD_PACKAGES: process.env.MUD_PACKAGES,
13
+ });
14
+ } catch (error) {
15
+ if (error instanceof ZodError) {
16
+ const { _errors, ...invalidEnvVars } = error.format();
17
+ console.error(`\nMissing or invalid environment variables:\n\n ${Object.keys(invalidEnvVars).join("\n ")}\n`);
18
+ process.exit(1);
19
+ }
20
+ throw error;
21
+ }
22
+ }
23
+
24
+ export const mudPackages = parseEnv().MUD_PACKAGES;