generated from SpaceStation09/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userOperation.ts
106 lines (100 loc) · 3 KB
/
userOperation.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
/*
* @Description:
* @Version: 1.0
* @Autor: [email protected]
* @Date: 2022-07-25 10:53:52
* @LastEditors: Chen
* @LastEditTime: 2022-12-01
*/
import { BytesLike } from "@ethersproject/bytes";
import { BigNumber, BigNumberish } from "ethers";
/**
* @link https://github.com/eth-infinitism/account-abstraction/blob/develop/contracts/UserOperation.sol
*/
class UserOperation {
/**
* @param sender the sender account of this request
*/
sender: string = "";
/**
* @param nonce unique value the sender uses to verify it is not a replay.
*/
nonce: BigNumberish = 0;
/**
* @param initCode if set, the account contract will be created by this constructor
*/
initCode: BytesLike = "0x";
/**
* @param callData the method call to execute on this account.
*/
callData: BytesLike = "0x";
/**
* @param callGas gas used for validateUserOp and validatePaymasterUserOp
*/
callGas: BigNumberish = 0;
/**
* @param verificationGas gas not calculated by the handleOps method, but added to the gas paid. Covers batch overhead.
*/
verificationGas: BigNumberish = 0;
/**
* @param preVerificationGas gas not calculated by the handleOps method, but added to the gas paid. Covers batch overhead.
*/
preVerificationGas: BigNumberish = 21000;
/**
* @param maxFeePerGas same as EIP-1559 gas parameter
*/
maxFeePerGas: BigNumberish = 0;
/**
* @param maxPriorityFeePerGas same as EIP-1559 gas parameter
*/
maxPriorityFeePerGas: BigNumberish = 0;
/**
* @param paymaster if set, the paymaster will pay for the transaction instead of the sender
*/
paymaster: string = "0x";
/**
* @param paymasterData extra data used by the paymaster for validation
*/
paymasterData: BytesLike = "0x";
/**
* @param signature sender-verified signature over the entire request, the EntryPoint address and the chain ID.
*/
signature: BytesLike = "0x";
/**
* update verificationGas
*/
private estimateVerificationGas() {
//100000 default verification gas. will add create2 cost (3200+200*length) if initCode exists
this.verificationGas = 100000;
if (this.initCode.length > 0) {
this.verificationGas += 3200 + 200 * this.initCode.length;
}
}
/**
* update callGas
* @param web3 web3 instance
* @param entryPointAddress entry point address
*/
private async estimateCallGas(provider: any, entryPointAddress: string) {
const calldata: string = this.callData as string;
const estimatedGas =
(
await provider.estimateGas({
from: entryPointAddress,
to: this.sender,
data: calldata,
})
).toNumber() * 1.5;
this.callGas = BigNumber.from(Math.floor(estimatedGas));
}
/**
* update Gas
* @param web3 web3 instance
* @param entryPointAddress entry point address
*/
public async estimateGas(provider: any, entryPointAddress: string) {
this.estimateVerificationGas();
await this.estimateCallGas(provider, entryPointAddress);
}
}
export { UserOperation };