1
1
---
2
- title : Communicating between OP Mainnet and Ethereum in Solidity
2
+ title : Communicating between OP Stack and Ethereum in Solidity
3
3
lang : en-US
4
- description : Learn how to write Solidity contracts on OP Mainnet and Ethereum that can talk to each other.
4
+ description : Learn how to write Solidity contracts on OP Stack and Ethereum that can talk to each other.
5
5
---
6
6
7
7
import { Steps , Callout , Tabs } from ' nextra/components'
8
- import { WipCallout } from ' @/components/WipCallout'
9
8
10
- <WipCallout />
11
- # Communicating between OP Mainnet and Ethereum in Solidity
9
+ # Communicating between OP Stack and Ethereum in Solidity
12
10
13
- This tutorial explains how to write Solidity contracts on OP Mainnet and Ethereum that can talk to each other.
14
- Here you'll use a contract on OP Mainnet that can set a "greeting" variable on a contract on Ethereum, and vice-versa.
11
+ This tutorial explains how to write Solidity contracts on OP Stack and Ethereum that can talk to each other.
12
+ Here you'll use a contract on OP Stack that can set a "greeting" variable on a contract on Ethereum, and vice-versa.
15
13
This is a simple example, but the same technique can be used to send any kind of message between the two chains.
16
14
17
15
You won't actually be deploying any smart contracts as part of this tutorial.
18
- Instead, you'll reuse existing contracts that have already been deployed to OP Mainnet and Ethereum.
16
+ Instead, you'll reuse existing contracts that have already been deployed to OP Stack and Ethereum.
19
17
Later in the tutorial you'll learn exactly how these contracts work so you can follow the same pattern to deploy your own contracts.
20
18
21
19
<Callout type = " info" >
22
- Just looking to bridge tokens between OP Mainnet and Ethereum?
23
- Check out the tutorial on [ Bridging ERC-20 Tokens to OP Mainnet With the Optimism SDK ] ( ./cross-dom-bridge-erc20 ) .
20
+ Just looking to bridge tokens between OP Stack and Ethereum?
21
+ Check out the tutorial on [ Bridging ERC-20 Tokens to OP Stack With the viem ] ( ./cross-dom-bridge-erc20 ) .
24
22
</Callout >
25
23
26
24
## Message passing basics
27
25
28
- OP Mainnet uses a smart contract called the ` CrossDomainMessenger ` to pass messages between OP Mainnet and Ethereum.
26
+ OP Stack uses a smart contract called the ` CrossDomainMessenger ` to pass messages between OP Stack and Ethereum.
29
27
Both chains have a version of this contract (the ` L1CrossDomainMessenger ` and the ` L2CrossDomainMessenger ` ).
30
- Messages sent from Ethereum to OP Mainnet are automatically relayed behind the scenes.
31
- Messages sent from OP Mainnet to Ethereum must be explicitly relayed with a second transaction on Ethereum.
28
+ Messages sent from Ethereum to OP Stack are automatically relayed behind the scenes.
29
+ Messages sent from OP Stack to Ethereum must be explicitly relayed with a second transaction on Ethereum.
32
30
Read more about message passing in the guide to [ Sending Data Between L1 and L2] ( /builders/app-developers/bridging/messaging ) .
33
31
34
32
## Dependencies
@@ -80,8 +78,8 @@ It will take a few minutes for your message to reach L2.
80
78
Feel free to take a quick break while you wait.
81
79
82
80
<Callout type = " info" >
83
- You can use the Optimism SDK to programmatically check the status of any message between L1 and L2.
84
- Later on in this tutorial you'll learn how to use the Optimism SDK and the ` waitForMessageStatus ` function to wait for various message statuses.
81
+ You can use Viem to programmatically check the status of any message between L1 and L2.
82
+ Later on in this tutorial you'll learn how to use Viem and the ` waitToProve ` function to wait for various message statuses.
85
83
This same function can be used to wait for a message to be relayed from L1 to L2.
86
84
</Callout >
87
85
@@ -127,36 +125,27 @@ Feel free to keep this tab open so you can easily copy the transaction hash late
127
125
128
126
{ <h3 >Create a demo project folder</h3 >}
129
127
130
- You're going to use the Optimism SDK to prove and relay your message to L1.
131
- Since the Optimism SDK is a [ Node.js] ( https://nodejs.org/en/ ) library, you'll need to create a Node.js project to use it.
128
+ You're going to use the viem to prove and relay your message to L1.
132
129
133
130
``` bash
134
- mkdir op-sample-project
135
- cd op-sample-project
131
+ mkdir cross-dom
132
+ cd cross-dom
136
133
```
137
134
138
- { <h3 >Initialize the Project </h3 >}
135
+ { <h3 >Initialize the project </h3 >}
139
136
140
137
Set up the project as a basic Node.js project with ` pnpm ` or your favorite package manager.
141
138
142
139
``` bash
143
140
pnpm init
144
141
```
145
142
146
- { <h3 >Install the Optimism SDK </h3 >}
143
+ { <h3 >Install viem </h3 >}
147
144
148
- Install the Optimism SDK with ` pnpm ` or your favorite package manager.
145
+ Install Viem with ` pnpm ` or your favorite package manager.
149
146
150
147
``` bash
151
- pnpm add @eth-optimism/sdk
152
- ```
153
-
154
- { <h3 >Install ethers.js</h3 >}
155
-
156
- Install ` ethers ` with ` pnpm ` or your favorite package manager.
157
-
158
- ``` bash
159
- pnpm add ethers@^5
148
+ pnpm add viem
160
149
```
161
150
162
151
{ <h3 >Add your private key to your environment</h3 >}
@@ -187,46 +176,27 @@ Start the Node.js REPL with the `node` command.
187
176
node
188
177
```
189
178
190
- { <h3 >Import the Optimism SDK</h3 >}
191
-
192
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L3 hash=26b2fdb17dd6c8326a54ec51f0769528
193
- ```
194
-
195
- { <h3 >Import ethers.js</h3 >}
179
+ { <h3 >Import Viem</h3 >}
196
180
197
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L4 hash=69a65ef97862612e4978b8563e6dbe3a
198
- ```
199
-
200
- { <h3 >Load your private key</h3 >}
201
-
202
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L6 hash=755b77a7ffc7dfdc186f36c37d3d847a
181
+ ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L3-L5 hash=65b9a5ad5b634bc2e424f5664e6e1f84
203
182
```
204
183
205
184
{ <h3 >Load your transaction hash</h3 >}
206
185
207
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L8 hash=320cd4f397d7bed8d914d4be0c99f8dc
186
+ ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L7 hash=320cd4f397d7bed8d914d4be0c99f8dc
208
187
```
209
188
210
189
{ <h3 >Create the RPC providers and wallets</h3 >}
211
190
212
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L10-L13 hash=9afdce50665ae93bce602068071ffaa1
213
- ```
214
-
215
- { <h3 >Create a CrossChainMessenger instance</h3 >}
216
-
217
- The Optimism SDK exports a ` CrossChainMessenger ` class that makes it easy to prove and relay cross-chain messages.
218
-
219
- Create an instance of the ` CrossChainMessenger ` class:
220
-
221
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L15-L20 hash=997b9c4cdd5fb1f9d4e0882a683ae016
191
+ ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L8-L9 hash=d47e4991c5153e1e1dc55de5047a8a3e
222
192
```
223
193
224
194
{ <h3 >Wait until the message is ready to prove</h3 >}
225
195
226
- The second step to send messages from L2 to L1 is to prove that the message was sent on L2.
196
+ Next, you will send messages from L2 to L1 is to prove that the message was sent on L2.
227
197
You first need to wait until the message is ready to prove.
228
198
229
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L23 hash=25a072666b6147f8d8983d8223f045b8
199
+ ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L12-L16 hash=df275e659d954eb72b8c5765d9baf6de
230
200
```
231
201
232
202
<Callout type = " info" >
@@ -238,34 +208,34 @@ Feel free to take a quick break while you wait.
238
208
239
209
Once the message is ready to be proven, you'll send an L1 transaction to prove that the message was sent on L2.
240
210
241
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L26 hash=17922abea43b3d379404fedd87422dde
211
+ ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L18-L23 hash=e4d608ac2c2ceb5a744c8474679bd8cb
242
212
```
243
213
244
214
{ <h3 >Wait until the message is ready for relay</h3 >}
245
215
246
216
The final step to sending messages from L2 to L1 is to relay the messages on L1.
247
217
This can only happen after the fault proof period has elapsed.
248
- On OP Mainnet , this takes 7 days.
218
+ On OP Stack , this takes 7 days.
249
219
250
220
<Callout >
251
221
We're currently testing fault proofs on OP Sepolia, so withdrawal times reflect Mainnet times.
252
222
</Callout >
253
223
254
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L29 hash=45d995aab47ec29afee4bb4577ae9303
224
+ ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L25- L29 hash=88cec1db2fde515ea9008eaa1bbdfd73
255
225
```
256
226
257
227
{ <h3 >Relay the message on L1</h3 >}
258
228
259
229
Once the withdrawal is ready to be relayed you can finally complete the message sending process.
260
230
261
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L32 hash=b5515811ffcf8b9ada15dea8ae666e44
231
+ ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L31-L33 hash=d7e47c9787d92e2140622a6bdcc6d7bb
262
232
```
263
233
264
234
{ <h3 >Wait until the message is relayed</h3 >}
265
235
266
236
Now you simply wait until the message is relayed.
267
237
268
- ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L35 hash=d6e7f89e929eea0ac3217a6751b7e578
238
+ ``` js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L35-L36 hash=4ff3cdc48f17cfd7de4a6ef2d2671dc2
269
239
```
270
240
271
241
{ <h3 >Check the L1 Greeter</h3 >}
@@ -348,10 +318,10 @@ You can follow a similar pattern in your own smart contracts.
348
318
## Conclusion
349
319
350
320
You just learned how you can write Solidity contracts on Sepolia and OP Sepolia that can talk to each other.
351
- You can follow the same pattern to write contracts that can talk to each other on Ethereum and OP Mainnet .
321
+ You can follow the same pattern to write contracts that can talk to each other on Ethereum and OP Stack .
352
322
353
323
This sort of cross-chain communication is useful for a variety of reasons.
354
- For example, the [ Standard Bridge] ( /builders/app-developers/bridging/standard-bridge ) contracts use this same system to bridge ETH and ERC-20 tokens between Ethereum and OP Mainnet .
324
+ For example, the [ Standard Bridge] ( /builders/app-developers/bridging/standard-bridge ) contracts use this same system to bridge ETH and ERC-20 tokens between Ethereum and OP Stack .
355
325
356
- One cool way to take advantage of cross-chain communication is to do most of your heavy lifting on OP Mainnet and then send a message to Ethereum only when you have important results to share.
357
- This way you can take advantage of the low gas costs on OP Mainnet while still being able to use Ethereum when you need it.
326
+ One cool way to take advantage of cross-chain communication is to do most of your heavy lifting on OP Stack and then send a message to Ethereum only when you have important results to share.
327
+ This way you can take advantage of the low gas costs on OP Stack while still being able to use Ethereum when you need it.
0 commit comments