title | lang | description |
---|---|---|
Estimating Transaction Costs on OP Mainnet |
en-US |
Learn how to use the Optimism SDK to estimate the cost of a transaction on OP Mainnet. |
import { Callout, Steps } from 'nextra/components'
Please **do not rely** on the content of this page as it is currently undergoing maintenance. **Code samples and solutions may not function as expected.** Please check back for an update or [signup to help us revise this page]( tutorialIn this tutorial, you'll learn how to use the Optimism SDK to estimate the cost of a transaction on OP Mainnet. You'll learn how to estimate the execution gas fee and the L1 data fee independently. You'll also learn how to estimate the total cost of the transaction all at once.
Check out the full explainer on [OP Mainnet transaction fees](/builders/app-developers/transactions/fees) for more information on how OP Mainnet charges fees under the hood.You're going to use the Optimism SDK for this tutorial. Since the Optimism SDK is a Node.js library, you'll need to create a Node.js project to use it.
{
}mkdir op-sample-project
cd op-sample-project
{
}pnpm init
{
}pnpm add @eth-optimism/sdk
{
}pnpm add ethers@^5
This tutorial explains how estimate transaction costs on OP Sepolia. You will need to get some ETH on OP Sepolia in order to run the code in this tutorial.
You can use the [Superchain Faucet](https://app.optimism.io/faucet?utm_source=docs) to get ETH on OP Sepolia.You need a private key in order to sign transactions.
Set your private key as an environment variable with the export
command.
Make sure this private key corresponds to an address that has ETH on OP Sepolia.
export TUTORIAL_PRIVATE_KEY=0x...
You're going to use the Node REPL to interact with the Optimism SDK. To start the Node REPL run the following command in your terminal:
node
This will bring up a Node REPL prompt that allows you to run javascript code.
You need to import some dependencies into your Node REPL session.
{
}{
}You'll need a few variables throughout this tutorial. Let's set those up now.
{
}{
}Here you're creating a standard Ethers RPC provider and wrapping it as an L2Provider
, a class provided by the Optimism SDK.
This will add a few extra functions to the provider object that you'll use later in this tutorial.
{
}You're now going to use the Optimism SDK to estimate the cost of a transaction on OP Mainnet.
Here you'll estimate the cost of a simple transaction that sends a small amount of ETH from your address to the address 0x1000000000000000000000000000000000000000
.
{
}Ethers makes it easy to create unsigned transactions so you can estimate the cost of a transaction before you a user to sign it.
Here you'll create an unsigned transaction that sends a small amount of ETH from your address to the address 0x1000000000000000000000000000000000000000
.
You can also create unsigned transactions that interact with contracts using Contract.populateTransaction
.
{
}You can estimate the execution gas fee the same way you'd estimate the gas fee for any transaction on Ethereum. Simply multiply the gas limit by the effective gas price.
{
}You can estimate the L1 data fee with the estimateL1GasCost
function.
Under the hood, this function is estimating the amount of Ethereum gas required to publish this transaction on Ethereum and multiplying it by the current Ethereum gas price (as tracked by the L2).
This function returns the current cost estimate in wei.
{
}Once you've individually estimated the execution gas fee and the L1 data fee, you can sum these two values together to get the total cost of the transaction.
{
}Now that you've estimated the total cost of the transaction, go ahead and send it to the network. This will make it possible to see the actual cost of the transaction to compare to your estimate.
{
}Once you get back the transaction receipt, check the actual execution gas fee.
{
}You can also check the actual L1 data fee.
{
}Sum these two together to get the actual total cost of the transaction.
{
}Finally, check the difference between the estimated total cost and the actual total cost. This will give you a sense of how accurate your estimate was. Estimates will never be entirely accurate, but they should be close!