Title
Implement gas estimation for UserOperations
Labels
enhancement, account-abstraction, gas, estimation
Description
Add accurate gas estimation for UserOperations, accounting for all ERC-4337 components including verification, execution, and paymaster gas.
Requirements
UserOperation Gas Components
Estimation Strategies
Gas Optimization
Estimation Middleware
Gas Calculation Formulas
// Pre-verification gas
pub fn calculatePreVerificationGas(
userOp: UserOperation,
bundler_overhead: u256,
) u256 {
const calldata_cost = calcdataGasCost(userOp);
return 21000 + calldata_cost + bundler_overhead;
}
// Calldata gas cost
fn calcdataGasCost(userOp: UserOperation) u256 {
var cost: u256 = 0;
const packed = packUserOp(userOp);
for (packed) |byte| {
cost += if (byte == 0) 4 else 16;
}
return cost;
}
File Structure
src/account_abstraction/
├── gas_estimation.zig # Core gas estimation logic
└── gas_middleware.zig # Middleware for auto-estimation
Example Usage
// Automatic estimation
var userOp = try UserOperationBuilder.init(allocator)
.setSender(account)
.setCallData(data)
.estimateGas(provider, entrypoint) // <-- automatic
.build();
// Manual estimation
const gas_estimate = try estimateUserOperationGas(
provider,
userOp,
entrypoint_address,
);
userOp.callGasLimit = gas_estimate.callGasLimit;
userOp.verificationGasLimit = gas_estimate.verificationGasLimit;
userOp.preVerificationGas = gas_estimate.preVerificationGas;
References
Title
Implement gas estimation for UserOperations
Labels
enhancement,account-abstraction,gas,estimationDescription
Add accurate gas estimation for UserOperations, accounting for all ERC-4337 components including verification, execution, and paymaster gas.
Requirements
UserOperation Gas Components
Estimate
callGasLimit:Estimate
verificationGasLimit:Calculate
preVerificationGas:21000 + calldataGas + bundlerOverheadEstimate paymaster gas (if used):
paymasterVerificationGasLimitpaymasterPostOpGasLimitEstimation Strategies
Via Bundler RPC:
eth_estimateUserOperationGasLocal Simulation:
eth_callto EntryPointHeuristic-based:
Gas Optimization
optimizeUserOpGas(userOp: *UserOperation)- Reduce gas where safeEstimation Middleware
UserOpGasMiddleware:Gas Calculation Formulas
File Structure
Example Usage
References