2022-10-17Web300
请注意,本文编写于 545 天前,最后修改于 545 天前,其中某些信息可能已经过时。

tasks

编写的tasks可以很方便的在终端中进行调用

const { task } = require("hardhat/config");

task("block-number", "Prints the current block number.").setAction(
  async (taskArgs, hre) => {
    const blockNumber = await hre.ethers.provider.getBlockNumber();
    console.log(`Current block Number is ${blockNumber}`);
  }
);

以上task实现了查看block-number,第一个参数为task的名称,第二个参数为描述,setAction用来描述真正这个task要实现的功能。

运行tasks

image.png

yarn hardhat block-number

test

const { ethers } = require("hardhat");
const { expect, assert } = require("chai");

describe("SimpleStorage", () => {
  let contractFactory, simpleStorage;
  beforeEach(async function () {
    // 测试前部署合同
    contractFactory = await ethers.getContractFactory("SimpleStorage");
    simpleStorage = await contractFactory.deploy();
  });

  it("Should start with a favorite number of 0", async () => {
    const currentNumber = await simpleStorage.getFavoriteNumber();
    const expectNumber = "0";
    assert.equal(currentNumber.toString(), expectNumber);
  });
});

本文作者:前端小毛

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!