-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathcallFrom.ts
199 lines (175 loc) · 7.02 KB
/
callFrom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {
slice,
concat,
type Transport,
type Chain,
type Account,
type Hex,
type WalletActions,
type Client,
type PublicActions,
type WriteContractParameters,
type EncodeFunctionDataParameters,
} from "viem";
import { getAction, encodeFunctionData } from "viem/utils";
import { readContract, writeContract as viem_writeContract } from "viem/actions";
import { readHex } from "@latticexyz/common";
import {
getKeySchema,
getValueSchema,
getSchemaTypes,
decodeValueArgs,
encodeKey,
} from "@latticexyz/protocol-parser/internal";
import worldConfig from "../../mud.config";
import { worldCallAbi } from "../worldCallAbi";
type CallFromParameters = {
worldAddress: Hex;
delegatorAddress: Hex;
worldFunctionToSystemFunction?: (worldFunctionSelector: Hex) => Promise<SystemFunction>;
publicClient?: Client;
};
type SystemFunction = { systemId: Hex; systemFunctionSelector: Hex };
// By extending viem clients with this function after delegation, the delegation is automatically applied to World contract writes.
// This means that these writes are made on behalf of the delegator.
// Internally, it transforms the write arguments to use `callFrom`.
//
// Accepts either `worldFunctionToSystemFunction` or `publicClient` as an argument.
// `worldFunctionToSystemFunction` allows manually providing the mapping function, thus users can utilize their client store for the lookup.
// If `publicClient` is provided instead, this function retrieves the corresponding system function from the World contract.
//
// The function mapping is cached to avoid redundant retrievals for the same World function.
export function callFrom(
params: CallFromParameters,
): <chain extends Chain, account extends Account | undefined>(
client: Client<Transport, chain, account>,
) => Pick<WalletActions<chain, account>, "writeContract"> {
return (client) => ({
async writeContract(writeArgs) {
const _writeContract = getAction(client, viem_writeContract, "writeContract");
// Skip if the contract isn't the World or the function called should not be redirected through `callFrom`.
if (
writeArgs.address !== params.worldAddress ||
writeArgs.functionName === "call" ||
writeArgs.functionName === "callFrom" ||
writeArgs.functionName === "batchCallFrom" ||
writeArgs.functionName === "callWithSignature"
) {
return _writeContract(writeArgs);
}
// Wrap system calls from `batchCall` with delegator for a `batchCallFrom`
// TODO: remove this specific workaround once https://github.com/latticexyz/mud/pull/3506 lands
if (writeArgs.functionName === "batchCall") {
const batchCallArgs = writeArgs as unknown as WriteContractParameters<worldCallAbi, "batchCall">;
const [systemCalls] = batchCallArgs.args;
if (!systemCalls.length) {
throw new Error("`batchCall` should have at least one system call.");
}
return _writeContract({
...batchCallArgs,
functionName: "batchCallFrom",
args: [systemCalls.map((systemCall) => ({ from: params.delegatorAddress, ...systemCall }))],
});
}
// Encode the World's calldata (which includes the World's function selector).
const worldCalldata = encodeFunctionData({
abi: writeArgs.abi,
functionName: writeArgs.functionName,
args: writeArgs.args,
} as unknown as EncodeFunctionDataParameters);
// The first 4 bytes of calldata represent the function selector.
const worldFunctionSelector = slice(worldCalldata, 0, 4);
// Get the systemId and System's function selector.
const { systemId, systemFunctionSelector } = await worldFunctionToSystemFunction({
...params,
publicClient: params.publicClient ?? client,
worldFunctionSelector,
});
// Construct the System's calldata by replacing the World's function selector with the System's.
// Use `readHex` instead of `slice` to prevent out-of-bounds errors with calldata that has no args.
const systemCalldata = concat([systemFunctionSelector, readHex(worldCalldata, 4)]);
// Call `writeContract` with the new args.
return _writeContract({
...(writeArgs as unknown as WriteContractParameters<worldCallAbi, "callFrom">),
functionName: "callFrom",
args: [params.delegatorAddress, systemId, systemCalldata],
});
},
});
}
const systemFunctionCache = new Map<Hex, SystemFunction>();
async function worldFunctionToSystemFunction(params: {
worldAddress: Hex;
delegatorAddress: Hex;
worldFunctionSelector: Hex;
worldFunctionToSystemFunction?: (worldFunctionSelector: Hex) => Promise<SystemFunction>;
publicClient: Client;
}): Promise<SystemFunction> {
const cacheKey = concat([params.worldAddress, params.worldFunctionSelector]);
// Use cache if the function has been called previously.
const cached = systemFunctionCache.get(cacheKey);
if (cached) return cached;
// If a mapping function is provided, use it. Otherwise, call the World contract.
const systemFunction = params.worldFunctionToSystemFunction
? await params.worldFunctionToSystemFunction(params.worldFunctionSelector)
: await retrieveSystemFunctionFromContract(params.publicClient, params.worldAddress, params.worldFunctionSelector);
systemFunctionCache.set(cacheKey, systemFunction);
return systemFunction;
}
async function retrieveSystemFunctionFromContract(
publicClient: Client,
worldAddress: Hex,
worldFunctionSelector: Hex,
): Promise<SystemFunction> {
const table = worldConfig.tables.world__FunctionSelectors;
const keySchema = getSchemaTypes(getKeySchema(table));
const valueSchema = getSchemaTypes(getValueSchema(table));
const _readContract = getAction(publicClient, readContract, "readContract") as PublicActions["readContract"];
const [staticData, encodedLengths, dynamicData] = await _readContract({
address: worldAddress,
abi: [
{
type: "function",
name: "getRecord",
inputs: [
{
name: "tableId",
type: "bytes32",
internalType: "ResourceId",
},
{
name: "keyTuple",
type: "bytes32[]",
internalType: "bytes32[]",
},
],
outputs: [
{
name: "staticData",
type: "bytes",
internalType: "bytes",
},
{
name: "encodedLengths",
type: "bytes32",
internalType: "EncodedLengths",
},
{
name: "dynamicData",
type: "bytes",
internalType: "bytes",
},
],
stateMutability: "view",
},
],
functionName: "getRecord",
args: [table.tableId, encodeKey(keySchema, { worldFunctionSelector })],
});
const decoded = decodeValueArgs(valueSchema, { staticData, encodedLengths, dynamicData });
const systemFunction: SystemFunction = {
systemId: decoded.systemId,
systemFunctionSelector: decoded.systemFunctionSelector,
};
return systemFunction;
}