Skip to main content
Verify your smart contracts work as intended before you deploy them by writing tests.

Testing Smart Contracts

1

Update Hardhat configuration

Ensure your Hardhat configuration file is configured to use zksolc, as outlined in the installation guide:
hardhat.config.ts
2

Install zksync-ethers

The zksync-ethers package provides a modified version of the ethers library that is compatible with Abstract and the ZKsync VM. Install the package by running the following command:
3

Write test definitions

Write test definitions inside the /test directory, for example, test/HelloWorld.test.ts.
test/HelloWorld.test.ts
4

Add a deployer private key

Create a new configuration variable called DEPLOYER_PRIVATE_KEY that contains the private key of a wallet you want to deploy the contract from.
Enter the private key of a new wallet you created for this step.
Use one of the rich wallets as the DEPLOYER_PRIVATE_KEY when using a local node.
5

Get ETH in the deployer account

The deployer account requires ETH to deploy a smart contract.
  • Testnet: Claim ETH via a faucet, or bridge ETH from Sepolia.
  • Mainnet: Use a bridge to transfer ETH to Abstract mainnet.
6

Run the tests

Run the tests by running the following command:

Running a local node

The zksync-cli package provides a command-line interface for instantiating local nodes. Run a local node as your test environment is beneficial for many reasons:
  • Speed: Local nodes are faster than testnet/mainnet, increasing iteration speed.
  • Rich wallets: Local nodes come with “rich wallets” pre-funded with ETH.
  • Isolation: Local nodes are separate environments with no existing state.
1

Run a local node

Docker is required to run a local node. Installation guide ↗
Run a local node by running the following command:
Select the anvil-zksync option when prompted:
This will start a local node and a development wallet for you to use:
2

Add the local node as a Hardhat network

Add the local node as a network in your Hardhat configuration file:
hardhat.config.ts
3

Update the deployer private key configuration variable

Update the DEPLOYER_PRIVATE_KEY configuration variable to use one of the pre-funded rich wallet private keys.
Enter the private key of one of the rich wallets.
Use one of the rich wallets as the DEPLOYER_PRIVATE_KEY when using a local node.
4

Run the tests

Run the tests on the local node using the following command:

Rich Wallets

The local node includes pre-configured “rich” accounts for testing:
Same mnemonic rich wallets Mnemonic: stuff slice staff easily soup parent arm payment cotton trade scatter struggle