OP Stack interop is in active development. Some features may be experimental.
Messages are relayed automatically in the interop devnet.
Overview
Learn to relay transactions directly by sending the correct transaction.About this tutorial
About this tutorial
Prerequisite technical knowledge
- Familiarity with blockchain concepts
- Familiarity with Foundry, especially
cast
- How to use
castto relay transactions when autorelay does not work - How to relay transactions using JavaScript
- Unix-like operating system (Linux, macOS, or WSL for Windows)
- Node.js version 16 or higher
- Git for version control
- Supersim environment configured and running
- Foundry tools installed (forge, cast, anvil)
What you’ll build
- A program to relay messages using the JavaScript library
- A shell script to relay messages using
cast
Setup
These steps are necessary to run the tutorial, regardless of whether you are usingcast or the JavaScript API.
1
Run Supersim
This exercise needs to be done on Supersim.
You cannot use the devnets because you cannot disable autorelay on them.
You cannot use the devnets because you cannot disable autorelay on them.
- Follow Install Supersim.
- Run Supersim without
--interop.relay.
2
Create the state for relaying messages
The results of this step are similar to what the message passing tutorial would produce if you did not have autorelay on.Execute this script:This script installs
These smart contracts let us send a message from chain A that needs to be relayed to chain B.Then, the script creates
That script is explained below.Finally, this script writes out some parameter setting lines that should be executed on the main shell before you continue.
With a fresh Supersim running, these should be:
Greeter.sol on chain B and GreetingSender.sol on chain A.These smart contracts let us send a message from chain A that needs to be relayed to chain B.Then, the script creates
./manual-relay/sendAndRelay.sh to manually relay a message from chain A to chain B.That script is explained below.Finally, this script writes out some parameter setting lines that should be executed on the main shell before you continue.
With a fresh Supersim running, these should be:
Manual relay using the API
1
Setup
2
Manual relaying app
Create a file
Run JavaScript program:
manual-relay.mjs with:Explanation
Explanation
3
Debugging
To see what messages were relayed by a specific transaction:
Manual relay using cast
You can see an example of how to manually relay using cast in manual-relay/sendAndRelay.sh.It is somewhat complicated, so the setup creates one that is tailored to your environment. Run the script:
-
Configuration parameters
This is the configuration. The greeter addresses are identical because the nonce for the user address has an identical nonce on both chains.
-
Send a message that needs to be relayed
Send a message from chain A to chain B. The
$$is the process ID, so if you rerun the script you’ll see that the information changes. -
Find the log entry to relay
Whenever
L2ToL2CrossDomainMessengersends a message to a different blockchain, it emits aSentMessageevent. Extract only the latestSendMessageevent from the logs.Example
log-entry -
Manipulate the log entry to obtain information
Consolidate the log topics into a single hex string.Read additional fields from the log entry.Consolidate the entire log entry.
- Create the access list for the executing message
CrossL2Inbox) is a bit complicated, but you dont need to worry about it.
Supersim exposes RPC calls that calculates it for you on port 8420.
The code above will calculate the correct access list even if you’re using a different interop cluster where autorelay is not functioning.
This is because the code implements a pure function, which produces consistent results regardless of external state.
In contrast, the admin_getAccessListByMsgHash RPC call is not a pure function, it is dependent on system state and therefore less flexible in these situations.
-
Show that the manual relay is necessary
Show the current greeting. The message has not been relayed yet, so it’s still the old greeting.
-
Actually relay the message
Call
relayMessageonL2ToL2CrossDomainMessengerto relay the message. -
Show the relay results
Again, show the current greeting. Now it’s the new one.
Next steps
- Review the 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.