Giter Club home page Giter Club logo

optimism-messenger-l2-to-l1-example's Introduction

Optimism Messenger L2->L1 Example

Send a message from L2 Optimism testnet to L1 Goerli.

Example

There's two contracts; L2Contract.sol and L1Contract.sol

The L2 contract has a method sendGreetingMessageToL1 that sends a message form L2 to L1 contract to set a greeting message on L1 contract. It sends the encoded calldata to execute setGreeting on L1 which can only be called if the message was sent by the L2 contract.

Files

Install

git clone https://github.com/miguelmota/optimism-messenger-l2-to-l1-example.git
cd optimism-messenger-l2-to-l1-example
npm install

Set Signer

Create .env

PRIVATE_KEY=123...

Make sure private key has funds on both Optimism testnet and Goerli.

Compile Contracts

npx hardhat compile

Deploy L2 Contract

Command

npx hardhat run --network optimism scripts/deployL2.js

Output

L2Contract deployed to: 0x91CB0FEd918626866aC82120727ec1B649Ce7f11

Deploy L1 Contract

Command

L2_CONTRACT=0x91CB0FEd918626866aC82120727ec1B649Ce7f11 \
npx hardhat run --network goerli scripts/deployL1.js

Output

L1Contract deployed to: 0xAdB642a1122413d826cBE35a5F4c328Fc80c3Ff0

Send L2->L1 Message

Command (replace env vars with your values)

GREETING="hello world" \
L2_CONTRACT=0x91CB0FEd918626866aC82120727ec1B649Ce7f11 \
L1_CONTRACT=0xAdB642a1122413d826cBE35a5F4c328Fc80c3Ff0 \
npx hardhat run --network optimism scripts/sendL2ToL1Message.js

Output

sent tx hash 0x0536dbf443e596b5c7f23dc09a2d03b8d8dbdb73e20ff53254f5360e9ff561f3
https://goerli-optimism.etherscan.io/tx/0x0536dbf443e596b5c7f23dc09a2d03b8d8dbdb73e20ff53254f5360e9ff561f3

Wait for L1 Root Inclusion

Command

L2_TX_HASH=0x0536dbf443e596b5c7f23dc09a2d03b8d8dbdb73e20ff53254f5360e9ff561f3 \
npx hardhat run --network optimism scripts/waitForInclusion.js

Output

Waiting for L1 root inclusion (this may take up to 10 minutes)...
L1 message ready to prove

Finalize Message On L1

Command

L2_TX_HASH=0x0536dbf443e596b5c7f23dc09a2d03b8d8dbdb73e20ff53254f5360e9ff561f3 \
npx hardhat run --network goerli scripts/finalizeMessageOnL1.js

Output

prove message tx: 0x3c87b97838a1f7674104683d0fc3801ed837c8dff6142862538531b20e538ec6
https://goerli.etherscan.io/tx/0x3c87b97838a1f7674104683d0fc3801ed837c8dff6142862538531b20e538ec6
relay message tx: 0x69b3dd18fa9033f445ba8d411e6b5e61edd3230f25e3accecf3c10c672234f4f
https://goerli.etherscan.io/tx/0x69b3dd18fa9033f445ba8d411e6b5e61edd3230f25e3accecf3c10c672234f4f
message relayed on L1

Get Greeting on L1

Command

L1_CONTRACT=0xd7f8d5a683D51fF90Aff7C25430CA3abAe3F80A0 \
npx hardhat run --network goerli scripts/getGreetingOnL1.js

Output

greeting: hello world

Send L1->L2 Message

See https://github.com/miguelmota/optimism-messenger-l1-to-l2-example

License

MIT @ Miguel Mota

optimism-messenger-l2-to-l1-example's People

Contributors

miguelmota avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.