Using Hardhat
Last updated
Last updated
This tutorial will teach you how to create, compile and deploy a smart contract on the KCC Testnet using Hardhat.
Set up Hardhat
Create a simple smart contract
Compile contract
Test contract
Deploy contract
There are a few technical requirements before we start.
There are a few technical requirements before we start, as listed below:
Once we have those installed, you need to create an npm project by going to an empty folder, running npm init --yes
, and following its instructions to install Hardhat. Once your project is ready, you should run the following:
To create your Hardhat project, run npx hardhat
in your project folder. Let’s create the sample project and follow these steps to try out a sample task and compile, test, and deploy the sample contract.
In your project folder, run npx hardhat
to create a sample project. You should see the following prompt:
Choose the TypeScript project
and The following prompt should appear:
A sample contract in the contracts
folder is Lock.sol
, which consists of a simple digital lock that allows users to withdraw funds only after a certain time has passed.
Go to hardhat.config.ts
Create .env
file in the root to store your private key
Your deployment contract account's private key is located in the .env
file.
To compile a Hardhat project, change to the root of the directory where the project is located and you first need to install Hardhat Toolbox:
And then run to compile:
To run tests with Hardhat, you need to type the following:
And this is an expected output:
Run this command in the root of the project directory:
If you have enough amount of KCS you will see the sample output:
Congratulations! You have successfully deployed the Lock
Smart Contract. The Smart Contract is now available for interaction.
Besides, if you do not have the required amount of KCS, the output may look like this:
is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging, and deploying your smart contracts and dApps, all of which work together to create a complete development environment.
(comes with Node)
The sample project used here comes from the , as well as its instructions.
Add KCC Explorer API key to .env
file to verify the contract on KCC explorer. You can generate an API key by
Find more instructions on how to use DOTENV on .
The contract will be deployed on KCC Testnet, and you can check the deployment status here:
You can go to to get more KCS for testing.