Skip to content

Commit

Permalink
Use fixed gas for estimating user op gas (when provided) (#226)
Browse files Browse the repository at this point in the history
* Update eth_estimateUserOperationGas

* Enable only for Vanguard and Vanar chains
  • Loading branch information
pavlovdog authored May 29, 2024
1 parent 8038613 commit 22f6f0d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/cli/setupServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const getValidator = ({
gasPriceManager,
parsedArgs["chain-type"],
parsedArgs["entrypoint-simulation-contract"],
parsedArgs["fixed-gas-limit-for-estimation"],
parsedArgs.tenderly,
parsedArgs["balance-override"]
)
Expand All @@ -93,6 +94,7 @@ const getValidator = ({
gasPriceManager,
parsedArgs["chain-type"],
parsedArgs["entrypoint-simulation-contract"],
parsedArgs["fixed-gas-limit-for-estimation"],
parsedArgs.tenderly,
parsedArgs["balance-override"],
parsedArgs["expiration-check"]
Expand Down
36 changes: 24 additions & 12 deletions src/rpc/gasEstimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,24 @@ export async function simulateHandleOpV06(
publicClient: PublicClient,
targetAddress: Address,
targetCallData: Hex,
finalParam: StateOverrides | undefined = undefined
finalParam: StateOverrides | undefined = undefined,
fixedGasLimitForEstimation?: bigint
): Promise<SimulateHandleOpResult> {
try {
await publicClient.request({
method: "eth_call",
// @ts-ignore
params: [
// @ts-ignore
{
to: entryPoint,
data: encodeFunctionData({
abi: EntryPointV06Abi,
functionName: "simulateHandleOp",
args: [userOperation, targetAddress, targetCallData]
})
}),
...(fixedGasLimitForEstimation !== undefined && {
gas: `0x${fixedGasLimitForEstimation.toString(16)}`
}),
},
// @ts-ignore
"latest",
// @ts-ignore
...(finalParam ? [finalParam] : [])
Expand Down Expand Up @@ -198,7 +199,8 @@ async function callPimlicoEntryPointSimulations(
entryPoint: Address,
entryPointSimulationsCallData: Hex[],
entryPointSimulationsAddress: Address,
stateOverride?: StateOverrides
stateOverride?: StateOverrides,
fixedGasLimitForEstimation?: bigint
) {
const callData = encodeFunctionData({
abi: PimlicoEntryPointSimulationsAbi,
Expand All @@ -211,7 +213,10 @@ async function callPimlicoEntryPointSimulations(
params: [
{
to: entryPointSimulationsAddress,
data: callData
data: callData,
...(fixedGasLimitForEstimation !== undefined && {
gas: `0x${fixedGasLimitForEstimation.toString(16)}`
})
},
"latest",
// @ts-ignore
Expand Down Expand Up @@ -382,7 +387,8 @@ export async function simulateHandleOpV07(
publicClient: PublicClient,
entryPointSimulationsAddress: Address,
chainId: number,
finalParam: StateOverrides | undefined = undefined
finalParam: StateOverrides | undefined = undefined,
fixedGasLimitForEstimation?: bigint
): Promise<SimulateHandleOpResult> {
const userOperations = [...queuedUserOperations, userOperation]

Expand Down Expand Up @@ -516,7 +522,8 @@ export async function simulateHandleOpV07(
entryPointSimulationsSimulateTargetCallData
],
entryPointSimulationsAddress,
finalParam
finalParam,
fixedGasLimitForEstimation
)

try {
Expand Down Expand Up @@ -576,7 +583,8 @@ export function simulateHandleOp(
balanceOverrideEnabled: boolean,
chainId: number,
stateOverride: StateOverrides = {},
entryPointSimulationsAddress?: Address
entryPointSimulationsAddress?: Address,
fixedGasLimitForEstimation?: bigint
): Promise<SimulateHandleOpResult> {
let finalStateOverride = undefined

Expand All @@ -596,7 +604,9 @@ export function simulateHandleOp(
publicClient,
targetAddress,
targetCallData,
finalStateOverride
finalStateOverride,
// Enable fixed gas limit for estimation only for Vanguard testnet and Vanar mainnet
chainId === 2040 || chainId ===78600 ? fixedGasLimitForEstimation : undefined
)
}

Expand All @@ -614,6 +624,8 @@ export function simulateHandleOp(
publicClient,
entryPointSimulationsAddress,
chainId,
finalStateOverride
finalStateOverride,
// Enable fixed gas limit for estimation only for Vanguard testnet and Vanar mainnet
chainId === 2040 || chainId ===78600 ? fixedGasLimitForEstimation : undefined
)
}
2 changes: 2 additions & 0 deletions src/rpc/validation/SafeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class SafeValidator
gasPriceManager: GasPriceManager,
chainType: ChainType,
entryPointSimulationsAddress?: Address,
fixedGasLimitForEstimation?: bigint,
usingTenderly = false,
balanceOverrideEnabled = false
) {
Expand All @@ -82,6 +83,7 @@ export class SafeValidator
gasPriceManager,
chainType,
entryPointSimulationsAddress,
fixedGasLimitForEstimation,
usingTenderly,
balanceOverrideEnabled
)
Expand Down
6 changes: 5 additions & 1 deletion src/rpc/validation/UnsafeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class UnsafeValidator implements InterfaceValidator {
chainId: number
gasPriceManager: GasPriceManager
entryPointSimulationsAddress?: Address
fixedGasLimitForEstimation?: bigint
chainType: ChainType

constructor(
Expand All @@ -147,6 +148,7 @@ export class UnsafeValidator implements InterfaceValidator {
gasPriceManager: GasPriceManager,
chainType: ChainType,
entryPointSimulationsAddress?: Address,
fixedGasLimitForEstimation?: bigint,
usingTenderly = false,
balanceOverrideEnabled = false,
expirationCheck = true
Expand All @@ -160,6 +162,7 @@ export class UnsafeValidator implements InterfaceValidator {
this.chainId = publicClient.chain.id
this.gasPriceManager = gasPriceManager
this.entryPointSimulationsAddress = entryPointSimulationsAddress
this.fixedGasLimitForEstimation = fixedGasLimitForEstimation
this.chainType = chainType
}

Expand All @@ -180,7 +183,8 @@ export class UnsafeValidator implements InterfaceValidator {
this.balanceOverrideEnabled,
this.chainId,
stateOverrides,
this.entryPointSimulationsAddress
this.entryPointSimulationsAddress,
this.fixedGasLimitForEstimation
)

if (error.result === "failed") {
Expand Down

0 comments on commit 22f6f0d

Please sign in to comment.