You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<Cardtitle="Issuing new assets with SuperchainERC20"href="/stack/interop/tutorials/deploy-superchain-erc20"icon={<imgsrc="/img/icons/shapes.svg" />} />
15
17
16
18
<Cardtitle="Transferring a SuperchainERC20"href="/stack/interop/tutorials/transfer-superchainERC20"icon={<imgsrc="/img/icons/shapes.svg" />} />
Copy file name to clipboardExpand all lines: pages/stack/interop/tutorials/message-passing.mdx
+77-137
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Interop message passing tutorial
3
3
lang: en-US
4
-
description: Learn how to pass messages between chains in the Superchain using the L2ToL2CrossDomainMessenger contract.
4
+
description: Learn to implement cross-chain communication in the Superchain by building a message passing system using the L2ToL2CrossDomainMessenger contract.
5
5
---
6
6
7
7
import { Callout } from'nextra/components'
@@ -12,34 +12,82 @@ import { InteropCallout } from '@/components/WipCallout'
12
12
13
13
# Interop message passing tutorial
14
14
15
-
<Callout>
16
-
This is a step-by-step tutorial.
17
-
You can find an explanation of how this works [here](../message-passing).
15
+
## Overview
16
+
17
+
This tutorial demonstrates how to implement cross-chain communication within the Superchain ecosystem. You'll build a complete
18
+
message passing system that enables different chains to interact with each other using the `L2ToL2CrossDomainMessenger` contract.
19
+
20
+
### What You'll Build
21
+
* A Greeter contract that stores and updates messages
22
+
* A GreetingSender contract that sends cross-chain messages
23
+
* A TypeScript application to relay messages between chains
24
+
25
+
### What you'll learn
18
26
19
-
[See here](#use-the-eth-optimismviem-library-to-relay-the-message), if you want a working code example.
27
+
* How to deploy contracts across different chains
28
+
* How to implement cross-chain message passing
29
+
* How to handle sender verification across chains
30
+
* How to relay messages manually between chains
31
+
32
+
<Callout>
33
+
This tutorial provides step-by-step instructions for implementing cross-chain messaging.
34
+
For a conceptual overview,
35
+
see the [message passing explainer](../message-passing).
20
36
</Callout>
21
37
22
38
In this tutorial, you will learn how to use the [`L2ToL2CrossDomainMessenger`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol) contract to pass messages between interoperable blockchains.
23
39
24
40
## Prerequisites
25
41
26
-
Before starting this tutorial, ensure you have:
42
+
Before starting this tutorial, ensure your development environment meets the following requirements:
27
43
28
-
* Basic knowledge of Solidity and TypeScript
29
-
* Experience with smart contract development
44
+
### Technical knowledge
45
+
46
+
* Intermediate Solidity programming
47
+
* Basic TypeScript knowledge
48
+
* Understanding of smart contract development
30
49
* Familiarity with blockchain concepts
31
-
* A Unix-like environment (Linux, macOS, or WSL for Windows)
32
50
51
+
### Development environment
52
+
53
+
* Unix-like operating system (Linux, macOS, or WSL for Windows)
54
+
* Node.js version 16 or higher
55
+
* Git for version control
56
+
57
+
### Required tools
58
+
59
+
The tutorial uses these primary tools:
60
+
* Foundry: For smart contract development
61
+
* Supersim: For local blockchain simulation
62
+
* TypeScript: For implementation
63
+
* Viem: For blockchain interaction
64
+
65
+
66
+
## Setting up your development environment
67
+
68
+
<Steps>
69
+
70
+
### Follow the [Installation Guide](/app-developers/tutorials/supersim/getting-started/installation) to install:
71
+
* Foundry for smart contract development
72
+
* Supersim for local blockchain simulation
33
73
34
-
## Installing required tools
74
+
### Verify your installation:
75
+
```sh
76
+
forge --version
77
+
supersim --version
78
+
```
35
79
36
-
Before starting this tutorial, refer to the [Installation Guide](/app-developers/tutorials/supersim/getting-started/installation) to set up **Foundry** and **Supersim** on your system.
80
+
</Steps>
37
81
38
82
## Implementing onchain message passing (in Solidity)
39
83
40
-
This section demonstrates how to deploy smart contracts for cross-chain messaging and send messages between them.
41
-
For now, we will ignore the need for executing messages by turning on autorelay.
42
-
Read more on [how to relay your own messages](#javascript-message-relaying).
84
+
The implementation consists of three main components:
85
+
86
+
1.**Greeter Contract**: Deployed on `Chain B`, receives and stores messages.
87
+
2.**GreetingSender Contract**: Deployed on `Chain A`, initiates cross-chain messages.
88
+
3.**Message relay system**: Ensures message delivery between chains.
89
+
90
+
For development purposes, we'll first use autorelay mode to handle message execution automatically. Later sections cover [manual message relaying](#javascript-message-relaying) for production environments.
43
91
44
92
<Steps>
45
93
@@ -48,7 +96,7 @@ Read more on [how to relay your own messages](#javascript-message-relaying).
48
96
1. In the directory where Supersim is installed, start it with autorelay.
49
97
50
98
```sh
51
-
./supersim --interop.autorelay
99
+
supersim
52
100
```
53
101
54
102
Supersim creates three `anvil` blockchains:
@@ -90,34 +138,13 @@ To verify that the chains are running, check the balance of `$USER_ADDR`.
90
138
```sh
91
139
mkdir onchain-code
92
140
cd onchain-code
93
-
forge init
141
+
forge init
94
142
```
95
-
96
143
2. In `src/Greeter.sol` put this file.
97
144
This is a variation on [Hardhat's Greeter contract](https://github.com/matter-labs/hardhat-zksync/blob/main/examples/upgradable-example/contracts/Greeter.sol).
98
145
99
-
```solidity
100
-
//SPDX-License-Identifier: MIT
101
-
pragma solidity ^0.8.0;
102
-
103
-
contract Greeter {
104
-
string greeting;
105
-
106
-
event SetGreeting(
107
-
address indexed sender, // msg.sender
108
-
string greeting
109
-
);
110
-
111
-
function greet() public view returns (string memory) {
112
-
return greeting;
113
-
}
114
-
115
-
function setGreeting(string memory _greeting) public {
The message is the [calldata](https://docs.soliditylang.org/en/latest/internals/layout_in_calldata.html) sent to the destination contract.
240
-
The easiest way to calldata is the use [`abi.encodeCall`](https://docs.soliditylang.org/en/latest/units-and-global-variables.html#abi-encoding-and-decoding-functions).
This function encodes a call to `setGreeting` and sends it to a contract on another chain.
247
234
248
-
Actually send the message using [`L2ToL2CrossDomainMessenger.sendMessage`](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L2ToL2CrossDomainMessenger.sol#L128-L154).
235
+
*`abi.encodeCall(Greeter.setGreeting, (greeting))` constructs the [calldata](https://docs.soliditylang.org/en/latest/internals/layout_in_calldata.html) by encoding the function selector and parameters.
236
+
* The encoded message is then passed to `messenger.sendMessage`, which forwards it to the destination contract (`greeterAddress`) on the specified chain (`greeterChainId`).
237
+
This ensures that `setGreeting` is executed remotely with the provided `greeting` value.
249
238
239
+
250
240
</details>
251
241
252
242
1. Deploy `GreetingSender` to chain A.
@@ -379,7 +369,7 @@ In this section we change `Greeter.sol` to emit a separate event in it receives
379
369
380
370
</Steps>
381
371
382
-
## Javascript message relaying
372
+
## Implement manual message relaying
383
373
384
374
So far we relied on `--interop.autorelay` to send the executing messages to chain B.
385
375
But we only have it because we're using a development system.
@@ -468,59 +458,9 @@ We use [TypeScript](https://www.typescriptlang.org/) to have type safety combine
468
458
469
459
1. Create or replace `src/app.mts` with this code.
0 commit comments