-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
368 lines (319 loc) · 9.84 KB
/
index.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import { ethers } from "ethers";
import EntryPointABI from "./abi/EntryPoint.json";
import SimpleAccountABI from "./abi/SimpleAccount.json";
import VerifyingPaymasterABI from "./abi/VerifyingPaymaster.json";
import SimpleAccountFactoryABI from "./abi/SimpleAccountFactory.json";
import ERC20VerifyingPaymasterABI from "./abi/ERC20VerifyingPaymaster.json";
import {
EntryPointAddress,
OracleAggregator,
MUMBAI_URL,
SimpleAccountFactoryAddress,
VerifyingPaymasterAddress,
ERC20VerifierAddress,
CoreTokenAddress,
GAS_FETCH_PRV,
} from "./data";
import { paymasterCall, ERC20paymasterCall } from "./api/paymasterHandler";
const getSCWallet = async (address) => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(
SimpleAccountFactoryAddress,
SimpleAccountFactoryABI,
provider
);
const wallet = await contract.createdAccounts(address, 0);
return wallet;
};
const getCurrnetNonce = async (address) => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(
EntryPointAddress,
EntryPointABI,
provider
);
const _nonce = await contract.getNonce(address, 0);
const nonce = ethers.utils.hexValue(_nonce);
return nonce;
};
const getUserOperation = async (
SCWAddress,
callContract,
minTx,
PaymasterRPC_URL,
PIMLICO_URL
) => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const simpleAccount = new ethers.Contract(
SCWAddress,
SimpleAccountABI,
provider
);
const NONCE = await getCurrnetNonce(SCWAddress);
const userOperation = {
sender: SCWAddress,
nonce: NONCE,
initCode: "0x",
callData: simpleAccount.interface.encodeFunctionData("execute", [
callContract,
0,
minTx.data,
]),
callGasLimit: 0,
verificationGasLimit: 0,
preVerificationGas: 0,
maxFeePerGas: 70_000_000_000,
maxPriorityFeePerGas: 70_000_000_000,
paymasterAndData: "0x",
signature: "0x",
};
if (SCWAddress == ethers.constants.AddressZero) {
const signer = provider.getSigner();
const address = await signer.getAddress();
const res = await getInitCode(address);
userOperation.initCode = res[0];
userOperation.sender = res[1];
}
const userOpWithPaymasterAndData = await getPaymasterAndData(
userOperation,
PaymasterRPC_URL
);
userOperation.paymasterAndData = userOpWithPaymasterAndData.paymasterAndData;
const res = await fetchGasValues(userOperation, PIMLICO_URL);
userOperation.callGasLimit = res[0].toNumber();
userOperation.verificationGasLimit = res[1].toNumber() + 1000;
userOperation.preVerificationGas = res[2].toNumber();
return userOperation;
};
function getTxTimeLimit() {
const currentTime = Math.floor(Date.now() / 1000); // mili to sec
const tenMinutesLater = currentTime;
return [currentTime + 3600, tenMinutesLater];
}
async function getPaymasterAndData(userOperation, PaymasterRPC_URL) {
const timeLimit = getTxTimeLimit();
const signedPaymasterHash = await getSignedPaymasterHash(
userOperation,
PaymasterRPC_URL
);
let paymasterAndData = ethers.utils.concat([
VerifyingPaymasterAddress,
ethers.utils.defaultAbiCoder.encode(
["uint48", "uint48"],
[timeLimit[0], timeLimit[1]]
),
signedPaymasterHash,
]);
paymasterAndData = ethers.utils.hexlify(paymasterAndData);
userOperation.paymasterAndData = paymasterAndData;
return userOperation;
}
async function getERC20PaymasterAndData(userOperation, PaymasterRPC_URL) {
const timeLimit = getTxTimeLimit();
const paymasterSignature = await getSignedERC20PaymasterHash(
userOperation,
timeLimit,
PaymasterRPC_URL
);
const priceSource = 1;
const exchangeRate = ethers.utils.parseUnits("1", 16);
const priceMarkup = 1e6 + 1e4;
const priceSourceBytes = numberToHexString(priceSource);
let paymasterAndData = ethers.utils.concat([
ERC20VerifierAddress,
priceSourceBytes,
ethers.utils.defaultAbiCoder.encode(
["uint48", "uint48", "address", "address", "uint256", "uint32"],
[
timeLimit[0],
timeLimit[1],
CoreTokenAddress,
OracleAggregator,
exchangeRate,
priceMarkup,
]
),
paymasterSignature,
]);
userOperation.paymasterAndData = ethers.utils.hexlify(paymasterAndData);
return userOperation;
}
const fetchGasValues = async (userOperation, PIMLICO_URL) => {
const tempProvider = new ethers.providers.JsonRpcProvider(MUMBAI_URL);
const tempWallet = new ethers.Wallet(GAS_FETCH_PRV, tempProvider);
const EntryPoint = new ethers.Contract(
EntryPointAddress,
EntryPointABI,
tempWallet
);
const finalUserOpHash = await EntryPoint.getUserOpHash(userOperation);
const finalUserOpSig = await tempWallet.signMessage(
ethers.utils.arrayify(finalUserOpHash)
);
userOperation.signature = finalUserOpSig;
const customProvider = new CustomJsonRpcProvider(PIMLICO_URL);
const respond = await customProvider.getUserOperationGasEstimate(
userOperation
);
const callGasLimit = ethers.BigNumber.from(respond.callGasLimit);
const verificationGasLimit = ethers.BigNumber.from(
respond.verificationGasLimit
);
const preVerificationGas = ethers.BigNumber.from(respond.preVerificationGas);
return [callGasLimit, verificationGasLimit, preVerificationGas];
};
function numberToHexString(number) {
if (number < 0) {
throw new Error("Number must be positive");
}
let hexString = number.toString(16);
// Ensure even number of characters (pad with a leading zero if necessary)
if (hexString.length % 2 !== 0) {
hexString = "0" + hexString;
}
return "0x" + hexString;
}
const getSignedPaymasterHash = async (userOp, PaymasterRPC_URL) => {
const provider = new ethers.providers.JsonRpcProvider(MUMBAI_URL);
const paymasterContract = new ethers.Contract(
VerifyingPaymasterAddress,
VerifyingPaymasterABI,
provider
);
const timeLimit = getTxTimeLimit();
const paymasterHash = await paymasterContract.getHash(
userOp,
timeLimit[0],
timeLimit[1]
);
const paymasterSignature = await paymasterCall(
paymasterHash,
PaymasterRPC_URL
);
return paymasterSignature;
};
const getSignedERC20PaymasterHash = async (
userOp,
timeLimit,
ERC20_PaymasterRPC_URL
) => {
const provider = new ethers.providers.JsonRpcProvider(MUMBAI_URL);
const paymasterContract = new ethers.Contract(
ERC20VerifierAddress,
ERC20VerifyingPaymasterABI,
provider
);
const paymasterHash = await paymasterContract.getHash(
userOp,
1,
timeLimit[0],
timeLimit[1],
CoreTokenAddress,
OracleAggregator,
ethers.utils.parseUnits("1", 16),
1e6 + 1e4
);
const paymasterSignature = await ERC20paymasterCall(
paymasterHash,
ERC20_PaymasterRPC_URL
);
return paymasterSignature;
};
const getInitCode = async (address) => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(
SimpleAccountFactoryAddress,
SimpleAccountFactoryABI,
provider
);
const initCode = ethers.utils.hexConcat([
SimpleAccountFactoryAddress,
contract.interface.encodeFunctionData("createAccount", [address, 0]),
]);
// const initCode = "0x";
// await contract.createAccount(address,0);
const _SCWAddress = await contract.getAddress(address, 0);
console.log("SCWAddress : ", _SCWAddress);
return [initCode, _SCWAddress];
};
function convertBigIntToString(obj) {
for (const key in obj) {
if (typeof obj[key] === "bigint") {
obj[key] = obj[key].toString();
} else if (typeof obj[key] === "object" && obj[key] !== null) {
convertBigIntToString(obj[key]);
}
}
}
class CustomJsonRpcProvider extends ethers.providers.JsonRpcProvider {
async sendUserOperation(userOperation) {
const method = "eth_sendUserOperation";
// Convert BigInt to string
convertBigIntToString(userOperation);
const params = [userOperation, EntryPointAddress];
return this.send(method, params);
}
async getTxHashByUserOp(userOpHash) {
const method = "eth_getUserOperationReceipt";
// Convert BigInt to string
const params = [userOpHash];
return this.send(method, params);
}
async getUserOperationGasEstimate(userOperation) {
const method = "eth_estimateUserOperationGas";
const params = [userOperation, EntryPointAddress];
return this.send(method, params);
}
}
async function waitForReceipt(
customProvider,
userOpHash,
interval = 1000,
timeout = 60000
) {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
try {
const res = await customProvider.getTxHashByUserOp(userOpHash);
if (res) return res.receipt.transactionHash;
} catch (error) {
// Optionally handle specific errors
}
await new Promise((resolve) => setTimeout(resolve, interval));
}
throw new Error("Transaction res not found within timeout period");
}
async function getSignedUserOp(userOperation, PaymasterRPC_URL, flag) {
if (flag) {
console.log("using paymaster...");
userOperation = await getPaymasterAndData(userOperation, PaymasterRPC_URL);
} else {
console.log("using ERC20 paymaster...");
userOperation = await getERC20PaymasterAndData(
userOperation,
PaymasterRPC_URL
);
}
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const EntryPoint = new ethers.Contract(
EntryPointAddress,
EntryPointABI,
signer
);
const finalUserOpHash = await EntryPoint.getUserOpHash(userOperation);
const finalUserOpSig = await signer.signMessage(
ethers.utils.arrayify(finalUserOpHash)
);
userOperation.signature = finalUserOpSig;
return userOperation;
}
export {
getERC20PaymasterAndData,
numberToHexString,
getSCWallet,
getUserOperation,
CustomJsonRpcProvider,
waitForReceipt,
getSignedUserOp,
};