Git Product home page Git Product logo

polygon-zkevm-messenger-l1-to-l2-example's Introduction

Polygon zkEVM Messenger L1->L2 Example

Send a message from L1 Goerli to L2 Polygon zkEVM testnet.

Example

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

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

Files

Install

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

Set Signer

Create .env

PRIVATE_KEY=123...

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

Compile Contracts

npx hardhat compile

Deploy L1 Contract

Command

npx hardhat run --network goerli scripts/deployL1.js

Output

L1Contract deployed to: 0xF20b8Fd13c48b68d1820cFd6d0D531a383b1AAaD

Deploy L2 Contract

Command

L1_CONTRACT=0xF20b8Fd13c48b68d1820cFd6d0D531a383b1AAaD \
npx hardhat run --network polygonzkevm scripts/deployL2.js

Output

L2Contract deployed to: 0x7aEF554acf5fBaCd2161027F24528f33fc7fCA95

Send L1->L2 Message

Command (replace env vars with your values)

GREETING="hello world" \
L1_CONTRACT=0xF20b8Fd13c48b68d1820cFd6d0D531a383b1AAaD \
L2_CONTRACT=0x7aEF554acf5fBaCd2161027F24528f33fc7fCA95 \
npx hardhat run --network goerli scripts/sendL1ToL2Message.js

Output

sent tx hash 0xdc954d46caa59e41710a70a4a5b0204fdb3f9f7aae983179a3ee6bfb82ad19bc
https://goerli.etherscan.io/tx/0xdc954d46caa59e41710a70a4a5b0204fdb3f9f7aae983179a3ee6bfb82ad19bc

Wait for L2 Root Inclusion

Command

L1_TX_HASH=0xdc954d46caa59e41710a70a4a5b0204fdb3f9f7aae983179a3ee6bfb82ad19bc \
L2_CONTRACT=0x7aEF554acf5fBaCd2161027F24528f33fc7fCA95 \
npx hardhat run --network polygonzkevm scripts/waitForInclusion.js

Output

Waiting for L2 root inclusion (this may take up to 20 minutes)...
{
  "leaf_type": 1,
  "orig_net": 0,
  "orig_addr": "0xF20b8Fd13c48b68d1820cFd6d0D531a383b1AAaD",
  "amount": "0",
  "dest_net": 1,
  "dest_addr": "0x7aEF554acf5fBaCd2161027F24528f33fc7fCA95",
  "block_num": "8811178",
  "deposit_cnt": "82717",
  "network_id": 0,
  "tx_hash": "0xdc954d46caa59e41710a70a4a5b0204fdb3f9f7aae983179a3ee6bfb82ad19bc",
  "claim_tx_hash": "",
  "metadata": "0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000",
  "ready_for_claim": true
}
Ready to finalize message

Finalize Message On L2

Command

L1_TX_HASH=0xdc954d46caa59e41710a70a4a5b0204fdb3f9f7aae983179a3ee6bfb82ad19bc \
L1_CONTRACT=0xF20b8Fd13c48b68d1820cFd6d0D531a383b1AAaD \
L2_CONTRACT=0x7aEF554acf5fBaCd2161027F24528f33fc7fCA95 \
npx hardhat run --network polygonzkevm scripts/finalizeMessageOnL2.js

Output

verified: true
sent tx hash 0xc98c79f95131148057d79411d55521b145f5c5cbf63df909539dbe589605ab63
https://testnet-zkevm.polygonscan.com/tx/0xc98c79f95131148057d79411d55521b145f5c5cbf63df909539dbe589605ab63

Get Greeting on L2

Command

L2_CONTRACT=0x7aEF554acf5fBaCd2161027F24528f33fc7fCA95 \
npx hardhat run --network polygonzkevm scripts/getGreetingOnL2.js

Output

greeting: hello world

Send L2->L1 Message

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

License

MIT @ Miguel Mota

polygon-zkevm-messenger-l1-to-l2-example's People

Contributors

miguelmota avatar

Stargazers

 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.