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 Enter the private key of a new wallet you created for this step.
DEPLOYER_PRIVATE_KEY
that contains the private key of a wallet you want to deploy the contract from.Use one of the rich wallets as the
DEPLOYER_PRIVATE_KEY
when using a local node.5
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 ↗
anvil-zksync
option when prompted: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 Enter the private key of one of the rich wallets.
DEPLOYER_PRIVATE_KEY
configuration variable to use one of the pre-funded rich wallet private keys.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:stuff slice staff easily soup parent arm payment cotton trade scatter struggle