Deploy Contract on zkSync Network

MZTACAT
5 min readJun 3, 2023

--

contract deployment on zkSync

INTRO ::

zkSync Era is a Layer-2 protocol that scales Ethereum with cutting-edge ZK tech.

Requirement

Before we begin, I would like to remind you…. this is as simple as ABC.

Take Note : this deployment would be needing your private key! as we are deploying with Hardhat which every developers use!

I made use of VPS which I recommend instead of having your private key saved on your PC!

2.

You need a Laptop for this deployment!!. Use A $7 VPS! its easier or use UBUNTU for those who don’t have fund, download Ubuntu from Microsoft Store.

VPS ALTERNATIVE (PC USERS)

Click below link to get VPS and pay with USDT (bep20 or trc20)!

https://vpsdime.com/a/3592/linux-vps

__________________________

For those setting up VPS, i’m sure you must have gotten and noted your login details (password and IP).

Lets get started

Open up Termius, click on NEW HOST Address should be your IP (e.g 124.64.74.423) you got from the VPS.. if you are using Ubuntu you dont need this. Just open up the Ubuntu.

if you see this, Add as NEW

The interface should look like this for both the Ubuntu and those running VPS. Lets proceed

Server Setup

sudo apt-get update && sudo apt-get install -y
you should get this…

INSTALL CURL :

apt install curl -y
curl installed
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh
sudo bash /tmp/nodesource_setup.sh

INSTALL NODEJS

sudo apt-get install -y nodejs
;nodejs installed

INSTALL VIM :

sudo apt-get install vim
vim installed

TIME FOR DEPLOYMENT:

npm init -y
npm install --save-dev hardhat

PROCEED:

npm install -g npm@9.6.2
npx hardhat
  1. Press the down arrow key and ENTER

2. Press ENTER three times

press ENTER 3 times

CREATE DIRECTORY AND MOVE INTO DIRECTORY

mkdir greeter
cd greeter
npm init -y
npm add -D typescript ts-node @types/node ethers@^5.7.2 zksync-web3@^0.14.3 @ethersproject/hash @ethersproject/web hardhat @matterlabs/hardhat-zksync-solc @matterlabs/hardhat-zksync-deploy

EDIT CONFIG BY PASTING THIS :

vim hardhat.config.ts

INSERT THIS CODE :

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

module.exports = {
zksolc: {
version: "1.3.6",
compilerSource: "binary",
settings: {},
},
defaultNetwork: "zkSyncMainnet",
networks: {
zkSyncMainnet: {
url: "https://zksync2-mainnet.zksync.io",
ethNetwork: "mainnet",
zksync: true,
},
},
solidity: {
version: "0.8.17",
},
};
  1. Press Esc
  2. Type :wq
  3. Press ENTER

CREATE TWO DIRECTORY FOR CONTRACT AND DEPLOY:

mkdir contracts
mkdir deploy
vim contracts/Greeter.sol

INSERT CODES:

//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;
}
}
  1. Press ESC
  2. Write :wq
  3. Press ENTER
npx hardhat compile
compile code
vim deploy/deploy.ts

Change *WALLET-PRIVATE-KEY* in the code to your own private key.

(make sure to protect your private key well…. if you can’t skip this tutorial completely)…. this is the same way most developers deploy contracts on zkSync

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");

// 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}`);
}
}
replace highlighted…. (i copied almost all leaving 6 then typing the rest manually)

Paste the modified code

Press ESC

Write :wq

Press ENTER

DEPLOY

npx hardhat deploy-zksync
THIS IS YOUR CONTRACT ADDRESS

PASTE IN EXPLORER, YOU SHOULD SEE YOUR CONTRACT

THAT’S ALL

Also, check here if you miss previous interaction ($0.9) for mint

https://twitter.com/mztacat/status/1662814413110345728?s=20

If you don’t Understand, go through it 10 times before asking questions! 🫡

--

--