Skip to content

Commit

Permalink
Revert "Fix/improve gas estimates (#399)" (#400)
Browse files Browse the repository at this point in the history
This reverts commit fd41218.
  • Loading branch information
mouseless0x authored Jan 10, 2025
1 parent 22a87a2 commit 87ac982
Show file tree
Hide file tree
Showing 7 changed files with 2,588 additions and 2,877 deletions.
2 changes: 1 addition & 1 deletion src/cli/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export const gasEstimationOptions: CliCommandOptions<IGasEstimationArgsInput> =
"Added to the initial minimum gas to determine the upper bound of the binary search",
type: "string",
require: false,
default: "30000000"
default: "1000000"
},
"call-gas-limit-multiplier": {
description:
Expand Down
18 changes: 7 additions & 11 deletions src/cli/deploySimulationsContract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DETERMINISTIC_DEPLOYER_TRANSACTION,
pimlicoEntrypointSimulationsDeployBytecode,
pimlicoEntrypointSimulationsSalt
ENTRY_POINT_SIMULATIONS_CREATECALL,
PimlicoEntryPointSimulationsDeployBytecode
} from "@alto/types"
import {
type Chain,
Expand All @@ -10,8 +10,7 @@ import {
type Hex,
http,
type PublicClient,
type Transport,
concat
type Transport
} from "viem"
import type { CamelCasedProperties } from "./parseArgs"
import type { IOptions } from "@alto/cli"
Expand Down Expand Up @@ -73,9 +72,9 @@ export const deploySimulationsContract = async ({

const contractAddress = getContractAddress({
opcode: "CREATE2",
bytecode: pimlicoEntrypointSimulationsDeployBytecode,
salt: pimlicoEntrypointSimulationsSalt,
from: args.deterministicDeployerAddress
bytecode: PimlicoEntryPointSimulationsDeployBytecode,
from: args.deterministicDeployerAddress,
salt: "0x3132333400000000000000000000000000000000000000000000000000000000" as Hex
})

if (await isContractDeployed({ publicClient, address: contractAddress })) {
Expand All @@ -85,10 +84,7 @@ export const deploySimulationsContract = async ({
const deployHash = await walletClient.sendTransaction({
chain: publicClient.chain,
to: args.deterministicDeployerAddress,
data: concat([
pimlicoEntrypointSimulationsSalt,
pimlicoEntrypointSimulationsDeployBytecode
])
data: ENTRY_POINT_SIMULATIONS_CREATECALL
})

await publicClient.waitForTransactionReceipt({
Expand Down
107 changes: 58 additions & 49 deletions src/rpc/estimation/gasEstimationsV07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class GasEstimatorV07 {
functionName:
| "binarySearchPaymasterVerificationGasLimit"
| "binarySearchVerificationGasLimit"
| "binarySearchCallGasLimit"
| "simulateCallData"
}): Hex {
const queuedOps = queuedUserOperations.map((op) => ({
op: toPackedUserOperation(op),
Expand Down Expand Up @@ -215,7 +215,7 @@ export class GasEstimatorV07 {
functionName:
| "binarySearchPaymasterVerificationGasLimit"
| "binarySearchVerificationGasLimit"
| "binarySearchCallGasLimit"
| "simulateCallData"
stateOverrides?: StateOverrides | undefined
authorizationList?: SignedAuthorizationList
}): Promise<SimulateBinarySearchRetryResult> {
Expand All @@ -228,7 +228,7 @@ export class GasEstimatorV07 {
// OptimalGas represents the current lowest gasLimit, so we set the gasAllowance to search range minGas <-> optimalGas
const gasAllowance = currentOptimalGas - currentMinGas

const binarySearchCallGasLimit = this.encodeBinarySearchGasLimit({
const simulateCallData = this.encodeBinarySearchGasLimit({
entryPoint,
userOperation: targetOp,
target,
Expand All @@ -241,7 +241,7 @@ export class GasEstimatorV07 {

let cause = await this.callPimlicoEntryPointSimulations({
entryPoint,
entryPointSimulationsCallData: [binarySearchCallGasLimit],
entryPointSimulationsCallData: [simulateCallData],
stateOverrides,
authorizationList
})
Expand All @@ -258,26 +258,26 @@ export class GasEstimatorV07 {
return decodedDelegateAndError.args[1] as Hex
})

const callGasLimitResult = validateBinarySearchDataResult(
const simulateCallDataResult = validateBinarySearchDataResult(
cause[0],
functionName
)

if (callGasLimitResult.result === "failed") {
return callGasLimitResult
if (simulateCallDataResult.result === "failed") {
return simulateCallDataResult
}

if (callGasLimitResult.result === "retry") {
currentOptimalGas = callGasLimitResult.optimalGas
currentMinGas = callGasLimitResult.minGas
if (simulateCallDataResult.result === "retry") {
currentOptimalGas = simulateCallDataResult.optimalGas
currentMinGas = simulateCallDataResult.minGas
retryCount++
continue
}

// If we reach here, it means we have a successful result
return {
result: "success",
data: callGasLimitResult.data
data: simulateCallDataResult.data
}
}

Expand Down Expand Up @@ -310,7 +310,6 @@ export class GasEstimatorV07 {

const binarySearchVerificationGasLimit =
this.encodeBinarySearchGasLimit({
initialMinGas: 9_000n,
entryPoint,
userOperation,
queuedUserOperations,
Expand All @@ -322,7 +321,6 @@ export class GasEstimatorV07 {
const binarySearchPaymasterVerificationGasLimit =
userOperation.paymaster
? this.encodeBinarySearchGasLimit({
initialMinGas: 9_000n,
entryPoint,
userOperation,
queuedUserOperations,
Expand All @@ -332,8 +330,7 @@ export class GasEstimatorV07 {
})
: null

const binarySearchCallGasLimit = this.encodeBinarySearchGasLimit({
initialMinGas: 9_000n,
const simulateCallData = this.encodeBinarySearchGasLimit({
entryPoint,
userOperation,
queuedUserOperations,
Expand All @@ -342,7 +339,7 @@ export class GasEstimatorV07 {
op: userOperation,
entryPoint
}),
functionName: "binarySearchCallGasLimit"
functionName: "simulateCallData"
})

let cause: readonly [Hex, Hex, Hex | null, Hex]
Expand All @@ -353,7 +350,7 @@ export class GasEstimatorV07 {
simulateHandleOpLastCause,
binarySearchVerificationGasLimitCause,
binarySearchPaymasterVerificationGasLimitCause,
binarySearchCallGasLimitCause
simulateCallDataCause
] = await Promise.all([
this.callPimlicoEntryPointSimulations({
entryPoint,
Expand Down Expand Up @@ -381,7 +378,7 @@ export class GasEstimatorV07 {
: null,
this.callPimlicoEntryPointSimulations({
entryPoint,
entryPointSimulationsCallData: [binarySearchCallGasLimit],
entryPointSimulationsCallData: [simulateCallData],
stateOverrides,
authorizationList
})
Expand All @@ -391,7 +388,7 @@ export class GasEstimatorV07 {
simulateHandleOpLastCause[0],
binarySearchVerificationGasLimitCause[0],
binarySearchPaymasterVerificationGasLimitCause?.[0] ?? null,
binarySearchCallGasLimitCause[0]
simulateCallDataCause[0]
]
} else {
const [
Expand Down Expand Up @@ -420,7 +417,7 @@ export class GasEstimatorV07 {
}),
await this.callPimlicoEntryPointSimulations({
entryPoint,
entryPointSimulationsCallData: [binarySearchCallGasLimit],
entryPointSimulationsCallData: [simulateCallData],
stateOverrides,
authorizationList
})
Expand Down Expand Up @@ -459,7 +456,7 @@ export class GasEstimatorV07 {
simulateHandleOpLastCause,
binarySearchVerificationGasLimitCause,
binarySearchPaymasterVerificationGasLimitCause,
binarySearchCallGasLimitCause
simulateCallDataCause
] = cause

try {
Expand All @@ -471,23 +468,26 @@ export class GasEstimatorV07 {
return simulateHandleOpLastResult as SimulateHandleOpResult<"failed">
}

const verificationGasLimitResult = validateBinarySearchDataResult(
binarySearchVerificationGasLimitCause,
"binarySearchVerificationGasLimit"
)
const binarySearchVerificationGasLimitResult =
validateBinarySearchDataResult(
binarySearchVerificationGasLimitCause,
"binarySearchVerificationGasLimit"
)

let verificationGasLimit = 0n

if (verificationGasLimitResult.result === "success") {
verificationGasLimit = verificationGasLimitResult.data.gasUsed
if (binarySearchVerificationGasLimitResult.result === "success") {
verificationGasLimit =
binarySearchVerificationGasLimitResult.data.gasUsed
}

if (verificationGasLimitResult.result === "failed") {
return verificationGasLimitResult
if (binarySearchVerificationGasLimitResult.result === "failed") {
return binarySearchVerificationGasLimitResult
}

if (verificationGasLimitResult.result === "retry") {
const { optimalGas, minGas } = verificationGasLimitResult
if (binarySearchVerificationGasLimitResult.result === "retry") {
const { optimalGas, minGas } =
binarySearchVerificationGasLimitResult
const binarySearchResult = await this.retryBinarySearch({
entryPoint,
optimalGas,
Expand All @@ -509,7 +509,7 @@ export class GasEstimatorV07 {
).data.gasUsed
}

const paymasterVerificationGasLimitResult =
const binarySearchPaymasterVerificationGasLimitResult =
binarySearchPaymasterVerificationGasLimitCause
? validateBinarySearchDataResult(
binarySearchPaymasterVerificationGasLimitCause,
Expand All @@ -526,18 +526,27 @@ export class GasEstimatorV07 {

let paymasterVerificationGasLimit = 0n

if (paymasterVerificationGasLimitResult.result === "success") {
if (
binarySearchPaymasterVerificationGasLimitResult.result ===
"success"
) {
paymasterVerificationGasLimit =
paymasterVerificationGasLimitResult.data.gasUsed
binarySearchPaymasterVerificationGasLimitResult.data.gasUsed
}

if (paymasterVerificationGasLimitResult.result === "failed") {
return paymasterVerificationGasLimitResult
if (
binarySearchPaymasterVerificationGasLimitResult.result ===
"failed"
) {
return binarySearchPaymasterVerificationGasLimitResult
}

if (paymasterVerificationGasLimitResult.result === "retry") {
if (
binarySearchPaymasterVerificationGasLimitResult.result ===
"retry"
) {
const { optimalGas, minGas } =
paymasterVerificationGasLimitResult
binarySearchPaymasterVerificationGasLimitResult
const binarySearchResult = await this.retryBinarySearch({
entryPoint,
optimalGas,
Expand All @@ -559,22 +568,22 @@ export class GasEstimatorV07 {
).data.gasUsed
}

const callGasLimitResult = validateBinarySearchDataResult(
binarySearchCallGasLimitCause,
"binarySearchCallGasLimit"
const simulateCallDataResult = validateBinarySearchDataResult(
simulateCallDataCause,
"simulateCallData"
)

let callGasLimit = 0n

if (callGasLimitResult.result === "success") {
callGasLimit = callGasLimitResult.data.gasUsed
if (simulateCallDataResult.result === "success") {
callGasLimit = simulateCallDataResult.data.gasUsed
}
if (callGasLimitResult.result === "failed") {
return callGasLimitResult
if (simulateCallDataResult.result === "failed") {
return simulateCallDataResult
}

if (callGasLimitResult.result === "retry") {
const { optimalGas, minGas } = callGasLimitResult
if (simulateCallDataResult.result === "retry") {
const { optimalGas, minGas } = simulateCallDataResult
const binarySearchResult = await this.retryBinarySearch({
entryPoint,
optimalGas,
Expand All @@ -585,7 +594,7 @@ export class GasEstimatorV07 {
op: userOperation,
entryPoint
}),
functionName: "binarySearchCallGasLimit",
functionName: "simulateCallData",
queuedOps: queuedUserOperations,
stateOverrides
})
Expand Down Expand Up @@ -805,7 +814,7 @@ export function getSimulateValidationResult(errorData: Hex): {
function validateBinarySearchDataResult(
data: Hex,
fnName:
| "binarySearchCallGasLimit"
| "simulateCallData"
| "binarySearchVerificationGasLimit"
| "binarySearchPaymasterVerificationGasLimit"
):
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/rpcHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class RpcHandler implements IRpcEndpoint {
entryPoint: Address,
stateOverrides?: StateOverrides
): Promise<EstimateUserOperationGasResponseResult> {
return await this.estimateGas({
return await this.doEstimateGas({
apiVersion,
userOperation,
entryPoint,
Expand Down Expand Up @@ -799,7 +799,7 @@ export class RpcHandler implements IRpcEndpoint {
)
}

return await this.estimateGas({
return await this.doEstimateGas({
apiVersion,
userOperation,
authorization,
Expand Down Expand Up @@ -1054,7 +1054,7 @@ export class RpcHandler implements IRpcEndpoint {
return currentNonceValue
}

async estimateGas({
async doEstimateGas({
apiVersion,
userOperation,
entryPoint,
Expand Down
Loading

0 comments on commit 87ac982

Please sign in to comment.