|
| 1 | +import { |
| 2 | + BundlerRpcSchema, |
| 3 | + decodeFunctionResult, |
| 4 | + DecodeFunctionResultReturnType, |
| 5 | + EIP1193RequestFn, |
| 6 | + encodeFunctionData, |
| 7 | + Hex, |
| 8 | + zeroAddress, |
| 9 | +} from "viem"; |
| 10 | +import { getRpcMethod } from "../common"; |
| 11 | +import { |
| 12 | + entryPoint07Address, |
| 13 | + formatUserOperation, |
| 14 | + formatUserOperationRequest, |
| 15 | + toPackedUserOperation, |
| 16 | + UserOperation, |
| 17 | +} from "viem/account-abstraction"; |
| 18 | +import { bigIntMax } from "../../utils"; |
| 19 | +import { entryPointGasSimulationsAbi } from "../entryPointGasSimulations"; |
| 20 | + |
| 21 | +type rpcMethod = getRpcMethod<BundlerRpcSchema, "eth_estimateUserOperationGas">; |
| 22 | + |
| 23 | +type EstimateUserOperationGasOptions = { |
| 24 | + request: EIP1193RequestFn; |
| 25 | + params: rpcMethod["Parameters"]; |
| 26 | +}; |
| 27 | + |
| 28 | +export async function estimateUserOperationGas({ |
| 29 | + request, |
| 30 | + params, |
| 31 | +}: EstimateUserOperationGasOptions): Promise<rpcMethod["ReturnType"]> { |
| 32 | + const userOp = formatUserOperation(params[0]); |
| 33 | + const gasSimulation = await simulateGas({ userOp, request }); |
| 34 | + const gasLimits = { |
| 35 | + verificationGasLimit: gasSimulation.verificationGas * 2n, |
| 36 | + callGasLimit: bigIntMax(gasSimulation.callGas * 2n, 9000n), |
| 37 | + paymasterVerificationGasLimit: gasSimulation.paymasterVerificationGas * 2n, |
| 38 | + paymasterPostOpGasLimit: gasSimulation.paymasterPostOpGas * 2n, |
| 39 | + preVerificationGas: 20_000n, |
| 40 | + }; |
| 41 | + |
| 42 | + return formatUserOperationRequest({ |
| 43 | + ...gasLimits, |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +type SimulateGasOptions = { |
| 48 | + request: EIP1193RequestFn; |
| 49 | + userOp: UserOperation<"0.7">; |
| 50 | +}; |
| 51 | + |
| 52 | +type SimulateGasResult = DecodeFunctionResultReturnType<typeof entryPointGasSimulationsAbi>; |
| 53 | + |
| 54 | +async function simulateGas({ request, userOp }: SimulateGasOptions): Promise<SimulateGasResult> { |
| 55 | + // Prepare user operation for simulation |
| 56 | + const simulationUserOp = { |
| 57 | + ...userOp, |
| 58 | + preVerificationGas: 0n, |
| 59 | + callGasLimit: 10_000_000n, |
| 60 | + verificationGasLimit: 10_000_000n, |
| 61 | + // https://github.com/pimlicolabs/alto/blob/471998695e5ec75ef88dda3f8a534f47c24bcd1a/src/rpc/methods/eth_estimateUserOperationGas.ts#L117 |
| 62 | + maxPriorityFeePerGas: userOp.maxFeePerGas, |
| 63 | + paymasterPostOpGasLimit: 2_000_000n, |
| 64 | + paymasterVerificationGasLimit: 5_000_000n, |
| 65 | + } satisfies UserOperation<"0.7">; |
| 66 | + |
| 67 | + const packedUserOp = toPackedUserOperation(simulationUserOp); |
| 68 | + const simulationData = encodeFunctionData({ |
| 69 | + abi: entryPointGasSimulationsAbi, |
| 70 | + functionName: "estimateGas", |
| 71 | + args: [packedUserOp], |
| 72 | + }); |
| 73 | + |
| 74 | + const hasPaymaster = userOp.paymaster != null && userOp.paymaster !== zeroAddress; |
| 75 | + const senderBalanceOverride = hasPaymaster ? {} : { [userOp.sender]: { balance: "0xFFFFFFFFFFFFFFFFFFFF" } }; |
| 76 | + const simulationParams = [ |
| 77 | + { |
| 78 | + to: entryPoint07Address, |
| 79 | + data: simulationData, |
| 80 | + }, |
| 81 | + "pending", |
| 82 | + { |
| 83 | + ...senderBalanceOverride, |
| 84 | + }, |
| 85 | + ]; |
| 86 | + const encodedSimulationResult: Hex = await request({ |
| 87 | + method: "wiresaw_callEntryPointSimulations", |
| 88 | + params: simulationParams, |
| 89 | + }); |
| 90 | + |
| 91 | + return decodeFunctionResult({ |
| 92 | + abi: entryPointGasSimulationsAbi, |
| 93 | + functionName: "estimateGas", |
| 94 | + data: encodedSimulationResult, |
| 95 | + }); |
| 96 | +} |
0 commit comments