DEV Community

EgorMajj
EgorMajj

Posted on • Updated on

Deploying a smart contract on the ZkSync Era test network

Image description
Minimum server requirements
1 CPUs, 1 GB RAM, 30 GB SSD, 32 TB Traffic, Ubuntu 20.04

I use Contabo
https://contabo.com

Deploying a smart contract

Updating system

sudo apt update
sudo apt upgrade

When installing, press y

Install the curl boot loader
sudo apt install -y curl

Running the installation script for Node.js 18 (LTS)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Installing Node.js 18
sudo apt install -y nodejs
We execute the commands one at a time
mkdir greeter-example
cd greeter-example
apt install cmdtest
Press y

PRC Official Source: https://era.zksync.io/docs/dev/fundamentals/interacting.html#connecting-to-zksync-era-on-metamask:~:text=Testnet%20network%20info

Adding the ZkSync Era Network to the Metamask: https://chainlist.org/?search=zkSync+Era+test&testnets=true

or manually

Network Name: zkSync Era Testnet
New RPC URL: https://zksync2-testnet.zksync.dev
Chain ID: 280
Currency Symbol: ETH
Block Explorer URL: https://goerli.explorer.zksync.io/
WebSocket URL: wss://zksync2-testnet.zksync.dev/ws

PRC Official Source: https://era.zksync.io/docs/dev/fundamentals/interacting.html#connecting-to-zksync-era-on-metamask:~:text=Testnet%20network%20inf

Bridging test tokens from Goerli to the ZkSync Era network: https://portal.zksync.io/bridge

Execute commands
npm init --y
npm install --save-dev hardhat
npm install -g npm@9.6.0
npx hardhat

Select Create a TypeScript project, then Enter and several times y

Execute commands
mkdir greeter
cd greeter
npm init -y

npm add -D typescript ts-node @types/node ethers@^5.7.2 zksync-web3@^0.13.1 @ethersproject/hash @ethersproject/web hardhat @matterlabs/hardhat-zksync-solc @matterlabs/hardhat-zksync-deploy

vim hardhat.config.ts

After opening the page, press i to edit and type the script below

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

module.exports = {
  zksolc: {
    version: "1.3.1",
    compilerSource: "binary",
    settings: {},
  },
  defaultNetwork: "zkTestnet",
  networks: {
    zkTestnet: {
      url: "https://zksync2-testnet.zksync.dev", // URL of the zkSync network RPC
      ethNetwork: "goerli", // Can also be the RPC URL of the Ethereum network (e.g. `https://goerli.infura.io/v3/<API_KEY>`)
      zksync: true,
    },
  },
  solidity: {
    version: "0.8.17",
  },
};
Enter fullscreen mode Exit fullscreen mode

Execute commands
mkdir contracts
mkdir deploy
vim contracts/Greeter.sol

After opening the page, press i to edit and type the script below

//SPDX-License-Identifier: Unlicensed
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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Then press Esc and :wq

Execute commands
npx hardhat compile
vim deploy/deploy.ts

The edit page will open, press i again and paste the script below

import { utils, Wallet } 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 we want to deploy.
  const deployer = new Deployer(hre, wallet);
  const artifact = await deployer.loadArtifact("Greeter");

  // Deposit some funds to L2 in order to be able to perform L2 transactions.
  const depositAmount = ethers.utils.parseEther("0.001");
  const depositHandle = await deployer.zkWallet.deposit({
    to: deployer.zkWallet.address,
    token: utils.ETH_ADDRESS,
    amount: depositAmount,
  });
  // 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 greeting = "Hi there!";
  const greeterContract = await deployer.deploy(artifact, [greeting]);

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

  // Call the deployed contract.
  const greetingFromContract = await greeterContract.greet();
  if (greetingFromContract == greeting) {
    console.log(`Contract greets us with ${greeting}!`);
  } else {
    console.error(`Contract said something unexpected: ${greetingFromContract}`);
  }

  // Edit the greeting of the contract
  const newGreeting = "Hey guys";
  const setNewGreetingHandle = await greeterContract.setGreeting(newGreeting);
  await setNewGreetingHandle.wait();

  const newGreetingFromContract = await greeterContract.greet();
  if (newGreetingFromContract == newGreeting) {
    console.log(`Contract greets us with ${newGreeting}!`);
  } else {
    console.error(`Contract said something unexpected: ${newGreetingFromContract}`);
  }
}
Enter fullscreen mode Exit fullscreen mode

At const wallet = new Wallet(""); change to your private metamask key.

DISCLAIMER: Deploy a smart contract from a new wallet with no real funds to avoid financial loss!

Then press Esc and :wq
Execute commands
npx hardhat deploy-zksync

The text should appear

Greeter was deployed to (your metamask number)
Contract greets us with Hi there!!
Contract greets us with Hey guys!
Enter fullscreen mode Exit fullscreen mode

Go to the explorer https://goerli.explorer.zksync.io/ , enter your metamask number and we see three transactions, because after the deploy command immediately occurs and interaction with the contract.

This means that the smart-contract is deployed!

Congratulations!

Top comments (0)