Skip to content

Commit

Permalink
Merge pull request #989 from ethereum-optimism/cross-chain
Browse files Browse the repository at this point in the history
Communicating Between OP Mainnet and Ethereum in Solidity
  • Loading branch information
krofax authored Nov 1, 2024
2 parents d52a005 + b458819 commit 0e1e783
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 91 deletions.
100 changes: 35 additions & 65 deletions pages/builders/app-developers/tutorials/cross-dom-solidity.mdx
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
---
title: Communicating between OP Mainnet and Ethereum in Solidity
title: Communicating between OP Stack and Ethereum in Solidity
lang: en-US
description: Learn how to write Solidity contracts on OP Mainnet and Ethereum that can talk to each other.
description: Learn how to write Solidity contracts on OP Stack and Ethereum that can talk to each other.
---

import { Steps, Callout, Tabs } from 'nextra/components'
import { WipCallout } from '@/components/WipCallout'

<WipCallout />
# Communicating between OP Mainnet and Ethereum in Solidity
# Communicating between OP Stack and Ethereum in Solidity

This tutorial explains how to write Solidity contracts on OP Mainnet and Ethereum that can talk to each other.
Here you'll use a contract on OP Mainnet that can set a "greeting" variable on a contract on Ethereum, and vice-versa.
This tutorial explains how to write Solidity contracts on OP Stack and Ethereum that can talk to each other.
Here you'll use a contract on OP Stack that can set a "greeting" variable on a contract on Ethereum, and vice-versa.
This is a simple example, but the same technique can be used to send any kind of message between the two chains.

You won't actually be deploying any smart contracts as part of this tutorial.
Instead, you'll reuse existing contracts that have already been deployed to OP Mainnet and Ethereum.
Instead, you'll reuse existing contracts that have already been deployed to OP Stack and Ethereum.
Later in the tutorial you'll learn exactly how these contracts work so you can follow the same pattern to deploy your own contracts.

<Callout type="info">
Just looking to bridge tokens between OP Mainnet and Ethereum?
Check out the tutorial on [Bridging ERC-20 Tokens to OP Mainnet With the Optimism SDK](./cross-dom-bridge-erc20).
Just looking to bridge tokens between OP Stack and Ethereum?
Check out the tutorial on [Bridging ERC-20 Tokens to OP Stack With the viem](./cross-dom-bridge-erc20).
</Callout>

## Message passing basics

OP Mainnet uses a smart contract called the `CrossDomainMessenger` to pass messages between OP Mainnet and Ethereum.
OP Stack uses a smart contract called the `CrossDomainMessenger` to pass messages between OP Stack and Ethereum.
Both chains have a version of this contract (the `L1CrossDomainMessenger` and the `L2CrossDomainMessenger`).
Messages sent from Ethereum to OP Mainnet are automatically relayed behind the scenes.
Messages sent from OP Mainnet to Ethereum must be explicitly relayed with a second transaction on Ethereum.
Messages sent from Ethereum to OP Stack are automatically relayed behind the scenes.
Messages sent from OP Stack to Ethereum must be explicitly relayed with a second transaction on Ethereum.
Read more about message passing in the guide to [Sending Data Between L1 and L2](/builders/app-developers/bridging/messaging).

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

<Callout type="info">
You can use the Optimism SDK to programmatically check the status of any message between L1 and L2.
Later on in this tutorial you'll learn how to use the Optimism SDK and the `waitForMessageStatus` function to wait for various message statuses.
You can use Viem to programmatically check the status of any message between L1 and L2.
Later on in this tutorial you'll learn how to use Viem and the `waitToProve` function to wait for various message statuses.
This same function can be used to wait for a message to be relayed from L1 to L2.
</Callout>

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

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

You're going to use the Optimism SDK to prove and relay your message to L1.
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.
You're going to use the viem to prove and relay your message to L1.

```bash
mkdir op-sample-project
cd op-sample-project
mkdir cross-dom
cd cross-dom
```

{<h3>Initialize the Project</h3>}
{<h3>Initialize the project</h3>}

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

```bash
pnpm init
```

{<h3>Install the Optimism SDK</h3>}
{<h3>Install viem</h3>}

Install the Optimism SDK with `pnpm` or your favorite package manager.
Install Viem with `pnpm` or your favorite package manager.

```bash
pnpm add @eth-optimism/sdk
```

{<h3>Install ethers.js</h3>}

Install `ethers` with `pnpm` or your favorite package manager.

```bash
pnpm add ethers@^5
pnpm add viem
```

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

{<h3>Import the Optimism SDK</h3>}

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L3 hash=26b2fdb17dd6c8326a54ec51f0769528
```

{<h3>Import ethers.js</h3>}
{<h3>Import Viem</h3>}

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L4 hash=69a65ef97862612e4978b8563e6dbe3a
```

