Hardhat Smart Contract Development

Prerequisites

  • MetaMask installed & connected to a local Creditcoin node or public testnet

  • A funded account

  • Node.js & NPM installed

Setting up Hardhat

To create a Hardhat project you must initialize a node project using npm. Navigate to the folder where you want to build your project and run:

npx hardhat init

If you don’t have Hardhat installed globally, you’ll be prompted to do so.

Follow the prompts to set up your project. You can choose options like creating a sample project or starting an empty project. For this tutorial, we will crate a TypeScript project.

Writing your first contract

For this tutorial we will use a simple Counter contract.

Copy the contents of https://github.com/gluwa/creditcoin3/blob/dev/docs/smart-contract-development/with-hardhat/contracts/Counter.sol into contracts/Counter.sol inside the directory of your Hardhat project.

Compiling

Before you can deploy or test your contract you need to compile it. Run the command:

npx hardhat compile

Testing

Before you deploy the contract, you want to make sure it works as expected. You can use Hardhat to write and run some tests for the Counter contract. Copy the contents of https://github.com/gluwa/creditcoin3/blob/dev/docs/smart-contract-development/with-hardhat/test/Counter.ts into test/Counter.ts inside the directory of your Hardhat project.

Test the contract by running the command

You should see output similar to:

where Counter contract is the contract that you had just added and Lock is another contract which was automatically generated by the hardhat init command. To add more tests to the contract, add more it statements to its test file!

Deploying a smart contract

To deploy your contract, you will need to write an ignition module for Hardhat. Copy the contents of https://github.com/gluwa/creditcoin3/blob/dev/docs/smart-contract-development/with-hardhat/ignition/modules/Counter.ts into ignition/modules/Counter.ts inside the directory of your Hardhat project!

You can deploy the contract by running the command:

Without the --network flag, the deployment will run against the local Hardhat Network, which means it gets lost after the command finishes running. But it is useful for testing that your ignition module works.

To deploy the contract to a remote network, you will need to define that network inside the hardhat.config.ts file. See for example https://github.com/gluwa/creditcoin3/blob/dev/docs/smart-contract-development/with-hardhat/hardhat.config.ts and https://github.com/gluwa/creditcoin3/blob/dev/docs/smart-contract-development/with-hardhat/hardhat-creditcoin3.config.ts

In this example we will use Creditcoin Testnet.

Once you have updated hardhat.config.ts you will then need to set the private key of your EVM account by running the Hardhat vars set command:

After setting up the new config, run the command:

More information

Please refer to the official documentation of Hardhat at https://hardhat.org/tutorial for more information.

Last updated