create-mud 2.2.17-78e9ed1294db2e924bd295734f3738bdd2786242 → 2.2.17-86f455c0a09d5d8af64d030669ebf30ffa71420b

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 (103) hide show
  1. package/dist/cli.js +1 -1
  2. package/dist/cli.js.map +1 -1
  3. package/package.json +1 -1
  4. package/templates/react/mprocs.yaml +9 -1
  5. package/templates/react/packages/client/index.html +2 -2
  6. package/templates/react/packages/client/package.json +17 -9
  7. package/templates/react/packages/client/postcss.config.cjs +6 -0
  8. package/templates/react/packages/client/src/App.tsx +41 -100
  9. package/templates/react/packages/client/src/Providers.tsx +35 -0
  10. package/templates/react/packages/client/src/common.ts +26 -0
  11. package/templates/react/packages/client/src/game/GameMap.tsx +102 -0
  12. package/templates/react/packages/client/src/game/useKeyboardMovement.ts +26 -0
  13. package/templates/react/packages/client/src/index.tsx +17 -32
  14. package/templates/react/packages/client/src/mud/Explorer.tsx +32 -0
  15. package/templates/react/packages/client/src/mud/Synced.tsx +14 -0
  16. package/templates/react/packages/client/src/mud/stash.ts +4 -0
  17. package/templates/react/packages/client/src/mud/useSyncStatus.ts +21 -0
  18. package/templates/react/packages/client/src/mud/useWorldContract.ts +44 -0
  19. package/templates/react/packages/client/src/ui/AsyncButton.tsx +41 -0
  20. package/templates/react/packages/client/src/ui/ErrorFallback.tsx +58 -0
  21. package/templates/react/packages/client/src/ui/icons/ArrowDownIcon.tsx +22 -0
  22. package/templates/react/packages/client/src/ui/icons/MUDIcon.tsx +25 -0
  23. package/templates/react/packages/client/src/wagmiConfig.ts +49 -0
  24. package/templates/react/packages/client/tailwind.config.ts +10 -0
  25. package/templates/react/packages/client/tsconfig.json +1 -1
  26. package/templates/react/packages/client/vite.config.ts +2 -7
  27. package/templates/react/packages/contracts/.env +1 -1
  28. package/templates/react/packages/contracts/mud.config.ts +13 -8
  29. package/templates/react/packages/contracts/out/IWorld.sol/IWorld.abi.json +2021 -0
  30. package/templates/react/packages/contracts/package.json +1 -0
  31. package/templates/react/packages/contracts/script/PostDeploy.s.sol +1 -9
  32. package/templates/react/packages/contracts/src/MoveSystem.sol +26 -0
  33. package/templates/react/packages/contracts/src/codegen/common.sol +11 -0
  34. package/templates/react/packages/contracts/src/codegen/index.sol +1 -1
  35. package/templates/react/packages/contracts/src/codegen/tables/Position.sol +318 -0
  36. package/templates/{react-ecs/packages/contracts/src/codegen/world/IIncrementSystem.sol → react/packages/contracts/src/codegen/world/IMoveSystem.sol} +5 -3
  37. package/templates/react/packages/contracts/src/codegen/world/IWorld.sol +2 -2
  38. package/templates/react/packages/contracts/test/MoveTest.t.sol +25 -0
  39. package/templates/react/packages/contracts/test/WorldTest.t.sol +16 -0
  40. package/templates/react/packages/contracts/worlds.json +1 -1
  41. package/templates/react-ecs/mprocs.yaml +9 -1
  42. package/templates/react-ecs/package.json +1 -2
  43. package/templates/react-ecs/packages/client/index.html +2 -2
  44. package/templates/react-ecs/packages/client/package.json +16 -9
  45. package/templates/react-ecs/packages/client/postcss.config.cjs +6 -0
  46. package/templates/react-ecs/packages/client/src/App.tsx +66 -21
  47. package/templates/react-ecs/packages/client/src/Providers.tsx +29 -0
  48. package/templates/react-ecs/packages/client/src/common.ts +27 -0
  49. package/templates/react-ecs/packages/client/src/game/GameMap.tsx +112 -0
  50. package/templates/react-ecs/packages/client/src/game/useKeyboardMovement.ts +26 -0
  51. package/templates/react-ecs/packages/client/src/index.tsx +17 -32
  52. package/templates/react-ecs/packages/client/src/mud/Explorer.tsx +32 -0
  53. package/templates/react-ecs/packages/client/src/mud/Synced.tsx +14 -0
  54. package/templates/react-ecs/packages/client/src/mud/recs.ts +6 -0
  55. package/templates/react-ecs/packages/client/src/mud/useSyncStatus.ts +17 -0
  56. package/templates/react-ecs/packages/client/src/mud/useWorldContract.ts +44 -0
  57. package/templates/react-ecs/packages/client/src/ui/AsyncButton.tsx +41 -0
  58. package/templates/react-ecs/packages/client/src/ui/ErrorFallback.tsx +58 -0
  59. package/templates/react-ecs/packages/client/src/ui/icons/ArrowDownIcon.tsx +22 -0
  60. package/templates/react-ecs/packages/client/src/ui/icons/MUDIcon.tsx +25 -0
  61. package/templates/react-ecs/packages/client/src/wagmiConfig.ts +49 -0
  62. package/templates/react-ecs/packages/client/tailwind.config.ts +10 -0
  63. package/templates/react-ecs/packages/client/tsconfig.json +1 -1
  64. package/templates/react-ecs/packages/client/vite.config.ts +2 -7
  65. package/templates/react-ecs/packages/contracts/.env +1 -1
  66. package/templates/react-ecs/packages/contracts/mud.config.ts +18 -6
  67. package/templates/react-ecs/packages/contracts/out/IWorld.sol/IWorld.abi.json +2039 -0
  68. package/templates/react-ecs/packages/contracts/package.json +1 -0
  69. package/templates/react-ecs/packages/contracts/script/PostDeploy.s.sol +1 -5
  70. package/templates/react-ecs/packages/contracts/src/Entity.sol +20 -0
  71. package/templates/react-ecs/packages/contracts/src/MoveSystem.sol +30 -0
  72. package/templates/react-ecs/packages/contracts/src/SpawnSystem.sol +18 -0
  73. package/templates/react-ecs/packages/contracts/src/codegen/common.sol +11 -0
  74. package/templates/react-ecs/packages/contracts/src/codegen/index.sol +3 -1
  75. package/templates/react-ecs/packages/contracts/src/codegen/tables/{Counter.sol → EntityCount.sol} +35 -35
  76. package/templates/react-ecs/packages/contracts/src/codegen/tables/Owner.sol +202 -0
  77. package/templates/react-ecs/packages/contracts/src/codegen/tables/Position.sol +321 -0
  78. package/templates/{react/packages/contracts/src/codegen/world/ITasksSystem.sol → react-ecs/packages/contracts/src/codegen/world/IMoveSystem.sol} +6 -9
  79. package/templates/react-ecs/packages/contracts/src/codegen/world/ISpawnSystem.sol +15 -0
  80. package/templates/react-ecs/packages/contracts/src/codegen/world/IWorld.sol +3 -2
  81. package/templates/react-ecs/packages/contracts/src/createEntity.sol +11 -0
  82. package/templates/react-ecs/packages/contracts/test/MoveTest.t.sol +39 -0
  83. package/templates/react-ecs/packages/contracts/test/WorldTest.t.sol +16 -0
  84. package/templates/react-ecs/packages/contracts/worlds.json +1 -1
  85. package/templates/react/packages/client/src/MUDContext.tsx +0 -21
  86. package/templates/react/packages/client/src/mud/createSystemCalls.ts +0 -56
  87. package/templates/react/packages/client/src/mud/getNetworkConfig.ts +0 -76
  88. package/templates/react/packages/client/src/mud/setup.ts +0 -18
  89. package/templates/react/packages/client/src/mud/setupNetwork.ts +0 -101
  90. package/templates/react/packages/client/src/mud/supportedChains.ts +0 -20
  91. package/templates/react/packages/contracts/src/codegen/tables/Tasks.sol +0 -522
  92. package/templates/react/packages/contracts/src/systems/TasksSystem.sol +0 -24
  93. package/templates/react/packages/contracts/test/TasksTest.t.sol +0 -30
  94. package/templates/react-ecs/packages/client/src/MUDContext.tsx +0 -21
  95. package/templates/react-ecs/packages/client/src/mud/createClientComponents.ts +0 -21
  96. package/templates/react-ecs/packages/client/src/mud/createSystemCalls.ts +0 -51
  97. package/templates/react-ecs/packages/client/src/mud/getNetworkConfig.ts +0 -77
  98. package/templates/react-ecs/packages/client/src/mud/setup.ts +0 -21
  99. package/templates/react-ecs/packages/client/src/mud/setupNetwork.ts +0 -106
  100. package/templates/react-ecs/packages/client/src/mud/supportedChains.ts +0 -20
  101. package/templates/react-ecs/packages/client/src/mud/world.ts +0 -3
  102. package/templates/react-ecs/packages/contracts/src/systems/IncrementSystem.sol +0 -14
  103. package/templates/react-ecs/packages/contracts/test/CounterTest.t.sol +0 -31