{<h3>Load your private key</h3>}

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L6 hash=755b77a7ffc7dfdc186f36c37d3d847a
```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L3-L5 hash=65b9a5ad5b634bc2e424f5664e6e1f84
```

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

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L8 hash=320cd4f397d7bed8d914d4be0c99f8dc
```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L7 hash=320cd4f397d7bed8d914d4be0c99f8dc
```

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

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L10-L13 hash=9afdce50665ae93bce602068071ffaa1
```

{<h3>Create a CrossChainMessenger instance</h3>}

The Optimism SDK exports a `CrossChainMessenger` class that makes it easy to prove and relay cross-chain messages.

Create an instance of the `CrossChainMessenger` class:

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L15-L20 hash=997b9c4cdd5fb1f9d4e0882a683ae016
```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L8-L9 hash=d47e4991c5153e1e1dc55de5047a8a3e
```

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

The second step to send messages from L2 to L1 is to prove that the message was sent on L2.
Next, you will send messages from L2 to L1 is to prove that the message was sent on L2.
You first need to wait until the message is ready to prove.

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L23 hash=25a072666b6147f8d8983d8223f045b8
```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L12-L16 hash=df275e659d954eb72b8c5765d9baf6de
```

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

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

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L26 hash=17922abea43b3d379404fedd87422dde
```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L18-L23 hash=e4d608ac2c2ceb5a744c8474679bd8cb
```

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

The final step to sending messages from L2 to L1 is to relay the messages on L1.
This can only happen after the fault proof period has elapsed.
On OP Mainnet, this takes 7 days.
On OP Stack, this takes 7 days.

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

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L29 hash=45d995aab47ec29afee4bb4577ae9303
```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L25-L29 hash=88cec1db2fde515ea9008eaa1bbdfd73
```

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

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

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L32 hash=b5515811ffcf8b9ada15dea8ae666e44
```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L31-L33 hash=d7e47c9787d92e2140622a6bdcc6d7bb
```

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

Now you simply wait until the message is relayed.

```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L35 hash=d6e7f89e929eea0ac3217a6751b7e578
```js file=<rootDir>/public/tutorials/cross-dom-solidity.js#L35-L36 hash=4ff3cdc48f17cfd7de4a6ef2d2671dc2
```

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

You just learned how you can write Solidity contracts on Sepolia and OP Sepolia that can talk to each other.
You can follow the same pattern to write contracts that can talk to each other on Ethereum and OP Mainnet.
You can follow the same pattern to write contracts that can talk to each other on Ethereum and OP Stack.

This sort of cross-chain communication is useful for a variety of reasons.
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.
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.

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.
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.
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.
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.
12 changes: 6 additions & 6 deletions pages/builders/chain-operators/configuration/batcher.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,24 @@ default value is `10`.
<Tabs.Tab>`OP_BATCHER_NUM_CONFIRMATIONS=10`</Tabs.Tab>
</Tabs>

### altda.da-server
### plasma.da-server

HTTP address of a DA Server.

<Tabs items={['Syntax', 'Example', 'Environment Variable']}>
<Tabs.Tab>`--altda.da-server=<value>`</Tabs.Tab>
<Tabs.Tab>`--altda.da-server=`</Tabs.Tab>
<Tabs.Tab>`--plasma.da-server=<value>`</Tabs.Tab>
<Tabs.Tab>`--plasma.da-server=`</Tabs.Tab>
<Tabs.Tab>`OP_BATCHER_PLASMA_DA_SERVER=`</Tabs.Tab>
</Tabs>

### altda.da-service
### plasma.da-service

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

<Tabs items={['Syntax', 'Example', 'Environment Variable']}>
<Tabs.Tab>`--altda.da-service=<value>`</Tabs.Tab>
<Tabs.Tab>`--altda.da-service=false`</Tabs.Tab>
<Tabs.Tab>`--plasma.da-service=<value>`</Tabs.Tab>
<Tabs.Tab>`--plasma.da-service=false`</Tabs.Tab>
<Tabs.Tab>`OP_BATCHER_PLASMA_DA_SERVICE=false`</Tabs.Tab>
</Tabs>

Expand Down
42 changes: 22 additions & 20 deletions public/tutorials/cross-dom-solidity.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
(async () => {

const optimism = require("@eth-optimism/sdk")
const ethers = require("ethers")

const privateKey = process.env.TUTORIAL_PRIVATE_KEY
const { createPublicClient, http } = require('viem');
const { optimismSepolia } = require('viem/chains');
const { publicActionsL1, publicActionsL2} = require('viem/op-stack');

const transactionHash = process.env.TUTORIAL_TRANSACTION_HASH

const l1Provider = new ethers.providers.StaticJsonRpcProvider("https://rpc.ankr.com/eth_sepolia")
const l2Provider = new ethers.providers.StaticJsonRpcProvider("https://sepolia.optimism.io")
const l1Wallet = new ethers.Wallet(privateKey, l1Provider)
const l2Wallet = new ethers.Wallet(privateKey, l2Provider)

const messenger = new optimism.CrossChainMessenger({
l1ChainId: 11155111, // 11155111 for Sepolia, 1 for Ethereum
l2ChainId: 11155420, // 11155420 for OP Sepolia, 10 for OP Mainnet
l1SignerOrProvider: l1Wallet,
l2SignerOrProvider: l2Wallet,
})
const l1Provider = createPublicClient({ chain: sepolia, transport: http("https://rpc.ankr.com/eth_sepolia") }).extend(publicActionsL1())
const l2Provider = createPublicClient({ chain: optimismSepolia, transport: http("https://sepolia.optimism.io") }).extend(publicActionsL2());

console.log('Waiting for message to be provable...')
await messenger.waitForMessageStatus(transactionHash, optimism.MessageStatus.READY_TO_PROVE)
await l1Provider.getWithdrawalStatus({
receipt,
targetChain: l2Provider.chain,
})

console.log('Proving message...')
await messenger.proveMessage(transactionHash)
const receipt = await l2Provider.getTransactionReceipt(transactionHash)
const output = await l1Provider.waitToProve({
receipt,
targetChain: l2Provider.chain,
})

console.log('Waiting for message to be relayable...')
await messenger.waitForMessageStatus(transactionHash, optimism.MessageStatus.READY_FOR_RELAY)
await l1Provider.getWithdrawalStatus({
receipt,
targetChain: l2Provider.chain,
})

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

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


})()

0 comments on commit 0e1e783

Please sign in to comment.