介绍

使用步骤

  • 创建工程
    • 安装:npm install —save hardhat
    • 在⽬录下执⾏ npx hardhat 创建项⽬
    • 查看所有任务:npx hardhat
  • 合约编译
    • 运行npx hardhat compile
  • 配置网络
    • hardhat.config文件下进行networks参数配置
  • 部署脚本
    • scripts目录下编写部署脚本
  • 启动Hardhat Node⽹络
    • 执行npx hardhat node
  • 执⾏部署
    • 执行npx hardhat run scripts/deploy.js --network localhost
  • 测试
    • 执行npx hardhat test

hardhat实战

  • 列出所有可以执行的hardhat任务
    • npx hardhat
  • 调试利器
    • 进入命令行调试
      • npx hardhat console --network localhost
    • fork主网/测试网进行调试
      • npx hardhat node --fork https://rinkeby.infura.io/v3/<key>
  • 灵活参数部署,利用hardhat可以在代码中引用
    • 设置HARDHAT_NETWORK环境变量
    • 使用node部署
  • 代码扁平 (用于合约验证)
    • npx hardhat flatten xxx.sol
  • 代码验证
    • require("@nomiclabs/hardhat-etherscan");
    • npx hardhat verify address --network xxx
  • ABI导出
    • require('hardhat-abi-exporter')
    • yarn run hardhat export-abi