Cross Chain Tutorial L2 Counter Part 1
L2 Counter
Now that we have an address for the L1 governance contract, we can build, deploy, and test the counter contract on L2.
cd
out ofL1-governance
and initialize theL2-counter
project:npx zksync-cli create L2-counter --template hardhat_solidity
- Use the same private key for the
WALLET_PRIVATE_KEY
variable in the.env
file. - For the purposes of this tutorial, we don't need the example smart contracts and deployment script files generated by
zksync-cli
. Run the following commands to remove them:cd L2-counter rm -rf ./contracts/* rm -rf ./scripts/*
Create L2 Counter Contract
- In the
L2-counter/contracts/
directory, create a new fileCounter.sol
.touch contracts/Counter.sol
This contract contains the address of the governance contract deployed previously on layer 1, and an incrementable counter which can only be invoked by the governance contract. - Copy/paste the following code into the file:Counter.sol
// SPDX-License-Identifier: Unlicense pragma solidity ^0.8.17; contract Counter { uint256 public value = 0; address public governance; constructor(address newGovernance) { governance = newGovernance; } function increment() public { require(msg.sender == governance, 'Only governance is allowed'); value += 1; } }
- Compile the contract from the
L2-counter
root:Compilations happens only after the installation of a compiler.npm run compile