hardhat-zksync-ethers

Integrate ZKsync with Hardhat using the hardhat-zksync-ethers plugin.


circle-info

Ensure you are using the correct version of the plugin with ethers:

  • For plugin version <1.0.0:

    • Compatible with ethers v5.

  • For plugin version ≥1.0.0:

    • Compatible with ethers v6 (⭐ Recommended)

Examples are adopted for plugin version >=1.0.0

Installation

@matterlabs/hardhat-zksync-ethersarrow-up-right

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

yarnnpmbun

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

Configuration

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

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

Tasks

This plugin creates no additional tasks.

Environment extensions

This plugins adds an zksync-ethers object to the Hardhat Runtime Environment.

This object has the same API as zksync-ethersarrow-up-right, with some extra Hardhat-specific functionality.

Helpers

Helpers added to zksync-ethers object:

  • providerL2() - returns a zk.Provider for L2, automatically connected to the selected network.

  • providerL1() - returns a ethers.Provider for L1, automatically connected to the selected network.

  • getWallet(privateKeyOrIndex?: string | number) - returns zk.Wallet for the given private key or index. If the network is set to local and the private key is not provided, the method will return a wallet for rich accounts with the default index of 0 or the specified index. If the accounts object is set in the hardhat config and the private key is not specified, the method will return the wallet for the given account with the default index 0 or for the specified index.

  • getWallets() - returns all wallets of type zk.Wallet. If the network is set to local, the method will return wallets for rich accounts. If the accounts object is set in the hardhat config for the used network, the method will return the wallets for the provided accounts.

  • getContractFactory(name: string, wallet?: zk.Wallet, deploymentType?: DeploymentType) - returns a zk.ContractFactory for provided artifact name.

  • getContractFactory: (abi: any[], bytecode: ethers.BytesLike,wallet?: Wallet,deploymentType?: DeploymentType) - returns a zk.ContractFactory for provided artifact abi and bytecode.

  • getContractFactoryFromArtifact(artifact: ZkSyncArtifact, wallet?: zk.Wallet, deploymentType?: DeploymentType) - returns a zk.ContractFactory for provided artifact.

  • getContractAt(nameOrAbi: string | any[], address: string | Address, wallet?: zk.Wallet) - returns zk.Contract for provided artifact name or abi and address of deployed contract

  • getContractAtFromArtifact: (artifact: ZkSyncArtifact, address: string, wallet?: zk.Wallet) - returns zk.ContractFactory for provided artifact and address of deployed contract

  • getImpersonatedSigner(address: string) - impersonates zk.Signer from address

  • loadArtifact(name: string) - load ZkSyncArtifact from contract name

  • extractFactoryDeps(artifact: ZkSyncArtifact) - extracts factory deps from artifact

  • deployContract(artifact: ZkSyncArtifact, constructorArguments: any[], wallet?: zk.Wallet, overrides?: ethers.Overrides, additionalFactoryDeps?: ethers.BytesLike[]) - deploys contract, for more details check out the deployment section of the quickstart.

  • If wallet?: zk.Wallet is not provided and if the network is set to local, the default wallet will be the first account in the list of rich accounts. If an accounts object is set in the hardhat config for the used network, the default wallet will be taken from that object.

  • If deploymentType?: DeploymentType is not provided default value will be create.

Usage

Install it and access zksync-ethers through the Hardhat Runtime Environment anywhere you need it (tasks, scripts, tests, etc). For example:

Task usage:

Script usage:

Last updated