@latticexyz/world 2.2.18-f0433092876e2ac9b5b12cd0ecae9c120a2d0368 → 2.2.19-900ac35deebfa260bafb1697d15e95eef855cd69
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.
- package/dist/IBaseWorld.abi-L4NKVDJN.js +2015 -0
- package/dist/{IBaseWorld.abi-SGUPOG6A.js.map → IBaseWorld.abi-L4NKVDJN.js.map} +1 -1
- package/dist/System.abi-ENBBMWQJ.js +67 -0
- package/dist/{System.abi-D2D3OAI5.js.map → System.abi-ENBBMWQJ.js.map} +1 -1
- package/dist/chunk-2W4TKC47.js +114 -0
- package/dist/{chunk-DIHBEOC2.js.map → chunk-2W4TKC47.js.map} +1 -1
- package/dist/chunk-5WRI5ZAA.js +31 -0
- package/dist/chunk-SGZK356L.js +132 -0
- package/dist/{chunk-AJUHOWGE.js.map → chunk-SGZK356L.js.map} +1 -1
- package/dist/chunk-XSBK355I.js +142 -0
- package/dist/{chunk-CGF3NYHC.js.map → chunk-XSBK355I.js.map} +1 -1
- package/dist/index.js +14 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.js +301 -3
- package/dist/internal.js.map +1 -1
- package/dist/mud.config.js +11 -1
- package/dist/node.js +7276 -102
- package/dist/node.js.map +1 -1
- package/out/AccessControl.t.sol/AccessControlTest.json +1 -1
- package/out/AccessManagementSystem.sol/AccessManagementSystem.json +1 -1
- package/out/BalanceTransferSystem.sol/BalanceTransferSystem.json +1 -1
- package/out/BatchCall.t.sol/BatchCallTest.json +1 -1
- package/out/BatchCall.t.sol/TestSystem.json +1 -1
- package/out/BatchCallSystem.sol/BatchCallSystem.json +1 -1
- package/out/Delegation.sol/Delegation.json +1 -1
- package/out/DelegationControl.sol/DelegationControl.json +1 -1
- package/out/DelegationControlMock.sol/DelegationControlMock.json +1 -1
- package/out/Factories.t.sol/FactoriesTest.json +1 -1
- package/out/IBaseWorld.sol/IBaseWorld.json +1 -1
- package/out/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.json +1 -1
- package/out/InitModule.sol/InitModule.json +1 -1
- package/out/InitModule.t.sol/InitModuleTest.json +1 -1
- package/out/InitModule.t.sol/WorldMock.json +1 -1
- package/out/InitSystems.t.sol/LimitedCallContextTest.json +1 -1
- package/out/Module.sol/Module.json +1 -1
- package/out/ModuleInstallationSystem.sol/ModuleInstallationSystem.json +1 -1
- package/out/RegistrationSystem.sol/RegistrationSystem.json +1 -1
- package/out/StoreRegistrationSystem.sol/StoreRegistrationSystem.json +1 -1
- package/out/System.sol/System.json +1 -1
- package/out/System.t.sol/SystemTest.json +1 -1
- package/out/System.t.sol/TestSystem.json +1 -1
- package/out/SystemCall.sol/SystemCall.json +1 -1
- package/out/Utils.t.sol/UtilsTest.json +1 -1
- package/out/Utils.t.sol/UtilsTestSystem.json +1 -1
- package/out/World.sol/World.json +1 -1
- package/out/World.t.sol/EchoSystemHook.json +1 -1
- package/out/World.t.sol/IWorldTestSystem.json +1 -1
- package/out/World.t.sol/PayableFallbackSystem.json +1 -1
- package/out/World.t.sol/RevertSystemHook.json +1 -1
- package/out/World.t.sol/WorldTest.json +1 -1
- package/out/World.t.sol/WorldTestSystem.json +1 -1
- package/out/WorldBalance.t.sol/WorldBalanceTest.json +1 -1
- package/out/WorldBalance.t.sol/WorldBalanceTestSystem.json +1 -1
- package/out/WorldContext.sol/WorldContextConsumer.json +1 -1
- package/out/WorldContext.sol/WorldContextConsumerLib.json +1 -1
- package/out/WorldContext.sol/WorldContextProviderLib.json +1 -1
- package/out/WorldContext.t.sol/TestContextConsumer.json +1 -1
- package/out/WorldContext.t.sol/WorldContextTest.json +1 -1
- package/out/WorldDynamicUpdate.t.sol/UpdateInDynamicFieldTest.json +1 -1
- package/out/WorldFactory.sol/WorldFactory.json +1 -1
- package/out/WorldProxy.sol/WorldProxy.json +1 -1
- package/out/WorldProxy.t.sol/WorldProxyTest.json +1 -1
- package/out/WorldProxyFactory.sol/WorldProxyFactory.json +1 -1
- package/out/WorldProxyFactory.t.sol/WorldProxyFactoryTest.json +1 -1
- package/out/WorldRegistrationSystem.sol/WorldRegistrationSystem.json +1 -1
- package/out/build-info/{5b08d15c729dccf7cb96a41c445f5026.json → 27bef2dac23cc424daa40171c98ac387.json} +1 -1
- package/package.json +9 -9
- package/src/WorldContext.sol +1 -1
- package/dist/IBaseWorld.abi-SGUPOG6A.js +0 -2
- package/dist/System.abi-D2D3OAI5.js +0 -2
- package/dist/chunk-AJUHOWGE.js +0 -2
- package/dist/chunk-CGF3NYHC.js +0 -2
- package/dist/chunk-DIHBEOC2.js +0 -2
- package/dist/chunk-FW4363Y4.js +0 -2
- /package/dist/{chunk-FW4363Y4.js.map → chunk-5WRI5ZAA.js.map} +0 -0
package/dist/internal.js
CHANGED
@@ -1,5 +1,303 @@
|
|
1
|
-
import
|
1
|
+
import {
|
2
|
+
mud_config_default
|
3
|
+
} from "./chunk-SGZK356L.js";
|
4
|
+
import {
|
5
|
+
defineNamespaces,
|
6
|
+
defineWorld,
|
7
|
+
resolveCodegen,
|
8
|
+
resolveDeploy,
|
9
|
+
resolveNamespaces,
|
10
|
+
resolveWorld,
|
11
|
+
validateNamespaces,
|
12
|
+
validateWorld
|
13
|
+
} from "./chunk-2W4TKC47.js";
|
14
|
+
import {
|
15
|
+
CODEGEN_DEFAULTS,
|
16
|
+
CONFIG_DEFAULTS,
|
17
|
+
DEPLOY_DEFAULTS,
|
18
|
+
MODULE_DEFAULTS,
|
19
|
+
SYSTEM_DEFAULTS,
|
20
|
+
SYSTEM_DEPLOY_DEFAULTS,
|
21
|
+
defineSystem,
|
22
|
+
resolveNamespace,
|
23
|
+
resolveSystem,
|
24
|
+
resolveSystems,
|
25
|
+
validateNamespace,
|
26
|
+
validateSystem,
|
27
|
+
validateSystems
|
28
|
+
} from "./chunk-XSBK355I.js";
|
29
|
+
import "./chunk-5WRI5ZAA.js";
|
2
30
|
|
3
|
-
|
4
|
-
|
31
|
+
// ts/encodeSystemCall.ts
|
32
|
+
import { encodeFunctionData } from "viem";
|
33
|
+
|
34
|
+
// ts/normalizeSystemFunctionName.ts
|
35
|
+
import { hexToResource, resourceToLabel } from "@latticexyz/common";
|
36
|
+
function internal_normalizeSystemFunctionName(systemId, functionName) {
|
37
|
+
const resource = hexToResource(systemId);
|
38
|
+
const worldFunctionPrefix = resource.namespace !== "" ? `${resource.namespace}__` : null;
|
39
|
+
if (worldFunctionPrefix != null && functionName.startsWith(worldFunctionPrefix)) {
|
40
|
+
console.warn(
|
41
|
+
// eslint-disable-next-line max-len
|
42
|
+
`Detected world function name "${functionName}" used in call to system "${resourceToLabel(resource)}".
|
43
|
+
|
44
|
+
It's recommended to use a system ABI and system function name with these methods instead.`
|
45
|
+
);
|
46
|
+
return functionName.slice(worldFunctionPrefix.length);
|
47
|
+
}
|
48
|
+
return functionName;
|
49
|
+
}
|
50
|
+
|
51
|
+
// ts/encodeSystemCall.ts
|
52
|
+
function encodeSystemCall({
|
53
|
+
abi,
|
54
|
+
systemId,
|
55
|
+
functionName,
|
56
|
+
args
|
57
|
+
}) {
|
58
|
+
return [
|
59
|
+
systemId,
|
60
|
+
encodeFunctionData({
|
61
|
+
abi,
|
62
|
+
functionName: internal_normalizeSystemFunctionName(systemId, functionName),
|
63
|
+
args
|
64
|
+
})
|
65
|
+
];
|
66
|
+
}
|
67
|
+
|
68
|
+
// ts/encodeSystemCallFrom.ts
|
69
|
+
import { encodeFunctionData as encodeFunctionData2 } from "viem";
|
70
|
+
function encodeSystemCallFrom({
|
71
|
+
abi,
|
72
|
+
from,
|
73
|
+
systemId,
|
74
|
+
functionName,
|
75
|
+
args
|
76
|
+
}) {
|
77
|
+
return [
|
78
|
+
from,
|
79
|
+
systemId,
|
80
|
+
encodeFunctionData2({
|
81
|
+
abi,
|
82
|
+
functionName: internal_normalizeSystemFunctionName(systemId, functionName),
|
83
|
+
args
|
84
|
+
})
|
85
|
+
];
|
86
|
+
}
|
87
|
+
|
88
|
+
// ts/encodeSystemCalls.ts
|
89
|
+
import { encodeFunctionData as encodeFunctionData3 } from "viem";
|
90
|
+
function encodeSystemCalls(abi, systemCalls) {
|
91
|
+
return [
|
92
|
+
systemCalls.map(({ systemId, functionName, args }) => ({
|
93
|
+
systemId,
|
94
|
+
callData: encodeFunctionData3({
|
95
|
+
abi,
|
96
|
+
functionName: internal_normalizeSystemFunctionName(systemId, functionName),
|
97
|
+
args
|
98
|
+
})
|
99
|
+
}))
|
100
|
+
];
|
101
|
+
}
|
102
|
+
|
103
|
+
// ts/encodeSystemCallsFrom.ts
|
104
|
+
import { encodeFunctionData as encodeFunctionData4 } from "viem";
|
105
|
+
function encodeSystemCallsFrom(abi, from, systemCalls) {
|
106
|
+
return [
|
107
|
+
systemCalls.map(({ systemId, functionName, args }) => ({
|
108
|
+
from,
|
109
|
+
systemId,
|
110
|
+
callData: encodeFunctionData4({
|
111
|
+
abi,
|
112
|
+
functionName: internal_normalizeSystemFunctionName(systemId, functionName),
|
113
|
+
args
|
114
|
+
})
|
115
|
+
}))
|
116
|
+
];
|
117
|
+
}
|
118
|
+
|
119
|
+
// ts/actions/callFrom.ts
|
120
|
+
import {
|
121
|
+
slice,
|
122
|
+
concat
|
123
|
+
} from "viem";
|
124
|
+
import { getAction, encodeFunctionData as encodeFunctionData5 } from "viem/utils";
|
125
|
+
import { readContract, writeContract as viem_writeContract } from "viem/actions";
|
126
|
+
import { readHex } from "@latticexyz/common";
|
127
|
+
import {
|
128
|
+
getKeySchema,
|
129
|
+
getValueSchema,
|
130
|
+
getSchemaTypes,
|
131
|
+
decodeValueArgs,
|
132
|
+
encodeKey
|
133
|
+
} from "@latticexyz/protocol-parser/internal";
|
134
|
+
function callFrom(params) {
|
135
|
+
return (client) => ({
|
136
|
+
async writeContract(writeArgs) {
|
137
|
+
const _writeContract = getAction(client, viem_writeContract, "writeContract");
|
138
|
+
if (writeArgs.address !== params.worldAddress || writeArgs.functionName === "call" || writeArgs.functionName === "callFrom" || writeArgs.functionName === "batchCallFrom" || writeArgs.functionName === "callWithSignature") {
|
139
|
+
return _writeContract(writeArgs);
|
140
|
+
}
|
141
|
+
if (writeArgs.functionName === "batchCall") {
|
142
|
+
const batchCallArgs = writeArgs;
|
143
|
+
const [systemCalls] = batchCallArgs.args;
|
144
|
+
if (!systemCalls.length) {
|
145
|
+
throw new Error("`batchCall` should have at least one system call.");
|
146
|
+
}
|
147
|
+
return _writeContract({
|
148
|
+
...batchCallArgs,
|
149
|
+
functionName: "batchCallFrom",
|
150
|
+
args: [systemCalls.map((systemCall) => ({ from: params.delegatorAddress, ...systemCall }))]
|
151
|
+
});
|
152
|
+
}
|
153
|
+
const worldCalldata = encodeFunctionData5({
|
154
|
+
abi: writeArgs.abi,
|
155
|
+
functionName: writeArgs.functionName,
|
156
|
+
args: writeArgs.args
|
157
|
+
});
|
158
|
+
const worldFunctionSelector = slice(worldCalldata, 0, 4);
|
159
|
+
const { systemId, systemFunctionSelector } = await worldFunctionToSystemFunction({
|
160
|
+
...params,
|
161
|
+
publicClient: params.publicClient ?? client,
|
162
|
+
worldFunctionSelector
|
163
|
+
});
|
164
|
+
const systemCalldata = concat([systemFunctionSelector, readHex(worldCalldata, 4)]);
|
165
|
+
return _writeContract({
|
166
|
+
...writeArgs,
|
167
|
+
functionName: "callFrom",
|
168
|
+
args: [params.delegatorAddress, systemId, systemCalldata]
|
169
|
+
});
|
170
|
+
}
|
171
|
+
});
|
172
|
+
}
|
173
|
+
var systemFunctionCache = /* @__PURE__ */ new Map();
|
174
|
+
async function worldFunctionToSystemFunction(params) {
|
175
|
+
const cacheKey = concat([params.worldAddress, params.worldFunctionSelector]);
|
176
|
+
const cached = systemFunctionCache.get(cacheKey);
|
177
|
+
if (cached) return cached;
|
178
|
+
const systemFunction = params.worldFunctionToSystemFunction ? await params.worldFunctionToSystemFunction(params.worldFunctionSelector) : await retrieveSystemFunctionFromContract(params.publicClient, params.worldAddress, params.worldFunctionSelector);
|
179
|
+
systemFunctionCache.set(cacheKey, systemFunction);
|
180
|
+
return systemFunction;
|
181
|
+
}
|
182
|
+
async function retrieveSystemFunctionFromContract(publicClient, worldAddress, worldFunctionSelector) {
|
183
|
+
const table = mud_config_default.tables.world__FunctionSelectors;
|
184
|
+
const keySchema = getSchemaTypes(getKeySchema(table));
|
185
|
+
const valueSchema = getSchemaTypes(getValueSchema(table));
|
186
|
+
const _readContract = getAction(publicClient, readContract, "readContract");
|
187
|
+
const [staticData, encodedLengths, dynamicData] = await _readContract({
|
188
|
+
address: worldAddress,
|
189
|
+
abi: [
|
190
|
+
{
|
191
|
+
type: "function",
|
192
|
+
name: "getRecord",
|
193
|
+
inputs: [
|
194
|
+
{
|
195
|
+
name: "tableId",
|
196
|
+
type: "bytes32",
|
197
|
+
internalType: "ResourceId"
|
198
|
+
},
|
199
|
+
{
|
200
|
+
name: "keyTuple",
|
201
|
+
type: "bytes32[]",
|
202
|
+
internalType: "bytes32[]"
|
203
|
+
}
|
204
|
+
],
|
205
|
+
outputs: [
|
206
|
+
{
|
207
|
+
name: "staticData",
|
208
|
+
type: "bytes",
|
209
|
+
internalType: "bytes"
|
210
|
+
},
|
211
|
+
{
|
212
|
+
name: "encodedLengths",
|
213
|
+
type: "bytes32",
|
214
|
+
internalType: "EncodedLengths"
|
215
|
+
},
|
216
|
+
{
|
217
|
+
name: "dynamicData",
|
218
|
+
type: "bytes",
|
219
|
+
internalType: "bytes"
|
220
|
+
}
|
221
|
+
],
|
222
|
+
stateMutability: "view"
|
223
|
+
}
|
224
|
+
],
|
225
|
+
functionName: "getRecord",
|
226
|
+
args: [table.tableId, encodeKey(keySchema, { worldFunctionSelector })]
|
227
|
+
});
|
228
|
+
const decoded = decodeValueArgs(valueSchema, { staticData, encodedLengths, dynamicData });
|
229
|
+
const systemFunction = {
|
230
|
+
systemId: decoded.systemId,
|
231
|
+
systemFunctionSelector: decoded.systemFunctionSelector
|
232
|
+
};
|
233
|
+
return systemFunction;
|
234
|
+
}
|
235
|
+
|
236
|
+
// ts/config/v2/dynamicResolution.ts
|
237
|
+
function resolveTableId(tableName) {
|
238
|
+
return {
|
239
|
+
type: "tableId",
|
240
|
+
input: tableName
|
241
|
+
};
|
242
|
+
}
|
243
|
+
function isDynamicResolution(value) {
|
244
|
+
return typeof value === "object" && value !== null && "type" in value && "input" in value;
|
245
|
+
}
|
246
|
+
function isValueWithType(value) {
|
247
|
+
return typeof value === "object" && value !== null && "type" in value && "value" in value;
|
248
|
+
}
|
249
|
+
function resolveWithContext(input, context) {
|
250
|
+
if (isValueWithType(input)) return input;
|
251
|
+
if (isDynamicResolution(input)) {
|
252
|
+
if (input.type === "tableId") {
|
253
|
+
const tableEntries = Object.entries(context.config.tables).filter(
|
254
|
+
([tableName, table]) => tableName === input.input || table.name === input.input
|
255
|
+
);
|
256
|
+
if (tableEntries.length > 1) {
|
257
|
+
throw new Error(
|
258
|
+
`Found more than one table with name "${input.input}". Try using one of the following table names instead: ${tableEntries.map(([tableName]) => tableName).join(", ")}`
|
259
|
+
);
|
260
|
+
}
|
261
|
+
if (tableEntries.length === 1) {
|
262
|
+
const [entry] = tableEntries;
|
263
|
+
const [, table] = entry;
|
264
|
+
return { type: "bytes32", value: table.tableId };
|
265
|
+
}
|
266
|
+
}
|
267
|
+
}
|
268
|
+
throw new Error(`Could not resolve dynamic resolution:
|
269
|
+
${JSON.stringify(input, null, 2)}`);
|
270
|
+
}
|
271
|
+
export {
|
272
|
+
CODEGEN_DEFAULTS,
|
273
|
+
CONFIG_DEFAULTS,
|
274
|
+
DEPLOY_DEFAULTS,
|
275
|
+
MODULE_DEFAULTS,
|
276
|
+
SYSTEM_DEFAULTS,
|
277
|
+
SYSTEM_DEPLOY_DEFAULTS,
|
278
|
+
callFrom,
|
279
|
+
defineNamespaces,
|
280
|
+
defineSystem,
|
281
|
+
defineWorld,
|
282
|
+
encodeSystemCall,
|
283
|
+
encodeSystemCallFrom,
|
284
|
+
encodeSystemCalls,
|
285
|
+
encodeSystemCallsFrom,
|
286
|
+
isDynamicResolution,
|
287
|
+
isValueWithType,
|
288
|
+
resolveCodegen,
|
289
|
+
resolveDeploy,
|
290
|
+
resolveNamespace,
|
291
|
+
resolveNamespaces,
|
292
|
+
resolveSystem,
|
293
|
+
resolveSystems,
|
294
|
+
resolveTableId,
|
295
|
+
resolveWithContext,
|
296
|
+
resolveWorld,
|
297
|
+
validateNamespace,
|
298
|
+
validateNamespaces,
|
299
|
+
validateSystem,
|
300
|
+
validateSystems,
|
301
|
+
validateWorld
|
302
|
+
};
|
5
303
|
//# sourceMappingURL=internal.js.map
|
package/dist/internal.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../ts/encodeSystemCall.ts","../ts/normalizeSystemFunctionName.ts","../ts/encodeSystemCallFrom.ts","../ts/encodeSystemCalls.ts","../ts/encodeSystemCallsFrom.ts","../ts/actions/callFrom.ts","../ts/config/v2/dynamicResolution.ts"],"sourcesContent":["import { Abi, EncodeFunctionDataParameters, Hex, encodeFunctionData, type ContractFunctionName } from \"viem\";\nimport type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from \"abitype\";\nimport { worldCallAbi } from \"./worldCallAbi\";\nimport { internal_normalizeSystemFunctionName } from \"./normalizeSystemFunctionName\";\n\nexport type SystemCall<abi extends Abi, functionName extends ContractFunctionName<abi>> = {\n /**\n * System ABI\n */\n readonly abi: abi;\n /**\n * System's resource ID\n */\n readonly systemId: Hex;\n /**\n * System function name to call\n */\n readonly functionName: functionName;\n} & Pick<EncodeFunctionDataParameters<abi, functionName>, \"args\">;\n\n/** Encode a system call to be passed as arguments into `World.call` */\nexport function encodeSystemCall<abi extends Abi, functionName extends ContractFunctionName<abi>>({\n abi,\n systemId,\n functionName,\n args,\n}: SystemCall<abi, functionName>): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, \"call\">[\"inputs\"]> {\n return [\n systemId,\n encodeFunctionData<abi, functionName>({\n abi,\n functionName: internal_normalizeSystemFunctionName(systemId, functionName),\n args,\n } as EncodeFunctionDataParameters<abi, functionName>),\n ];\n}\n","import { hexToResource, resourceToLabel } from \"@latticexyz/common\";\nimport { Hex } from \"viem\";\n\n/** @internal */\nexport function internal_normalizeSystemFunctionName(systemId: Hex, functionName: string) {\n const resource = hexToResource(systemId);\n const worldFunctionPrefix = resource.namespace !== \"\" ? `${resource.namespace}__` : null;\n if (worldFunctionPrefix != null && functionName.startsWith(worldFunctionPrefix)) {\n console.warn(\n // eslint-disable-next-line max-len\n `Detected world function name \"${functionName}\" used in call to system \"${resourceToLabel(resource)}\".\\n\\nIt's recommended to use a system ABI and system function name with these methods instead.`,\n );\n return functionName.slice(worldFunctionPrefix.length);\n }\n return functionName;\n}\n","import { Abi, EncodeFunctionDataParameters, encodeFunctionData, Address, type ContractFunctionName } from \"viem\";\nimport type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from \"abitype\";\nimport { SystemCall } from \"./encodeSystemCall\";\nimport { worldCallAbi } from \"./worldCallAbi\";\nimport { internal_normalizeSystemFunctionName } from \"./normalizeSystemFunctionName\";\n\nexport type SystemCallFrom<abi extends Abi, functionName extends ContractFunctionName<abi>> = SystemCall<\n abi,\n functionName\n> & {\n readonly from: Address;\n};\n\n/** Encode a system call to be passed as arguments into `World.callFrom` */\nexport function encodeSystemCallFrom<abi extends Abi, functionName extends ContractFunctionName<abi>>({\n abi,\n from,\n systemId,\n functionName,\n args,\n}: SystemCallFrom<abi, functionName>): AbiParametersToPrimitiveTypes<\n ExtractAbiFunction<worldCallAbi, \"callFrom\">[\"inputs\"]\n> {\n return [\n from,\n systemId,\n encodeFunctionData<abi, functionName>({\n abi,\n functionName: internal_normalizeSystemFunctionName(systemId, functionName),\n args,\n } as EncodeFunctionDataParameters<abi, functionName>),\n ];\n}\n","import { Abi, EncodeFunctionDataParameters, encodeFunctionData, type ContractFunctionName } from \"viem\";\nimport { SystemCall } from \"./encodeSystemCall\";\nimport type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from \"abitype\";\nimport { worldCallAbi } from \"./worldCallAbi\";\nimport { internal_normalizeSystemFunctionName } from \"./normalizeSystemFunctionName\";\n\n/** Encode system calls to be passed as arguments into `World.batchCall` */\nexport function encodeSystemCalls<abi extends Abi, functionName extends ContractFunctionName<abi>>(\n abi: abi,\n systemCalls: readonly Omit<SystemCall<abi, functionName>, \"abi\">[],\n): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, \"batchCall\">[\"inputs\"]> {\n return [\n systemCalls.map(({ systemId, functionName, args }) => ({\n systemId,\n callData: encodeFunctionData<abi, functionName>({\n abi,\n functionName: internal_normalizeSystemFunctionName(systemId, functionName),\n args,\n } as EncodeFunctionDataParameters<abi, functionName>),\n })),\n ];\n}\n","import { Abi, Address, EncodeFunctionDataParameters, encodeFunctionData, type ContractFunctionName } from \"viem\";\nimport { SystemCallFrom } from \"./encodeSystemCallFrom\";\nimport type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from \"abitype\";\nimport { worldCallAbi } from \"./worldCallAbi\";\nimport { internal_normalizeSystemFunctionName } from \"./normalizeSystemFunctionName\";\n\n/** Encode system calls to be passed as arguments into `World.batchCallFrom` */\nexport function encodeSystemCallsFrom<abi extends Abi, functionName extends ContractFunctionName<abi>>(\n abi: abi,\n from: Address,\n systemCalls: readonly Omit<SystemCallFrom<abi, functionName>, \"abi\" | \"from\">[],\n): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, \"batchCallFrom\">[\"inputs\"]> {\n return [\n systemCalls.map(({ systemId, functionName, args }) => ({\n from,\n systemId,\n callData: encodeFunctionData<abi, functionName>({\n abi,\n functionName: internal_normalizeSystemFunctionName(systemId, functionName),\n args,\n } as EncodeFunctionDataParameters<abi, functionName>),\n })),\n ];\n}\n","import {\n slice,\n concat,\n type Transport,\n type Chain,\n type Account,\n type Hex,\n type WalletActions,\n type Client,\n type PublicActions,\n type WriteContractParameters,\n type EncodeFunctionDataParameters,\n} from \"viem\";\nimport { getAction, encodeFunctionData } from \"viem/utils\";\nimport { readContract, writeContract as viem_writeContract } from \"viem/actions\";\nimport { readHex } from \"@latticexyz/common\";\nimport {\n getKeySchema,\n getValueSchema,\n getSchemaTypes,\n decodeValueArgs,\n encodeKey,\n} from \"@latticexyz/protocol-parser/internal\";\nimport worldConfig from \"../../mud.config\";\nimport { worldCallAbi } from \"../worldCallAbi\";\n\ntype CallFromParameters = {\n worldAddress: Hex;\n delegatorAddress: Hex;\n worldFunctionToSystemFunction?: (worldFunctionSelector: Hex) => Promise<SystemFunction>;\n publicClient?: Client;\n};\n\ntype SystemFunction = { systemId: Hex; systemFunctionSelector: Hex };\n\n// By extending viem clients with this function after delegation, the delegation is automatically applied to World contract writes.\n// This means that these writes are made on behalf of the delegator.\n// Internally, it transforms the write arguments to use `callFrom`.\n//\n// Accepts either `worldFunctionToSystemFunction` or `publicClient` as an argument.\n// `worldFunctionToSystemFunction` allows manually providing the mapping function, thus users can utilize their client store for the lookup.\n// If `publicClient` is provided instead, this function retrieves the corresponding system function from the World contract.\n//\n// The function mapping is cached to avoid redundant retrievals for the same World function.\nexport function callFrom(\n params: CallFromParameters,\n): <chain extends Chain, account extends Account | undefined>(\n client: Client<Transport, chain, account>,\n) => Pick<WalletActions<chain, account>, \"writeContract\"> {\n return (client) => ({\n async writeContract(writeArgs) {\n const _writeContract = getAction(client, viem_writeContract, \"writeContract\");\n\n // Skip if the contract isn't the World or the function called should not be redirected through `callFrom`.\n if (\n writeArgs.address !== params.worldAddress ||\n writeArgs.functionName === \"call\" ||\n writeArgs.functionName === \"callFrom\" ||\n writeArgs.functionName === \"batchCallFrom\" ||\n writeArgs.functionName === \"callWithSignature\"\n ) {\n return _writeContract(writeArgs);\n }\n\n // Wrap system calls from `batchCall` with delegator for a `batchCallFrom`\n // TODO: remove this specific workaround once https://github.com/latticexyz/mud/pull/3506 lands\n if (writeArgs.functionName === \"batchCall\") {\n const batchCallArgs = writeArgs as unknown as WriteContractParameters<worldCallAbi, \"batchCall\">;\n const [systemCalls] = batchCallArgs.args;\n if (!systemCalls.length) {\n throw new Error(\"`batchCall` should have at least one system call.\");\n }\n\n return _writeContract({\n ...batchCallArgs,\n functionName: \"batchCallFrom\",\n args: [systemCalls.map((systemCall) => ({ from: params.delegatorAddress, ...systemCall }))],\n });\n }\n\n // Encode the World's calldata (which includes the World's function selector).\n const worldCalldata = encodeFunctionData({\n abi: writeArgs.abi,\n functionName: writeArgs.functionName,\n args: writeArgs.args,\n } as unknown as EncodeFunctionDataParameters);\n\n // The first 4 bytes of calldata represent the function selector.\n const worldFunctionSelector = slice(worldCalldata, 0, 4);\n\n // Get the systemId and System's function selector.\n const { systemId, systemFunctionSelector } = await worldFunctionToSystemFunction({\n ...params,\n publicClient: params.publicClient ?? client,\n worldFunctionSelector,\n });\n\n // Construct the System's calldata by replacing the World's function selector with the System's.\n // Use `readHex` instead of `slice` to prevent out-of-bounds errors with calldata that has no args.\n const systemCalldata = concat([systemFunctionSelector, readHex(worldCalldata, 4)]);\n\n // Call `writeContract` with the new args.\n return _writeContract({\n ...(writeArgs as unknown as WriteContractParameters<worldCallAbi, \"callFrom\">),\n functionName: \"callFrom\",\n args: [params.delegatorAddress, systemId, systemCalldata],\n });\n },\n });\n}\n\nconst systemFunctionCache = new Map<Hex, SystemFunction>();\n\nasync function worldFunctionToSystemFunction(params: {\n worldAddress: Hex;\n delegatorAddress: Hex;\n worldFunctionSelector: Hex;\n worldFunctionToSystemFunction?: (worldFunctionSelector: Hex) => Promise<SystemFunction>;\n publicClient: Client;\n}): Promise<SystemFunction> {\n const cacheKey = concat([params.worldAddress, params.worldFunctionSelector]);\n\n // Use cache if the function has been called previously.\n const cached = systemFunctionCache.get(cacheKey);\n if (cached) return cached;\n\n // If a mapping function is provided, use it. Otherwise, call the World contract.\n const systemFunction = params.worldFunctionToSystemFunction\n ? await params.worldFunctionToSystemFunction(params.worldFunctionSelector)\n : await retrieveSystemFunctionFromContract(params.publicClient, params.worldAddress, params.worldFunctionSelector);\n\n systemFunctionCache.set(cacheKey, systemFunction);\n\n return systemFunction;\n}\n\nasync function retrieveSystemFunctionFromContract(\n publicClient: Client,\n worldAddress: Hex,\n worldFunctionSelector: Hex,\n): Promise<SystemFunction> {\n const table = worldConfig.tables.world__FunctionSelectors;\n\n const keySchema = getSchemaTypes(getKeySchema(table));\n const valueSchema = getSchemaTypes(getValueSchema(table));\n\n const _readContract = getAction(publicClient, readContract, \"readContract\") as PublicActions[\"readContract\"];\n\n const [staticData, encodedLengths, dynamicData] = await _readContract({\n address: worldAddress,\n abi: [\n {\n type: \"function\",\n name: \"getRecord\",\n inputs: [\n {\n name: \"tableId\",\n type: \"bytes32\",\n internalType: \"ResourceId\",\n },\n {\n name: \"keyTuple\",\n type: \"bytes32[]\",\n internalType: \"bytes32[]\",\n },\n ],\n outputs: [\n {\n name: \"staticData\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"encodedLengths\",\n type: \"bytes32\",\n internalType: \"EncodedLengths\",\n },\n {\n name: \"dynamicData\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n ],\n stateMutability: \"view\",\n },\n ],\n functionName: \"getRecord\",\n args: [table.tableId, encodeKey(keySchema, { worldFunctionSelector })],\n });\n\n const decoded = decodeValueArgs(valueSchema, { staticData, encodedLengths, dynamicData });\n\n const systemFunction: SystemFunction = {\n systemId: decoded.systemId,\n systemFunctionSelector: decoded.systemFunctionSelector,\n };\n\n return systemFunction;\n}\n","import { World } from \"./output\";\n\nexport type DynamicResolution = {\n // TODO: add systemAddress support\n type: \"tableId\";\n input: string;\n};\n\nexport type ValueWithType = {\n value: string | number | Uint8Array;\n type: string;\n};\n\n/**\n * Dynamically resolve a table name to a table id at deploy time\n */\nexport function resolveTableId(tableName: string) {\n return {\n type: \"tableId\",\n input: tableName,\n } as const;\n}\n\n/** Type guard for DynamicResolution */\nexport function isDynamicResolution(value: unknown): value is DynamicResolution {\n return typeof value === \"object\" && value !== null && \"type\" in value && \"input\" in value;\n}\n\n/** Type guard for ValueWithType */\nexport function isValueWithType(value: unknown): value is ValueWithType {\n return typeof value === \"object\" && value !== null && \"type\" in value && \"value\" in value;\n}\n\n/**\n * Turn a DynamicResolution object into a ValueWithType based on the provided context\n */\nexport function resolveWithContext(\n input: unknown,\n context: { config: World; systemAddresses?: Record<string, Promise<string>> },\n): ValueWithType {\n if (isValueWithType(input)) return input;\n\n if (isDynamicResolution(input)) {\n if (input.type === \"tableId\") {\n const tableEntries = Object.entries(context.config.tables).filter(\n ([tableName, table]) => tableName === input.input || table.name === input.input,\n );\n\n if (tableEntries.length > 1) {\n throw new Error(\n `Found more than one table with name \"${input.input}\". Try using one of the following table names instead: ${tableEntries.map(([tableName]) => tableName).join(\", \")}`,\n );\n }\n\n if (tableEntries.length === 1) {\n const [entry] = tableEntries;\n const [, table] = entry;\n return { type: \"bytes32\", value: table.tableId };\n }\n }\n }\n\n throw new Error(`Could not resolve dynamic resolution:\\n${JSON.stringify(input, null, 2)}`);\n}\n"],"mappings":"6RAAA,OAAiD,sBAAAA,MAAqD,OCAtG,OAAS,iBAAAC,EAAe,mBAAAC,MAAuB,qBAIxC,SAASC,EAAqCC,EAAeC,EAAsB,CACxF,IAAMC,EAAWL,EAAcG,CAAQ,EACjCG,EAAsBD,EAAS,YAAc,GAAK,GAAGA,EAAS,SAAS,KAAO,KACpF,OAAIC,GAAuB,MAAQF,EAAa,WAAWE,CAAmB,GAC5E,QAAQ,KAEN,iCAAiCF,CAAY,6BAA6BH,EAAgBI,CAAQ,CAAC;AAAA;AAAA,0FACrG,EACOD,EAAa,MAAME,EAAoB,MAAM,GAE/CF,CACT,CDMO,SAASG,GAAkF,CAChG,IAAAC,EACA,SAAAC,EACA,aAAAC,EACA,KAAAC,CACF,EAAqH,CACnH,MAAO,CACLF,EACAG,EAAsC,CACpC,IAAAJ,EACA,aAAcK,EAAqCJ,EAAUC,CAAY,EACzE,KAAAC,CACF,CAAoD,CACtD,CACF,CEnCA,OAA4C,sBAAAG,MAA8D,OAcnG,SAASC,GAAsF,CACpG,IAAAC,EACA,KAAAC,EACA,SAAAC,EAC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AMR,IAAcK,EAAM,OAASG,EAAM,OAASH,EAAM,KAC5E,EAEA,GAAIE,EAAa,OAAS,EACxB,MAAM,IAAI,MACR,wCAAwCF,EAAM,KAAK,0DAA0DE,EAAa,IAAI,CAAC,CAACP,CAAS,IAAMA,CAAS,EAAE,KAAK,IAAI,CAAC,EACtK,EAGF,GAAIO,EAAa,SAAW,EAAG,CAC7B,GAAM,CAACE,CAAK,EAAIF,EACV,CAAC,CAAEC,CAAK,EAAIC,EAClB,MAAO,CAAE,KAAM,UAAW,MAAOD,EAAM,OAAQ,CACjD,CACF,CAGF,MAAM,IAAI,MAAM;AAAA,EAA0C,KAAK,UAAUH,EAAO,KAAM,CAAC,CAAC,EAAE,CAC5F","names":["encodeFunctionData","hexToResource","resourceToLabel","internal_normalizeSystemFunctionName","systemId","functionName","resource","worldFunctionPrefix","encodeSystemCall","abi","systemId","functionName","args","encodeFunctionData","internal_normalizeSystemFunctionName","encodeFunctionData","encodeSystemCallFrom","abi","from","systemId","functionName","args","encodeFunctionData","internal_normalizeSystemFunctionName","encodeFunctionData","encodeSystemCalls","abi","systemCalls","systemId","functionName","args","encodeFunctionData","internal_normalizeSystemFunctionName","encodeFunctionData","encodeSystemCallsFrom","abi","from","systemCalls","systemId","functionName","args","encodeFunctionData","internal_normalizeSystemFunctionName","slice","concat","getAction","encodeFunctionData","readContract","viem_writeContract","readHex","getKeySchema","getValueSchema","getSchemaTypes","decodeValueArgs","encodeKey","callFrom","params","client","writeArgs","_writeContract","getAction","viem_writeContract","batchCallArgs","systemCalls","systemCall","worldCalldata","encodeFunctionData","worldFunctionSelector","slice","systemId","systemFunctionSelector","worldFunctionToSystemFunction","systemCalldata","concat","readHex","systemFunctionCache","cacheKey","cached","systemFunction","retrieveSystemFunctionFromContract","publicClient","worldAddress","table","mud_config_default","keySchema","getSchemaTypes","getKeySchema","valueSchema","getValueSchema","_readContract","readContract","staticData","encodedLengths","dynamicData","encodeKey","decoded","decodeValueArgs","resolveTableId","tableName","isDynamicResolution","value","isValueWithType","resolveWithContext","input","context","tableEntries","table","entry"]}
|
1
|
+
{"version":3,"sources":["../ts/encodeSystemCall.ts","../ts/normalizeSystemFunctionName.ts","../ts/encodeSystemCallFrom.ts","../ts/encodeSystemCalls.ts","../ts/encodeSystemCallsFrom.ts","../ts/actions/callFrom.ts","../ts/config/v2/dynamicResolution.ts"],"sourcesContent":["import { Abi, EncodeFunctionDataParameters, Hex, encodeFunctionData, type ContractFunctionName } from \"viem\";\nimport type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from \"abitype\";\nimport { worldCallAbi } from \"./worldCallAbi\";\nimport { internal_normalizeSystemFunctionName } from \"./normalizeSystemFunctionName\";\n\nexport type SystemCall<abi extends Abi, functionName extends ContractFunctionName<abi>> = {\n /**\n * System ABI\n */\n readonly abi: abi;\n /**\n * System's resource ID\n */\n readonly systemId: Hex;\n /**\n * System function name to call\n */\n readonly functionName: functionName;\n} & Pick<EncodeFunctionDataParameters<abi, functionName>, \"args\">;\n\n/** Encode a system call to be passed as arguments into `World.call` */\nexport function encodeSystemCall<abi extends Abi, functionName extends ContractFunctionName<abi>>({\n abi,\n systemId,\n functionName,\n args,\n}: SystemCall<abi, functionName>): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, \"call\">[\"inputs\"]> {\n return [\n systemId,\n encodeFunctionData<abi, functionName>({\n abi,\n functionName: internal_normalizeSystemFunctionName(systemId, functionName),\n args,\n } as EncodeFunctionDataParameters<abi, functionName>),\n ];\n}\n","import { hexToResource, resourceToLabel } from \"@latticexyz/common\";\nimport { Hex } from \"viem\";\n\n/** @internal */\nexport function internal_normalizeSystemFunctionName(systemId: Hex, functionName: string) {\n const resource = hexToResource(systemId);\n const worldFunctionPrefix = resource.namespace !== \"\" ? `${resource.namespace}__` : null;\n if (worldFunctionPrefix != null && functionName.startsWith(worldFunctionPrefix)) {\n console.warn(\n // eslint-disable-next-line max-len\n `Detected world function name \"${functionName}\" used in call to system \"${resourceToLabel(resource)}\".\\n\\nIt's recommended to use a system ABI and system function name with these methods instead.`,\n );\n return functionName.slice(worldFunctionPrefix.length);\n }\n return functionName;\n}\n","import { Abi, EncodeFunctionDataParameters, encodeFunctionData, Address, type ContractFunctionName } from \"viem\";\nimport type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from \"abitype\";\nimport { SystemCall } from \"./encodeSystemCall\";\nimport { worldCallAbi } from \"./worldCallAbi\";\nimport { internal_normalizeSystemFunctionName } from \"./normalizeSystemFunctionName\";\n\nexport type SystemCallFrom<abi extends Abi, functionName extends ContractFunctionName<abi>> = SystemCall<\n abi,\n functionName\n> & {\n readonly from: Address;\n};\n\n/** Encode a system call to be passed as arguments into `World.callFrom` */\nexport function encodeSystemCallFrom<abi extends Abi, functionName extends ContractFunctionName<abi>>({\n abi,\n from,\n systemId,\n functionName,\n args,\n}: SystemCallFrom<abi, functionName>): AbiParametersToPrimitiveTypes<\n ExtractAbiFunction<worldCallAbi, \"callFrom\">[\"inputs\"]\n> {\n return [\n from,\n systemId,\n encodeFunctionData<abi, functionName>({\n abi,\n functionName: internal_normalizeSystemFunctionName(systemId, functionName),\n args,\n } as EncodeFunctionDataParameters<abi, functionName>),\n ];\n}\n","import { Abi, EncodeFunctionDataParameters, encodeFunctionData, type ContractFunctionName } from \"viem\";\nimport { SystemCall } from \"./encodeSystemCall\";\nimport type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from \"abitype\";\nimport { worldCallAbi } from \"./worldCallAbi\";\nimport { internal_normalizeSystemFunctionName } from \"./normalizeSystemFunctionName\";\n\n/** Encode system calls to be passed as arguments into `World.batchCall` */\nexport function encodeSystemCalls<abi extends Abi, functionName extends ContractFunctionName<abi>>(\n abi: abi,\n systemCalls: readonly Omit<SystemCall<abi, functionName>, \"abi\">[],\n): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, \"batchCall\">[\"inputs\"]> {\n return [\n systemCalls.map(({ systemId, functionName, args }) => ({\n systemId,\n callData: encodeFunctionData<abi, functionName>({\n abi,\n functionName: internal_normalizeSystemFunctionName(systemId, functionName),\n args,\n } as EncodeFunctionDataParameters<abi, functionName>),\n })),\n ];\n}\n","import { Abi, Address, EncodeFunctionDataParameters, encodeFunctionData, type ContractFunctionName } from \"viem\";\nimport { SystemCallFrom } from \"./encodeSystemCallFrom\";\nimport type { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from \"abitype\";\nimport { worldCallAbi } from \"./worldCallAbi\";\nimport { internal_normalizeSystemFunctionName } from \"./normalizeSystemFunctionName\";\n\n/** Encode system calls to be passed as arguments into `World.batchCallFrom` */\nexport function encodeSystemCallsFrom<abi extends Abi, functionName extends ContractFunctionName<abi>>(\n abi: abi,\n from: Address,\n systemCalls: readonly Omit<SystemCallFrom<abi, functionName>, \"abi\" | \"from\">[],\n): AbiParametersToPrimitiveTypes<ExtractAbiFunction<worldCallAbi, \"batchCallFrom\">[\"inputs\"]> {\n return [\n systemCalls.map(({ systemId, functionName, args }) => ({\n from,\n systemId,\n callData: encodeFunctionData<abi, functionName>({\n abi,\n functionName: internal_normalizeSystemFunctionName(systemId, functionName),\n args,\n } as EncodeFunctionDataParameters<abi, functionName>),\n })),\n ];\n}\n","import {\n slice,\n concat,\n type Transport,\n type Chain,\n type Account,\n type Hex,\n type WalletActions,\n type Client,\n type PublicActions,\n type WriteContractParameters,\n type EncodeFunctionDataParameters,\n} from \"viem\";\nimport { getAction, encodeFunctionData } from \"viem/utils\";\nimport { readContract, writeContract as viem_writeContract } from \"viem/actions\";\nimport { readHex } from \"@latticexyz/common\";\nimport {\n getKeySchema,\n getValueSchema,\n getSchemaTypes,\n decodeValueArgs,\n encodeKey,\n} from \"@latticexyz/protocol-parser/internal\";\nimport worldConfig from \"../../mud.config\";\nimport { worldCallAbi } from \"../worldCallAbi\";\n\ntype CallFromParameters = {\n worldAddress: Hex;\n delegatorAddress: Hex;\n worldFunctionToSystemFunction?: (worldFunctionSelector: Hex) => Promise<SystemFunction>;\n publicClient?: Client;\n};\n\ntype SystemFunction = { systemId: Hex; systemFunctionSelector: Hex };\n\n// By extending viem clients with this function after delegation, the delegation is automatically applied to World contract writes.\n// This means that these writes are made on behalf of the delegator.\n// Internally, it transforms the write arguments to use `callFrom`.\n//\n// Accepts either `worldFunctionToSystemFunction` or `publicClient` as an argument.\n// `worldFunctionToSystemFunction` allows manually providing the mapping function, thus users can utilize their client store for the lookup.\n// If `publicClient` is provided instead, this function retrieves the corresponding system function from the World contract.\n//\n// The function mapping is cached to avoid redundant retrievals for the same World function.\nexport function callFrom(\n params: CallFromParameters,\n): <chain extends Chain, account extends Account | undefined>(\n client: Client<Transport, chain, account>,\n) => Pick<WalletActions<chain, account>, \"writeContract\"> {\n return (client) => ({\n async writeContract(writeArgs) {\n const _writeContract = getAction(client, viem_writeContract, \"writeContract\");\n\n // Skip if the contract isn't the World or the function called should not be redirected through `callFrom`.\n if (\n writeArgs.address !== params.worldAddress ||\n writeArgs.functionName === \"call\" ||\n writeArgs.functionName === \"callFrom\" ||\n writeArgs.functionName === \"batchCallFrom\" ||\n writeArgs.functionName === \"callWithSignature\"\n ) {\n return _writeContract(writeArgs);\n }\n\n // Wrap system calls from `batchCall` with delegator for a `batchCallFrom`\n // TODO: remove this specific workaround once https://github.com/latticexyz/mud/pull/3506 lands\n if (writeArgs.functionName === \"batchCall\") {\n const batchCallArgs = writeArgs as unknown as WriteContractParameters<worldCallAbi, \"batchCall\">;\n const [systemCalls] = batchCallArgs.args;\n if (!systemCalls.length) {\n throw new Error(\"`batchCall` should have at least one system call.\");\n }\n\n return _writeContract({\n ...batchCallArgs,\n functionName: \"batchCallFrom\",\n args: [systemCalls.map((systemCall) => ({ from: params.delegatorAddress, ...systemCall }))],\n });\n }\n\n // Encode the World's calldata (which includes the World's function selector).\n const worldCalldata = encodeFunctionData({\n abi: writeArgs.abi,\n functionName: writeArgs.functionName,\n args: writeArgs.args,\n } as unknown as EncodeFunctionDataParameters);\n\n // The first 4 bytes of calldata represent the function selector.\n const worldFunctionSelector = slice(worldCalldata, 0, 4);\n\n // Get the systemId and System's function selector.\n const { systemId, systemFunctionSelector } = await worldFunctionToSystemFunction({\n ...params,\n publicClient: params.publicClient ?? client,\n worldFunctionSelector,\n });\n\n // Construct the System's calldata by replacing the World's function selector with the System's.\n // Use `readHex` instead of `slice` to prevent out-of-bounds errors with calldata that has no args.\n const systemCalldata = concat([systemFunctionSelector, readHex(worldCalldata, 4)]);\n\n // Call `writeContract` with the new args.\n return _writeContract({\n ...(writeArgs as unknown as WriteContractParameters<worldCallAbi, \"callFrom\">),\n functionName: \"callFrom\",\n args: [params.delegatorAddress, systemId, systemCalldata],\n });\n },\n });\n}\n\nconst systemFunctionCache = new Map<Hex, SystemFunction>();\n\nasync function worldFunctionToSystemFunction(params: {\n worldAddress: Hex;\n delegatorAddress: Hex;\n worldFunctionSelector: Hex;\n worldFunctionToSystemFunction?: (worldFunctionSelector: Hex) => Promise<SystemFunction>;\n publicClient: Client;\n}): Promise<SystemFunction> {\n const cacheKey = concat([params.worldAddress, params.worldFunctionSelector]);\n\n // Use cache if the function has been called previously.\n const cached = systemFunctionCache.get(cacheKey);\n if (cached) return cached;\n\n // If a mapping function is provided, use it. Otherwise, call the World contract.\n const systemFunction = params.worldFunctionToSystemFunction\n ? await params.worldFunctionToSystemFunction(params.worldFunctionSelector)\n : await retrieveSystemFunctionFromContract(params.publicClient, params.worldAddress, params.worldFunctionSelector);\n\n systemFunctionCache.set(cacheKey, systemFunction);\n\n return systemFunction;\n}\n\nasync function retrieveSystemFunctionFromContract(\n publicClient: Client,\n worldAddress: Hex,\n worldFunctionSelector: Hex,\n): Promise<SystemFunction> {\n const table = worldConfig.tables.world__FunctionSelectors;\n\n const keySchema = getSchemaTypes(getKeySchema(table));\n const valueSchema = getSchemaTypes(getValueSchema(table));\n\n const _readContract = getAction(publicClient, readContract, \"readContract\") as PublicActions[\"readContract\"];\n\n const [staticData, encodedLengths, dynamicData] = await _readContract({\n address: worldAddress,\n abi: [\n {\n type: \"function\",\n name: \"getRecord\",\n inputs: [\n {\n name: \"tableId\",\n type: \"bytes32\",\n internalType: \"ResourceId\",\n },\n {\n name: \"keyTuple\",\n type: \"bytes32[]\",\n internalType: \"bytes32[]\",\n },\n ],\n outputs: [\n {\n name: \"staticData\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"encodedLengths\",\n type: \"bytes32\",\n internalType: \"EncodedLengths\",\n },\n {\n name: \"dynamicData\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n ],\n stateMutability: \"view\",\n },\n ],\n functionName: \"getRecord\",\n args: [table.tableId, encodeKey(keySchema, { worldFunctionSelector })],\n });\n\n const decoded = decodeValueArgs(valueSchema, { staticData, encodedLengths, dynamicData });\n\n const systemFunction: SystemFunction = {\n systemId: decoded.systemId,\n systemFunctionSelector: decoded.systemFunctionSelector,\n };\n\n return systemFunction;\n}\n","import { World } from \"./output\";\n\nexport type DynamicResolution = {\n // TODO: add systemAddress support\n type: \"tableId\";\n input: string;\n};\n\nexport type ValueWithType = {\n value: string | number | Uint8Array;\n type: string;\n};\n\n/**\n * Dynamically resolve a table name to a table id at deploy time\n */\nexport function resolveTableId(tableName: string) {\n return {\n type: \"tableId\",\n input: tableName,\n } as const;\n}\n\n/** Type guard for DynamicResolution */\nexport function isDynamicResolution(value: unknown): value is DynamicResolution {\n return typeof value === \"object\" && value !== null && \"type\" in value && \"input\" in value;\n}\n\n/** Type guard for ValueWithType */\nexport function isValueWithType(value: unknown): value is ValueWithType {\n return typeof value === \"object\" && value !== null && \"type\" in value && \"value\" in value;\n}\n\n/**\n * Turn a DynamicResolution object into a ValueWithType based on the provided context\n */\nexport function resolveWithContext(\n input: unknown,\n context: { config: World; systemAddresses?: Record<string, Promise<string>> },\n): ValueWithType {\n if (isValueWithType(input)) return input;\n\n if (isDynamicResolution(input)) {\n if (input.type === \"tableId\") {\n const tableEntries = Object.entries(context.config.tables).filter(\n ([tableName, table]) => tableName === input.input || table.name === input.input,\n );\n\n if (tableEntries.length > 1) {\n throw new Error(\n `Found more than one table with name \"${input.input}\". Try using one of the following table names instead: ${tableEntries.map(([tableName]) => tableName).join(\", \")}`,\n );\n }\n\n if (tableEntries.length === 1) {\n const [entry] = tableEntries;\n const [, table] = entry;\n return { type: \"bytes32\", value: table.tableId };\n }\n }\n }\n\n throw new Error(`Could not resolve dynamic resolution:\\n${JSON.stringify(input, null, 2)}`);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAiD,0BAAqD;;;ACAtG,SAAS,eAAe,uBAAuB;AAIxC,SAAS,qCAAqC,UAAe,cAAsB;AACxF,QAAM,WAAW,cAAc,QAAQ;AACvC,QAAM,sBAAsB,SAAS,cAAc,KAAK,GAAG,SAAS,SAAS,OAAO;AACpF,MAAI,uBAAuB,QAAQ,aAAa,WAAW,mBAAmB,GAAG;AAC/E,YAAQ;AAAA;AAAA,MAEN,iCAAiC,YAAY,6BAA6B,gBAAgB,QAAQ,CAAC;AAAA;AAAA;AAAA,IACrG;AACA,WAAO,aAAa,MAAM,oBAAoB,MAAM;AAAA,EACtD;AACA,SAAO;AACT;;;ADMO,SAAS,iBAAkF;AAAA,EAChG;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAqH;AACnH,SAAO;AAAA,IACL;AAAA,IACA,mBAAsC;AAAA,MACpC;AAAA,MACA,cAAc,qCAAqC,UAAU,YAAY;AAAA,MACzE;AAAA,IACF,CAAoD;AAAA,EACtD;AACF;;;AEnCA,SAA4C,sBAAAA,2BAA8D;AAcnG,SAAS,qBAAsF;AAAA,EACpG;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAEE;AACA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACAC,oBAAsC;AAAA,MACpC;AAAA,MACA,cAAc,qCAAqC,UAAU,YAAY;AAAA,MACzE;AAAA,IACF,CAAoD;AAAA,EACtD;AACF;;;AChCA,SAA4C,sBAAAC,2BAAqD;AAO1F,SAAS,kBAC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AAG,QAAO;AAEnC,MAAI,oBAAoB,KAAK,GAAG;AAC9B,QAAI,MAAM,SAAS,WAAW;AAC5B,YAAM,eAAe,OAAO,QAAQ,QAAQ,OAAO,MAAM,EAAE;AAAA,QACzD,CAAC,CAAC,WAAW,KAAK,MAAM,cAAc,MAAM,SAAS,MAAM,SAAS,MAAM;AAAA,MAC5E;AAEA,UAAI,aAAa,SAAS,GAAG;AAC3B,cAAM,IAAI;AAAA,UACR,wCAAwC,MAAM,KAAK,0DAA0D,aAAa,IAAI,CAAC,CAAC,SAAS,MAAM,SAAS,EAAE,KAAK,IAAI,CAAC;AAAA,QACtK;AAAA,MACF;AAEA,UAAI,aAAa,WAAW,GAAG;AAC7B,cAAM,CAAC,KAAK,IAAI;AAChB,cAAM,CAAC,EAAE,KAAK,IAAI;AAClB,eAAO,EAAE,MAAM,WAAW,OAAO,MAAM,QAAQ;AAAA,MACjD;AAAA,IACF;AAAA,EACF;AAEA,QAAM,IAAI,MAAM;AAAA,EAA0C,KAAK,UAAU,OAAO,MAAM,CAAC,CAAC,EAAE;AAC5F;","names":["encodeFunctionData","encodeFunctionData","encodeFunctionData","encodeFunctionData","encodeFunctionData","encodeFunctionData","encodeFunctionData","encodeFunctionData"]}
|
package/dist/mud.config.js
CHANGED
@@ -1,2 +1,12 @@
|
|
1
|
-
import{
|
1
|
+
import {
|
2
|
+
configInput,
|
3
|
+
mud_config_default
|
4
|
+
} from "./chunk-SGZK356L.js";
|
5
|
+
import "./chunk-2W4TKC47.js";
|
6
|
+
import "./chunk-XSBK355I.js";
|
7
|
+
import "./chunk-5WRI5ZAA.js";
|
8
|
+
export {
|
9
|
+
configInput,
|
10
|
+
mud_config_default as default
|
11
|
+
};
|
2
12
|
//# sourceMappingURL=mud.config.js.map
|