DEV Community

hideckies
hideckies

Posted on • Updated on • Originally published at blog.hdks.org

Tasks in Hardhat For Everything

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:

// 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 MyToken.attach("0x80c5...");

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

module.exports = {
    // ...
}
Enter fullscreen mode Exit fullscreen mode

After that, run the command to mint.

npx hardhat mint --address 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --amount 10
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)