Develop and Test Smart Contracts by Hardhat
Ethereum, one of the most widely used blockchain platforms, allows developers to create smart contracts that are fully transparent and programmable based on rules.
Here, in the form of a basic-level project, you will become familiar with Hardhat, a powerful tool for developing, testing, and deploying smart contracts on the Ethereum network.
Building with Hardhat
First, create a new folder for your project and navigate to it in your terminal. Then, run the following command to create a new project using PNPM.
pnpm init
Next, install Hardhat and its dependencies by running the following command:
pnpm add -D hardhat
After the installation is complete, create the Hardhat project structure in your project directory by running the following command.
pnpm hardhat
This command creates a sample project structure with some default configuration files.
You can choose between TypeScript and JavaScript. We'll use TypeScript here.
Creating a Smart Contract
To create an ERC20 contract, first install the OpenZeppelin contracts library. To do this, run the following command.
pnpm add @openzeppelin/contracts
Next, create a new Solidity file named MyToken.sol
in the contracts
directory and import the ERC20 contract from the OpenZeppelin library.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(uint256 initialSupply) ERC20("My Token", "MYT") {
_mint(msg.sender, initialSupply);
}
}
This contract extends the ERC20 contract and sets the name and symbol of your token. The constructor also generates the initial supply and assigns it to the contract sender.
Smart Contract Testing
Hardhat comes with an internal testing framework that allows you to write tests for your smart contracts.
To write tests, create a new test file named MyToken.ts in the test directory and include the necessary dependencies.
import { ethers } from 'hardhat'
import { Contract, Signer } from 'ethers'
import { expect } from 'chai'
describe('MyToken', function () {
let accounts: Signer[]
let myToken: Contract
beforeEach(async function () {
// Get a list of signers/accounts
accounts = await ethers.getSigners()
// Deploy the MyToken contract
const MyToken = await ethers.getContractFactory('MyToken')
myToken = await MyToken.deploy(1000000)
// Wait for the contract to be mined
await myToken.deployed()
})
it('should have a name and symbol', async function () {
expect(await myToken.name()).to.equal('My Token')
expect(await myToken.symbol()).to.equal('MYT')
})
it('should have an initial supply of 1,000,000', async function () {
const ownerAddress = await accounts[0].getAddress()
const balance = await myToken.balanceOf(ownerAddress)
expect(balance.toNumber()).to.equal(1000000)
})
})
This file imports the MyToken
contract test and tests its name, symbol, and initial supply. To run the tests, execute the following command.
pnpm hardhat test
With Hardhat, you can easily initialize a new project, develop and test your smart contract.