@latticexyz/cli 2.0.0-next.0 → 2.0.0-next.10

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 (49) 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 +7 -7
  5. package/dist/mud.js.map +1 -1
  6. package/package.json +19 -17
  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/tablegen.ts +3 -2
  11. package/src/commands/test.ts +3 -2
  12. package/src/commands/trace.ts +17 -9
  13. package/src/commands/worldgen.ts +1 -1
  14. package/src/index.ts +0 -1
  15. package/src/mud.ts +5 -2
  16. package/src/utils/deploy.ts +190 -538
  17. package/src/utils/deployHandler.ts +9 -3
  18. package/src/utils/modules/constants.ts +23 -0
  19. package/src/utils/modules/getInstallModuleCallData.ts +27 -0
  20. package/src/utils/modules/getUserModules.ts +5 -0
  21. package/src/utils/modules/types.ts +14 -0
  22. package/src/utils/systems/getGrantAccessCallData.ts +29 -0
  23. package/src/utils/systems/getRegisterFunctionSelectorsCallData.ts +57 -0
  24. package/src/utils/systems/getRegisterSystemCallData.ts +17 -0
  25. package/src/utils/systems/types.ts +9 -0
  26. package/src/utils/systems/utils.ts +42 -0
  27. package/src/utils/tables/getRegisterTableCallData.ts +49 -0
  28. package/src/utils/tables/getTableIds.ts +18 -0
  29. package/src/utils/tables/types.ts +12 -0
  30. package/src/utils/utils/confirmNonce.ts +24 -0
  31. package/src/utils/utils/deployContract.ts +33 -0
  32. package/src/utils/utils/fastTxExecute.ts +56 -0
  33. package/src/utils/utils/getContractData.ts +29 -0
  34. package/src/utils/utils/postDeploy.ts +25 -0
  35. package/src/utils/utils/setInternalFeePerGas.ts +49 -0
  36. package/src/utils/utils/types.ts +21 -0
  37. package/src/utils/world.ts +28 -0
  38. package/dist/chunk-LFIOIY67.js +0 -30
  39. package/dist/chunk-LFIOIY67.js.map +0 -1
  40. package/src/commands/tsgen.ts +0 -34
  41. package/src/render-ts/index.ts +0 -5
  42. package/src/render-ts/recsV1TableOptions.ts +0 -39
  43. package/src/render-ts/renderRecsV1Tables.ts +0 -31
  44. package/src/render-ts/schemaTypesToRecsTypeStrings.ts +0 -202
  45. package/src/render-ts/tsgen.ts +0 -12
  46. package/src/render-ts/types.ts +0 -13
  47. package/src/utils/index.ts +0 -7
  48. package/src/utils/worldtypes.ts +0 -21
  49. /package/src/utils/{getChainId.ts → utils/getChainId.ts} +0 -0
@@ -1,24 +1,28 @@
1
- import { existsSync, readFileSync } from "fs";
2
- import path from "path";
3
1
  import chalk from "chalk";
4
- import { BigNumber, ContractInterface, ethers } from "ethers";
5
- import { defaultAbiCoder as abi, Fragment, ParamType } from "ethers/lib/utils.js";
6
-
7
- import { getOutDirectory, getScriptDirectory, cast, forge } from "@latticexyz/common/foundry";
8
- import { resolveWithContext } from "@latticexyz/config";
9
- import { MUDError } from "@latticexyz/common/errors";
10
- import { encodeSchema } from "@latticexyz/schema-type/deprecated";
2
+ import path from "path";
3
+ import { ethers } from "ethers";
4
+ import { getOutDirectory, cast, getSrcDirectory, getRemappings } from "@latticexyz/common/foundry";
11
5
  import { StoreConfig } from "@latticexyz/store";
12
- import { resolveAbiOrUserType } from "@latticexyz/store/codegen";
13
6
  import { WorldConfig, resolveWorldConfig } from "@latticexyz/world";
