L1 governance contract
In this tutorial we'll deploy a simple smart contract on ZKsync Era. Then we'll deploy another smart contract on Ethereum and send transactions that update the state of the contract on ZKsync Era using the ZKsync Bridge Hub.
What you'll learn:
- How to deploy smart contracts in L1 and L2.
- How to send transactions in L1 that interact with contracts in L2.
This tutorial shows you how to implement communication between L1 and L2 with the following example:
- A Governance Solidity smart contract is deployed on layer 1. This contract has a function that sends a transaction to ZKsync Era layer 2.
- A Counter Solidity smart contract is deployed on ZKsync Era layer 2. This contract stores a number that is incremented
by calling theincrement
method. TheGovernance
contract on layer 1 calls this function.
Prerequisites
- A wallet with sufficient Sepolia
ETH
on Ethereum and ZKsync Sepolia Testnet to pay for deploying smart contracts. You can get Sepolia ETH from the network faucets.- Get testnet
ETH
for ZKsync Era using bridges to bridge funds to ZKsync.
- Get testnet
- You know how to get your private key from your MetaMask wallet.
- You must have Node.js installed.
Complete Project
Download the complete project on GitHub.
Project Setup
Open a terminal window, create a new folder for the project tutorial, e.g. mkdir cross-chain-tutorial
, and cd
into
the folder.
Now create separate folders to store contracts and scripts on L1 and L2. For now we will start with L1-governance folder.
mkdir L1-governance
L1-governance
code is a default Hardhat project used to deploy a contract on L1.
The L2-counter
code includes all ZKsync dependencies and configurations for L2.L1 Governance
cd
into theL1-governance
folder.
cd L1-governance
- Initialise and set up the L1 project.
npx hardhat init
Select the option Create a Typescript project and accept the defaults for everything else.
- Import it from the
@matterlabs/zksync-contracts
npm package (preferred). - Download it from the contracts repo.
- Install the following dependencies:
npm i -D typescript ts-node @openzeppelin/contracts @matterlabs/zksync-contracts@beta @nomicfoundation/hardhat-ethers @typechain/ethers-v6 @typechain/hardhat typechain ethers dotenv
Create L1 Governance Contract
L1-governance
folder.The following Solidity code defines the Governance smart contract.
The constructor sets the contract creator as the single governor.
The callZkSync
function calls a transaction on L2 which can only be called by the governor.
- Remove the existing
/test
directory and any contracts that exist in/contracts
. - Create a new file called
Governance.sol
inside the emptycontracts
folder.
touch contracts/Governance.sol
- Copy/paste the code below into it.
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;
import { IBridgehub, L2TransactionRequestDirect } from '@matterlabs/zksync-contracts/contracts/l1-contracts/bridgehub/IBridgehub.sol';
contract Governance {
address public governor;
constructor() {
governor = msg.sender;
}
function callZkSync(
uint256 chainId,
address bridgeHubAddress,
address contractAddr,
bytes memory data,
uint256 gasLimit,
uint256 gasPerPubdataByteLimit,
uint256 cost
) external payable {
require(msg.sender == governor, 'Only governor is allowed');
IBridgehub zksyncBridgeHub = IBridgehub(bridgeHubAddress);
L2TransactionRequestDirect memory requestInput = L2TransactionRequestDirect(
chainId,
cost,
contractAddr,
0,
data,
gasLimit,
gasPerPubdataByteLimit,
new bytes[](0),
msg.sender
);
zksyncBridgeHub.requestL2TransactionDirect{ value: msg.value }(requestInput);
}
}
Deploy L1 Governance Contract
Create the file L1-Governance/.env
and copy/paste the code below, filling in the relevant values.
Find node provider urls here.
You have to connect your wallet to the network and add the network to the wallet in advance.
NODE_RPC_URL=
PRIVATE_KEY=
Replace the code in hardhat.config.ts
with the following:
import type { HardhatUserConfig } from 'hardhat/config';
import '@nomicfoundation/hardhat-toolbox';
import dotenv from 'dotenv';
dotenv.config();
const config: HardhatUserConfig = {
solidity: '0.8.24',
networks: {
// Sepolia network
sepolia: {
url: process.env.NODE_RPC_URL,
accounts: [process.env.PRIVATE_KEY!],
},
},
};
export default config;
Next, create the file Governance.ts
inside the /ignition/modules
folder and copy/paste the following code into it:
touch ignition/modules/Governance.ts
import { buildModule } from '@nomicfoundation/hardhat-ignition/modules';
const GovernanceModule = buildModule('GovernanceModule', (m) => {
const governance = m.contract('Governance', [], {});
return { governance };
});
export default GovernanceModule;
Then, from the L1-governance
folder root, compile and deploy the contract:
npx hardhat compile
npx hardhat ignition deploy ./ignition/modules/Governance.ts --network sepolia
You should see the following output:
Deploying [ GovernanceModule ]
Batch #1
Executed GovernanceModule#Governance
[ GovernanceModule ] successfully deployed 🚀
Deployed Addresses
GovernanceModule#Governance - 0xA7d27A1202bE1237919Cf2cb60970141100725b4
Save the address to use in a later step.