forked from alchemyplatform/aa-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexus.ts
170 lines (151 loc) · 4.42 KB
/
nexus.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
import hre from "hardhat";
import {
encodeFunctionData,
encodePacked,
getAbiItem,
getContract,
pad,
parseEther,
toHex,
walletActions,
zeroAddress,
zeroHash,
} from "viem";
import {AccountConfig, AccountDataV07} from "../accounts";
import {NEXUS_ARTIFACTS} from "../artifacts/nexus";
import {getEntryPointV07} from "../utils/entryPoint";
async function accountFixture(): Promise<AccountDataV07> {
const [walletClient] = await hre.viem.getWalletClients();
const publicClient = await hre.viem.getPublicClient();
const testClient = (await hre.viem.getTestClient()).extend(walletActions);
// Deploy Nexus-related contracts to the network using hardhat_setCode.
for (const {address, bytecode} of Object.values(NEXUS_ARTIFACTS)) {
await hre.network.provider.send("hardhat_setCode", [address, bytecode]);
}
// Get the Nexus factory contract
const nexusFactory = getContract({
address: NEXUS_ARTIFACTS.K1ValidatorFactory.address,
abi: NEXUS_ARTIFACTS.K1ValidatorFactory.abi,
publicClient,
walletClient,
});
// If the factory requires setting up using an impersonated account
await testClient.impersonateAccount({address: zeroAddress});
await hre.network.provider.send("hardhat_setBalance", [
zeroAddress,
toHex(parseEther("10000")),
]);
// No need to call `setImplementation` here; it's already set in the constructor.
await testClient.stopImpersonatingAccount({address: zeroAddress});
return {
entryPoint: getEntryPointV07({walletClient, publicClient}),
createAccount: async (salt, ownerAddress) => {
const attesters = [ownerAddress];
const threshold = 1;
const hash = await nexusFactory.write.createAccount([
ownerAddress,
salt,
attesters,
threshold,
]);
return hash;
},
getAccountAddress: async (salt, ownerAddress) => {
const attesters = [ownerAddress];
const threshold = 1;
return await nexusFactory.read.computeAccountAddress([
ownerAddress,
salt,
attesters,
threshold,
]);
},
getOwnerSignature: async (owner, userOp, entryPoint) => {
const userOpHash = await entryPoint.read.getUserOpHash([userOp]);
const signature = await owner.signMessage({
message: {raw: userOpHash},
});
return signature;
},
encodeUserOpExecute: (target, value, data) => {
try {
const executionCalldata = encodePacked(
["address", "uint256", "bytes"],
[target, value, data],
);
const defaultMode = toHex(0, {size: 32});
return encodeFunctionData({
abi: [
getAbiItem({
abi: NEXUS_ARTIFACTS.Nexus.abi,
name: "execute",
}),
],
args: [defaultMode, executionCalldata],
});
} catch (error) {
console.error("Error encoding UserOpExecute:", error);
throw error;
}
},
encodeRuntimeExecute: async (
target,
value,
data,
owner,
accountAddress,
) => {
if (!accountAddress) {
throw new Error("Account address is required");
}
try {
const executionCalldata = encodePacked(
["address", "uint256", "bytes"],
[target, value, data],
);
const defaultMode = toHex(0, {size: 32});
return encodeFunctionData({
abi: [
getAbiItem({
abi: NEXUS_ARTIFACTS.Nexus.abi,
name: "execute",
}),
],
args: [defaultMode, executionCalldata],
});
} catch (error) {
console.error("Error encoding RuntimeExecute:", error);
throw error;
}
},
getDummySignature: (_userOp) => {
return "0x";
},
getInitCode: (salt, ownerAddress) => {
const attesters = [ownerAddress];
const threshold = 1;
return encodePacked(
["address", "bytes"],
[
nexusFactory.address,
encodeFunctionData({
abi: [
getAbiItem({
abi: nexusFactory.abi,
name: "createAccount",
}),
],
args: [ownerAddress, salt, attesters, threshold],
}),
],
);
},
getNonceKey: () => {
return pad(NEXUS_ARTIFACTS.K1Validator.address, {size: 24, dir: "left"});
},
};
}
export const nexus: AccountConfig = {
name: "Nexus",
accountFixture,
};