In Hardhat, "tasks" are a powerful feature that allows developers to automate repetitive tasks and streamline their development workflow. Tasks enable you to define custom commands that can be executed in the Hardhat environment, making it easier to interact with your smart contracts, deploy them, and perform various actions without writing extensive scripts each time.

What are Hardhat Tasks?

Tasks are essentially reusable scripts that can be executed from the command line. They can be used for a variety of purposes, such as deploying contracts, running tests, interacting with deployed contracts, or even performing complex operations like generating reports. Hardhat comes with several built-in tasks, but you can also create your own custom tasks to suit your project's needs.

Creating a Custom Task

To create a custom task, you need to define it in your Hardhat project. Here’s how to do it:

  1. Create a new file in the tasks directory (if it doesn't exist, create it).
  2. Define your task in that file.
  3. Run the task from the command line.

Sample Code for a Custom Task

Let's create a simple task that retrieves the balance of an Ethereum address. First, create a new file named getBalance.js in the tasks directory:

// tasks/getBalance.js
const { task } = require("hardhat/config");

task("getBalance", "Prints an account's balance")
.addParam("account", "The account's address")
.setAction(async (taskArgs, hre) => {
const balance = await hre.ethers.provider.getBalance(taskArgs.account);
console.log(`Balance of ${taskArgs.account}: ${hre.ethers.utils.formatEther(balance)} ETH`);
});

module.exports = {};

In this code:

  • We import the task function from hardhat/config.
  • We define a new task named getBalance.
  • We add a parameter account to the task, which will be the Ethereum address for which we want to check the balance.
  • We use the setAction method to define the action that will be performed when the task is executed. In this case, we retrieve the balance and print it in ETH.

Running the Task

After defining your task, you need to run it from the command line. Make sure your local Hardhat network is running:

npx hardhat node

In another terminal window, you can run the task as follows:

getBalance.js0

Replace getBalance.js1 with the Ethereum address you want to check. This command will output the balance of the specified account.

Built-in Tasks

Hardhat comes with several built-in tasks that you can use out of the box. Some common built-in tasks include:

  • getBalance.js2: Compiles your Solidity contracts.
  • getBalance.js3: Runs your tests.
  • getBalance.js4: Deploys your contracts to a specified network.

You can view all available tasks by running:

getBalance.js5

Conclusion

Tasks in Hardhat are a powerful way to automate and streamline your development process. By creating custom tasks, you can simplify repetitive actions, making your workflow more efficient. Whether you are deploying contracts, checking balances, or running tests, tasks provide a flexible and reusable solution to enhance your Ethereum development experience.