Tools for estimating gas on OP chains
- Tip the specs file has usage examples of every method in this library.
This package is designed to provide an easy way to estimate gas on OP chains.
Fee estimation on OP-chains has both an l2 and l1 component. By default tools such as Viem, Wagmi, Ethers, and Web3.js do not support the l1 component. They will support this soon but in meantime, this library can help estimate fees for transactions, or act as a reference. As these tools add support for gas estimation natively this README will be updated with framework specific instructions.
For more detailed information about gas fees on Optimism's Layer 2, you can visit their official documentation.
- The l2 contract that can estimate l1Fees is called GasPriceOracle contract. This library provides utils for interacting with it at a high level.
- The GasPriceOracle is deployed to Optimism and other OP chains at a predeployed address of
0x420000000000000000000000000000000000000F
This library provides a higher level abstraction over the gasPriceOracle
pnpm install @eth-optimism/fee-estimation
npm install @eth-optimism/fee-estimation
yarn add @eth-optimism/fee-estimation
import { estimateFees } from '@eth-optimism/fee-estimation'
import { optimistABI } from '@eth-optimism/contracts-ts'
import { viemClient } from './viem-client'
const optimistOwnerAddress =
'0x77194aa25a06f932c10c0f25090f3046af2c85a6' as const
const tokenId = BigInt(optimistOwnerAddress)
const fees = await estimateFees({
client: viemClient,
// If not using in viem can pass in this instead
/*
client: {
chainId: 10,
rpcUrl: 'https://mainnet.optimism.io',
},
*/
functionName: 'burn',
abi: optimistABI,
args: [tokenId],
account: optimistOwnerAddress,
to: '0x2335022c740d17c2837f9C884Bfe4fFdbf0A95D5',
})
estimateFees(options: OracleTransactionParameters<TAbi, TFunctionName> & GasPriceOracleOptions & Omit<EstimateGasParameters, 'data'>): Promise<bigint>
options
: An object with the following fields:
-
abi
: A JSON object ABI of contract. -
account
: A hex address of the account making the transaction. -
args
: Array of arguments to contract function. The types of this will be inferred from the ABI -
blockNumber
(optional): A BigInt representing the block number at which you want to estimate the fees. -
chainId
: An integer chain id. -
client
: An object with rpcUrl field, or an instance of a Viem PublicClient. -
functionName
: A string representing the function name for the transaction call data. -
maxFeePerGas
(optional): A BigInt representing the maximum fee per gas that the user is willing to pay. -
maxPriorityFeePerGas
(optional): A BigInt representing the maximum priority fee per gas that the user is willing to pay. -
to
: A hex address of the recipient of the transaction. -
value
(optional): A BigInt representing the value in wei sent along with the transaction.
A Promise that resolves to a BigInt representing the estimated fee in wei.
This package also provides lower level methods for estimating gas
This method returns a Layer 2 (L2) client that communicates with an L2 network.
getL2Client(options: ClientOptions): PublicClient;
options: ClientOptions
- The options required to initialize the L2 client.
PublicClient
- Returns a public client that can interact with the L2 network.
const clientParams = {
chainId: 10,
rpcUrl: process.env.VITE_L2_RPC_URL ?? 'https://mainnet.optimism.io',
} as const
const client = getL2Client(clientParams)
Returns the base fee.
baseFee({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
{ client, ...params }: GasPriceOracleOptions
- The options required to fetch the base fee.
Promise<bigint>
- Returns a promise that resolves to the base fee.
const blockNumber = BigInt(106889079)
const paramsWithClient = {
client: clientParams,
blockNumber,
}
const baseFeeValue = await baseFee(paramsWithClient)
Returns the decimals used in the scalar.
decimals({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
{ client, ...params }: GasPriceOracleOptions
- The options required to fetch the decimals.
Promise<bigint>
- Returns a promise that resolves to the decimals used in the scalar.
const decimalsValue = await decimals(paramsWithClient)
Returns the gas price.
gasPrice({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
{ client, ...params }: GasPriceOracleOptions
- The options required to fetch the gas price.
Promise<bigint>
- Returns a promise that resolves to the gas price.
const gasPriceValue = await gasPrice(paramsWithClient)
Computes the L1 portion of the fee based on the size of the rlp encoded input transaction, the current L1 base fee, and the various dynamic parameters.
getL1Fee(data: Bytes, { client, ...params }: GasPriceOracleOptions): Promise<bigint>;
data: Bytes
- The transaction call data as a 0x-prefixed hex string.{ client, ...params }: GasPriceOracleOptions
- Optional lock options and provider options.
Promise<bigint>
- Returns a promise that resolves to the L1 portion of the fee.
const data =
'0x5c19a95c00000000000000000000000046abfe1c972fca43766d6ad70e1c1df72f4bb4d1'
const l1FeeValue = await getL1Fee(data, paramsWithClient)
This method returns the amount of gas used on the L1 network for a given transaction.
getL1GasUsed(data: Bytes, { client, ...params }: GasPriceOracleOptions): Promise<bigint>;
data: Bytes
- The transaction call data as a 0x-prefixed hex string.{ client, ...params }: GasPriceOracleOptions
- Optional lock options and provider options.
Promise<bigint>
- Returns a promise that resolves to the amount of gas used on the L1 network for the given transaction.
const data =
'0x5c19a95c00000000000000000000000046abfe1c972fca43766d6ad70e1c1df72f4bb4d1'
const l1GasUsed = await getL1GasUsed(data, paramsWithClient)
Returns the base fee on the L1 network.
l1BaseFee({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
{ client, ...params }: GasPriceOracleOptions
- Optional lock options and provider options.
Promise<bigint>
- Returns a promise that resolves to the base fee on the L1 network.
const l1BaseFeeValue = await l1BaseFee(paramsWithClient)
Returns the overhead for the given transaction.
overhead({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
{ client, ...params }: GasPriceOracleOptions
- Optional lock options and provider options.
Promise<bigint>
- Returns a promise that resolves to the overhead for the given transaction.
const overheadValue = await overhead(paramsWithClient)
Returns the scalar value for the gas estimation.
scalar({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
{ client, ...params }: GasPriceOracleOptions
- Optional lock options and provider options.
Promise<bigint>
- Returns a promise that resolves to the scalar value for the gas estimation.
const scalarValue = await scalar(paramsWithClient)
Returns the version of the fee estimation library.
version({ client, ...params }: GasPriceOracleOptions): Promise<string>;
{ client, ...params }: GasPriceOracleOptions
- Optional lock options and provider options.
Promise<string>
- Returns a promise that resolves to the version of the fee estimation library.
const libraryVersion = await version(paramsWithClient)