Send a message from L1 Goerli to L2 Optimism testnet.
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.
L2Contract.sol
L1Contract.sol
deployL2.js
deployL1.js
sendL1ToL2Message.js
waitForInclusion.js
getGreetingOnL2.js
git clone https://github.com/miguelmota/optimism-messenger-l1-to-l2-example.git
cd optimism-messenger-l1-to-l2-example
npm install
Create .env
PRIVATE_KEY=123...
Make sure private key has funds on both Goerli and Optimism testnet.
npx hardhat compile
Command
npx hardhat run --network goerli scripts/deployL1.js
Output
L1Contract deployed to: 0x21DEC68E1D07B3cC138A5b70063cC744793865e5
Command
L1_CONTRACT=0x21DEC68E1D07B3cC138A5b70063cC744793865e5 \
npx hardhat run --network optimism scripts/deployL2.js
Output
L2Contract deployed to: 0x0bbe3d35D1aA220ad238f578E7720dA4a1FdC30C
Command (replace env vars with your values)
GREETING="hello world" \
L1_CONTRACT=0x21DEC68E1D07B3cC138A5b70063cC744793865e5 \
L2_CONTRACT=0x0bbe3d35D1aA220ad238f578E7720dA4a1FdC30C \
npx hardhat run --network goerli scripts/sendL1ToL2Message.js
Output
sent tx hash 0xadf720ae44585f5244afc2050c09469a2d55d4ea559e0620fe92fa6733b2bf71
https://goerli.etherscan.io/tx/0xadf720ae44585f5244afc2050c09469a2d55d4ea559e0620fe92fa6733b2bf71
Command
L1_TX_HASH=0xadf720ae44585f5244afc2050c09469a2d55d4ea559e0620fe92fa6733b2bf71 \
npx hardhat run --network optimism scripts/waitForInclusion.js
Output
Waiting for L2 root inclusion (this may take up to 5 minutes)...
L2 root inclusion confirmed
L2 message relayed
Command
L2_CONTRACT=0x0bbe3d35D1aA220ad238f578E7720dA4a1FdC30C \
npx hardhat run --network optimism scripts/getGreetingOnL2.js
Output
greeting: hello world
See https://github.com/miguelmota/optimism-messenger-l2-to-l1-example