|
| 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 { entryPointSimulationsAbi } from "../entryPointSimulationsAbi"; |
| 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 hasPaymaster = userOp.paymaster != null && userOp.paymaster !== zeroAddress; |
| 34 | + |
| 35 | + const [simulationResult, simulationResultWithPaymaster] = await Promise.all([ |
| 36 | + simulateHandleOp({ userOp, removePaymaster: hasPaymaster, request }), |
| 37 | + hasPaymaster ? simulateHandleOp({ userOp, request }) : undefined, |
| 38 | + ]); |
| 39 | + |
| 40 | + const gasEstimates = getGasEstimates({ userOp, simulationResult, simulationResultWithPaymaster }); |
| 41 | + |
| 42 | + return formatUserOperationRequest({ |
| 43 | + ...gasEstimates, |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +type SimulateHandleOpOptions = { |
| 48 | + request: EIP1193RequestFn; |
| 49 | + userOp: UserOperation<"0.7">; |
| 50 | + removePaymaster?: boolean; |
| 51 | +}; |
| 52 | + |
| 53 | +type SimulationResult = DecodeFunctionResultReturnType<typeof entryPointSimulationsAbi>; |
| 54 | + |
| 55 | +async function simulateHandleOp({ |
| 56 | + userOp, |
| 57 | + removePaymaster, |
| 58 | + request, |
| 59 | +}: SimulateHandleOpOptions): Promise<SimulationResult> { |
| 60 | + if (removePaymaster) { |
| 61 | + const { |
| 62 | + /* eslint-disable */ |
| 63 | + paymaster, |
| 64 | + paymasterData, |
| 65 | + paymasterPostOpGasLimit, |
| 66 | + paymasterVerificationGasLimit, |
| 67 | + /* eslint-enable */ |
| 68 | + ...userOpWithoutPaymaster |
| 69 | + } = userOp; |
| 70 | + userOp = userOpWithoutPaymaster; |
| 71 | + } |
| 72 | + |
| 73 | + // Prepare user operation for simulation |
| 74 | + const paymasterGasLimits = |
| 75 | + userOp.paymaster && !removePaymaster |
| 76 | + ? { |
| 77 | + paymasterPostOpGasLimit: 2_000_000n, |
| 78 | + paymasterVerificationGasLimit: 5_000_000n, |
| 79 | + } |
| 80 | + : {}; |
| 81 | + const simulationUserOp = { |
| 82 | + ...userOp, |
| 83 | + preVerificationGas: 0n, |
| 84 | + callGasLimit: 10_000_000n, |
| 85 | + verificationGasLimit: 10_000_000n, |
| 86 | + // https://github.com/pimlicolabs/alto/blob/471998695e5ec75ef88dda3f8a534f47c24bcd1a/src/rpc/methods/eth_estimateUserOperationGas.ts#L117 |
| 87 | + maxPriorityFeePerGas: userOp.maxFeePerGas, |
| 88 | + ...paymasterGasLimits, |
| 89 | + } satisfies UserOperation<"0.7">; |
| 90 | + |
| 91 | + const packedUserOp = toPackedUserOperation(simulationUserOp); |
| 92 | + const simulationData = encodeFunctionData({ |
| 93 | + abi: entryPointSimulationsAbi, |
| 94 | + functionName: "simulateHandleOp", |
| 95 | + args: [packedUserOp, zeroAddress, "0x"], |
| 96 | + }); |
| 97 | + |
| 98 | + const senderBalanceOverride = removePaymaster ? { [userOp.sender]: { balance: "0xFFFFFFFFFFFFFFFFFFFF" } } : {}; |
| 99 | + const simulationParams = [ |
| 100 | + { |
| 101 | + to: entryPoint07Address, |
| 102 | + data: simulationData, |
| 103 | + }, |
| 104 | + "pending", |
| 105 | + { |
| 106 | + ...senderBalanceOverride, |
| 107 | + }, |
| 108 | + ]; |
| 109 | + const encodedSimulationResult: Hex = await request({ |
| 110 | + method: "wiresaw_callEntryPointSimulations", |
| 111 | + params: simulationParams, |
| 112 | + }); |
| 113 | + |
| 114 | + return decodeFunctionResult({ |
| 115 | + abi: entryPointSimulationsAbi, |
| 116 | + functionName: "simulateHandleOp", |
| 117 | + data: encodedSimulationResult, |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +type GetGasEstimatesOptions = { |
| 122 | + userOp: UserOperation<"0.7">; |
| 123 | + simulationResult: SimulationResult; |
| 124 | + simulationResultWithPaymaster?: SimulationResult; |
| 125 | +}; |
| 126 | + |
| 127 | +type GasEstimates = { |
| 128 | + verificationGasLimit: bigint; |
| 129 | + callGasLimit: bigint; |
| 130 | + paymasterVerificationGasLimit: bigint; |
| 131 | + paymasterPostOpGasLimit: bigint; |
| 132 | + preVerificationGas: bigint; |
| 133 | +}; |
| 134 | + |
| 135 | +function getGasEstimates({ |
| 136 | + userOp, |
| 137 | + simulationResult, |
| 138 | + simulationResultWithPaymaster, |
| 139 | +}: GetGasEstimatesOptions): GasEstimates { |
| 140 | + const hasPaymaster = simulationResultWithPaymaster != null; |
| 141 | + |
| 142 | + // The verification gas is the total gas available during the validation phase, including the gas used by the paymaster |
| 143 | + const verificationGas = hasPaymaster ? simulationResultWithPaymaster.preOpGas : simulationResult.preOpGas; |
| 144 | + |
| 145 | + // The paymaster verification gas is the difference between verification gas with and without paymaster |
| 146 | + const paymasterVerificationGas = hasPaymaster |
| 147 | + ? simulationResultWithPaymaster.preOpGas - simulationResult.preOpGas |
| 148 | + : 0n; |
| 149 | + |
| 150 | + // The call gas is only the gas used by the user operation, not the paymaster |
| 151 | + const callGas = simulationResult.paid / userOp.maxFeePerGas - simulationResult.preOpGas; |
| 152 | + |
| 153 | + // The paymaster post-op gas is the difference between non-verification gas with and without paymaster |
| 154 | + const paymasterPostOpGas = hasPaymaster |
| 155 | + ? simulationResultWithPaymaster.paid / userOp.maxFeePerGas - simulationResultWithPaymaster.preOpGas - callGas |
| 156 | + : 0n; |
| 157 | + |
| 158 | + // Apply a 2x buffer to the calculated limits |
| 159 | + const verificationGasLimit = verificationGas * 2n; |
| 160 | + const callGasLimit = bigIntMax(callGas * 2n, 9000n); |
| 161 | + const paymasterVerificationGasLimit = paymasterVerificationGas * 2n; |
| 162 | + const paymasterPostOpGasLimit = paymasterPostOpGas * 2n; |
| 163 | + |
| 164 | + return { |
| 165 | + verificationGasLimit, |
| 166 | + callGasLimit, |
| 167 | + paymasterVerificationGasLimit, |
| 168 | + paymasterPostOpGasLimit, |
| 169 | + preVerificationGas: 20_000n, |
| 170 | + }; |
| 171 | +} |
0 commit comments