Dependencies
Create a demo project
You’re going to use theviem
package for this tutorial. Since Viem is a Node.js library, you’ll need to create a Node.js project to use it.
1
Make a Project Folder
2
Initialize the Project
3
Install Viem
Want to create a new wallet for this tutorial?
If you have
cast
installed you can run cast wallet new
in your terminal to create a new wallet and get the private key.Get ETH on Sepolia and OP Sepolia
This tutorial explains how to bridge tokens from Sepolia to OP Sepolia. You will need to get some ETH on both of these testnets.You can use this faucet to get ETH on Sepolia.
You can use the Superchain Faucet to get ETH on OP Sepolia.
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 theexport
command.
Make sure this private key corresponds to an address that has ETH on both Sepolia and OP Sepolia.
Start the Node REPL
You’re going to use the Node REPL to interact with Viem. To start the Node REPL run the following command in your terminal:Import dependencies
You need to import some dependencies into your Node REPL session.1
Import Viem
Set session variables
You’ll need a few variables throughout this tutorial. Let’s set those up now.1
Load your private key
2
Create the RPC providers and wallets
Check your initial balance
You’ll be sending a small amount of ETH as part of this tutorial. Quickly check your balance on OP Sepolia so that you know how much you had at the start of the tutorial.Trigger the transaction
Now you’ll use theOptimismPortal
contract to trigger a transaction on OP Sepolia by sending a transaction on Sepolia.
1
Create the OptimismPortal object
2
Estimate the required gas
When sending transactions via the
OptimismPortal
contract it’s important to always include a gas buffer. This is because the OptimismPortal
charges a variable amount of gas depending on the current demand for L2 transactions triggered via L1. If you do not include a gas buffer, your transactions may fail.3
Send the transaction
Now you’ll send the transaction. Note that you are including a buffer of 20% on top of the gas estimate.
4
Wait for the L1 transaction
First you’ll need to wait for the L1 transaction to be mined.
5
Wait for the L2 transaction
Now you’ll need to wait for the corresponding L2 transaction to be included in a block. This transaction is automatically created as a result of your L1 transaction. Here you’ll determine the hash of the L2 transaction and then wait for that transaction to be included in the L2 blockchain.