@latticexyz/cli 2.0.0-next.8 → 2.0.0-next.9

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