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

Estimate cost tut #913

Merged
merged 22 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions pages/builders/app-developers/transactions/estimates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,18 @@ This means you can feed your transaction to the [`eth_estimateGas`](https://ethe

{<h3>Estimate the max fee per gas</h3>}

Like Ethereum, OP Mainnet uses an EIP-1559 style fee market to determine the current base 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](./parameters) to learn more about how to select an appropriate max fee per gas for your transaction.

{<h3>Calculate the execution gas fee</h3>}

Once you've estimated the gas limit and the max fee per gas for your transaction, you can calculate the execution gas fee by multiplying these two values together.

For instance, suppose that your transaction has a gas limit of `420000 gas`, a base fee of `0.05 gwei`, and a priority fee of `0.1 gwei`.
The execution gas fee for your transaction would be:

```javascript
// Start with your parameters
gas_limit = 420000
base_fee_per_gas = 0.05 gwei
priority_fee_per_gas = 0.1 gwei

// Max fee per gas is the sum of the base fee and the priority fee
max_fee_per_gas = base_fee_per_gas + priority_fee_per_gas = 0.15 gwei

// Execution gas fee is the product of the gas limit and the max fee per gas
execution_gas_fee = gas_limit * max_fee_per_gas = 420000 * 0.15 gwei = 0.000063 ETH
```

</Steps>

## L1 Data Fee

<Callout type="info">
The Optimism SDK provides a convenient method for estimating the L1 data fee for a transaction.
Check out the tutorial on [Estimating Transaction Costs on OP Mainnet](/builders/app-developers/tutorials/sdk-estimate-costs) to learn how to use the Optimism SDK 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 Optimism SDK.
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](/builders/app-developers/tutorials/sdk-estimate-costs) 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.
</Callout>

The L1 data fee is a fee paid to the Sequencer for the cost of publishing your transaction to Ethereum.
Expand Down Expand Up @@ -116,8 +96,6 @@ Several tools are available to help you estimate the L1 Data Fee for your transa
Selecting the right tool for your use case will depend on your specific needs.

