Skip to content

Commit 0e1e783

Browse files
authored
Merge pull request #989 from ethereum-optimism/cross-chain
Communicating Between OP Mainnet and Ethereum in Solidity
2 parents d52a005 + b458819 commit 0e1e783

File tree

3 files changed

+63
-91
lines changed

3 files changed

+63
-91
lines changed

pages/builders/app-developers/tutorials/cross-dom-solidity.mdx

+35-65
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
---
2-
title: Communicating between OP Mainnet and Ethereum in Solidity
2+
title: Communicating between OP Stack and Ethereum in Solidity
33
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.
55
---
66

77
import { Steps, Callout, Tabs } from 'nextra/components'
8-
import { WipCallout } from '@/components/WipCallout'
98

10-
<WipCallout />
11-
# Communicating between OP Mainnet and Ethereum in Solidity
9+
# Communicating between OP Stack and Ethereum in Solidity
1210

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.
1513
This is a simple example, but the same technique can be used to send any kind of message between the two chains.
1614

1715
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.
1917
Later in the tutorial you'll learn exactly how these contracts work so you can follow the same pattern to deploy your own contracts.
2018

2119
<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).
2422
</Callout>
2523

2624
## Message passing basics
2725

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.
2927
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.
3230
Read more about message passing in the guide to [Sending Data Between L1 and L2](/builders/app-developers/bridging/messaging).
3331

3432
## Dependencies
@@ -80,8 +78,8 @@ It will take a few minutes for your message to reach L2.
8078
Feel free to take a quick break while you wait.
8179

8280
<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.
8583
This same function can be used to wait for a message to be relayed from L1 to L2.
8684
</Callout>
8785

@@ -127,36 +125,27 @@ Feel free to keep this tab open so you can easily copy the transaction hash late
127125

128126
{<h3>Create a demo project folder</h3>}
129127

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.
132129

133130
```bash
134-
mkdir op-sample-project
135-
cd op-sample-project
131+
mkdir cross-dom
132+
cd cross-dom
136133
```
137134

138-
{<h3>Initialize the Project</h3>}
135+
{<h3>Initialize the project</h3>}
139136

140137
Set up the project as a basic Node.js project with `pnpm` or your favorite package manager.
141138

142139
```bash
143140
pnpm init
144141
```
145142

146-
{<h3>Install the Optimism SDK</h3>}
143+
{<h3>Install viem</h3>}
147144

148-
Install the Optimism SDK with `pnpm` or your favorite package manager.
145+
Install Viem with `pnpm` or your favorite package manager.
149146

150147
```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
160149
```
161150

162151
{<h3>Add your private key to your environment</h3>}
@@ -187,46 +176,27 @@ Start the Node.js REPL with the `node` command.
187176
node
188177
```
189178

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>}
196180

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
203182
```
204183

205184
{<h3>Load your transaction hash</h3>}
206185

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
208187
```
209188

210189
{<h3>Create the RPC providers and wallets</h3>}
211190

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
222192
```
223193

224194
{<h3>Wait until the message is ready to prove</h3>}
225195

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.
227197
You first need to wait until the message is ready to prove.
228198

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
230200
```
231201

232202
<Callout type="info">
@@ -238,34 +208,34 @@ Feel free to take a quick break while you wait.
238208

239209
Once the message is ready to be proven, you'll send an L1 transaction to prove that the message was sent on L2.
240210

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
242212
```
243213

244214
{<h3>Wait until the message is ready for relay</h3>}
245215

246216
The final step to sending messages from L2 to L1 is to relay the messages on L1.
247217
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.
249219

250220
<Callout>
251221
We're currently testing fault proofs on OP Sepolia, so withdrawal times reflect Mainnet times.
252222
</Callout>
253223

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
255225
```
256226

257227
{<h3>Relay the message on L1</h3>}
258228

259229
Once the withdrawal is ready to be relayed you can finally complete the message sending process.
260230

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
262232
```
263233

264234
{<h3>Wait until the message is relayed</h3>}
265235

