Skip to content

Commit

Permalink
Fix pre-verification gas (#43)
Browse files Browse the repository at this point in the history
* No need to have complete userOp succeed

* Revert tenderly change

* generate predective bytes

* don't depend on user's input

* Add static l2 fee

* use gasPrice instead of estimateMaxGaxPrice
  • Loading branch information
plusminushalf authored Nov 23, 2023
1 parent b9dd097 commit 11f5c24
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/rpc/src/rpcHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ export class RpcHandler implements IRpcEndpoint {
this.publicClient,
userOperation,
entryPoint,
preVerificationGas
preVerificationGas,
this.logger
)
} else if (this.chainId === chains.arbitrum.id) {
preVerificationGas = await calcArbitrumPreVerificationGas(
Expand Down
35 changes: 23 additions & 12 deletions packages/utils/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
toBytes,
toHex
} from "viem"
import { getGasPrice, Logger } from "."

export interface GasOverheads {
/**
Expand Down Expand Up @@ -195,13 +196,18 @@ const getL1FeeAbi = [
] as const

export async function calcOptimismPreVerificationGas(
publicClient: PublicClient<Transport, Chain | undefined>,
publicClient: PublicClient<Transport, Chain>,
op: UserOperation,
entryPoint: Address,
staticFee: bigint
staticFee: bigint,
logger: Logger
) {
const randomDataUserOp: UserOperation = {
...op
}

const selector = getFunctionSelector(EntryPointAbi[27])
const paramData = encodeAbiParameters(EntryPointAbi[27].inputs, [[op], entryPoint])
const paramData = encodeAbiParameters(EntryPointAbi[27].inputs, [[randomDataUserOp], entryPoint])
const data = concat([selector, paramData])

const latestBlock = await publicClient.getBlock()
Expand All @@ -212,7 +218,7 @@ export async function calcOptimismPreVerificationGas(
const serializedTx = serializeTransaction(
{
to: entryPoint,
chainId: publicClient.chain?.id ?? 10,
chainId: publicClient.chain.id,
nonce: 999999,
gasLimit: maxUint64,
gasPrice: maxUint64,
Expand All @@ -233,8 +239,10 @@ export async function calcOptimismPreVerificationGas(

const { result: l1Fee } = await opGasPriceOracle.simulate.getL1Fee([serializedTx])

const l2MaxFee = op.maxFeePerGas
const l2PriorityFee = latestBlock.baseFeePerGas + op.maxPriorityFeePerGas
const gasPrice = await getGasPrice(publicClient.chain.id, publicClient, logger)

const l2MaxFee = gasPrice.maxFeePerGas
const l2PriorityFee = latestBlock.baseFeePerGas + gasPrice.maxPriorityFeePerGas

const l2price = l2MaxFee < l2PriorityFee ? l2MaxFee : l2PriorityFee

Expand Down Expand Up @@ -327,19 +335,22 @@ export function parseViemError(err: unknown) {
const e = err.cause
if (e instanceof NonceTooLowError) {
return e
} else if (e instanceof FeeCapTooLowError) {
}
if (e instanceof FeeCapTooLowError) {
return e
} else if (e instanceof InsufficientFundsError) {
}
if (e instanceof InsufficientFundsError) {
return e
} else if (e instanceof IntrinsicGasTooLowError) {
}
if (e instanceof IntrinsicGasTooLowError) {
return e
} else if (e instanceof ContractFunctionRevertedError) {
}
if (e instanceof ContractFunctionRevertedError) {
return e
} else if (e instanceof EstimateGasExecutionError) {
return e
}
return
} else {
return
}
return
}

0 comments on commit 11f5c24

Please sign in to comment.