* [Viem](https://viem.sh/op-stack#getting-started-with-op-stack) 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.
* If you are using Ethers v5, the [Optimism SDK](https://sdk.optimism.io/) provides methods for estimating the L1 Data Fee for your transactions and for sending cross-chain transactions. The Optimism SDK is designed to be used alongside Ethers v5 and does not yet support Ethers v6.
* If you are using Ethers v6, the [Optimistic Utilities Extension](https://github.com/ethers-io/ext-utils-optimism) provides methods for estimating the L1 Data Fee. The Ethers v6 extension does not yet support sending cross-chain transactions. Use Viem or the Optimism SDK if you need to send cross-chain transactions.

### Future Proofing

Expand Down
196 changes: 89 additions & 107 deletions pages/builders/app-developers/tutorials/sdk-estimate-costs.mdx
Original file line number Diff line number Diff line change
@@ -1,66 +1,70 @@
---
title: Estimating Transaction Costs on OP Mainnet
title: Estimating Transaction Costs on OP Stack
lang: en-US
description: Learn how to use the Optimism SDK to estimate the cost of a transaction on OP Mainnet.
description: Learn how to use Viem to estimate the cost of a transaction on OP Mainnet.
---

import { Callout, Steps } from 'nextra/components'
import { WipCallout } from '@/components/WipCallout'
import { Callout, Steps, Tabs } from 'nextra/components'

<WipCallout />
# Estimating Transaction Costs on OP Mainnet
# Estimating Transaction Costs on OP Stack


In this tutorial, you'll learn how to use the [Optimism SDK](https://sdk.optimism.io) to estimate the cost of a transaction on OP Mainnet.
In this tutorial, you'll learn how to use [Viem](https://viem.sh/op-stack/) to estimate the cost of a transaction on OP Mainnet.
You'll learn how to estimate the [execution gas fee](/builders/app-developers/transactions/fees#execution-gas-fee) and the [L1 data fee](/builders/app-developers/transactions/fees#l1-data-fee) independently.
You'll also learn how to estimate the total cost of the transaction all at once.

<Callout>
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.
Check out the full explainer on [OP Stack transaction fees](/builders/app-developers/transactions/fees) for more information on how OP Mainnet charges fees under the hood.
</Callout>
krofax marked this conversation as resolved.
Show resolved Hide resolved

## Supported Networks

Viem supports any of the [Superchain networks](/chain/networks).
The OP Stack networks are included in Viem by default.
If you want to use a network that isn't included by default, you can add it to Viem's chain configurations.

## Dependencies

* [node](https://nodejs.org/en/)
* [pnpm](https://pnpm.io/installation)

## Create a Demo Project

You're going to use the Optimism SDK for this tutorial.
Since the Optimism SDK is a [Node.js](https://nodejs.org/en/) library, you'll need to create a Node.js project to use it.
You're going to use Viem for this tutorial.
Since Viem is a [Node.js](https://nodejs.org/en/) library, you'll need to create a Node.js project to use it.

<Steps>
{<h3>Make a Project Folder</h3>}

{<h3>Make a Project Folder</h3>}
```bash
mkdir op-est-cost-tutorial
cd op-est-cost-tutorial
```

```bash
mkdir op-sample-project
cd op-sample-project
```
{<h3>Initialize the Project</h3>}

{<h3>Initialize the Project</h3>}
```bash
pnpm init
```

```bash
pnpm init
```

{<h3>Install the Optimism SDK</h3>}
{<h3>Install Viem</h3>}

```bash
pnpm add @eth-optimism/sdk
```
```bash
pnpm add viem
```
</Steps>

{<h3>Install ethers.js</h3>}
<Callout type="info">
Want to create a new wallet for this tutorial?
If you have [`cast`](https://book.getfoundry.sh/getting-started/installation) installed you can run `cast wallet new` in your terminal to create a new wallet and get the private key.
</Callout>

```bash
pnpm add ethers@^5
```
## Get ETH on Sepolia

</Steps>
This tutorial explains how to bridge ETH from Sepolia to OP Sepolia.
You will need to get some ETH on Sepolia to follow along.

<Callout type="info">
Want to create a new wallet for this tutorial?
If you have [`cast`](https://book.getfoundry.sh/getting-started/installation) installed you can run `cast wallet new` in your terminal to create a new wallet and get the private key.
You can use [this faucet](https://sepoliafaucet.com) to get ETH on Sepolia.
</Callout>

## Get ETH on OP Sepolia
Expand All @@ -69,47 +73,29 @@ 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.

<Callout type="info">
You can use the [Superchain Faucet](https://console.optimism.io/faucet?utm_source=docs) to get ETH on OP Sepolia.
You can use the [Superchain Faucet](https://console.optimism.io/faucet?utm_source=docs) to get ETH on OP Sepolia.
</Callout>

## Add a Private Key to Your Environment

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.
Make sure this private key corresponds to an address that has ETH on Sepolia.
krofax marked this conversation as resolved.
Show resolved Hide resolved

```bash
export TUTORIAL_PRIVATE_KEY=0x...
```

## Start the Node REPL

You're going to use the Node REPL to interact with the Optimism SDK.
You're going to use the Node REPL to interact with Viem.
To start the Node REPL run the following command in your terminal:

```bash
node
```

This will bring up a Node REPL prompt that allows you to run javascript code.

## Import Dependencies

You need to import some dependencies into your Node REPL session.

<Steps>

{<h3>Import the Optimism SDK</h3>}

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L3 hash=26b2fdb17dd6c8326a54ec51f0769528
```

{<h3>Import ethers.js</h3>}

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L4 hash=69a65ef97862612e4978b8563e6dbe3a
```

</Steps>
This will bring up a Node REPL prompt that allows you to run JavaScript code.

## Set Session Variables

Expand All @@ -118,102 +104,98 @@ Let's set those up now.

<Steps>

{<h3>Load your private key</h3>}
{<h3>Import Viem and other necessary modules</h3>}

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L6 hash=755b77a7ffc7dfdc186f36c37d3d847a
smartcontracts marked this conversation as resolved.
Show resolved Hide resolved
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L3-L6 hash=32ecaac58846bfe7e785e2cc35562120
```

{<h3>Create the RPC provider</h3>}

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.
{<h3>Set up the account</h3>}

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L8 hash=1db780739476f924536f5fa58794b67f
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L8-L9 hash=165490e75b825c786a937fba7b8e159d
```

{<h3>Create the wallet instance</h3>}
{<h3>Create the public client</h3>}

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L9 hash=d315a1ba59b2ee3f43d178bab816e930
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L11-L14 hash=42293ff382c932f806beb7252803a848
```

{<h3>Create the wallet client</h3>}

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L16-L19 hash=e7b6423850765242512e71589382791b
```
</Steps>

## Estimate Transaction Costs

You're now going to use the Optimism SDK to estimate the cost of a transaction on OP Mainnet.
You're now going to use the Viem 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`.

<Steps>
{<h3>Create the unsigned transaction</h3>}

{<h3>Create the unsigned transaction</h3>}

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`](https://docs.ethers.org/v5/api/contract/contract/#contract-populateTransaction).
Viem 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`.

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L11-L15 hash=22d44a7322d2d378e886a0ba5a0c6fec
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L21-L26 hash=d2f3fc3df8298253bd45a226dd171dcf
```

{<h3>Estimate the execution gas fee</h3>}
{<h3>Estimate the total cost</h3>}

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.
With Viem you can estimate the total cost of a transaction using the [estimateTotalFee](https://viem.sh/op-stack/actions/estimateTotalFee) method.

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L18-L21 hash=8090c6513655722e1194d4d9f0f794af
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L28-L29 hash=db562f050d0affe866e2114779656d3a
```

{<h3>Estimate the L1 data fee</h3>}
{<h3>Send the transaction</h3>}

You can estimate the L1 data fee with the [`estimateL1GasCost`](https://sdk.optimism.io/modules#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.
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.

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L24-L25 hash=c5b1b1754aede507d071419fa051e3d7
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L31-L35 hash=419162648c0c6c0d5e82ca8f6d4942d5
```

{<h3>Estimate the total cost</h3>}
{<h3>Check the actual execution gas fee</h3>}

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.
Once you get back the transaction receipt, check the actual execution gas fee.
You can do so by accessing the `gasUsed` and `effectiveGasPrice` from the transaction receipt.
You can then multiply these values to get the actual L2 cost of the transaction

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L28-L29 hash=f7315f3dbf96423569a42c902eeee45c
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L37-L38 hash=57dba68f78481bea90e7629270a1f58b
```

{<h3>Send the transaction</h3>}
{<h3>Check the actual L1 data fee</h3>}

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.
You can also check the actual L1 data fee.

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L32-L34 hash=f0cc7ae37a28a884aa7f47f13b381681
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L40-L41 hash=e728708954c71fe52c55912dbe778042
```

{<h3>Check the actual execution gas fee</h3>}
{<h3>Check the actual total cost</h3>}

Once you get back the transaction receipt, check the actual execution gas fee.
Sum these two together to get the actual total cost of the transaction.

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L37-L38 hash=3b3ce48412906a44c1d2f6861a99c8a0
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L43-L44 hash=38a1eadb48d89d476ea51658514d23c0
```

{<h3>Check the actual L1 data fee</h3>}
{<h3>Check the difference</h3>}

You can also check the actual L1 data fee.
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!

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L41-L42 hash=3438ad167823b837f3511759a06e73f3
```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L46-L47 hash=20c8c60af1cc39e842b207cfd2dfa2cb
```
</Steps>

{<h3>Check the actual total cost</h3>}

Sum these two together to get the actual total cost of the transaction.

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L45-L46 hash=d23b6db1b716cba154932fd3d261995e
```

{<h3>Check the difference</h3>}

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!
<Callout type="info">
Estimates will never be entirely accurate due to network conditions and gas price fluctuations, but they should be close to the actual costs.
</Callout>

```js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L49-L50 hash=358adb5552c9f00484a6bb0580109fd8
```
## Next Steps

</Steps>
* Always estimate before sending: Estimating costs before sending a transaction helps prevent unexpected fees and failed transactions.
* Account for gas price volatility: Gas prices can change rapidly. Consider adding a buffer to your estimates or implementing a gas price oracle for more accurate pricing.
* Optimize transaction data: Minimize the amount of data in your transactions to reduce L1 data fees.
* Monitor network conditions: Keep an eye on network congestion and adjust your estimates accordingly.
* Use appropriate gas limits: Setting too low a gas limit can cause transactions to fail, while setting it too high can result in unnecessary costs.
* Implement retry mechanisms: If a transaction fails due to underestimated gas, implement a retry mechanism with adjusted gas parameters.
Loading
Loading