266236
Now you simply wait until the message is relayed.
267237

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
269239
```
270240

271241
{<h3>Check the L1 Greeter</h3>}
@@ -348,10 +318,10 @@ You can follow a similar pattern in your own smart contracts.
348318
## Conclusion
349319

350320
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.
352322

353323
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.
355325

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.

pages/builders/chain-operators/configuration/batcher.mdx

+6-6
Original file line numberDiff line numberDiff line change
@@ -262,24 +262,24 @@ default value is `10`.
262262
<Tabs.Tab>`OP_BATCHER_NUM_CONFIRMATIONS=10`</Tabs.Tab>
263263
</Tabs>
264264

265-
### altda.da-server
265+
### plasma.da-server
266266

267267
HTTP address of a DA Server.
268268

269269
<Tabs items={['Syntax', 'Example', 'Environment Variable']}>
270-
<Tabs.Tab>`--altda.da-server=<value>`</Tabs.Tab>
271-
<Tabs.Tab>`--altda.da-server=`</Tabs.Tab>
270+
<Tabs.Tab>`--plasma.da-server=<value>`</Tabs.Tab>
271+
<Tabs.Tab>`--plasma.da-server=`</Tabs.Tab>
272272
<Tabs.Tab>`OP_BATCHER_PLASMA_DA_SERVER=`</Tabs.Tab>
273273
</Tabs>
274274

275-
### altda.da-service
275+
### plasma.da-service
276276

277277
Use DA service type where commitments are generated by plasma server. The
278278
default value is `false`.
279279

280280
<Tabs items={['Syntax', 'Example', 'Environment Variable']}>
281-
<Tabs.Tab>`--altda.da-service=<value>`</Tabs.Tab>
282-
<Tabs.Tab>`--altda.da-service=false`</Tabs.Tab>
281+
<Tabs.Tab>`--plasma.da-service=<value>`</Tabs.Tab>
282+
<Tabs.Tab>`--plasma.da-service=false`</Tabs.Tab>
283283
<Tabs.Tab>`OP_BATCHER_PLASMA_DA_SERVICE=false`</Tabs.Tab>
284284
</Tabs>
285285

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
(async () => {
22

3-
const optimism = require("@eth-optimism/sdk")
4-
const ethers = require("ethers")
5-
6-
const privateKey = process.env.TUTORIAL_PRIVATE_KEY
3+
const { createPublicClient, http } = require('viem');
4+
const { optimismSepolia } = require('viem/chains');
5+
const { publicActionsL1, publicActionsL2} = require('viem/op-stack');
76

87
const transactionHash = process.env.TUTORIAL_TRANSACTION_HASH
98

10-
const l1Provider = new ethers.providers.StaticJsonRpcProvider("https://rpc.ankr.com/eth_sepolia")
11-
const l2Provider = new ethers.providers.StaticJsonRpcProvider("https://sepolia.optimism.io")
12-
const l1Wallet = new ethers.Wallet(privateKey, l1Provider)
13-
const l2Wallet = new ethers.Wallet(privateKey, l2Provider)
14-
15-
const messenger = new optimism.CrossChainMessenger({
16-
l1ChainId: 11155111, // 11155111 for Sepolia, 1 for Ethereum
17-
l2ChainId: 11155420, // 11155420 for OP Sepolia, 10 for OP Mainnet
18-
l1SignerOrProvider: l1Wallet,
19-
l2SignerOrProvider: l2Wallet,
20-
})
9+
const l1Provider = createPublicClient({ chain: sepolia, transport: http("https://rpc.ankr.com/eth_sepolia") }).extend(publicActionsL1())
10+
const l2Provider = createPublicClient({ chain: optimismSepolia, transport: http("https://sepolia.optimism.io") }).extend(publicActionsL2());
2111

2212
console.log('Waiting for message to be provable...')
23-
await messenger.waitForMessageStatus(transactionHash, optimism.MessageStatus.READY_TO_PROVE)
13+
await l1Provider.getWithdrawalStatus({
14+
receipt,
15+
targetChain: l2Provider.chain,
16+
})
2417

2518
console.log('Proving message...')
26-
await messenger.proveMessage(transactionHash)
19+
const receipt = await l2Provider.getTransactionReceipt(transactionHash)
20+
const output = await l1Provider.waitToProve({
21+
receipt,
22+
targetChain: l2Provider.chain,
23+
})
2724

2825
console.log('Waiting for message to be relayable...')
29-
await messenger.waitForMessageStatus(transactionHash, optimism.MessageStatus.READY_FOR_RELAY)
26+
await l1Provider.getWithdrawalStatus({
27+
receipt,
28+
targetChain: l2Provider.chain,
29+
})
3030

3131
console.log('Relaying message...')
32-
await messenger.finalizeMessage(transactionHash)
32+
const [message] = getWithdrawals(receipt)
33+
await l1Provider.waitToFinalize({ withdrawalHash: message.withdrawalHash, targetChain: l2Provider.chain })
3334

3435
console.log('Waiting for message to be relayed...')
35-
await messenger.waitForMessageStatus(transactionHash, optimism.MessageStatus.RELAYED)
36+
await l1Provider.getWithdrawalStatus({ receipt, targetChain: l2Provider.chain })
37+
3638

3739
})()

0 commit comments

Comments
 (0)