任务和插件

本章介绍 Hardhat 中的任务和插件。

推特@Hita_DAO    DiscordHitaDAO

hardhat 提供了命令行界面 CLI 工具,称为 Hardhat Runner,可以与 hardhat 进行交互。

Hardhat Runner 是一个可扩展的任务运行器,围绕 任务插件 的概念而设计。我们每次从 CLI  运行 hardhat 时,都是在运行一个任务。

例如:

npx hardhat compile
npx hardhat test

这里运行的是内置的 compiletest 任务。 任务可以调用其他任务,允许定义复杂的工作流程。我们既可以开发自己的任务,也可以覆盖现有的任务,从而定制和扩展工作流程。

1. 查看系统任务

我们可以使用命令查看系统内置的任务:

npx hardhat

我们可以看到列出的内置任务:

  ... ...
AVAILABLE TASKS:
  check             	Check whatever you need
  clean             	Clears the cache and deletes all artifacts
  compile           	Compiles the entire project, building all artifacts
  console           	Opens a hardhat console
  coverage          	Generates a code coverage report for tests
  flatten           	Flattens and prints contracts and their dependencies. If no file is passed, all the contracts in the project will be flattened.
  gas-reporter:merge	
  help              	Prints this message
  node              	Starts a JSON-RPC server on top of Hardhat Network
  run               	Runs a user-defined script after compiling the project
  test              	Runs mocha tests
  typechain         	Generate Typechain typings for compiled contracts
  verify            	Verifies a contract on Etherscan

2. 扩展自定义任务

我们在 hardhat 的配置文件中,添加自己开发的扩展任务。

比如,我们开发了一个任务,用于查看当前 Hardhat Network 中创建的所有账号。

代码如下:

task("accounts", "List all of accounts and balances")
  .setAction(async() => {
    // 获取 Hardhat Network 的默认账户列表
    const accounts = await ethers.getSigners();
    // 遍历账户列表,并输出账户地址和余额
    for (const account of accounts) {
      const balance = await ethers.provider.getBalance(account.address);
      console.log(`Account: NULL, Balance: NULL`);
    }
  });

代码中的 task 关键字,用于定义一个任务。两个参数分别代表任务名称和任务描述。

task 的 setAction 用来设置实现任务的代码。

我们把这段代码插入到配置文件 hardhat.config.js 中:

require("@nomicfoundation/hardhat-toolbox");

task("accounts", "List all of accounts and balances")
  .setAction(async() => {
    // 获取 Hardhat Network 的默认账户列表
    const accounts = await ethers.getSigners();
    // 遍历账户列表,并输出账户地址和余额
    for (const account of accounts) {
      const balance = await ethers.provider.getBalance(account.address);
      console.log(`Account: NULL, Balance: NULL`);
    }
  });

module.exports = {
  solidity: "0.8.19",
};

3. 运行自定义任务

在配置文件 hardhat.config.js 中,添加了 task 之后,我们再次运行命令:

npx hardhat

可以看到,已经出现了新添加了 accounts 任务。

 ... ...
AVAILABLE TASKS:
  accounts          	List all of accounts and balances
  check             	Check whatever you need
  clean             	Clears the cache and deletes all artifacts
  

我们运行这个任务:

npx hardhat accounts

可以看到 Hardhat Network 列出了预建的账户和余额:

Account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, Balance: 10000000000000000000000
Account: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8, Balance: 10000000000000000000000
......

4. hardhat 插件

在配置文件 hardhat.config.js 中:

require("@nomicfoundation/hardhat-toolbox");

这里的 @nomicfoundation/hardhat-toolbox 就是插件。

hardhat 的很多功能都来自于插件,我们可以按照规范自行开发插件,来扩展 hardhat  的功能。

作为开发者,我们可以自由选择自己想使用的插件。