14
- import { IBaseWorld } from "@latticexyz/world/types/ethers-contracts/IBaseWorld";
15
- import WorldData from "@latticexyz/world/abi/World.sol/World.json" assert { type: "json" };
16
- import IBaseWorldData from "@latticexyz/world/abi/IBaseWorld.sol/IBaseWorld.json" assert { type: "json" };
17
- import CoreModuleData from "@latticexyz/world/abi/CoreModule.sol/CoreModule.json" assert { type: "json" };
18
- import KeysWithValueModuleData from "@latticexyz/world/abi/KeysWithValueModule.sol/KeysWithValueModule.json" assert { type: "json" };
19
- import KeysInTableModuleData from "@latticexyz/world/abi/KeysInTableModule.sol/KeysInTableModule.json" assert { type: "json" };
20
- import UniqueEntityModuleData from "@latticexyz/world/abi/UniqueEntityModule.sol/UniqueEntityModule.json" assert { type: "json" };
21
- import SnapSyncModuleData from "@latticexyz/world/abi/SnapSyncModule.sol/SnapSyncModule.json" assert { type: "json" };
7
+ import { deployWorldContract } from "./world";
8
+ import IBaseWorldAbi from "@latticexyz/world/out/IBaseWorld.sol/IBaseWorld.abi.json" assert { type: "json" };
9
+ import CoreModuleData from "@latticexyz/world/out/CoreModule.sol/CoreModule.json" assert { type: "json" };
10
+ import { defaultModuleContracts } from "./modules/constants";
11
+ import { getInstallModuleCallData } from "./modules/getInstallModuleCallData";
12
+ import { getUserModules } from "./modules/getUserModules";
13
+ import { getGrantAccessCallData } from "./systems/getGrantAccessCallData";
14
+ import { getRegisterFunctionSelectorsCallData } from "./systems/getRegisterFunctionSelectorsCallData";
15
+ import { getRegisterSystemCallData } from "./systems/getRegisterSystemCallData";
16
+ import { getRegisterTableCallData } from "./tables/getRegisterTableCallData";
17
+ import { getTableIds } from "./tables/getTableIds";
18
+ import { confirmNonce } from "./utils/confirmNonce";
19
+ import { deployContract } from "./utils/deployContract";
20
+ import { fastTxExecute } from "./utils/fastTxExecute";
21
+ import { getContractData } from "./utils/getContractData";
22
+ import { postDeploy } from "./utils/postDeploy";
23
+ import { setInternalFeePerGas } from "./utils/setInternalFeePerGas";
24
+ import { ContractCode } from "./utils/types";
25
+ import { resourceIdToHex } from "@latticexyz/common";
22
26
 
