Tasks in Hardhat For Everything

2021-09-29

I’m using the Hardhat when implementation of the Smart contract.
When we implement the smart contracts, there are many things to do. For example, test, deploy, mint, etc…

In fact, I think it’s much easier to do that with Hardhat Task than creating deploy.js or mint.js in some cases. By the way, npx hardhat test is the easiest way for testing.

For example, add task() functions in hardhat.config.js to mint your token:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// hardhat.config.js

require("@nomiclabs/hardhat-ethers");

task("mint", "Mints a token")
.addParam("address", "The address to receive a token")
.addParam("amount", "The amount of token")
.setAction(async (taskArgs) => {
// Create the contract instance
const MyToken = await ethers.getContractFactory("MyToken");
const myToken = await Aus.attach("0x80c5...");

// Mint
await myToken.mint(taskArgs.address, taskArgs.amount);
});

module.exports = {
// ...
}

After that, run the command to mint.

1
npx hardhat mint --address 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --amount 10

I feel that it is easier to manage frequetly used things by writing them together in hardhat.config.js as a task.