hardhat-zksync-vyper

Guide on using the hardhat-zksync-vyper plugin for compiling Vyper smart contracts on ZKsync Era.

Description

The hardhat-zksync-vyper plugin extends Hardhat's compilation capabilities to support ZKsync Era smart contracts written in Vyper. Key features include:

  • Compilation of Vyper contracts with ZKsync Era-specific compiler
  • Support for ZKsync system contracts and libraries
  • Integration with ZKsync Era VM extensions
  • Flexible optimization modes for different contract sizes

Resources:

Installation

Prerequisites

  • Node.js version 18 or higher
  • Hardhat version 2.16.0 or higher

Setup

Add the latest version of this plugin to your project with the following command:

yarn add -D @matterlabs/hardhat-zksync-vyper @nomiclabs/hardhat-vyper

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

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

Configuration

Configuration Options

NameTypeDescriptionDefaultNotes
versionstring (optional)zkvyper compiler version"latest"Version from zkvyper-bin repository
settings.compilerPathstring (optional)Path to zkvyper binarynone
settings.librariesobject (optional)Non-inlinable library dependencies{}Key-value pairs of library addresses
settings.optimizer.mode"3" | "z" (optional)Optimization mode"3""z" for large contracts
settings.optimizer.fallback_to_optimizing_for_sizeboolean (optional)Compile with mode "z" if bytecode is too largefalseRequires zkvyper ≥1.3.15

Example Configuration

import "@matterlabs/hardhat-zksync-vyper";
import { HardhatUserConfig } from "hardhat/config";

const config: HardhatUserConfig = {
  zkvyper: {
    version: "1.5.10",
    settings: {
      // Compiler Settings
      compilerPath: "zkvyper",

      // Optimization Settings
      optimizer: {
        mode: "3",
        fallback_to_optimizing_for_size: true
      }
    }
  },
  networks: {
    ZKsyncTestnet: {
      url: "https://testnet.era.zksync.dev",
      ethNetwork: "sepolia",
      zksync: true
    }
  }
};

export default config;

Network Configuration

Configure the zksync parameter in the networks to enable the zkvyper compiler:

networks: {
  sepolia: {
    url: "https://sepolia.infura.io/v3/<API_KEY>",
    zksync: false, // disables zkvyper compiler
  },
  ZKsyncTestnet: {
    url: "https://sepolia.era.zksync.dev",
    ethNetwork: "sepolia",
    zksync: true, // enables zkvyper compiler
  }
}

Important Notes

hardhat-zksync-vyper v0.2.0 introduced a default configuration so all parameters are optional.
Compilers are no longer released as Docker images and its usage is no longer recommended.
fallback_to_optimizing_for_size option is supported for zkvyper compiler version 1.3.15 or higher.

Compiler Integrations

The zkvyper compilers are stored in the cache folder with the path {cache}/hardhat-nodejs/compilers-v2/zkvyper. In this location, you can inspect the locally stored compiler versions.

{cache} is a placeholder for a path that is resolved by Hardhat.

The compilerVersion.json file is used by the plugin to get the latest available version and the minimum required compiler version. This file undergoes invalidation every 24 hours (currently), subsequently being updated with fresh information. This approach is implemented to provide a caching mechanism, avoiding the risk of encountering GitHub throttling issues during fetching new releases.

Usage

Basic Compilation

  1. Create your Vyper contracts in the contracts directory
  2. Configure hardhat.config.ts with ZKsync settings
  3. Run compilation:
    npx hardhat compile
    
  4. Find compilation artifacts in artifacts-zk/contracts

Working with Libraries

  1. Define library dependencies in configuration:
    zksolc: {
      settings: {
        libraries: {
          "contracts/MyContract.sol": {
            "MyLibrary": "0x..."
          }
        }
      }
    }
    

Optimization Strategies

  1. For standard contracts:
    optimizer: {
      mode: "3"
    }
    
  2. For large contracts:
    optimizer: {
      mode: "z",
      fallback_to_optimizing_for_size: true
    }
    

Commands

yarn hardhat compile

Compiles all the smart contracts in the contracts directory and creates the artifacts-zk folder with all the compilation artifacts, including factory dependencies for the contracts, which could be used for contract deployment.

For more help, please refer to the GitHub issues.


Made with ❤️ by the ZKsync Community