@latticexyz/cli 2.0.0-skystrife-playtest-f0f9ff56 → 2.0.0-snapshot-test-32d38619

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