How to deploy contract ZKSYNC ERA TESTNET.
March 13th, 2023

Today i will teach you how to deploy @zksync contract in ERA ZKSYNC TESNET.

*1 Open https://goerlifaucet.com/ *

login/register from Alchemy and faucet some ETH Goerli.

2 Add ZKSync tesnet chain in your wallet

https://chainlist.org/?testnets=true&search=zksync+era+testnet

3 *Connect to official bridge https://goerli.portal.zksync.io/bridge/withdraw *

and bridge some funds in tesntet chain from goerli (Switch chain like on screen at right bottom corner)

Funds will moved in ~3 mins.
Funds will moved in ~3 mins.

4 Open/Install Visual Studio Code - https://code.visualstudio.com/ and install extensions - Solidity, npm Intellisense

5 Create folder and open from visual code.

6 Open terminal and type this code:

npm init -y   

7 Type next code:

npm i -D typescript ts-node ethers zksync-web3 hardhat @matterlabs/hardhat-zksync-solc @matterlabs/hardhat-zksync-deploy

8 Create the hardhat.config.ts file and paste the following code there:

import "@matterlabs/hardhat-zksync-deploy";
import "@matterlabs/hardhat-zksync-solc";

module.exports = {
  zksolc: {
    version: "1.3.1",
    compilerSource: "binary",
    settings: {},
  },
  defaultNetwork: "zkSyncTestnet",

  networks: {
    zkSyncTestnet: {
      url: "https://zksync2-testnet.zksync.dev",
      ethNetwork: "goerli", // Can also be the RPC URL of the network (e.g. `https://goerli.infura.io/v3/<API_KEY>`)
      zksync: true,
    },
  },
  solidity: {
    version: "0.8.17",
  },
};

9 Create the contracts and deploy folders. The former is the place where we will store all the smart contracts' *.sol files, and the latter is the place where we will put all the scripts related to deploying the contracts.

10 Create the contracts/Greeter.sol contract and paste the following code in it:

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        greeting = _greeting;
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        greeting = _greeting;
    }
}

11 Compile the contract with the following command in terminal:

npx hardhat compile

12 Create the following deployment script in deploy/deploy.ts

import { Wallet, utils } from "zksync-web3";
import * as ethers from "ethers";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { Deployer } from "@matterlabs/hardhat-zksync-deploy";

// An example of a deploy script that will deploy and call a simple contract.
export default async function (hre: HardhatRuntimeEnvironment) {
  console.log(`Running deploy script for the Greeter contract`);

  // Initialize the wallet.
  const wallet = new Wallet("<WALLET-PRIVATE-KEY>");

  // Create deployer object and load the artifact of the contract you want to deploy.
  const deployer = new Deployer(hre, wallet);
  const artifact = await deployer.loadArtifact("Greeter");

  // Estimate contract deployment fee
  const greeting = "Hi there!";
  const deploymentFee = await deployer.estimateDeployFee(artifact, [greeting]);

  // OPTIONAL: Deposit funds to L2
  // Comment this block if you already have funds on zkSync.
  const depositHandle = await deployer.zkWallet.deposit({
    to: deployer.zkWallet.address,
    token: utils.ETH_ADDRESS,
    amount: deploymentFee.mul(2),
  });
  // Wait until the deposit is processed on zkSync
  await depositHandle.wait();

  // Deploy this contract. The returned object will be of a `Contract` type, similarly to ones in `ethers`.
  // `greeting` is an argument for contract constructor.
  const parsedFee = ethers.utils.formatEther(deploymentFee.toString());
  console.log(`The deployment is estimated to cost ${parsedFee} ETH`);

  const greeterContract = await deployer.deploy(artifact, [greeting]);

  //obtain the Constructor Arguments
  console.log("constructor args:" + greeterContract.interface.encodeDeploy([greeting]));

  // Show the contract info.
  const contractAddress = greeterContract.address;
  console.log(`${artifact.contractName} was deployed to ${contractAddress}`);
}

13 Replacing the WALLET-PRIVATE-KEY with the private key of the Ethereum wallet you're using for development, and run the script using the following command to run the deployment script:

hardhat deploy-zksync

14 Terminal gives you log like this

15 Copy address from “Greeter was deployed to ADDRESS HERE

16 Open goerli.explorer.zksync.io, past contract address and search it

17 Click “Contract” and after click “Verify contract”.

Name of contract - Greeter

*Code contract copy from step #10 *

And signature code copy from Visual Studio Code in last log from terminal (Its somethink like

constructor args:0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005211241212657265210000000000000000000000000000000000000000000000

Click VERIFY.

Congratulations! You have deployed a smart contract to zkSync Era Testnet 🎉

Follow me in twitter and DM your questions:

Official links of ZKSYNC:

Subscribe to wizard
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from wizard

Skeleton

Skeleton

Skeleton