diff --git a/packages/rpc/src/rpcHandler.ts b/packages/rpc/src/rpcHandler.ts index c734ad2a..606da28c 100644 --- a/packages/rpc/src/rpcHandler.ts +++ b/packages/rpc/src/rpcHandler.ts @@ -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( diff --git a/packages/utils/src/validation.ts b/packages/utils/src/validation.ts index dc96dda9..f5e4f818 100644 --- a/packages/utils/src/validation.ts +++ b/packages/utils/src/validation.ts @@ -16,9 +16,9 @@ import { getFunctionSelector, serializeTransaction, toBytes, - toHex, - bytesToHex + toHex } from "viem" +import { getGasPrice, Logger } from "." export interface GasOverheads { /** @@ -194,28 +194,15 @@ const getL1FeeAbi = [ } ] as const -// Assuming you have a function to generate random bytes -function randomBytes(length: number): Uint8Array { - const pattern = "10101010101" - const repeatedPattern = pattern.repeat(Math.ceil(length / pattern.length)) - - const byteArray = repeatedPattern.split("").map(Number) - - const bytes = new Uint8Array(byteArray) - - return bytes.slice(0, length) -} - export async function calcOptimismPreVerificationGas( - publicClient: PublicClient, + publicClient: PublicClient, op: UserOperation, entryPoint: Address, - staticFee: bigint + staticFee: bigint, + logger: Logger ) { const randomDataUserOp: UserOperation = { - ...op, - signature: bytesToHex(randomBytes(op.signature.length)), - paymasterAndData: bytesToHex(randomBytes(op.paymasterAndData.length)) + ...op } const selector = getFunctionSelector(EntryPointAbi[27]) @@ -227,12 +214,10 @@ export async function calcOptimismPreVerificationGas( throw new RpcError("block does not have baseFeePerGas") } - const maxPriorityFeePerGas = await publicClient.estimateMaxPriorityFeePerGas() - const serializedTx = serializeTransaction( { to: entryPoint, - chainId: publicClient.chain?.id ?? 10, + chainId: publicClient.chain.id, nonce: 999999, gasLimit: maxUint64, gasPrice: maxUint64, @@ -253,9 +238,14 @@ export async function calcOptimismPreVerificationGas( const { result: l1Fee } = await opGasPriceOracle.simulate.getL1Fee([serializedTx]) - const l2PriorityFee = latestBlock.baseFeePerGas + 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 - return staticFee + l1Fee / l2PriorityFee + return staticFee + l1Fee / l2price } const getArbitrumL1FeeAbi = [