|
1 | 1 | import { decodeErrorResult } from "viem"; |
| 2 | +import type { ThirdwebClient } from "../../../client/client.js"; |
| 3 | +import { getContract } from "../../../contract/contract.js"; |
2 | 4 | import { parseEventLogs } from "../../../event/actions/parse-logs.js"; |
3 | 5 | import { userOperationRevertReasonEvent } from "../../../extensions/erc4337/__generated__/IEntryPoint/events/UserOperationRevertReason.js"; |
4 | 6 | import { postOpRevertReasonEvent } from "../../../extensions/erc4337/__generated__/IEntryPoint_v07/events/PostOpRevertReason.js"; |
| 7 | +import type { PreparedTransaction } from "../../../transaction/prepare-transaction.js"; |
5 | 8 | import type { SerializableTransaction } from "../../../transaction/serialize-transaction.js"; |
6 | 9 | import type { TransactionReceipt } from "../../../transaction/types.js"; |
| 10 | +import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed.js"; |
7 | 11 | import { type Hex, hexToBigInt } from "../../../utils/encoding/hex.js"; |
8 | 12 | import { getClientFetch } from "../../../utils/fetch.js"; |
9 | 13 | import { stringify } from "../../../utils/json.js"; |
| 14 | +import { toEther } from "../../../utils/units.js"; |
| 15 | +import type { Account } from "../../interfaces/wallet.js"; |
| 16 | +import { getEntrypointFromFactory } from "../index.js"; |
10 | 17 | import { |
11 | 18 | type BundlerOptions, |
12 | 19 | type EstimationResult, |
13 | 20 | type GasPriceResult, |
14 | 21 | type PmTransactionData, |
| 22 | + type SmartWalletOptions, |
15 | 23 | type UserOperationReceipt, |
16 | 24 | type UserOperationV06, |
17 | 25 | type UserOperationV07, |
18 | 26 | formatUserOperationReceipt, |
19 | 27 | } from "../types.js"; |
| 28 | +import { predictSmartAccountAddress } from "./calls.js"; |
20 | 29 | import { |
21 | 30 | ENTRYPOINT_ADDRESS_v0_6, |
22 | 31 | MANAGED_ACCOUNT_GAS_BUFFER, |
23 | 32 | getDefaultBundlerUrl, |
24 | 33 | } from "./constants.js"; |
| 34 | +import { prepareUserOp } from "./userop.js"; |
25 | 35 | import { hexlifyUserOp } from "./utils.js"; |
26 | 36 |
|
27 | 37 | /** |
@@ -111,6 +121,91 @@ export async function estimateUserOpGas( |
111 | 121 | }; |
112 | 122 | } |
113 | 123 |
|
| 124 | +/** |
| 125 | + * Estimate the gas cost of a user operation. |
| 126 | + * @param args - The options for estimating the gas cost of a user operation. |
| 127 | + * @returns The estimated gas cost of the user operation. |
| 128 | + * @example |
| 129 | + * ```ts |
| 130 | + * import { estimateUserOpGasCost } from "thirdweb/wallets/smart"; |
| 131 | + * |
| 132 | + * const gasCost = await estimateUserOpGasCost({ |
| 133 | + * transactions, |
| 134 | + * adminAccount, |
| 135 | + * client, |
| 136 | + * smartWalletOptions, |
| 137 | + * }); |
| 138 | + * ``` |
| 139 | + * @walletUtils |
| 140 | + */ |
| 141 | +export async function estimateUserOpGasCost(args: { |
| 142 | + transactions: PreparedTransaction[]; |
| 143 | + adminAccount: Account; |
| 144 | + client: ThirdwebClient; |
| 145 | + smartWalletOptions: SmartWalletOptions; |
| 146 | +}) { |
| 147 | + // if factory is passed, but no entrypoint, try to resolve entrypoint from factory |
| 148 | + if ( |
| 149 | + args.smartWalletOptions.factoryAddress && |
| 150 | + !args.smartWalletOptions.overrides?.entrypointAddress |
| 151 | + ) { |
| 152 | + const entrypointAddress = await getEntrypointFromFactory( |
| 153 | + args.smartWalletOptions.factoryAddress, |
| 154 | + args.client, |
| 155 | + args.smartWalletOptions.chain, |
| 156 | + ); |
| 157 | + if (entrypointAddress) { |
| 158 | + args.smartWalletOptions.overrides = { |
| 159 | + ...args.smartWalletOptions.overrides, |
| 160 | + entrypointAddress, |
| 161 | + }; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + const userOp = await prepareUserOp({ |
| 166 | + transactions: args.transactions, |
| 167 | + adminAccount: args.adminAccount, |
| 168 | + client: args.client, |
| 169 | + smartWalletOptions: args.smartWalletOptions, |
| 170 | + isDeployedOverride: await isContractDeployed( |
| 171 | + getContract({ |
| 172 | + address: await predictSmartAccountAddress({ |
| 173 | + adminAddress: args.adminAccount.address, |
| 174 | + factoryAddress: args.smartWalletOptions.factoryAddress, |
| 175 | + chain: args.smartWalletOptions.chain, |
| 176 | + client: args.client, |
| 177 | + }), |
| 178 | + chain: args.smartWalletOptions.chain, |
| 179 | + client: args.client, |
| 180 | + }), |
| 181 | + ), |
| 182 | + }); |
| 183 | + |
| 184 | + let gasLimit = 0n; |
| 185 | + if ("paymasterVerificationGasLimit" in userOp) { |
| 186 | + // v0.7 |
| 187 | + gasLimit = |
| 188 | + BigInt(userOp.paymasterVerificationGasLimit ?? 0) + |
| 189 | + BigInt(userOp.paymasterPostOpGasLimit ?? 0) + |
| 190 | + BigInt(userOp.verificationGasLimit ?? 0) + |
| 191 | + BigInt(userOp.preVerificationGas ?? 0) + |
| 192 | + BigInt(userOp.callGasLimit ?? 0); |
| 193 | + } else { |
| 194 | + // v0.6 |
| 195 | + gasLimit = |
| 196 | + BigInt(userOp.verificationGasLimit ?? 0) + |
| 197 | + BigInt(userOp.preVerificationGas ?? 0) + |
| 198 | + BigInt(userOp.callGasLimit ?? 0); |
| 199 | + } |
| 200 | + |
| 201 | + const gasCost = gasLimit * (userOp.maxFeePerGas ?? 0n); |
| 202 | + |
| 203 | + return { |
| 204 | + ether: toEther(gasCost), |
| 205 | + wei: gasCost, |
| 206 | + }; |
| 207 | +} |
| 208 | + |
114 | 209 | /** |
115 | 210 | * Get the gas fees of a user operation. |
116 | 211 | * @param args - The options for getting the gas price of a user operation. |
|
0 commit comments