-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgasless.js
92 lines (75 loc) · 2.81 KB
/
gasless.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
/** @format */
const hre = require('hardhat');
const FACTORY_ADDRESS = '0x16feE653f3E79BAE1F6019765ba993a48636bc34';
const EP_ADDRESS = '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789';
const PM_ADDRESS = '0x69032Afff72CEbF8700050fA8840f2e10944e43B';
const NFT_ADDRESS = '0x96BD0c7aC2784D294DCeF91Ba4C85dDB5BcDb918';
async function main() {
const entryPoint = await hre.ethers.getContractAt('EntryPoint', EP_ADDRESS);
const AccountFactory = await hre.ethers.getContractFactory('AccountFactory');
// const NFT = await hre.ethers.getContractFactory("EGG", NFTAddress);
const [signer] = await hre.ethers.getSigners();
let initCode =
FACTORY_ADDRESS +
AccountFactory.interface
.encodeFunctionData('createAccount', [signer.address])
.slice(2);
let sender;
try {
await entryPoint.getSenderAddress(initCode);
} catch (ex) {
sender = '0x' + ex.data.slice(-40);
}
const code = await ethers.provider.getCode(sender);
if (code !== '0x') {
initCode = '0x';
}
// await entryPoint.depositTo(PM_ADDRESS, {
// value: hre.ethers.parseEther('0.5'),
// });
console.log({ sender });
const Account = await hre.ethers.getContractFactory('Account');
const NFT = await hre.ethers.getContractFactory('NFT');
const userOp = {
sender, // smart account address
nonce: '0x' + (await entryPoint.getNonce(sender, 0)).toString(16),
initCode,
callData: Account.interface.encodeFunctionData('mintNft'),
paymasterAndData: PM_ADDRESS,
signature:
'0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c',
};
const { preVerificationGas, verificationGasLimit, callGasLimit } =
await ethers.provider.send('eth_estimateUserOperationGas', [
userOp,
EP_ADDRESS,
]);
userOp.preVerificationGas = preVerificationGas;
userOp.verificationGasLimit = verificationGasLimit;
userOp.callGasLimit = callGasLimit;
const { maxFeePerGas } = await ethers.provider.getFeeData();
console.log({ maxFeePerGas });
const totalFee = maxFeePerGas;
userOp.maxFeePerGas = '0x' + totalFee.toString(16);
const maxPriorityFeePerGas = await ethers.provider.send(
'rundler_maxPriorityFeePerGas'
);
userOp.maxPriorityFeePerGas = maxPriorityFeePerGas;
const userOpHash = await entryPoint.getUserOpHash(userOp);
userOp.signature = await signer.signMessage(hre.ethers.getBytes(userOpHash));
const opHash = await ethers.provider.send('eth_sendUserOperation', [
userOp,
EP_ADDRESS,
]);
setTimeout(async () => {
const { transactionHash } = await ethers.provider.send(
'eth_getUserOperationByHash',
[opHash]
);
console.log(`https://amoy.polygonscan.com/tx/${transactionHash}`);
}, 10000);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});