23
27
  export interface DeployConfig {
24
28
  profile?: string;
@@ -41,562 +45,210 @@ export async function deploy(
41
45
  existingContractNames: string[],
42
46
  deployConfig: DeployConfig
43
47
  ): Promise<DeploymentInfo> {
44
- const resolvedConfig = resolveWorldConfig(mudConfig, existingContractNames);
45
-
46
48
  const startTime = Date.now();
47
- const { worldContractName, namespace, postDeployScript } = mudConfig;
48
49
  const { profile, rpc, privateKey, priorityFeeMultiplier, debug, worldAddress, disableTxWait, pollInterval } =
49
50
  deployConfig;
51
+ const resolvedConfig = resolveWorldConfig(mudConfig, existingContractNames);
50
52
  const forgeOutDirectory = await getOutDirectory(profile);
53
+ const remappings = await getRemappings(profile);
54
+ const outputBaseDirectory = path.join(await getSrcDirectory(profile), mudConfig.codegenDirectory);
51
55
 
52
56
  // Set up signer for deployment
53
57
  const provider = new ethers.providers.StaticJsonRpcProvider(rpc);
54
58
  provider.pollingInterval = pollInterval;
55
59
  const signer = new ethers.Wallet(privateKey, provider);
60
+ console.log("Deploying from", signer.address);
56
61
 
57
- // Manual nonce handling to allow for faster sending of transactions without waiting for previous transactions
58
62
  let nonce = await signer.getTransactionCount();
59
63
  console.log("Initial nonce", nonce);
60
64
 
61
- // Compute maxFeePerGas and maxPriorityFeePerGas like ethers, but allow for a multiplier to allow replacing pending transactions
62
- let maxPriorityFeePerGas: number;
63
- let maxFeePerGas: BigNumber;
64
- await setInternalFeePerGas(priorityFeeMultiplier);
65
+ const txParams = await setInternalFeePerGas(signer, priorityFeeMultiplier);
65
66
 
66
- // Catch all to await any promises before exiting the script
67
- let promises: Promise<unknown>[] = [];
67
+ const txConfig = {
68
+ ...txParams,
69
+ signer,
70
+ debug: Boolean(debug),
71
+ disableTxWait,
72
+ confirmations: disableTxWait ? 0 : 1,
73
+ };
68
74
 
69
75
  // Get block number before deploying
70
76
  const blockNumber = Number(await cast(["block-number", "--rpc-url", rpc], { profile }));
71
77
  console.log("Start deployment at block", blockNumber);
72
78
 
73
- // Deploy World
74
- const worldPromise = {
75
- World: worldAddress
76
- ? Promise.resolve(worldAddress)
77
- : worldContractName
78
- ? deployContractByName(worldContractName, disableTxWait)
79
- : deployContract(IBaseWorldData.abi, WorldData.bytecode, disableTxWait, "World"),
80
- };
81
-
82
- // Deploy Systems
83
- const systemPromises = Object.keys(resolvedConfig.systems).reduce<Record<string, Promise<string>>>(
84
- (acc, systemName) => {
85
- acc[systemName] = deployContractByName(systemName, disableTxWait);
86
- return acc;
87
- },
88
- {}
89
- );
90
-
91
- // Deploy default World modules
92
- const defaultModules: Record<string, Promise<string>> = {
93
- // TODO: these only need to be deployed once per chain, add a check if they exist already
94
- CoreModule: deployContract(CoreModuleData.abi, CoreModuleData.bytecode, disableTxWait, "CoreModule"),
95
- KeysWithValueModule: deployContract(
96
- KeysWithValueModuleData.abi,
97
- KeysWithValueModuleData.bytecode,
98
- disableTxWait,
99
- "KeysWithValueModule"
100
- ),
101
- KeysInTableModule: deployContract(
102
- KeysInTableModuleData.abi,
103
- KeysInTableModuleData.bytecode,
104
- disableTxWait,
105
- "KeysInTableModule"
106
- ),
107
- UniqueEntityModule: deployContract(
108
- UniqueEntityModuleData.abi,
109
- UniqueEntityModuleData.bytecode,
110
- disableTxWait,
111
- "UniqueEntityModule"
112
- ),
113
- SnapSyncModule: deployContract(
114
- SnapSyncModuleData.abi,
115
- SnapSyncModuleData.bytecode,
116
- disableTxWait,
117
- "SnapSyncModule"
118
- ),
119
- };
120
-
121
- // Deploy user Modules
122
- const modulePromises = mudConfig.modules
123
- .filter((module) => !defaultModules[module.name]) // Only deploy user modules here, not default modules
124
- .reduce<Record<string, Promise<string>>>((acc, module) => {
125
- acc[module.name] = deployContractByName(module.name, disableTxWait);
126
- return acc;
127
- }, defaultModules);
128
-
129
- // Combine all contracts into one object
130
- const contractPromises: Record<string, Promise<string>> = { ...worldPromise, ...systemPromises, ...modulePromises };
131
-
132
- // Create World contract instance from deployed address
133
- const WorldContract = new ethers.Contract(await contractPromises.World, IBaseWorldData.abi, signer) as IBaseWorld;
134
-
135
- const confirmations = disableTxWait ? 0 : 1;
136
-
137
- // Install core Modules
138
- if (!worldAddress) {
139
- console.log(chalk.blue("Installing core World modules"));
140
- await fastTxExecute(WorldContract, "installRootModule", [await modulePromises.CoreModule, "0x"], confirmations);
141
- console.log(chalk.green("Installed core World modules"));
142
- }
143
-
144
- // Register namespace
145
- if (namespace) await fastTxExecute(WorldContract, "registerNamespace", [toBytes16(namespace)], confirmations);
146
-
147
- // Register tables
148
- const tableIds: { [tableName: string]: Uint8Array } = {};
149
- promises = [
150
- ...promises,
151
- ...Object.entries(mudConfig.tables).map(async ([tableName, { name, schema, keySchema }]) => {
152
- console.log(chalk.blue(`Registering table ${tableName} at ${namespace}/${name}`));
153
-
154
- // Store the tableId for later use
155
- tableIds[tableName] = toResourceSelector(namespace, name);
156
-
157
- // Register table
158
- const schemaTypes = Object.values(schema).map((abiOrUserType) => {
159
- const { schemaType } = resolveAbiOrUserType(abiOrUserType, mudConfig);
160
- return schemaType;
161
- });
162
-
163
- const keyTypes = Object.values(keySchema).map((abiOrUserType) => {
164
- const { schemaType } = resolveAbiOrUserType(abiOrUserType, mudConfig);
165
- return schemaType;
79
+ // Deploy the World contract. Non-blocking.
80
+ const worldPromise: Promise<string> = worldAddress
81
+ ? Promise.resolve(worldAddress)
82
+ : deployWorldContract({
83
+ ...txConfig,
84
+ nonce: nonce++,
85
+ worldContractName: mudConfig.worldContractName,
86
+ forgeOutDirectory,
166
87
  });
167
88
 
168
- await fastTxExecute(
169
- WorldContract,
170
- "registerTable",
171
- [toBytes16(namespace), toBytes16(name), encodeSchema(schemaTypes), encodeSchema(keyTypes)],
172
- confirmations
173
- );
174
-
175
- // Register table metadata
176
- await fastTxExecute(
177
- WorldContract,
178
- "setMetadata(bytes16,bytes16,string,string[])",
179
- [toBytes16(namespace), toBytes16(name), tableName, Object.keys(schema)],
180
- confirmations
181
- );
182
-
183
- console.log(chalk.green(`Registered table ${tableName} at ${name}`));
184
- }),
185
- ];
186
-
187
- // Register systems (using forEach instead of for..of to avoid blocking on async calls)
188
- promises = [
189
- ...promises,
190
- ...Object.entries(resolvedConfig.systems).map(
191
- async ([systemName, { name, openAccess, registerFunctionSelectors }]) => {
192
- // Register system at route
193
- console.log(chalk.blue(`Registering system ${systemName} at ${namespace}/${name}`));
194
- await fastTxExecute(
195
- WorldContract,
196
- "registerSystem",
197
- [toBytes16(namespace), toBytes16(name), await contractPromises[systemName], openAccess],
198
- confirmations
199
- );
200
- console.log(chalk.green(`Registered system ${systemName} at ${namespace}/${name}`));
201
-
202
- // Register function selectors for the system
203
- if (registerFunctionSelectors) {
204
- const functionSignatures: FunctionSignature[] = await loadFunctionSignatures(systemName);
205
- const isRoot = namespace === "";
206
- // Using Promise.all to avoid blocking on async calls
207
- await Promise.all(
208
- functionSignatures.map(async ({ functionName, functionArgs }) => {
209
- const functionSignature = isRoot
210
- ? functionName + functionArgs
211
- : `${namespace}_${name}_${functionName}${functionArgs}`;
212
-
213
- console.log(chalk.blue(`Registering function "${functionSignature}"`));
214
- if (isRoot) {
215
- const worldFunctionSelector = toFunctionSelector(
216
- functionSignature === ""
217
- ? { functionName: systemName, functionArgs } // Register the system's fallback function as `<systemName>(<args>)`
218
- : { functionName, functionArgs }
219
- );
220
- const systemFunctionSelector = toFunctionSelector({ functionName, functionArgs });
221
- await fastTxExecute(
222
- WorldContract,
223
- "registerRootFunctionSelector",
224
- [toBytes16(namespace), toBytes16(name), worldFunctionSelector, systemFunctionSelector],
225
- confirmations
226
- );
227
- } else {
228
- await fastTxExecute(
229
- WorldContract,
230
- "registerFunctionSelector",
231
- [toBytes16(namespace), toBytes16(name), functionName, functionArgs],
232
- confirmations
233
- );
234
- }
235
- console.log(chalk.green(`Registered function "${functionSignature}"`));
236
- })
237
- );
238
- }
239
- }
240
- ),
241
- ];
242
-
243
- // Wait for resources to be registered before granting access to them
244
- await Promise.all(promises); // ----------------------------------------------------------------------------------------------
245
- promises = [];
246
-
247
- // Grant access to systems
248
- for (const [systemName, { name, accessListAddresses, accessListSystems }] of Object.entries(resolvedConfig.systems)) {
249
- const resourceSelector = `${namespace}/${name}`;
250
-
251
- // Grant access to addresses
252
- promises = [
253
- ...promises,
254
- ...accessListAddresses.map(async (address) => {
255
- console.log(chalk.blue(`Grant ${address} access to ${systemName} (${resourceSelector})`));
256
- await fastTxExecute(
257
- WorldContract,
258
- "grantAccess",
259
- [toBytes16(namespace), toBytes16(name), address],
260
- confirmations
261
- );
262
- console.log(chalk.green(`Granted ${address} access to ${systemName} (${namespace}/${name})`));
263
- }),
264
- ];
265
-
266
- // Grant access to other systems
267
- promises = [
268
- ...promises,
269
- ...accessListSystems.map(async (granteeSystem) => {
270
- console.log(chalk.blue(`Grant ${granteeSystem} access to ${systemName} (${resourceSelector})`));
271
- await fastTxExecute(
272
- WorldContract,
273
- "grantAccess",
274
- [toBytes16(namespace), toBytes16(name), await contractPromises[granteeSystem]],
275
- confirmations
276
- );
277
- console.log(chalk.green(`Granted ${granteeSystem} access to ${systemName} (${resourceSelector})`));
278
- }),
279
- ];
280
- }
281
-
282
- // Wait for access to be granted before installing modules
283
- await Promise.all(promises); // ----------------------------------------------------------------------------------------------
284
- promises = [];
285
-
286
- // Install modules
287
- promises = [
288
- ...promises,
289
- ...mudConfig.modules.map(async (module) => {
290
- console.log(chalk.blue(`Installing${module.root ? " root " : " "}module ${module.name}`));
291
- // Resolve arguments
292
- const resolvedArgs = await Promise.all(
293
- module.args.map((arg) => resolveWithContext(arg, { tableIds, systemAddresses: contractPromises }))
294
- );
295
- const values = resolvedArgs.map((arg) => arg.value);
296
- const types = resolvedArgs.map((arg) => arg.type);
297
- const moduleAddress = await contractPromises[module.name];
298
- if (!moduleAddress) throw new Error(`Module ${module.name} not found`);
299
-
300
- // Send transaction to install module
301
- await fastTxExecute(
302
- WorldContract,
303
- module.root ? "installRootModule" : "installModule",
304
- [moduleAddress, abi.encode(types, values)],
305
- confirmations
306
- );
307
-
308
- console.log(chalk.green(`Installed${module.root ? " root " : " "}module ${module.name}`));
309
- }),
89
+ // Filters any default modules from config
90
+ const userModules = getUserModules(defaultModuleContracts, mudConfig.modules);
91
+ const userModuleContracts = Object.keys(userModules).map((name) => {
92
+ const { abi, bytecode } = getContractData(name, forgeOutDirectory);
93
+ return {
94
+ name,
95
+ abi,
96
+ bytecode,
97
+ } as ContractCode;
98
+ });
99
+
100
+ const systemContracts = Object.keys(resolvedConfig.systems).map((name) => {
101
+ const { abi, bytecode } = getContractData(name, forgeOutDirectory);
102
+ return {
103
+ name,
104
+ abi,
105
+ bytecode,
106
+ } as ContractCode;
107
+ });
108
+
109
+ const contracts: ContractCode[] = [
110
+ {
111
+ name: "CoreModule",
112
+ abi: CoreModuleData.abi,
113
+ bytecode: CoreModuleData.bytecode,
114
+ },
115
+ ...defaultModuleContracts,
116
+ ...userModuleContracts,
117
+ ...systemContracts,
310
118
  ];
311
119
 
312
- // Await all promises before executing PostDeploy script
313
- await Promise.all(promises); // ----------------------------------------------------------------------------------------------
314
-
315
- // Confirm the current nonce is the expected nonce to make sure all transactions have been included
316
- let remoteNonce = await signer.getTransactionCount();
317
- let retryCount = 0;
318
- const maxRetries = 100;
319
- while (remoteNonce !== nonce && retryCount < maxRetries) {
320
- console.log(
321
- chalk.gray(
322
- `Waiting for transactions to be included before executing ${postDeployScript} (local nonce: ${nonce}, remote nonce: ${remoteNonce}, retry number ${retryCount}/${maxRetries})`
323
- )
324
- );
325
- await new Promise((resolve) => setTimeout(resolve, pollInterval));
326
- retryCount++;
327
- remoteNonce = await signer.getTransactionCount();
328
- }
329
- if (remoteNonce !== nonce) {
330
- throw new MUDError(
331
- "Remote nonce doesn't match local nonce, indicating that not all deploy transactions were included."
332
- );
333
- }
334
-
335
- promises = [];
336
-
337
- // Execute postDeploy forge script
338
- const postDeployPath = path.join(await getScriptDirectory(), postDeployScript + ".s.sol");
339
- if (existsSync(postDeployPath)) {
340
- console.log(chalk.blue(`Executing post deploy script at ${postDeployPath}`));
341
- await forge(
342
- [
343
- "script",
344
- postDeployScript,
345
- "--sig",
346
- "run(address)",
347
- await contractPromises.World,
348
- "--broadcast",
349
- "--rpc-url",
350
- rpc,
351
- "-vvv",
352
- ],
353
- {
354
- profile,
355
- }
356
- );
357
- } else {
358
- console.log(`No script at ${postDeployPath}, skipping post deploy hook`);
359
- }
360
-
361
- console.log(chalk.green("Deployment completed in", (Date.now() - startTime) / 1000, "seconds"));
362
-
363
- return { worldAddress: await contractPromises.World, blockNumber };
364
-
365
- // ------------------- INTERNAL FUNCTIONS -------------------
366
- // (Inlined to avoid having to pass around nonce, signer and forgeOutDir)
367
-
368
- /**
369
- * Deploy a contract and return the address
370
- * @param contractName Name of the contract to deploy (must exist in the file system)
371
- * @param disableTxWait Disable waiting for contract deployment
372
- * @returns Address of the deployed contract
373
- */
374
- async function deployContractByName(contractName: string, disableTxWait: boolean): Promise<string> {
375
- console.log(chalk.blue("Deploying", contractName));
376
-
377
- const { abi, bytecode } = await getContractData(contractName);
378
- return deployContract(abi, bytecode, disableTxWait, contractName);
379
- }
380
-
381
- /**
382
- * Deploy a contract and return the address
383
- * @param abi The contract interface
384
- * @param bytecode The contract bytecode
385
- * @param disableTxWait Disable waiting for contract deployment
386
- * @param contractName The contract name (optional, used for logs)
387
- * @param retryCount
388
- * @returns Address of the deployed contract
389
- */
390
- async function deployContract(
391
- abi: ContractInterface,
392
- bytecode: string | { object: string },
393
- disableTxWait: boolean,
394
- contractName?: string,
395
- retryCount = 0
396
- ): Promise<string> {
397
- try {
398
- const factory = new ethers.ContractFactory(abi, bytecode, signer);
399
- console.log(chalk.gray(`executing deployment of ${contractName} with nonce ${nonce}`));
400
- const deployPromise = factory
401
- .deploy({
402
- nonce: nonce++,
403
- maxPriorityFeePerGas,
404
- maxFeePerGas,
405
- })
406
- .then((c) => (disableTxWait ? c : c.deployed()));
407
-
408
- promises.push(deployPromise);
409
- const { address } = await deployPromise;
410
-
411
- console.log(chalk.green("Deployed", contractName, "to", address));
412
- return address;
413
- } catch (error: any) {
414
- if (debug) console.error(error);
415
- if (retryCount === 0 && error?.message.includes("transaction already imported")) {
416
- // If the deployment failed because the transaction was already imported,
417
- // retry with a higher priority fee
418
- setInternalFeePerGas(priorityFeeMultiplier * 1.1);
419
- return deployContract(abi, bytecode, disableTxWait, contractName, retryCount++);
420
- } else if (error?.message.includes("invalid bytecode")) {
421
- throw new MUDError(
422
- `Error deploying ${contractName}: invalid bytecode. Note that linking of public libraries is not supported yet, make sure none of your libraries use "external" functions.`
423
- );
424
- } else if (error?.message.includes("CreateContractLimit")) {
425
- throw new MUDError(`Error deploying ${contractName}: CreateContractLimit exceeded.`);
426
- } else throw error;
427
- }
428
- }
429
-
430
- /**
431
- * Deploy a contract and return the address
432
- * @param contractName Name of the contract to deploy (must exist in the file system)
433
- * @returns Address of the deployed contract
434
- *
435
- * NOTE: Forge deploy seems to be slightly slower than ethers
436
- * (probably due to the extra overhead spawning a child process to run forge),
437
- * so we mostly use ethersDeployContract here.
438
- * However, for contracts not in the user directory (eg. the vanilla World contract),
439
- * using forge is more convenient because it automatically finds the contract in the @latticexyz/world package.
440
- */
441
- // async function forgeDeployContract(contractName: string): Promise<string> {
442
- // console.log(chalk.blue("Deploying", contractName));
443
-
444
- // const { deployedTo } = JSON.parse(
445
- // await forge(
446
- // ["create", contractName, "--rpc-url", rpc, "--private-key", privateKey, "--json", "--nonce", String(nonce++)],
447
- // { profile, silent: true }
448
- // )
449
- // );
450
- // return deployedTo;
451
- // }
452
-
453
- async function loadFunctionSignatures(contractName: string): Promise<FunctionSignature[]> {
454
- const { abi } = await getContractData(contractName);
120
+ // Deploy the System and Module contracts
121
+ const deployedContracts = contracts.reduce<Record<string, Promise<string>>>((acc, contract) => {
122
+ acc[contract.name] = deployContract({
123
+ ...txConfig,
124
+ nonce: nonce++,
125
+ contract,
126
+ });
127
+ return acc;
128
+ }, {});
455
129
 
456
- return abi
457
- .filter((item) => ["fallback", "function"].includes(item.type))
458
- .map((item) => {
459
- if (item.type === "fallback") return { functionName: "", functionArgs: "" };
130
+ // Wait for world to be deployed
131
+ const deployedWorldAddress = await worldPromise;
132
+ const worldContract = new ethers.Contract(deployedWorldAddress, IBaseWorldAbi);
460
133
 
461
- return {
462
- functionName: item.name,
463
- functionArgs: parseComponents(item.inputs),
464
- };
465
- });
466
- }
467
-
468
- /**
469
- * Recursively turn (nested) structs in signatures into tuples
470
- */
471
- function parseComponents(params: ParamType[]): string {
472
- const components = params.map((param) => {
473
- const tupleMatch = param.type.match(/tuple(.*)/);
474
- if (tupleMatch) {
475
- // there can be arrays of tuples,
476
- // `tupleMatch[1]` preserves the array brackets (or is empty string for non-arrays)
477
- return parseComponents(param.components) + tupleMatch[1];
478
- } else {
479
- return param.type;
480
- }
134
+ // If an existing World is passed assume its coreModule is already installed - blocking to install if not
135
+ if (!worldAddress) {
136
+ console.log(chalk.blue("Installing CoreModule"));
137
+ await fastTxExecute({
138
+ ...txConfig,
139
+ nonce: nonce++,
140
+ contract: worldContract,
141
+ func: "initialize",
142
+ args: [await deployedContracts["CoreModule"]],
481
143
  });
482
- return `(${components})`;
144
+ console.log(chalk.green("Installed CoreModule"));
483
145
  }
484
146
 
485
- /**
486
- * Only await gas estimation (for speed), only execute if gas estimation succeeds (for safety)
487
- */
488
- async function fastTxExecute<C extends { estimateGas: any; [key: string]: any }, F extends keyof C>(
489
- contract: C,
490
- func: F,
491
- args: Parameters<C[F]>,
492
- confirmations = 1,
493
- retryCount = 0
494
- ): Promise<Awaited<ReturnType<Awaited<ReturnType<C[F]>>["wait"]>>> {
495
- const functionName = `${func as string}(${args.map((arg) => `'${arg}'`).join(",")})`;
496
- try {
497
- const gasLimit = await contract.estimateGas[func].apply(null, args);
498
- console.log(chalk.gray(`executing transaction: ${functionName} with nonce ${nonce}`));
499
- const txPromise = contract[func]
500
- .apply(null, [...args, { gasLimit, nonce: nonce++, maxPriorityFeePerGas, maxFeePerGas }])
501
- .then((tx: any) => (confirmations === 0 ? tx : tx.wait(confirmations)));
502
- promises.push(txPromise);
503
- return txPromise;
504
- } catch (error: any) {
505
- if (debug) console.error(error);
506
- if (retryCount === 0 && error?.message.includes("transaction already imported")) {
507
- // If the deployment failed because the transaction was already imported,
508
- // retry with a higher priority fee
509
- setInternalFeePerGas(priorityFeeMultiplier * 1.1);
510
- return fastTxExecute(contract, func, args, confirmations, retryCount++);
511
- } else throw new MUDError(`Gas estimation error for ${functionName}: ${error?.reason}`);
512
- }
147
+ if (mudConfig.namespace) {
148
+ console.log(chalk.blue("Registering Namespace"));
149
+ await fastTxExecute({
150
+ ...txConfig,
151
+ nonce: nonce++,
152
+ contract: worldContract,
153
+ func: "registerNamespace",
154
+ args: [resourceIdToHex({ type: "namespace", namespace: mudConfig.namespace, name: "" })],
155
+ });
156
+ console.log(chalk.green("Namespace registered"));
513
157
  }
514
158
 
515
- /**
516
- * Load the contract's abi and bytecode from the file system
517
- * @param contractName: Name of the contract to load
518
- */
519
- async function getContractData(contractName: string): Promise<{ bytecode: string; abi: Fragment[] }> {
520
- let data: any;
521
- const contractDataPath = path.join(forgeOutDirectory, contractName + ".sol", contractName + ".json");
522
- try {
523
- data = JSON.parse(readFileSync(contractDataPath, "utf8"));
524
- } catch (error: any) {
525
- throw new MUDError(`Error reading file at ${contractDataPath}`);
526
- }
527
-
528
- const bytecode = data?.bytecode?.object;
529
- if (!bytecode) throw new MUDError(`No bytecode found in ${contractDataPath}`);
530
-
531
- const abi = data?.abi;
532
- if (!abi) throw new MUDError(`No ABI found in ${contractDataPath}`);
159
+ const tableIds = getTableIds(mudConfig);
533
160
 
534
- return { abi, bytecode };
535
- }
536
-
537
- /**
538
- * Set the maxFeePerGas and maxPriorityFeePerGas based on the current base fee and the given multiplier.
539
- * The multiplier is used to allow replacing pending transactions.
540
- * @param multiplier Multiplier to apply to the base fee
541
- */
542
- async function setInternalFeePerGas(multiplier: number) {
543
- // Compute maxFeePerGas and maxPriorityFeePerGas like ethers, but allow for a multiplier to allow replacing pending transactions
544
- const feeData = await provider.getFeeData();
545
- if (!feeData.lastBaseFeePerGas) throw new MUDError("Can not fetch lastBaseFeePerGas from RPC");
546
- if (!feeData.lastBaseFeePerGas.eq(0) && (await signer.getBalance()).eq(0)) {
547
- throw new MUDError(`Attempting to deploy to a chain with non-zero base fee with an account that has no balance.
548
- If you're deploying to the Lattice testnet, you can fund your account by running 'pnpm mud faucet --address ${await signer.getAddress()}'`);
549
- }
161
+ const registerTableCalls = Object.values(mudConfig.tables).map((table) =>
162
+ getRegisterTableCallData(table, mudConfig, outputBaseDirectory, remappings)
163
+ );
550
164
 
551
- // Set the priority fee to 0 for development chains with no base fee, to allow transactions from unfunded wallets
552
- maxPriorityFeePerGas = feeData.lastBaseFeePerGas.eq(0) ? 0 : Math.floor(1_500_000_000 * multiplier);
553
- maxFeePerGas = feeData.lastBaseFeePerGas.mul(2).add(maxPriorityFeePerGas);
554
- }
555
- }
165
+ console.log(chalk.blue("Registering tables"));
166
+ await Promise.all(
167
+ registerTableCalls.map((call) =>
168
+ fastTxExecute({
169
+ ...txConfig,
170
+ nonce: nonce++,
171
+ contract: worldContract,
172
+ ...call,
173
+ })
174
+ )
175
+ );
176
+ console.log(chalk.green(`Tables registered`));
177
+
178
+ console.log(chalk.blue("Registering Systems and Functions"));
179
+ const systemCalls = await Promise.all(
180
+ Object.entries(resolvedConfig.systems).map(([systemKey, system]) =>
181
+ getRegisterSystemCallData({
182
+ systemContracts: deployedContracts,
183
+ systemKey,
184
+ system,
185
+ namespace: mudConfig.namespace,
186
+ })
187
+ )
188
+ );
189
+ const functionCalls = Object.entries(resolvedConfig.systems).flatMap(([systemKey, system]) =>
190
+ getRegisterFunctionSelectorsCallData({
191
+ systemContractName: systemKey,
192
+ system,
193
+ namespace: mudConfig.namespace,
194
+ forgeOutDirectory,
195
+ })
196
+ );
197
+ await Promise.all(
198
+ [...systemCalls, ...functionCalls].map((call) =>
199
+ fastTxExecute({
200
+ ...txConfig,
201
+ nonce: nonce++,
202
+ contract: worldContract,
203
+ ...call,
204
+ })
205
+ )
206
+ );
207
+ console.log(chalk.green(`Systems and Functions registered`));
208
+
209
+ // Wait for System access to be granted before installing modules
210
+ const grantCalls = await getGrantAccessCallData({
211
+ systems: Object.values(resolvedConfig.systems),
212
+ systemContracts: deployedContracts,
213
+ namespace: mudConfig.namespace,
214
+ });
215
+
216
+ console.log(chalk.blue("Granting Access"));
217
+ await Promise.all(
218
+ grantCalls.map((call) =>
219
+ fastTxExecute({
220
+ ...txConfig,
221
+ nonce: nonce++,
222
+ contract: worldContract,
223
+ ...call,
224
+ })
225
+ )
226
+ );
227
+ console.log(chalk.green(`Access granted`));
556
228
 
557
- // TODO: use stringToBytes16 from utils as soon as utils are usable inside cli
558
- // (see https://github.com/latticexyz/mud/issues/499)
559
- function toBytes16(input: string) {
560
- if (input.length > 16) throw new Error("String does not fit into 16 bytes");
229
+ const moduleCalls = await Promise.all(
230
+ mudConfig.modules.map((m) => getInstallModuleCallData(deployedContracts, m, tableIds))
231
+ );
561
232
 
562
- const result = new Uint8Array(16);
563
- // Set ascii bytes
564
- for (let i = 0; i < input.length; i++) {
565
- result[i] = input.charCodeAt(i);
566
- }
567
- // Set the remaining bytes to 0
568
- for (let i = input.length; i < 16; i++) {
569
- result[i] = 0;
570
- }
571
- return result;
572
- }
233
+ console.log(chalk.blue("Installing User Modules"));
234
+ await Promise.all(
235
+ moduleCalls.map((call) =>
236
+ fastTxExecute({
237
+ ...txConfig,
238
+ nonce: nonce++,
239
+ contract: worldContract,
240
+ ...call,
241
+ })
242
+ )
243
+ );
244
+ console.log(chalk.green(`User Modules Installed`));
573
245
 
574
- // TODO: use TableId from utils as soon as utils are usable inside cli
575
- // (see https://github.com/latticexyz/mud/issues/499)
576
- function toResourceSelector(namespace: string, file: string): Uint8Array {
577
- const namespaceBytes = toBytes16(namespace);
578
- const fileBytes = toBytes16(file);
579
- const result = new Uint8Array(32);
580
- result.set(namespaceBytes);
581
- result.set(fileBytes, 16);
582
- return result;
583
- }
246
+ // Double check that all transactions have been included by confirming the current nonce is the expected nonce
247
+ await confirmNonce(signer, nonce, pollInterval);
584
248
 
585
- interface FunctionSignature {
586
- functionName: string;
587
- functionArgs: string;
588
- }
249
+ await postDeploy(mudConfig.postDeployScript, deployedWorldAddress, rpc, profile);
589
250
 
590
- // TODO: move this to utils as soon as utils are usable inside cli
591
- // (see https://github.com/latticexyz/mud/issues/499)
592
- function toFunctionSelector({ functionName, functionArgs }: FunctionSignature): string {
593
- const functionSignature = functionName + functionArgs;
594
- if (functionSignature === "") return "0x";
595
- return sigHash(functionSignature);
596
- }
251
+ console.log(chalk.green("Deployment completed in", (Date.now() - startTime) / 1000, "seconds"));
597
252
 
598
- // TODO: move this to utils as soon as utils are usable inside cli
599
- // (see https://github.com/latticexyz/mud/issues/499)
600
- function sigHash(signature: string) {
601
- return ethers.utils.hexDataSlice(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(signature)), 0, 4);
253
+ return { worldAddress: deployedWorldAddress, blockNumber };
602
254
  }