@@ -1,101 +0,0 @@
1
- /*
2
- * The MUD client code is built on top of viem
3
- * (https://viem.sh/docs/getting-started.html).
4
- * This line imports the functions we need from it.
5
- */
6
- import {
7
- createPublicClient,
8
- fallback,
9
- webSocket,
10
- http,
11
- createWalletClient,
12
- Hex,
13
- ClientConfig,
14
- getContract,
15
- } from "viem";
16
- import { syncToZustand } from "@latticexyz/store-sync/zustand";
17
- import { getNetworkConfig } from "./getNetworkConfig";
18
- import IWorldAbi from "contracts/out/IWorld.sol/IWorld.abi.json";
19
- import { createBurnerAccount, transportObserver, ContractWrite } from "@latticexyz/common";
20
- import { transactionQueue, writeObserver } from "@latticexyz/common/actions";
21
- import { Subject, share } from "rxjs";
22
-
23
- /*
24
- * Import our MUD config, which includes strong types for
25
- * our tables and other config options. We use this to generate
26
- * things like RECS components and get back strong types for them.
27
- *
28
- * See https://mud.dev/templates/typescript/contracts#mudconfigts
29
- * for the source of this information.
30
- */
31
- import mudConfig from "contracts/mud.config";
32
-
33
- export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;
34
-
35
- export async function setupNetwork() {
36
- const networkConfig = await getNetworkConfig();
37
-
38
- /*
39
- * Create a viem public (read only) client
40
- * (https://viem.sh/docs/clients/public.html)
41
- */
42
- const clientOptions = {
43
- chain: networkConfig.chain,
44
- transport: transportObserver(fallback([webSocket(), http()])),
45
- pollingInterval: 1000,
46
- } as const satisfies ClientConfig;
47
-
48
- const publicClient = createPublicClient(clientOptions);
49
-
50
- /*
51
- * Create an observable for contract writes that we can
52
- * pass into MUD dev tools for transaction observability.
53
- */
54
- const write$ = new Subject<ContractWrite>();
55
-
56
- /*
57
- * Create a temporary wallet and a viem client for it
58
- * (see https://viem.sh/docs/clients/wallet.html).
59
- */
60
- const burnerAccount = createBurnerAccount(networkConfig.privateKey as Hex);
61
- const burnerWalletClient = createWalletClient({
62
- ...clientOptions,
63
- account: burnerAccount,
64
- })
65
- .extend(transactionQueue())
66
- .extend(writeObserver({ onWrite: (write) => write$.next(write) }));
67
-
68
- /*
69
- * Create an object for communicating with the deployed World.
70
- */
71
- const worldContract = getContract({
72
- address: networkConfig.worldAddress as Hex,
73
- abi: IWorldAbi,
74
- client: { public: publicClient, wallet: burnerWalletClient },
75
- });
76
-
77
- /*
78
- * Sync on-chain state into RECS and keeps our client in sync.
79
- * Uses the MUD indexer if available, otherwise falls back
80
- * to the viem publicClient to make RPC calls to fetch MUD
81
- * events from the chain.
82
- */
83
- const { tables, useStore, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToZustand({
84
- config: mudConfig,
85
- address: networkConfig.worldAddress as Hex,
86
- publicClient,
87
- startBlock: BigInt(networkConfig.initialBlockNumber),
88
- });
89
-
90
- return {
91
- tables,
92
- useStore,
93
- publicClient,
94
- walletClient: burnerWalletClient,
95
- latestBlock$,
96
- storedBlockLogs$,
97
- waitForTransaction,
98
- worldContract,
99
- write$: write$.asObservable().pipe(share()),
100
- };
101
- }
@@ -1,20 +0,0 @@
1
- /*
2
- * The supported chains.
3
- * By default, there are only two chains here:
4
- *
5
- * - mudFoundry, the chain running on anvil that pnpm dev
6
- * starts by default. It is similar to the viem anvil chain
7
- * (see https://viem.sh/docs/clients/test.html), but with the
8
- * basefee set to zero to avoid transaction fees.
9
- * - Redstone, our production blockchain (https://redstone.xyz/)
10
- * - Garnet, our test blockchain (https://garnetchain.com/))
11
- *
12
- */
13
-
14
- import { MUDChain, mudFoundry, redstone, garnet } from "@latticexyz/common/chains";
15
-
16
- /*
17
- * See https://mud.dev/guides/hello-world/add-chain-client
18
- * for instructions on how to add networks.
19
- */
20
- export const supportedChains: MUDChain[] = [mudFoundry, redstone, garnet];
@@ -1,522 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity >=0.8.24;
3
-
4
- /* Autogenerated file. Do not edit manually. */
5
-
6
- // Import store internals
7
- import { IStore } from "@latticexyz/store/src/IStore.sol";
8
- import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
9
- import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
10
- import { Bytes } from "@latticexyz/store/src/Bytes.sol";
11
- import { Memory } from "@latticexyz/store/src/Memory.sol";
12
- import { SliceLib } from "@latticexyz/store/src/Slice.sol";
13
- import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol";
14
- import { FieldLayout } from "@latticexyz/store/src/FieldLayout.sol";
15
- import { Schema } from "@latticexyz/store/src/Schema.sol";
16
- import { EncodedLengths, EncodedLengthsLib } from "@latticexyz/store/src/EncodedLengths.sol";
17
- import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";
18
-
19
- struct TasksData {
20
- uint256 createdAt;
21
- uint256 completedAt;
22
- string description;
23
- }
24
-
25
- library Tasks {
26
- // Hex below is the result of `WorldResourceIdLib.encode({ namespace: "app", name: "Tasks", typeId: RESOURCE_TABLE });`
27
- ResourceId constant _tableId = ResourceId.wrap(0x746261707000000000000000000000005461736b730000000000000000000000);
28
-
29
- FieldLayout constant _fieldLayout =
30
- FieldLayout.wrap(0x0040020120200000000000000000000000000000000000000000000000000000);
31
-
32
- // Hex-encoded key schema of (bytes32)
33
- Schema constant _keySchema = Schema.wrap(0x002001005f000000000000000000000000000000000000000000000000000000);
34
- // Hex-encoded value schema of (uint256, uint256, string)
35
- Schema constant _valueSchema = Schema.wrap(0x004002011f1fc500000000000000000000000000000000000000000000000000);
36
-
37
- /**
38
- * @notice Get the table's key field names.
39
- * @return keyNames An array of strings with the names of key fields.
40
- */
41
- function getKeyNames() internal pure returns (string[] memory keyNames) {
42
- keyNames = new string[](1);
43
- keyNames[0] = "id";
44
- }
45
-
46
- /**
47
- * @notice Get the table's value field names.
48
- * @return fieldNames An array of strings with the names of value fields.
49
- */
50
- function getFieldNames() internal pure returns (string[] memory fieldNames) {
51
- fieldNames = new string[](3);
52
- fieldNames[0] = "createdAt";
53
- fieldNames[1] = "completedAt";
54
- fieldNames[2] = "description";
55
- }
56
-
57
- /**
58
- * @notice Register the table with its config.
59
- */
60
- function register() internal {
61
- StoreSwitch.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames());
62
- }
63
-
64
- /**
65
- * @notice Register the table with its config.
66
- */
67
- function _register() internal {
68
- StoreCore.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames());
69
- }
70
-
71
- /**
72
- * @notice Get createdAt.
73
- */
74
- function getCreatedAt(bytes32 id) internal view returns (uint256 createdAt) {
75
- bytes32[] memory _keyTuple = new bytes32[](1);
76
- _keyTuple[0] = id;
77
-
78
- bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
79
- return (uint256(bytes32(_blob)));
80
- }
81
-
82
- /**
83
- * @notice Get createdAt.
84
- */
85
- function _getCreatedAt(bytes32 id) internal view returns (uint256 createdAt) {
86
- bytes32[] memory _keyTuple = new bytes32[](1);
87
- _keyTuple[0] = id;
88
-
89
- bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
90
- return (uint256(bytes32(_blob)));
91
- }
92
-
93
- /**
94
- * @notice Set createdAt.
95
- */
96
- function setCreatedAt(bytes32 id, uint256 createdAt) internal {
97
- bytes32[] memory _keyTuple = new bytes32[](1);
98
- _keyTuple[0] = id;
99
-
100
- StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((createdAt)), _fieldLayout);
101
- }
102
-
103
- /**
104
- * @notice Set createdAt.
105
- */
106
- function _setCreatedAt(bytes32 id, uint256 createdAt) internal {
107
- bytes32[] memory _keyTuple = new bytes32[](1);
108
- _keyTuple[0] = id;
109
-
110
- StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((createdAt)), _fieldLayout);
111
- }
112
-
113
- /**
114
- * @notice Get completedAt.
115
- */
116
- function getCompletedAt(bytes32 id) internal view returns (uint256 completedAt) {
117
- bytes32[] memory _keyTuple = new bytes32[](1);
118
- _keyTuple[0] = id;
119
-
120
- bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
121
- return (uint256(bytes32(_blob)));
122
- }
123
-
124
- /**
125
- * @notice Get completedAt.
126
- */
127
- function _getCompletedAt(bytes32 id) internal view returns (uint256 completedAt) {
128
- bytes32[] memory _keyTuple = new bytes32[](1);
129
- _keyTuple[0] = id;
130
-
131
- bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
132
- return (uint256(bytes32(_blob)));
133
- }
134
-
135
- /**
136
- * @notice Set completedAt.
137
- */
138
- function setCompletedAt(bytes32 id, uint256 completedAt) internal {
139
- bytes32[] memory _keyTuple = new bytes32[](1);
140
- _keyTuple[0] = id;
141
-
142
- StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((completedAt)), _fieldLayout);
143
- }
144
-
145
- /**
146
- * @notice Set completedAt.
147
- */
148
- function _setCompletedAt(bytes32 id, uint256 completedAt) internal {
149
- bytes32[] memory _keyTuple = new bytes32[](1);
150
- _keyTuple[0] = id;
151
-
152
- StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((completedAt)), _fieldLayout);
153
- }
154
-
155
- /**
156
- * @notice Get description.
157
- */
158
- function getDescription(bytes32 id) internal view returns (string memory description) {
159
- bytes32[] memory _keyTuple = new bytes32[](1);
160
- _keyTuple[0] = id;
161
-
162
- bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0);
163
- return (string(_blob));
164
- }
165
-
166
- /**
167
- * @notice Get description.
168
- */
169
- function _getDescription(bytes32 id) internal view returns (string memory description) {
170
- bytes32[] memory _keyTuple = new bytes32[](1);
171
- _keyTuple[0] = id;
172
-
173
- bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0);
174
- return (string(_blob));
175
- }
176
-
177
- /**
178
- * @notice Set description.
179
- */
180
- function setDescription(bytes32 id, string memory description) internal {
181
- bytes32[] memory _keyTuple = new bytes32[](1);
182
- _keyTuple[0] = id;
183
-
184
- StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, bytes((description)));
185
- }
186
-
187
- /**
188
- * @notice Set description.
189
- */
190
- function _setDescription(bytes32 id, string memory description) internal {
191
- bytes32[] memory _keyTuple = new bytes32[](1);
192
- _keyTuple[0] = id;
193
-
194
- StoreCore.setDynamicField(_tableId, _keyTuple, 0, bytes((description)));
195
- }
196
-
197
- /**
198
- * @notice Get the length of description.
199
- */
200
- function lengthDescription(bytes32 id) internal view returns (uint256) {
201
- bytes32[] memory _keyTuple = new bytes32[](1);
202
- _keyTuple[0] = id;
203
-
204
- uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0);
205
- unchecked {
206
- return _byteLength / 1;
207
- }
208
- }
209
-
210
- /**
211
- * @notice Get the length of description.
212
- */
213
- function _lengthDescription(bytes32 id) internal view returns (uint256) {
214
- bytes32[] memory _keyTuple = new bytes32[](1);
215
- _keyTuple[0] = id;
216
-
217
- uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0);
218
- unchecked {
219
- return _byteLength / 1;
220
- }
221
- }
222
-
223
- /**
224
- * @notice Get an item of description.
225
- * @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array.
226
- */
227
- function getItemDescription(bytes32 id, uint256 _index) internal view returns (string memory) {
228
- bytes32[] memory _keyTuple = new bytes32[](1);
229
- _keyTuple[0] = id;
230
-
231
- unchecked {
232
- bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1);
233
- return (string(_blob));
234
- }
235
- }
236
-
237
- /**
238
- * @notice Get an item of description.
239
- * @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array.
240
- */
241
- function _getItemDescription(bytes32 id, uint256 _index) internal view returns (string memory) {
242
- bytes32[] memory _keyTuple = new bytes32[](1);
243
- _keyTuple[0] = id;
244
-
245
- unchecked {
246
- bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1);
247
- return (string(_blob));
248
- }
249
- }
250
-
251
- /**
252
- * @notice Push a slice to description.
253
- */
254
- function pushDescription(bytes32 id, string memory _slice) internal {
255
- bytes32[] memory _keyTuple = new bytes32[](1);
256
- _keyTuple[0] = id;
257
-
258
- StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice)));
259
- }
260
-
261
- /**
262
- * @notice Push a slice to description.
263
- */
264
- function _pushDescription(bytes32 id, string memory _slice) internal {
265
- bytes32[] memory _keyTuple = new bytes32[](1);
266
- _keyTuple[0] = id;
267
-
268
- StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice)));
269
- }
270
-
271
- /**
272
- * @notice Pop a slice from description.
273
- */
274
- function popDescription(bytes32 id) internal {
275
- bytes32[] memory _keyTuple = new bytes32[](1);
276
- _keyTuple[0] = id;
277
-
278
- StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 1);
279
- }
280
-
281
- /**
282
- * @notice Pop a slice from description.
283
- */
284
- function _popDescription(bytes32 id) internal {
285
- bytes32[] memory _keyTuple = new bytes32[](1);
286
- _keyTuple[0] = id;
287
-
288
- StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 1);
289
- }
290
-
291
- /**
292
- * @notice Update a slice of description at `_index`.
293
- */
294
- function updateDescription(bytes32 id, uint256 _index, string memory _slice) internal {
295
- bytes32[] memory _keyTuple = new bytes32[](1);
296
- _keyTuple[0] = id;
297
-
298
- unchecked {
299
- bytes memory _encoded = bytes((_slice));
300
- StoreSwitch.spliceDynamicData(_tableId, _keyTuple, 0, uint40(_index * 1), uint40(_encoded.length), _encoded);
301
- }
302
- }
303
-
304
- /**
305
- * @notice Update a slice of description at `_index`.
306
- */
307
- function _updateDescription(bytes32 id, uint256 _index, string memory _slice) internal {
308
- bytes32[] memory _keyTuple = new bytes32[](1);
309
- _keyTuple[0] = id;
310
-
311
- unchecked {
312
- bytes memory _encoded = bytes((_slice));
313
- StoreCore.spliceDynamicData(_tableId, _keyTuple, 0, uint40(_index * 1), uint40(_encoded.length), _encoded);
314
- }
315
- }
316
-
317
- /**
318
- * @notice Get the full data.
319
- */
320
- function get(bytes32 id) internal view returns (TasksData memory _table) {
321
- bytes32[] memory _keyTuple = new bytes32[](1);
322
- _keyTuple[0] = id;
323
-
324
- (bytes memory _staticData, EncodedLengths _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord(
325
- _tableId,
326
- _keyTuple,
327
- _fieldLayout
328
- );
329
- return decode(_staticData, _encodedLengths, _dynamicData);
330
- }
331
-
332
- /**
333
- * @notice Get the full data.
334
- */
335
- function _get(bytes32 id) internal view returns (TasksData memory _table) {
336
- bytes32[] memory _keyTuple = new bytes32[](1);
337
- _keyTuple[0] = id;
338
-
339
- (bytes memory _staticData, EncodedLengths _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord(
340
- _tableId,
341
- _keyTuple,
342
- _fieldLayout
343
- );
344
- return decode(_staticData, _encodedLengths, _dynamicData);
345
- }
346
-
347
- /**
348
- * @notice Set the full data using individual values.
349
- */
350
- function set(bytes32 id, uint256 createdAt, uint256 completedAt, string memory description) internal {
351
- bytes memory _staticData = encodeStatic(createdAt, completedAt);
352
-
353
- EncodedLengths _encodedLengths = encodeLengths(description);
354
- bytes memory _dynamicData = encodeDynamic(description);
355
-
356
- bytes32[] memory _keyTuple = new bytes32[](1);
357
- _keyTuple[0] = id;
358
-
359
- StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData);
360
- }
361
-
362
- /**
363
- * @notice Set the full data using individual values.
364
- */
365
- function _set(bytes32 id, uint256 createdAt, uint256 completedAt, string memory description) internal {
366
- bytes memory _staticData = encodeStatic(createdAt, completedAt);
367
-
368
- EncodedLengths _encodedLengths = encodeLengths(description);
369
- bytes memory _dynamicData = encodeDynamic(description);
370
-
371
- bytes32[] memory _keyTuple = new bytes32[](1);
372
- _keyTuple[0] = id;
373
-
374
- StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
375
- }
376
-
377
- /**
378
- * @notice Set the full data using the data struct.
379
- */
380
- function set(bytes32 id, TasksData memory _table) internal {
381
- bytes memory _staticData = encodeStatic(_table.createdAt, _table.completedAt);
382
-
383
- EncodedLengths _encodedLengths = encodeLengths(_table.description);
384
- bytes memory _dynamicData = encodeDynamic(_table.description);
385
-
386
- bytes32[] memory _keyTuple = new bytes32[](1);
387
- _keyTuple[0] = id;
388
-
389
- StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData);
390
- }
391
-
392
- /**
393
- * @notice Set the full data using the data struct.
394
- */
395
- function _set(bytes32 id, TasksData memory _table) internal {
396
- bytes memory _staticData = encodeStatic(_table.createdAt, _table.completedAt);
397
-
398
- EncodedLengths _encodedLengths = encodeLengths(_table.description);
399
- bytes memory _dynamicData = encodeDynamic(_table.description);
400
-
401
- bytes32[] memory _keyTuple = new bytes32[](1);
402
- _keyTuple[0] = id;
403
-
404
- StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
405
- }
406
-
407
- /**
408
- * @notice Decode the tightly packed blob of static data using this table's field layout.
409
- */
410
- function decodeStatic(bytes memory _blob) internal pure returns (uint256 createdAt, uint256 completedAt) {
411
- createdAt = (uint256(Bytes.getBytes32(_blob, 0)));
412
-
413
- completedAt = (uint256(Bytes.getBytes32(_blob, 32)));
414
- }
415
-
416
- /**
417
- * @notice Decode the tightly packed blob of dynamic data using the encoded lengths.
418
- */
419
- function decodeDynamic(
420
- EncodedLengths _encodedLengths,
421
- bytes memory _blob
422
- ) internal pure returns (string memory description) {
423
- uint256 _start;
424
- uint256 _end;
425
- unchecked {
426
- _end = _encodedLengths.atIndex(0);
427
- }
428
- description = (string(SliceLib.getSubslice(_blob, _start, _end).toBytes()));
429
- }
430
-
431
- /**
432
- * @notice Decode the tightly packed blobs using this table's field layout.
433
- * @param _staticData Tightly packed static fields.
434
- * @param _encodedLengths Encoded lengths of dynamic fields.
435
- * @param _dynamicData Tightly packed dynamic fields.
436
- */
437
- function decode(
438
- bytes memory _staticData,
439
- EncodedLengths _encodedLengths,
440
- bytes memory _dynamicData
441
- ) internal pure returns (TasksData memory _table) {
442
- (_table.createdAt, _table.completedAt) = decodeStatic(_staticData);
443
-
444
- (_table.description) = decodeDynamic(_encodedLengths, _dynamicData);
445
- }
446
-
447
- /**
448
- * @notice Delete all data for given keys.
449
- */
450
- function deleteRecord(bytes32 id) internal {
451
- bytes32[] memory _keyTuple = new bytes32[](1);
452
- _keyTuple[0] = id;
453
-
454
- StoreSwitch.deleteRecord(_tableId, _keyTuple);
455
- }
456
-
457
- /**
458
- * @notice Delete all data for given keys.
459
- */
460
- function _deleteRecord(bytes32 id) internal {
461
- bytes32[] memory _keyTuple = new bytes32[](1);
462
- _keyTuple[0] = id;
463
-
464
- StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout);
465
- }
466
-
467
- /**
468
- * @notice Tightly pack static (fixed length) data using this table's schema.
469
- * @return The static data, encoded into a sequence of bytes.
470
- */
471
- function encodeStatic(uint256 createdAt, uint256 completedAt) internal pure returns (bytes memory) {
472
- return abi.encodePacked(createdAt, completedAt);
473
- }
474
-
475
- /**
476
- * @notice Tightly pack dynamic data lengths using this table's schema.
477
- * @return _encodedLengths The lengths of the dynamic fields (packed into a single bytes32 value).
478
- */
479
- function encodeLengths(string memory description) internal pure returns (EncodedLengths _encodedLengths) {
480
- // Lengths are effectively checked during copy by 2**40 bytes exceeding gas limits
481
- unchecked {
482
- _encodedLengths = EncodedLengthsLib.pack(bytes(description).length);
483
- }
484
- }
485
-
486
- /**
487
- * @notice Tightly pack dynamic (variable length) data using this table's schema.
488
- * @return The dynamic data, encoded into a sequence of bytes.
489
- */
490
- function encodeDynamic(string memory description) internal pure returns (bytes memory) {
491
- return abi.encodePacked(bytes((description)));
492
- }
493
-
494
- /**
495
- * @notice Encode all of a record's fields.
496
- * @return The static (fixed length) data, encoded into a sequence of bytes.
497
- * @return The lengths of the dynamic fields (packed into a single bytes32 value).
498
- * @return The dynamic (variable length) data, encoded into a sequence of bytes.
499
- */
500
- function encode(
501
- uint256 createdAt,
502
- uint256 completedAt,
503
- string memory description
504
- ) internal pure returns (bytes memory, EncodedLengths, bytes memory) {
505
- bytes memory _staticData = encodeStatic(createdAt, completedAt);
506
-
507
- EncodedLengths _encodedLengths = encodeLengths(description);
508
- bytes memory _dynamicData = encodeDynamic(description);
509
-
510
- return (_staticData, _encodedLengths, _dynamicData);
511
- }
512
-
513
- /**
514
- * @notice Encode keys as a bytes32 array using this table's field layout.
515
- */
516
- function encodeKeyTuple(bytes32 id) internal pure returns (bytes32[] memory) {
517
- bytes32[] memory _keyTuple = new bytes32[](1);
518
- _keyTuple[0] = id;
519
-
520
- return _keyTuple;
521
- }
522
- }
@@ -1,24 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity >=0.8.24;
3
-
4
- import { System } from "@latticexyz/world/src/System.sol";
5
- import { Tasks, TasksData } from "../codegen/index.sol";
6
-
7
- contract TasksSystem is System {
8
- function addTask(string memory description) public returns (bytes32 id) {
9
- id = keccak256(abi.encode(block.prevrandao, _msgSender(), description));
10
- Tasks.set(id, TasksData({ description: description, createdAt: block.timestamp, completedAt: 0 }));
11
- }
12
-
13
- function completeTask(bytes32 id) public {
14
- Tasks.setCompletedAt(id, block.timestamp);
15
- }
16
-
17
- function resetTask(bytes32 id) public {
18
- Tasks.setCompletedAt(id, 0);
19
- }
20
-
21
- function deleteTask(bytes32 id) public {
22
- Tasks.deleteRecord(id);
23
- }
24
- }
@@ -1,30 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity >=0.8.24;
3
-
4
- import "forge-std/Test.sol";
5
- import { MudTest } from "@latticexyz/world/test/MudTest.t.sol";
6
-
7
- import { IWorld } from "../src/codegen/world/IWorld.sol";
8
- import { Tasks, TasksData } from "../src/codegen/index.sol";
9
-
10
- contract TasksTest is MudTest {
11
- function testWorldExists() public {
12
- uint256 codeSize;
13
- address addr = worldAddress;
14
- assembly {
15
- codeSize := extcodesize(addr)
16
- }
17
- assertTrue(codeSize > 0);
18
- }
19
-
20
- function testTasks() public {
21
- // Expect task to exist that we created during PostDeploy script
22
- TasksData memory task = Tasks.get("1");
23
- assertEq(task.description, "Walk the dog");
24
- assertEq(task.completedAt, 0);
25
-
26
- // Expect the task to be completed after calling completeTask from our TasksSystem
27
- IWorld(worldAddress).app__completeTask("1");
28
- assertEq(Tasks.getCompletedAt("1"), block.timestamp);
29
- }
30
- }