Getting started
Learn how to use Hardhat with ZKsync.
Hardhat is an Ethereum development environment, designed for easy smart contract development. One of its most prominent features is extendability: you can easily add new plugins to your hardhat project.
Along with the official plugins, there are other plugins from the community that you can use with Validium.
To learn more about Hardhat itself, check out the official documentation.
This tutorial shows you how to setup a Validium Solidity project with Hardhat using the ZKsync CLI.
If you are using Vyper, check out the Vyper plugin documentation or the vyper-example in GitHub!
Project setup
To create a new project run the zksync-cli create command, passing a project name:
Solidity project
npx zksync-cli create demo --template hardhat_solidityVyper project
npx zksync-cli create demo --template hardhat_vyperThis command creates a demo folder and clones a Hardhat template project inside it. The downloaded project is already configured and contains all the required plugins.
If you want to migrate an existing project, please check the project migration guide.
Hardhat configuration
The hardhat.config.ts file contains some Validium specific configurations:
The Validium deployment and compiler plugin imports:
Solidity project
The zksolc block contains the minimal configuration for the compiler.
Vyper project
The zkvyper block contains the minimal configuration for the compiler.
Network
The network endpoints of the zkSyncTestnet network change dynamically for local tests.
For local Validium testing, modify url and ethNetwork in hardhat.config.ts to align with your local Validium and Ethereum node's L2 and L1 RPC URLs, respectively.This template project includes a basic unit test in the /test folder that runs with the local-setup and can be executed with yarn test.
Set your Private Key
Rename .env.example to .env and set your private key:
Your private key will be used for paying the costs of deploying the smart contract.
Compile and deploy a contract
Smart contracts belong in the contracts folder.
1. To compile the contract, run
You'll see the following output:
The artifacts-zk and cache-zk folders appear in the root directory (instead of the regular Hardhat's artifacts and cache). These folders contain the compilation artifacts (including contract's ABIs) and compiler cache files.
The artifacts-zk and cache-zk folders are included in the .gitignore file.
The deploy-greeter.ts script is in the deploy folder. This script uses the Deployer class from the hardhat-zksync-deploy package to deploy the Greeter.sol/Greeter.vy contract.
2. To execute the deployment script run
This script deploys the Greeting contract with the message "Hi there!" to Validium Sepolia Testnet.
You should see something like this:
Congratulations! You have deployed a smart contract project to Validium Sepolia Testnet with Hardhat 🎉
Request-Rate Exceeded message:
This message is caused by using the default RPC endpoints provided by ethers.
To avoid this, use your own Sepolia RPC endpoint in the
hardhat.config.tsfile.Find multiple node providers here.
Interact with the contract
The template project contains another script to interact with the contract.
Enter the address of the deployed Greeter contract in the
CONTRACT_ADDRESSvariable of theuse-greeter.tsscript:use-greeter.tsTo execute the script, run:
The script will:
Retrieve the message from the contract by calling the
greet()method.Update the greeting message in the contract with the
setGreeting()method.Retrieve the message from the contract again.
You should see something like this:
Learn more
To learn more about the ZKsync Hardhat plugins check out the plugins documentation.
If you want to know more about how to interact with ZKsync using Javascript, check out the zksync-ethers Javascript SDK documentation.
Check the installation guide for instructions!
Last updated