Skip to content

miguelmota/polygon-zkevm-messenger-l1-to-l2-example

Repository files navigation

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