Send a message from L1 Goerli to L2 zkSync testnet.
There's two contracts; L1Contract.sol
and L2Contract.sol
The L1 contract has a method sendGreetingMessageToL2
that sends a message 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
deployL1.js
deployL2.js
sendL1ToL2Message.js
waitForInclusion.js
getGreetingOnL2.js
git clone https://github.com/miguelmota/zksync-messenger-l1-to-l2-example.git
cd zksync-messenger-l1-to-l2-example
npm install
Create .env
PRIVATE_KEY=123...
Make sure private key has funds on both zkSync testnet and Goerli.
npx hardhat compile
Command
npx hardhat run --network goerli scripts/deployL1.js
Output
deployed to 0xbb027F3d45CEb4c9F8F1FaB6c7Bad5053A66c9b0
Command
L1_CONTRACT=0xbb027F3d45CEb4c9F8F1FaB6c7Bad5053A66c9b0 \
npx hardhat deploy-zksync --network zksync
Output
deployed to 0xdA90933EAC728c6e9AF0f5403514439a4Bb6ae90
Command (replace env vars with your values)
GREETING="hello world" \
L1_CONTRACT=0xbb027F3d45CEb4c9F8F1FaB6c7Bad5053A66c9b0 \
L2_CONTRACT=0xdA90933EAC728c6e9AF0f5403514439a4Bb6ae90 \
npx hardhat run --network goerli scripts/sendL1ToL2Message.js
Output
sent tx hash 0x70b1c11f608a2319fd07dc64f6a0de35ce4e1d483fe0c2a8f09be45c61fb035b
https://goerli.etherscan.io/tx/0x70b1c11f608a2319fd07dc64f6a0de35ce4e1d483fe0c2a8f09be45c61fb0
Command
L1_TX_HASH=0x70b1c11f608a2319fd07dc64f6a0de35ce4e1d483fe0c2a8f09be45c61fb035b \
npx hardhat run --network zksync scripts/waitForInclusion.js
Output
Waiting for L2 block inclusion (this may take up to 20 minutes)...
{
to: '0xdA90933EAC728c6e9AF0f5403514439a4Bb6ae90',
from: '0xbb027F3d45CEb4c9F8F1FaB6c7Bad5053A66c9b0',
contractAddress: null,
transactionIndex: 0,
...
l1BatchNumber: 626758,
l1BatchTxIndex: 123,
l2ToL1Logs: [ ... ],
byzantium: true
}
Command
L2_CONTRACT=0xdA90933EAC728c6e9AF0f5403514439a4Bb6ae90 \
npx hardhat run --network zksync scripts/getGreetingOnL2.js
Output
greeting: hello world