hardhat-zksync-node

Guide on using the hardhat-zksync-node plugin for running local ZKSync Era node.

Description

The hardhat-zksync-node plugin provides a convenient way to run anvil-zksync locally using Hardhat. It adds ZKSync-specific provider functionality to the Hardhat Runtime Environment (HRE) and provides helpers for running local nodes.

Resources:

Installation

Prerequisites

  • Node.js version 18 or higher
  • Hardhat version 2.18.0 or higher
Windows Support: anvil-zksync in-memory node binaries are not supported on Windows. Use Windows Subsystem for Linux (WSL) as an alternative.

Setup

Install the plugin using your preferred package manager:

yarn add -D @matterlabs/hardhat-zksync-node

Import the package in your hardhat.config.ts file:

import "@matterlabs/hardhat-zksync-node";

Configuration

Anvil-zksync Configuration

To configure a specific version of anvil-zksync or set a custom binary path, add the zksyncAnvil configuration to your hardhat.config.ts:

const config: HardhatUserConfig = {
  zksyncAnvil: {
    version: '0.3.*', // optional
    binaryPath: 'zksync/target/release/anvil-zksync', // optional
  },
  // ... rest of your config
};

Configuration Options

OptionTypeDescriptionDefault
versionstringanvil-zksync version. Can be latest, specific version, or version with * as patch (e.g., 0.3.*)0.3.*
binaryPathstringLocal path to anvil-zksync binary. Takes precedence over version if set-
Version Resolution: When using a version with * (e.g., 0.3.*), it resolves to the latest patch version for the specified major and minor version.

Network Configuration

Options

OptionTypeDescriptionDefault
zksyncbooleanEnable ZKSync functionality for the networkfalse

Example Configuration

import "@matterlabs/hardhat-zksync-node";

const config: HardhatUserConfig = {
  networks: {
    hardhat: {
      zksync: true,
    }
  },
};

export default config;

Additions

Added Tasks

  • node-zksync: Runs a local anvil-zksync node
  • Overrides hardhat node when zksync: true is set
  • Overrides hardhat run and deployment tasks when zksync: true is set on the hardhat network

Added HRE Extensions

  • network.provider: Extended with ZKSync-specific functionality
  • network.config: Added ZKSync-specific configuration options

Commands

node-zksync

Runs a local anvil-zksync node.

hardhat node-zksync [options]

Options

OptionDescriptionDefault
--portServer listening port8011
--logLog filter level (error/warn/info/debug)info
--log-file-pathLog file locationanvil_zksync.log
--spawn-l1Launch an Anvil L1 node on a specified port8012
--external-l1Connect to an External L1 Node-
--cacheCache type (none/disk/memory)disk
--cache-dirDisk cache directory.cache
--override-bytecodes-dirReplace / override the contract bytecode with the local version-
--show-storage-logsStorage logs (none/read/write/all)none
--show-vm-detailsVM details level (none/all)none
--show-gas-detailsGas details level (none/all)none
--dev-system-contractsLoad locally compiled system contracts (built-in/built-in-no-verify/local)-
--forkFork network (testnet/mainnet/URL)-
--fork-block-numberBlock height for forking-
--replay-txTransaction hash to replay-
--quietStarts in the the quiet mode without logs-
Parameter Restrictions:
  • --replay-tx and --fork-block-number cannot be used together
  • --fork must be specified when using either --replay-tx or --fork-block-number
  • --spawn-l1 and --external-l1 flags cannot be used together because they are mutually exclusive.
Replacing bytecodes:You can also replace / override the contract bytecode with the local version. This is especially useful if you are replaying some mainnet transactions and would like to see how they would behave on the different bytecode. Or when you want to fork mainnet to see how your code would behave on mainnet state.You have to prepare a directory, with files in format 0xabc..93f.json that contain the json outputs that you can get from zkout directories from your compiler.

Usage

Running a Local Node

Start a local ZKSync node using:

yarn hardhat node-zksync

Testing with hardhat-zksync-node

The plugin enhances Hardhat's test task to run against anvil-zksync node:

  1. Use the hardhat network with zksync: true
  2. Node starts automatically with tests
  3. Node shuts down after test completion
  4. Port allocation starts from 8011 to 8021 depends on availability

Troubleshooting

Configuration Issues

Problem: Configuration Issues

  • Plugin import order is incorrect
  • Network configuration is missing ZKSync settings
  • Incompatible anvil-zksync version

Solution:

  • Ensure hardhat-zksync-node is imported after hardhat-zksync-deploy in your config
  • Verify network configuration has zksync: true set
  • Check if anvil-zksync version is compatible with your setup
import "@matterlabs/hardhat-zksync-deploy";
import "@matterlabs/hardhat-zksync-node";
networks: {
  hardhat: {
    zksync: true
  }
}

Provider Connection Issues

Problem: Provider Connection Issues

  • Node is not running
  • Incorrect RPC URL
  • Permission issues

Solution:

  • Ensure the node is running before executing commands
  • Verify RPC URL is correct and accessible
  • Check if you have sufficient permissions to access the node

Compiler Version Issues

Problem: Invalid zksolc compiler version

Solution:

  • Update your anvil-zksync version in config:
    zksyncAnvil: {
      version: "0.3.*" // Use latest compatible version
    }
    
  • Check minimum required version in documentation

Port Issues

Problem: Port already in use

Solution:

  • Change port using --port flag:
    hardhat node-zksync --port 8012
    
  • Or stop other processes using the port

Node Startup Issues

Problem: Failed to start node

Solution:

  • Check system requirements
  • Verify binary path if using custom path
  • Ensure sufficient system resources

For more help, please refer to the GitHub issues.


Made with ❤️ by the ZKsync Community