Installation

How to install hardhat-zksync for your project.

Before starting development with Hardhat and ZKsync, it's important to have your environment correctly configured. This guide will help you set up a ZKsync project using the ZKsync CLI, a tool that simplifies the process by providing pre-built templates with all necessary Hardhat plugins.

If you're migrating an existing Hardhat project to zksync-era, please refer to the migration guide for detailed instructions.

Prerequisites

System requirements

  • Ensure your machine meets the system requirements for running ZKsync and Hardhat. For Windows users, using Windows Subsystem for Linux (WSL 2) is highly recommended for better compatibility.

Node.js and package manager

Basic knowledge of Ethereum development

  • Familiarity with Ethereum development, including deploying smart contracts. If you are new to this, review the Contract Deployment documentation.

Wallet and testnet funds

  • A wallet with ETH is needed to deploy contracts. Visit the Network Faucets page to get testnet funds.

Install ZKsync CLI

The ZKsync CLI makes it easy to set up a project and perform local testing by providing pre-configured templates. To install the ZKsync CLI globally, use npm or yarn:

npm install -g zksync-cli
Skip the hassle for test ETH by using zksync-cli for local testing. Use the npx zksync-cli dev start command to initialize a local ZKsync development environment, which includes local Ethereum and ZKsync nodes. This method allows you to test contracts without requesting external testnet funds. Explore more in the zksync-cli documentation.

Set Up a new project

Create a new project

Use the ZKsync CLI to create a new Hardhat project. Replace <project-name> with your desired project name. This command will create a <project-name> folder and clone a Hardhat template project into it. Choose the template based on your preferred contract language (Solidity or Vyper):

zksync-cli create <project-name> --template hardhat_solidity

This creates a project folder that is already configured and contains all the required plugins for ZKsync development.

Configure your environment

  1. Environment variables:
    • Rename the .env.example file in your project directory to .env.
    • Add your wallet’s private key to the .env file:
    WALLET_PRIVATE_KEY=YourPrivateKeyHere
    
    Security tip: Use burner wallets for development and testing to avoid using real private keys, which could lead to security risks.
  2. Check configuration:
    • Open the hardhat.config.ts file to confirm the configurations for ZKsync are correct. The default setup should already include the necessary settings for deploying and interacting with smart contracts on ZKsync.

Compile and deploy your contracts

Compile contracts

Ensure your smart contracts are located in the contracts folder of your project. To compile them, use:

yarn hardhat compile

This will compile your contracts and generate the necessary artifacts in the artifacts-zk and cache-zk folders.

  • Contracts must be compiled using the official ZKsync Era compilers, with their respective Hardhat plugins.
  • Contracts compiled with other compilers will fail to deploy to ZKsync Era.

Deploy contracts

Deploy your contracts using Hardhat scripts. A basic deployment script is usually included in the template project (deploy/deploy-greeter.ts):

yarn hardhat deploy-zksync --script deploy/deploy-greeter.ts

This script will deploy your smart contracts to the specified ZKsync testnet.

For more detailed instructions on deploying and testing, refer to the Getting Started guide.

Testing and interaction

Once deployed, you can interact with your smart contracts using scripts included in your project. Make sure to update these scripts with the correct contract addresses and any other required parameters.


Made with ❤️ by the ZKsync Community