OP Stack currently uses a single-Sequencer block production model. This means that there is only one Sequencer active on the network at any given time. Single-Sequencer models are simpler than their highly decentralized counterparts but they are also more vulnerable to potential downtime. Sequencer downtime must not be able to prevent users from transacting on the network. As a result, OP Stack includes a mechanism for “forcing” transactions to be included in the blockchain. This mechanism involves triggering a transaction on OP Stack by sending a transaction on Ethereum. In this tutorial you’ll learn how to trigger a transaction on OP Stack from Ethereum using Viem. You’ll use the OP Sepolia testnet, but the same logic will apply to OP Stack.Documentation Index
Fetch the complete documentation index at: https://docs.optimism.io/llms.txt
Use this file to discover all available pages before exploring further.
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.
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.Set session variables
You’ll need a few variables throughout this tutorial. Let’s set those up now.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.
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.Send the transaction
Now you’ll send the transaction. Note that you are including a buffer of 20% on top of the gas estimate.
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.