2022/07/14 Solidity_Hardhat_Day3

# 2022/07/14 Solidity_HardHat_Day2

### 创建任务

**`Hardhat`创建任务是自动化的核心组件.在`Hardhat`中,一切都被定义为任务.开箱即用的默认动作是内置任务,使用与用户相同的`API`来实现的
**

**`Hardhat`中的任务是`JavaScript`函数.可以访问`Hardhat`运行时环境(`HRE`),通过`HRE`中可以访问配置、参数、其他的任务程序以及插件可能注入的任何对象.**

- **`HRE`运行时顺序:额皮质文件总是在启动时执行,后面才执行其他事情.**

**一个简单且完整的`Hardhat`任务创建:**

`
import { task } from "hardhat/config";
import { web3 } from "hardhat";

task("balance", "This is a task about get account")
.addParam("account", "This is an account")
.setAction(async taskArgs => {
const account = web3.utils.toChecksumAddress(taskArgs.account);
const balance = await web3.eth.balanceOf(account);

console.log(web3.utils.fromWei(balance, "ether"), "ETH");
});

module.exports = { };
`

 

posted @ 2022-08-10 18:10  俊king  阅读(33)  评论(0)    收藏  举报