From 1836024420dbeb547d888f28740e267bcee32fd9 Mon Sep 17 00:00:00 2001 From: Ori Pomerantz Date: Tue, 14 May 2024 11:48:54 -0500 Subject: [PATCH] feat(create-mud): use app namespace (#2823) Co-authored-by: Kevin Ingersoll --- .changeset/fuzzy-tools-explain.md | 5 +++++ .../phaser/packages/client/src/mud/createSystemCalls.ts | 2 +- templates/phaser/packages/contracts/mud.config.ts | 1 + .../phaser/packages/contracts/script/PostDeploy.s.sol | 2 +- .../packages/contracts/src/codegen/tables/Counter.sol | 4 ++-- .../contracts/src/codegen/world/IIncrementSystem.sol | 2 +- .../phaser/packages/contracts/test/CounterTest.t.sol | 2 +- .../packages/client/src/mud/createSystemCalls.ts | 2 +- templates/react-ecs/packages/contracts/mud.config.ts | 1 + .../react-ecs/packages/contracts/script/PostDeploy.s.sol | 2 +- .../packages/contracts/src/codegen/tables/Counter.sol | 4 ++-- .../contracts/src/codegen/world/IIncrementSystem.sol | 2 +- .../react-ecs/packages/contracts/test/CounterTest.t.sol | 2 +- .../react/packages/client/src/mud/createSystemCalls.ts | 8 +++++--- templates/react/packages/contracts/mud.config.ts | 1 + .../react/packages/contracts/script/PostDeploy.s.sol | 6 +++--- .../react/packages/contracts/src/codegen/tables/Tasks.sol | 4 ++-- .../packages/contracts/src/codegen/world/ITasksSystem.sol | 8 ++++---- templates/react/packages/contracts/test/TasksTest.t.sol | 2 +- .../threejs/packages/client/src/mud/createSystemCalls.ts | 2 +- templates/threejs/packages/contracts/mud.config.ts | 1 + .../packages/contracts/src/codegen/tables/Position.sol | 4 ++-- .../packages/contracts/src/codegen/world/IMoveSystem.sol | 2 +- .../vanilla/packages/client/src/mud/createSystemCalls.ts | 2 +- templates/vanilla/packages/contracts/mud.config.ts | 1 + .../vanilla/packages/contracts/script/PostDeploy.s.sol | 2 +- .../packages/contracts/src/codegen/tables/Counter.sol | 4 ++-- .../contracts/src/codegen/world/IIncrementSystem.sol | 2 +- .../vanilla/packages/contracts/test/CounterTest.t.sol | 2 +- 29 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 .changeset/fuzzy-tools-explain.md diff --git a/.changeset/fuzzy-tools-explain.md b/.changeset/fuzzy-tools-explain.md new file mode 100644 index 0000000000..23fafdbd5b --- /dev/null +++ b/.changeset/fuzzy-tools-explain.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/create-mud": patch +--- + +Templates now use an `app` namespace by default, instead of the root namespace. This helps keep the root namespace clear for intentionally root-level things and avoids pitfalls with root systems calling other root systems. diff --git a/templates/phaser/packages/client/src/mud/createSystemCalls.ts b/templates/phaser/packages/client/src/mud/createSystemCalls.ts index fef3e78936..4781c33635 100644 --- a/templates/phaser/packages/client/src/mud/createSystemCalls.ts +++ b/templates/phaser/packages/client/src/mud/createSystemCalls.ts @@ -46,7 +46,7 @@ export function createSystemCalls( * is in the root namespace, `.increment` can be called directly * on the World contract. */ - const tx = await worldContract.write.increment(); + const tx = await worldContract.write.app__increment(); await waitForTransaction(tx); return getComponentValue(Counter, singletonEntity); }; diff --git a/templates/phaser/packages/contracts/mud.config.ts b/templates/phaser/packages/contracts/mud.config.ts index 4a495d0c4e..7bad3572a1 100644 --- a/templates/phaser/packages/contracts/mud.config.ts +++ b/templates/phaser/packages/contracts/mud.config.ts @@ -1,6 +1,7 @@ import { defineWorld } from "@latticexyz/world"; export default defineWorld({ + namespace: "app", tables: { Counter: { schema: { diff --git a/templates/phaser/packages/contracts/script/PostDeploy.s.sol b/templates/phaser/packages/contracts/script/PostDeploy.s.sol index 115adfec53..1d04b11b1e 100644 --- a/templates/phaser/packages/contracts/script/PostDeploy.s.sol +++ b/templates/phaser/packages/contracts/script/PostDeploy.s.sol @@ -21,7 +21,7 @@ contract PostDeploy is Script { // ------------------ EXAMPLES ------------------ // Call increment on the world via the registered function selector - uint32 newValue = IWorld(worldAddress).increment(); + uint32 newValue = IWorld(worldAddress).app__increment(); console.log("Increment via IWorld:", newValue); vm.stopBroadcast(); diff --git a/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol b/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol index 45558b3f11..53378b9d24 100644 --- a/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/phaser/packages/contracts/src/codegen/tables/Counter.sol @@ -17,8 +17,8 @@ import { EncodedLengths, EncodedLengthsLib } from "@latticexyz/store/src/Encoded import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; library Counter { - // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "", name: "Counter", typeId: RESOURCE_TABLE });` - ResourceId constant _tableId = ResourceId.wrap(0x74620000000000000000000000000000436f756e746572000000000000000000); + // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "app", name: "Counter", typeId: RESOURCE_TABLE });` + ResourceId constant _tableId = ResourceId.wrap(0x74626170700000000000000000000000436f756e746572000000000000000000); FieldLayout constant _fieldLayout = FieldLayout.wrap(0x0004010004000000000000000000000000000000000000000000000000000000); diff --git a/templates/phaser/packages/contracts/src/codegen/world/IIncrementSystem.sol b/templates/phaser/packages/contracts/src/codegen/world/IIncrementSystem.sol index 6a1a4db250..39c7df755b 100644 --- a/templates/phaser/packages/contracts/src/codegen/world/IIncrementSystem.sol +++ b/templates/phaser/packages/contracts/src/codegen/world/IIncrementSystem.sol @@ -9,5 +9,5 @@ pragma solidity >=0.8.24; * @dev This interface is automatically generated from the corresponding system contract. Do not edit manually. */ interface IIncrementSystem { - function increment() external returns (uint32); + function app__increment() external returns (uint32); } diff --git a/templates/phaser/packages/contracts/test/CounterTest.t.sol b/templates/phaser/packages/contracts/test/CounterTest.t.sol index 4946ef57ff..56e6f3d5da 100644 --- a/templates/phaser/packages/contracts/test/CounterTest.t.sol +++ b/templates/phaser/packages/contracts/test/CounterTest.t.sol @@ -24,7 +24,7 @@ contract CounterTest is MudTest { assertEq(counter, 1); // Expect the counter to be 2 after calling increment. - IWorld(worldAddress).increment(); + IWorld(worldAddress).app__increment(); counter = Counter.get(); assertEq(counter, 2); } diff --git a/templates/react-ecs/packages/client/src/mud/createSystemCalls.ts b/templates/react-ecs/packages/client/src/mud/createSystemCalls.ts index 30b4f7f6b7..81c5979d8a 100644 --- a/templates/react-ecs/packages/client/src/mud/createSystemCalls.ts +++ b/templates/react-ecs/packages/client/src/mud/createSystemCalls.ts @@ -40,7 +40,7 @@ export function createSystemCalls( * is in the root namespace, `.increment` can be called directly * on the World contract. */ - const tx = await worldContract.write.increment(); + const tx = await worldContract.write.app__increment(); await waitForTransaction(tx); return getComponentValue(Counter, singletonEntity); }; diff --git a/templates/react-ecs/packages/contracts/mud.config.ts b/templates/react-ecs/packages/contracts/mud.config.ts index 4a495d0c4e..7bad3572a1 100644 --- a/templates/react-ecs/packages/contracts/mud.config.ts +++ b/templates/react-ecs/packages/contracts/mud.config.ts @@ -1,6 +1,7 @@ import { defineWorld } from "@latticexyz/world"; export default defineWorld({ + namespace: "app", tables: { Counter: { schema: { diff --git a/templates/react-ecs/packages/contracts/script/PostDeploy.s.sol b/templates/react-ecs/packages/contracts/script/PostDeploy.s.sol index 115adfec53..1d04b11b1e 100644 --- a/templates/react-ecs/packages/contracts/script/PostDeploy.s.sol +++ b/templates/react-ecs/packages/contracts/script/PostDeploy.s.sol @@ -21,7 +21,7 @@ contract PostDeploy is Script { // ------------------ EXAMPLES ------------------ // Call increment on the world via the registered function selector - uint32 newValue = IWorld(worldAddress).increment(); + uint32 newValue = IWorld(worldAddress).app__increment(); console.log("Increment via IWorld:", newValue); vm.stopBroadcast(); diff --git a/templates/react-ecs/packages/contracts/src/codegen/tables/Counter.sol b/templates/react-ecs/packages/contracts/src/codegen/tables/Counter.sol index 45558b3f11..53378b9d24 100644 --- a/templates/react-ecs/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/react-ecs/packages/contracts/src/codegen/tables/Counter.sol @@ -17,8 +17,8 @@ import { EncodedLengths, EncodedLengthsLib } from "@latticexyz/store/src/Encoded import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; library Counter { - // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "", name: "Counter", typeId: RESOURCE_TABLE });` - ResourceId constant _tableId = ResourceId.wrap(0x74620000000000000000000000000000436f756e746572000000000000000000); + // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "app", name: "Counter", typeId: RESOURCE_TABLE });` + ResourceId constant _tableId = ResourceId.wrap(0x74626170700000000000000000000000436f756e746572000000000000000000); FieldLayout constant _fieldLayout = FieldLayout.wrap(0x0004010004000000000000000000000000000000000000000000000000000000); diff --git a/templates/react-ecs/packages/contracts/src/codegen/world/IIncrementSystem.sol b/templates/react-ecs/packages/contracts/src/codegen/world/IIncrementSystem.sol index 6a1a4db250..39c7df755b 100644 --- a/templates/react-ecs/packages/contracts/src/codegen/world/IIncrementSystem.sol +++ b/templates/react-ecs/packages/contracts/src/codegen/world/IIncrementSystem.sol @@ -9,5 +9,5 @@ pragma solidity >=0.8.24; * @dev This interface is automatically generated from the corresponding system contract. Do not edit manually. */ interface IIncrementSystem { - function increment() external returns (uint32); + function app__increment() external returns (uint32); } diff --git a/templates/react-ecs/packages/contracts/test/CounterTest.t.sol b/templates/react-ecs/packages/contracts/test/CounterTest.t.sol index 4946ef57ff..56e6f3d5da 100644 --- a/templates/react-ecs/packages/contracts/test/CounterTest.t.sol +++ b/templates/react-ecs/packages/contracts/test/CounterTest.t.sol @@ -24,7 +24,7 @@ contract CounterTest is MudTest { assertEq(counter, 1); // Expect the counter to be 2 after calling increment. - IWorld(worldAddress).increment(); + IWorld(worldAddress).app__increment(); counter = Counter.get(); assertEq(counter, 2); } diff --git a/templates/react/packages/client/src/mud/createSystemCalls.ts b/templates/react/packages/client/src/mud/createSystemCalls.ts index a7447d143c..d4fb0c8d50 100644 --- a/templates/react/packages/client/src/mud/createSystemCalls.ts +++ b/templates/react/packages/client/src/mud/createSystemCalls.ts @@ -31,18 +31,20 @@ export function createSystemCalls( { tables, useStore, worldContract, waitForTransaction }: SetupNetworkResult, ) { const addTask = async (label: string) => { - const tx = await worldContract.write.addTask([label]); + const tx = await worldContract.write.app__addTask([label]); await waitForTransaction(tx); }; const toggleTask = async (id: Hex) => { const isComplete = (useStore.getState().getValue(tables.Tasks, { id })?.completedAt ?? 0n) > 0n; - const tx = isComplete ? await worldContract.write.resetTask([id]) : await worldContract.write.completeTask([id]); + const tx = isComplete + ? await worldContract.write.app__resetTask([id]) + : await worldContract.write.app__completeTask([id]); await waitForTransaction(tx); }; const deleteTask = async (id: Hex) => { - const tx = await worldContract.write.deleteTask([id]); + const tx = await worldContract.write.app__deleteTask([id]); await waitForTransaction(tx); }; diff --git a/templates/react/packages/contracts/mud.config.ts b/templates/react/packages/contracts/mud.config.ts index 2f11567141..a0828bf515 100644 --- a/templates/react/packages/contracts/mud.config.ts +++ b/templates/react/packages/contracts/mud.config.ts @@ -1,6 +1,7 @@ import { defineWorld } from "@latticexyz/world"; export default defineWorld({ + namespace: "app", tables: { Tasks: { schema: { diff --git a/templates/react/packages/contracts/script/PostDeploy.s.sol b/templates/react/packages/contracts/script/PostDeploy.s.sol index 48b7a0cd40..93da4ddd8c 100644 --- a/templates/react/packages/contracts/script/PostDeploy.s.sol +++ b/templates/react/packages/contracts/script/PostDeploy.s.sol @@ -23,10 +23,10 @@ contract PostDeploy is Script { Tasks.set("1", TasksData({ description: "Walk the dog", createdAt: block.timestamp, completedAt: 0 })); // Or we can call our own systems - IWorld(worldAddress).addTask("Take out the trash"); + IWorld(worldAddress).app__addTask("Take out the trash"); - bytes32 key = IWorld(worldAddress).addTask("Do the dishes"); - IWorld(worldAddress).completeTask(key); + bytes32 key = IWorld(worldAddress).app__addTask("Do the dishes"); + IWorld(worldAddress).app__completeTask(key); vm.stopBroadcast(); } diff --git a/templates/react/packages/contracts/src/codegen/tables/Tasks.sol b/templates/react/packages/contracts/src/codegen/tables/Tasks.sol index 6f26bc3cab..48535061b8 100644 --- a/templates/react/packages/contracts/src/codegen/tables/Tasks.sol +++ b/templates/react/packages/contracts/src/codegen/tables/Tasks.sol @@ -23,8 +23,8 @@ struct TasksData { } library Tasks { - // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "", name: "Tasks", typeId: RESOURCE_TABLE });` - ResourceId constant _tableId = ResourceId.wrap(0x746200000000000000000000000000005461736b730000000000000000000000); + // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "app", name: "Tasks", typeId: RESOURCE_TABLE });` + ResourceId constant _tableId = ResourceId.wrap(0x746261707000000000000000000000005461736b730000000000000000000000); FieldLayout constant _fieldLayout = FieldLayout.wrap(0x0040020120200000000000000000000000000000000000000000000000000000); diff --git a/templates/react/packages/contracts/src/codegen/world/ITasksSystem.sol b/templates/react/packages/contracts/src/codegen/world/ITasksSystem.sol index bfddd5c830..7ac8215848 100644 --- a/templates/react/packages/contracts/src/codegen/world/ITasksSystem.sol +++ b/templates/react/packages/contracts/src/codegen/world/ITasksSystem.sol @@ -9,11 +9,11 @@ pragma solidity >=0.8.24; * @dev This interface is automatically generated from the corresponding system contract. Do not edit manually. */ interface ITasksSystem { - function addTask(string memory description) external returns (bytes32 id); + function app__addTask(string memory description) external returns (bytes32 id); - function completeTask(bytes32 id) external; + function app__completeTask(bytes32 id) external; - function resetTask(bytes32 id) external; + function app__resetTask(bytes32 id) external; - function deleteTask(bytes32 id) external; + function app__deleteTask(bytes32 id) external; } diff --git a/templates/react/packages/contracts/test/TasksTest.t.sol b/templates/react/packages/contracts/test/TasksTest.t.sol index fb37696955..69afe731f2 100644 --- a/templates/react/packages/contracts/test/TasksTest.t.sol +++ b/templates/react/packages/contracts/test/TasksTest.t.sol @@ -24,7 +24,7 @@ contract TasksTest is MudTest { assertEq(task.completedAt, 0); // Expect the task to be completed after calling completeTask from our TasksSystem - IWorld(worldAddress).completeTask("1"); + IWorld(worldAddress).app__completeTask("1"); assertEq(Tasks.getCompletedAt("1"), block.timestamp); } } diff --git a/templates/threejs/packages/client/src/mud/createSystemCalls.ts b/templates/threejs/packages/client/src/mud/createSystemCalls.ts index d537c578fb..fd458e1880 100644 --- a/templates/threejs/packages/client/src/mud/createSystemCalls.ts +++ b/templates/threejs/packages/client/src/mud/createSystemCalls.ts @@ -37,7 +37,7 @@ export function createSystemCalls( * Because MoveSystem is in the root namespace, .move can be called directly * on the World contract. */ - const tx = await worldContract.write.move([x, y, z]); + const tx = await worldContract.write.app__move([x, y, z]); await waitForTransaction(tx); }; diff --git a/templates/threejs/packages/contracts/mud.config.ts b/templates/threejs/packages/contracts/mud.config.ts index ad59df29ee..5c009a8d2c 100644 --- a/templates/threejs/packages/contracts/mud.config.ts +++ b/templates/threejs/packages/contracts/mud.config.ts @@ -1,6 +1,7 @@ import { defineWorld } from "@latticexyz/world"; export default defineWorld({ + namespace: "app", tables: { Position: { schema: { diff --git a/templates/threejs/packages/contracts/src/codegen/tables/Position.sol b/templates/threejs/packages/contracts/src/codegen/tables/Position.sol index 00091e91bc..23d18273a1 100644 --- a/templates/threejs/packages/contracts/src/codegen/tables/Position.sol +++ b/templates/threejs/packages/contracts/src/codegen/tables/Position.sol @@ -23,8 +23,8 @@ struct PositionData { } library Position { - // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "", name: "Position", typeId: RESOURCE_TABLE });` - ResourceId constant _tableId = ResourceId.wrap(0x74620000000000000000000000000000506f736974696f6e0000000000000000); + // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "app", name: "Position", typeId: RESOURCE_TABLE });` + ResourceId constant _tableId = ResourceId.wrap(0x74626170700000000000000000000000506f736974696f6e0000000000000000); FieldLayout constant _fieldLayout = FieldLayout.wrap(0x000c030004040400000000000000000000000000000000000000000000000000); diff --git a/templates/threejs/packages/contracts/src/codegen/world/IMoveSystem.sol b/templates/threejs/packages/contracts/src/codegen/world/IMoveSystem.sol index 4311d0cb72..33a83db16c 100644 --- a/templates/threejs/packages/contracts/src/codegen/world/IMoveSystem.sol +++ b/templates/threejs/packages/contracts/src/codegen/world/IMoveSystem.sol @@ -9,5 +9,5 @@ pragma solidity >=0.8.24; * @dev This interface is automatically generated from the corresponding system contract. Do not edit manually. */ interface IMoveSystem { - function move(int32 x, int32 y, int32 z) external; + function app__move(int32 x, int32 y, int32 z) external; } diff --git a/templates/vanilla/packages/client/src/mud/createSystemCalls.ts b/templates/vanilla/packages/client/src/mud/createSystemCalls.ts index f153b51e86..cbe1a4821d 100644 --- a/templates/vanilla/packages/client/src/mud/createSystemCalls.ts +++ b/templates/vanilla/packages/client/src/mud/createSystemCalls.ts @@ -40,7 +40,7 @@ export function createSystemCalls( * is in the root namespace, `.increment` can be called directly * on the World contract. */ - const tx = await worldContract.write.increment(); + const tx = await worldContract.write.app__increment(); await waitForTransaction(tx); return getComponentValue(Counter, singletonEntity); }; diff --git a/templates/vanilla/packages/contracts/mud.config.ts b/templates/vanilla/packages/contracts/mud.config.ts index 4a495d0c4e..7bad3572a1 100644 --- a/templates/vanilla/packages/contracts/mud.config.ts +++ b/templates/vanilla/packages/contracts/mud.config.ts @@ -1,6 +1,7 @@ import { defineWorld } from "@latticexyz/world"; export default defineWorld({ + namespace: "app", tables: { Counter: { schema: { diff --git a/templates/vanilla/packages/contracts/script/PostDeploy.s.sol b/templates/vanilla/packages/contracts/script/PostDeploy.s.sol index 115adfec53..1d04b11b1e 100644 --- a/templates/vanilla/packages/contracts/script/PostDeploy.s.sol +++ b/templates/vanilla/packages/contracts/script/PostDeploy.s.sol @@ -21,7 +21,7 @@ contract PostDeploy is Script { // ------------------ EXAMPLES ------------------ // Call increment on the world via the registered function selector - uint32 newValue = IWorld(worldAddress).increment(); + uint32 newValue = IWorld(worldAddress).app__increment(); console.log("Increment via IWorld:", newValue); vm.stopBroadcast(); diff --git a/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol b/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol index 45558b3f11..53378b9d24 100644 --- a/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol +++ b/templates/vanilla/packages/contracts/src/codegen/tables/Counter.sol @@ -17,8 +17,8 @@ import { EncodedLengths, EncodedLengthsLib } from "@latticexyz/store/src/Encoded import { ResourceId } from "@latticexyz/store/src/ResourceId.sol"; library Counter { - // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "", name: "Counter", typeId: RESOURCE_TABLE });` - ResourceId constant _tableId = ResourceId.wrap(0x74620000000000000000000000000000436f756e746572000000000000000000); + // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "app", name: "Counter", typeId: RESOURCE_TABLE });` + ResourceId constant _tableId = ResourceId.wrap(0x74626170700000000000000000000000436f756e746572000000000000000000); FieldLayout constant _fieldLayout = FieldLayout.wrap(0x0004010004000000000000000000000000000000000000000000000000000000); diff --git a/templates/vanilla/packages/contracts/src/codegen/world/IIncrementSystem.sol b/templates/vanilla/packages/contracts/src/codegen/world/IIncrementSystem.sol index 6a1a4db250..39c7df755b 100644 --- a/templates/vanilla/packages/contracts/src/codegen/world/IIncrementSystem.sol +++ b/templates/vanilla/packages/contracts/src/codegen/world/IIncrementSystem.sol @@ -9,5 +9,5 @@ pragma solidity >=0.8.24; * @dev This interface is automatically generated from the corresponding system contract. Do not edit manually. */ interface IIncrementSystem { - function increment() external returns (uint32); + function app__increment() external returns (uint32); } diff --git a/templates/vanilla/packages/contracts/test/CounterTest.t.sol b/templates/vanilla/packages/contracts/test/CounterTest.t.sol index 4946ef57ff..56e6f3d5da 100644 --- a/templates/vanilla/packages/contracts/test/CounterTest.t.sol +++ b/templates/vanilla/packages/contracts/test/CounterTest.t.sol @@ -24,7 +24,7 @@ contract CounterTest is MudTest { assertEq(counter, 1); // Expect the counter to be 2 after calling increment. - IWorld(worldAddress).increment(); + IWorld(worldAddress).app__increment(); counter = Counter.get(); assertEq(counter, 2); }