-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdelegatorEnvironment.ts
More file actions
298 lines (274 loc) · 9.83 KB
/
Copy pathdelegatorEnvironment.ts
File metadata and controls
298 lines (274 loc) · 9.83 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import {
EntryPoint,
SimpleFactory,
DelegationManager,
MultiSigDeleGator,
HybridDeleGator,
EIP7702StatelessDeleGator,
SCL_RIP7212,
AllowedTargetsEnforcer,
AllowedMethodsEnforcer,
DeployedEnforcer,
TimestampEnforcer,
NonceEnforcer,
AllowedCalldataEnforcer,
BlockNumberEnforcer,
LimitedCallsEnforcer,
ERC20BalanceChangeEnforcer,
ERC20StreamingEnforcer,
IdEnforcer,
ERC20TransferAmountEnforcer,
ValueLteEnforcer,
NativeTokenTransferAmountEnforcer,
NativeBalanceChangeEnforcer,
NativeTokenStreamingEnforcer,
NativeTokenPaymentEnforcer,
RedeemerEnforcer,
ArgsEqualityCheckEnforcer,
ERC721BalanceChangeEnforcer,
ERC721TransferEnforcer,
ERC1155BalanceChangeEnforcer,
OwnershipTransferEnforcer,
SpecificActionERC20TransferBatchEnforcer,
ERC20PeriodTransferEnforcer,
NativeTokenPeriodTransferEnforcer,
ExactCalldataBatchEnforcer,
ExactCalldataEnforcer,
ExactExecutionEnforcer,
ExactExecutionBatchEnforcer,
MultiTokenPeriodEnforcer,
} from '@metamask/delegation-abis';
import { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';
import type { Chain, Hex, PublicClient, WalletClient } from 'viem';
import type { ContractMetaData, DeleGatorEnvironment } from './types';
import { deployContract } from './write';
type SupportedVersion = '1.0.0' | '1.1.0' | '1.2.0' | '1.3.0';
export const PREFERRED_VERSION: SupportedVersion = '1.3.0';
const contractOverrideMap: Map<string, DeleGatorEnvironment> = new Map();
const getContractOverrideKey = (chainId: number, version: SupportedVersion) =>
`${version}:${chainId}`;
/**
* Overrides the deployed environment for a specific chain and version.
* @param chainId - The chain ID to override.
* @param version - The version of the environment to override.
* @param environment - The environment to use as override.
*/
export function overrideDeployedEnvironment(
chainId: number,
version: SupportedVersion,
environment: DeleGatorEnvironment,
) {
contractOverrideMap.set(
getContractOverrideKey(chainId, version),
environment,
);
}
/**
* Gets the DeleGator environment for the specified chain and version.
* @param chainId - The chain ID to get the environment for.
* @param version - The version of the environment to get.
* @returns The DeleGator environment.
*/
export function getDeleGatorEnvironment(
chainId: number,
version: SupportedVersion = PREFERRED_VERSION,
): DeleGatorEnvironment {
const overrideKey = getContractOverrideKey(chainId, version);
const overriddenContracts = contractOverrideMap.get(overrideKey);
if (overriddenContracts) {
return overriddenContracts;
}
const contracts = DELEGATOR_CONTRACTS[version]?.[chainId];
if (!contracts) {
throw new Error(
`No contracts found for version ${version} chain ${chainId}`,
);
}
return getDeleGatorEnvironmentV1(contracts);
}
/**
* Creates a DeleGator environment from contract addresses.
* @param contracts - The contract addresses to create the environment from.
* @returns The created DeleGator environment.
*/
export function getDeleGatorEnvironmentV1(contracts: {
[contract: string]: Hex;
}) {
return {
DelegationManager: contracts.DelegationManager,
EntryPoint: contracts.EntryPoint,
SimpleFactory: contracts.SimpleFactory,
implementations: {
MultiSigDeleGatorImpl: contracts.MultiSigDeleGatorImpl,
HybridDeleGatorImpl: contracts.HybridDeleGatorImpl,
EIP7702StatelessDeleGatorImpl: contracts.EIP7702StatelessDeleGatorImpl,
},
caveatEnforcers: {
AllowedCalldataEnforcer: contracts.AllowedCalldataEnforcer,
AllowedMethodsEnforcer: contracts.AllowedMethodsEnforcer,
AllowedTargetsEnforcer: contracts.AllowedTargetsEnforcer,
ArgsEqualityCheckEnforcer: contracts.ArgsEqualityCheckEnforcer,
BlockNumberEnforcer: contracts.BlockNumberEnforcer,
DeployedEnforcer: contracts.DeployedEnforcer,
ERC20BalanceChangeEnforcer: contracts.ERC20BalanceChangeEnforcer,
ERC20TransferAmountEnforcer: contracts.ERC20TransferAmountEnforcer,
ERC20StreamingEnforcer: contracts.ERC20StreamingEnforcer,
ERC721BalanceChangeEnforcer: contracts.ERC721BalanceChangeEnforcer,
ERC721TransferEnforcer: contracts.ERC721TransferEnforcer,
ERC1155BalanceChangeEnforcer: contracts.ERC1155BalanceChangeEnforcer,
IdEnforcer: contracts.IdEnforcer,
LimitedCallsEnforcer: contracts.LimitedCallsEnforcer,
NonceEnforcer: contracts.NonceEnforcer,
TimestampEnforcer: contracts.TimestampEnforcer,
ValueLteEnforcer: contracts.ValueLteEnforcer,
NativeTokenTransferAmountEnforcer:
contracts.NativeTokenTransferAmountEnforcer,
NativeBalanceChangeEnforcer: contracts.NativeBalanceChangeEnforcer,
NativeTokenStreamingEnforcer: contracts.NativeTokenStreamingEnforcer,
NativeTokenPaymentEnforcer: contracts.NativeTokenPaymentEnforcer,
OwnershipTransferEnforcer: contracts.OwnershipTransferEnforcer,
RedeemerEnforcer: contracts.RedeemerEnforcer,
SpecificActionERC20TransferBatchEnforcer:
contracts.SpecificActionERC20TransferBatchEnforcer,
ERC20PeriodTransferEnforcer: contracts.ERC20PeriodTransferEnforcer,
NativeTokenPeriodTransferEnforcer:
contracts.NativeTokenPeriodTransferEnforcer,
ExactCalldataBatchEnforcer: contracts.ExactCalldataBatchEnforcer,
ExactCalldataEnforcer: contracts.ExactCalldataEnforcer,
ExactExecutionEnforcer: contracts.ExactExecutionEnforcer,
ExactExecutionBatchEnforcer: contracts.ExactExecutionBatchEnforcer,
MultiTokenPeriodEnforcer: contracts.MultiTokenPeriodEnforcer,
},
} as DeleGatorEnvironment;
}
export type DeployedContract = {
name: string;
address: string;
};
/**
* Deploys the contracts needed for the Delegation Framework and DeleGator SCA to be functional as well as all Caveat Enforcers.
* @param walletClient - The wallet client to use for deployment.
* @param publicClient - The public client to use for deployment.
* @param chain - The chain to deploy to.
* @param deployedContracts - Optional map of already deployed contracts.
* @returns A promise that resolves when all contracts are deployed.
*/
export async function deployDeleGatorEnvironment(
walletClient: WalletClient,
publicClient: PublicClient,
chain: Chain,
deployedContracts: { [contract: string]: Hex } = {},
) {
const deployContractCurried = async (
name: string,
contract: ContractMetaData,
params: any[] = [],
) => {
const existingAddress = deployedContracts[name];
if (existingAddress) {
return {
address: existingAddress,
name,
};
}
const deployedContract = await deployContract(
walletClient,
publicClient,
chain,
contract,
params,
);
const newDeployedContracts = { ...deployedContracts };
newDeployedContracts[name] = deployedContract.address;
Object.assign(deployedContracts, newDeployedContracts);
return { ...deployedContract, name };
};
// Deploy v1.3.0 DeleGator contracts
// - deploy standalone contracts
const standaloneContracts = {
SimpleFactory,
AllowedCalldataEnforcer,
AllowedTargetsEnforcer,
AllowedMethodsEnforcer,
ArgsEqualityCheckEnforcer,
DeployedEnforcer,
TimestampEnforcer,
BlockNumberEnforcer,
LimitedCallsEnforcer,
ERC20BalanceChangeEnforcer,
ERC20TransferAmountEnforcer,
ERC20StreamingEnforcer,
ERC721BalanceChangeEnforcer,
ERC721TransferEnforcer,
ERC1155BalanceChangeEnforcer,
IdEnforcer,
NonceEnforcer,
ValueLteEnforcer,
NativeTokenTransferAmountEnforcer,
NativeBalanceChangeEnforcer,
NativeTokenStreamingEnforcer,
OwnershipTransferEnforcer,
RedeemerEnforcer,
SpecificActionERC20TransferBatchEnforcer,
ERC20PeriodTransferEnforcer,
NativeTokenPeriodTransferEnforcer,
ExactCalldataBatchEnforcer,
ExactCalldataEnforcer,
ExactExecutionEnforcer,
ExactExecutionBatchEnforcer,
MultiTokenPeriodEnforcer,
};
for (const [name, contract] of Object.entries(standaloneContracts)) {
await deployContractCurried(name, contract);
}
// - deploy dependencies
const delegationManager = await deployContractCurried(
'DelegationManager',
DelegationManager,
[walletClient.account?.address],
);
// - NativeTokenPaymentEnforcer DelegationManager and ArgsEqualityCheckEnforcer as constructor args
await deployContractCurried(
'NativeTokenPaymentEnforcer',
NativeTokenPaymentEnforcer,
[delegationManager.address, deployedContracts.ArgsEqualityCheckEnforcer],
);
const entryPoint = await deployContractCurried('EntryPoint', EntryPoint);
// This is a hack to work around the SCL_RIP7212 being deployed as a library.
// Forge handles this gracefully, but in the tests we need to manually link
// the library.
// We don't use the curried function here because we don't need it added to
// the environment.
const { address: sclRIP7212 } = await deployContract(
walletClient,
publicClient,
chain,
SCL_RIP7212,
[],
);
// replace linked library address in bytecode https://docs.soliditylang.org/en/latest/using-the-compiler.html#library-linking
const hybridDeleGatorWithLinkedLibrary = {
...HybridDeleGator,
bytecode: HybridDeleGator.bytecode.replace(
/__\$b8f96b288d4d0429e38b8ed50fd423070f\$__/gu,
sclRIP7212.slice(2),
) as Hex,
};
// - deploy DeleGator implementations
await deployContractCurried(
'HybridDeleGatorImpl',
hybridDeleGatorWithLinkedLibrary,
[delegationManager.address, entryPoint.address],
);
await deployContractCurried('MultiSigDeleGatorImpl', MultiSigDeleGator, [
delegationManager.address,
entryPoint.address,
]);
await deployContractCurried(
'EIP7702StatelessDeleGatorImpl',
EIP7702StatelessDeleGator,
[delegationManager.address, entryPoint.address],
);
// Format deployments
return getDeleGatorEnvironmentV1(deployedContracts);
}