OP Stack interop is in active development. Some features may be experimental.
Overview
This tutorial demonstrates how to implement cross-chain communication within the OP Stack ecosystem. You’ll build a complete message passing system that enables different chains to interact with each other using theL2ToL2CrossDomainMessenger contract.
What You’ll Build
- A
Greetercontract that stores and updates a greeting - A
GreetingSendercontract that sends cross-chain messages to update the greeting - A TypeScript application to relay messages between chains
This tutorial provides step-by-step instructions for implementing cross-chain messaging.
For a conceptual overview,
see the Message Passing Explainer.
L2ToL2CrossDomainMessenger contract to pass messages between interoperable blockchains.
Setting up your development environment
Follow the [Installation Guide](/app-developers/tutorials/development/supersim/installation) to install:
- Foundry for smart contract development (required in all cases)
- Supersim for local blockchain simulation (optional)
Implementing onchain message passing (in Solidity)
The implementation consists of three main components:- Greeter Contract: Deployed on
Chain B, receives and stores messages. - GreetingSender Contract: Deployed on
Chain A, initiates cross-chain messages.
Setting up test networks
-
If you are using [Supersim](/> app-developers/tools/development/supersim), go to the directory where Supersim is installed and start it with autorelay.
If you are using the devnets, just skip this step.
- Supersim
- Devnets
Supersim creates threeanvilblockchains:Role ChainID RPC URL L1 900 http://127.0.0.1:8545 OPChainA 901 http://127.0.0.1:9545 OPChainB 902 http://127.0.0.1:9546 -
In a separate shell, store the configuration in environment variables.
- Supersim
- Devnets
Set these parameters for Supersim.
Create the contracts
-
Create a new Foundry project.
-
In
src/Greeter.solput this file. This is a variation on Hardhat’s Greeter contract. -
Deploy the
Greetercontract to Chain B and store the resulting contract address in theGREETER_B_ADDRESSenvironment variable.
-
Install the Optimism Solidity libraries into the project.
-
Create
src/GreetingSender.sol. -
Deploy
GreetingSenderto chain A.
Sender information
Run this command to view the events to see who calledsetGreeting.
L2ToL2CrossDomainMessenger contract address (4200000000000000000000000000000000000023), making it ineffective for identifying the original sender.
In this section we change Greeter.sol to emit a separate event in it receives a cross domain message, with the sender’s identity (address and chain ID).
Modify the Greeter contract
-
Modify
src/Greeter.solto this code. -
Redeploy the contracts.
Because the address of
Greeteris immutable inGreetingSender, we need to redeploy both contracts.
Verify you can see cross chain sender information
-
Set the greeting through
GreetingSender. -
Read the log entries.
See that the second topic (the first indexed log parameter) is the same as
$GREETER_A_ADDRESS. The third topic can be either0x385=901, which is the chain ID for supersim chain A, or0x190a85c0=420120000, which is the chain ID for devnet alpha 0.
Next steps
- Review the [OP Stack Interop Explainer](/> op-stack/interop/explainer) for answers to common questions about interoperability.
- Read the Message Passing Explainer to understand what happens “under the hood”.
- Write a revolutionary app that uses multiple blockchains within the OP Stack ecosystem.