Just looking to bridge tokens between OP Stack and Ethereum?
Check out the tutorial on Bridging ERC-20 Tokens to OP Stack With viem.
Message passing basics
OP Stack uses a smart contract called theCrossDomainMessenger to pass messages between OP Stack and Ethereum.
Both chains have a version of this contract (the L1CrossDomainMessenger and the L2CrossDomainMessenger).
Messages sent from Ethereum to OP Stack are automatically relayed behind the scenes.
Messages sent from OP Stack to Ethereum must be explicitly relayed with a second transaction on Ethereum.
Read more about message passing in the guide to Sending Data Between L1 and L2.
Dependencies
Get ETH on Sepolia and OP Sepolia
This tutorial explains how to send messages 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.
Review the contracts
You’re about to use two contracts that have already been deployed to Sepolia and OP Sepolia, theGreeter contracts.
You can review the source code for the L1 Greeter contract here on Etherscan.
You can review the source code for the L2 Greeter contract here on Etherscan.
Both contracts have exactly the same source code.
Feel free to review the source code for these two contracts now if you’d like.
This tutorial will explain how these contracts work in detail later on in the How It Works section below.
Interact with the L1 Greeter
You’re first going to use the L1Greeter contract to set the greeting on the L2 Greeter contract.
You’ll send a transaction directly to the L1 Greeter contract which will ask the L1CrossDomainMessenger to send a message to the L2 Greeter contract.
After just a few minutes, you’ll see the corresponding greeting set on the L2 Greeter contract.
1
Connect to Etherscan
Sending a message to the L2
Greeter contract via the L1 Greeter contract requires that you call the sendGreeting function.
For simplicity, you’ll interact with the contract directly on Etherscan.
Open up the L1 Greeter contract on Sepolia Etherscan and click the “Connect to Web3” button.2
Send your greeting
Put a greeting into the field next to the “sendGreeting” function and click the “Write” button.
You can use any greeting you’d like.
3
Wait a few minutes
It will take a few minutes for your message to reach L2.
Feel free to take a quick break while you wait.
You can use viem to programmatically check the status of any message between L1 and L2.
Later on in this tutorial you’ll learn how to use viem and the
waitToProve function to wait for various message statuses.
This same function can be used to wait for a message to be relayed from L1 to L2.4
Check the L2 Greeter
After a few minutes, you should see the greeting on the L2
Greeter contract change to the greeting you set.
Open up the L2 Greeter contract on OP Sepolia Etherscan and click the “Read Contract” button.
Paste your address into the field next to the “greeting” function and click the “Query” button.
You should see the message you sent from L1.5
Haven’t seen your message yet?
You might need to wait a little longer.
L2 transactions triggered on L1 are typically processed within one minute but can occasionally be slightly delayed.
Interact with the L2 Greeter
Now you’re going to use the L2Greeter contract to set the greeting on the L1 Greeter contract.
You’ll send a transaction directly to the L2 Greeter contract which will ask the L2CrossDomainMessenger to send a message to the L1 Greeter contract.
Unlike the previous step, you’ll need to relay the message from L2 to L1 yourself.
You’ll do this by sending two transactions on Sepolia, one proving transaction and one relaying transaction.
1
Connect to Etherscan
Just like before, sending a message to the L1
Greeter contract via the L2 Greeter contract requires that you call the sendGreeting function.
Open up the L2 Greeter contract on OP Sepolia Etherscan and click the “Connect to Web3” button.2
Send your greeting
Put a greeting into the field next to the “sendGreeting” function and click the “Write” button.
You can use any greeting you’d like.
Copy the transaction hash from the transaction you just sent.
You’ll need this for the next few steps.
Feel free to keep this tab open so you can easily copy the transaction hash later.
3
Create a demo project
You’re going to use viem to prove and relay your message to L1.
4
Add environment variables
Set your private key and transaction hash as environment variables.
5
Run the script in a Node REPL
Start a Node.js REPL with
node and paste the following script to monitor, prove, and finalize the cross-domain message:6
Verify the L1 greeting
After finalization, open the L1
Greeter contract on Sepolia Etherscan.
Confirm that the greeting has been updated to the message you sent from L2.How it works
Congratulations! You’ve successfully sent a message from L1 to L2 and from L2 to L1. This section explains how theGreeter contracts work so you can follow the same pattern to deploy your own contracts.
Luckily, both Greeter contracts are exactly the same so it’s easy to see how everything comes together.
The Messenger variable
TheGreeter contract has a MESSENGER variable that keeps track of the CrossDomainMessenger contract on the current chain.
Check out the Contract Addresses page to see the addresses of the CrossDomainMessenger contracts on whichever network you’ll be using.
The other Greeter variable
TheGreeter contract also has an OTHER_GREETER variable that keeps track of the Greeter contract on the other chain.
On L1, this variable is set to the address of the L2 Greeter contract, and vice-versa.
The Greetings mapping
TheGreeter contract keeps track of the different greetings that users have sent inside a greetings mapping.
By using a mapping, this contract can keep track of greetings from different users at the same time.
The Constructor
TheGreeter has a simple constructor that sets the MESSENGER and OTHER_GREETER variables.
The sendGreeting function
ThesendGreeting function is the most important function in the Greeter contract.
This is what you called earlier to send messages in both directions.
All this function does is use the sendMessage function found within the CrossDomainMessenger contract to send a message to the Greeter contract on the other chain.
The setGreeting function
ThesetGreeting function is the function that actually sets the greeting.
This function is called by the CrossDomainMessenger contract on the other chain.
It checks explicitly that the function can only be called by the CrossDomainMessenger contract.
It also checks that the CrossChainMessenger says that the message came from the Greeter contract on the other chain.
Finally, it sets the greeting in the greetings mapping.
The two
require statements in this function are important.
Without them, anyone could call this function and set the greeting to whatever they want.
You can follow a similar pattern in your own smart contracts.