Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pre-verification gas #41

Closed
wants to merge 9 commits into from
3 changes: 2 additions & 1 deletion packages/rpc/src/rpcHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,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 @@ -18,6 +18,7 @@ import {
toBytes,
toHex
} from "viem"
import { getGasPrice, Logger } from "."

export interface GasOverheads {
/**
Expand Down Expand Up @@ -194,13 +195,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 @@ -211,7 +217,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 @@ -232,8 +238,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 @@ -326,17 +334,20 @@ 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
}
return
} else {
return
}
return
}
Loading