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.

  1. cd out of L1-governance and initialize the L2-counter project:
    npx zksync-cli create L2-counter --template hardhat_solidity
    
  2. Use the same private key for the WALLET_PRIVATE_KEY variable in the .env file.
  3. 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

  1. In the L2-counter/contracts/ directory, create a new file Counter.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.
  2. 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;
      }
    }
    
  3. Compile the contract from the L2-counter root:
    Compilations happens only after the installation of a compiler.
    npm run compile
    

Made with ❤️ by the ZKsync Community