Check out the guide on understanding Transaction Fees on OP Mainnet for an in-depth explanation of how OP Mainnet transaction fees work.
Execution gas fee
Estimating the execution gas fee on OP Mainnet is just like estimating the execution gas fee on Ethereum.
Steps are provided here for reference and convenience, but you can use the same tooling that you’d use to estimate the execution gas fee for a transaction on Ethereum.
1
Estimate the gas limit
Using the same tooling that you’d use to estimate the gas limit for a transaction on Ethereum, estimate the gas limit for your transaction on OP Mainnet.
OP Mainnet is designed to be EVM equivalent so transactions will use the same amount of gas on OP Mainnet as they would on Ethereum.
This means you can feed your transaction to the
eth_estimateGas
JSON-RPC method just like you would on Ethereum. Alternatively, use Tenderly’s tenderly_estimateGas
for 100% accurate gas estimations.2
Estimate the max fee per gas
Like Ethereum, OP Mainnet uses an
EIP-1559
style fee market to determine the current base fee per gas.
You can then additionally specify a priority fee (also known as a tip) to incentivize the Sequencer to include your transaction more quickly.
Make sure to check out the guide on Setting Transaction Gas Parameters on OP Mainnet to learn more about how to select an appropriate max fee per gas for your transaction.L1 data fee
The Viem library provides a convenient method for estimating the L1 data fee for a transaction.
Check out the tutorial on Estimating Transaction Costs on OP Mainnet to learn how to use the Viem library to estimate the L1 data fee for your transaction.
Keep reading if you’d like to learn how to estimate the L1 data fee without the Viem library.
The L1 data fee is paid based on the current Ethereum gas price as tracked within the
GasPriceOracle
smart contract.
This gas price is updated automatically by the OP Mainnet protocol.
Your transaction will be charged the Ethereum gas price seen by the protocol at the time that your transaction is included in an OP Mainnet block.
This means that the L1 data fee for your transaction may differ from your estimated L1 data fee.1
Serialize your transaction
The L1 data fee is calculated based on the size of your serialized transaction in bytes.
Most Ethereum tooling will provide a method for serializing a transaction.
For instance, Ethers.js provides the
ethers.utils.serializeTransaction
method.2
Estimate the L1 data fee
Once you have serialized your transaction, you can estimate the L1 data fee by calling the
getL1Fee
method on the GasPriceOracle
smart contract available on OP Mainnet and all OP Stack chains.
This method takes the serialized transaction as input and returns the L1 data fee in wei using the formula described in the Transaction Fees on OP Mainnet guide.Fee estimation is typically performed before the transaction is signed.
As a result, the
getL1Fee
method assumes that your input is an unsigned Ethereum transaction.Tooling
Several tools are available to help you estimate the L1 Data Fee for your transaction. Selecting the right tool for your use case will depend on your specific needs.- Viem provides first-class support for OP Stack chains, including OP Mainnet. You can use Viem to estimate gas costs and send cross-chain transactions (like transactions through the Standard Bridge system). It’s strongly recommended to use Viem if you are able to do so as it will provide the best native support at the moment.
Future proofing
The L1 Data Fee formula is subject to change in the future, especially as the data availability landscape evolves. As a result, it’s important to future-proof your transaction fee estimation code to ensure that it will continue to function properly as the L1 Data Fee formula changes.- Use existing tooling to estimate the L1 Data Fee for your transaction if possible. This tooling will be updated to reflect any changes to the L1 Data Fee formula. This way you won’t need to modify your code to account for any changes to the formula.
- Use the
getL1Fee
method on theGasPriceOracle
if you are unable to use existing tooling. ThegetL1Fee
method will be updated to reflect any changes to the L1 Data Fee formula. It’s strongly recommended that you do not implement the L1 Data Fee formula yourself.