部署环境

操作系统:Win10

前提条件

安装Nodejs(版本在v14+),可参考:安装 Nodejs (CentOS7 与 Windows)_ling1998的博客-CSDN博客

安装hardhat并展示样例

(1)创建并进入目录

F:\>mkdir hardhatdemo
F:\>cd hardhatdemo

(2)初始化项目

F:\hardhatdemo>npm init --yes  #初始化项目,--yes跳过提示信息
Wrote to F:\hardhatdemo\package.json:{"name": "hardhatdemo","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC"
}

此时项目根目录中生成package.json文件

(3)安装hardhat

F:\hardhatdemo>npm install --save-dev hardhatadded 298 packages in 44s53 packages are looking for fundingrun `npm fund` for details

安装时间较长,耐心等待吧。

安装成功生成node-modules依赖包目录,里面包含许多依赖包

此安装并非顺利,出现了许多问题,但当时未解决,第二天再试突然就成功了,出现的错误可参见:Hardhat安装与相关插件报错记录_ling1998的博客-CSDN博客

既然成功了,那就继续吧。

(4)启动项目

F:\hardhatdemo>npx hardhat
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888Welcome to Hardhat v2.9.3? What do you want to do? ...
> Create a basic sample projectCreate an advanced sample projectCreate an advanced sample project that uses TypeScriptCreate an empty hardhat.config.jsQuit

通过键盘上的上下键选择”Create a basic sample project“ ,确认项目根目录及确认增加.gitignore,连续回车确认即可。

888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888Welcome to Hardhat v2.9.3√ What do you want to do? · Create a basic sample project
√ Hardhat project root: · F:\hardhatdemo      #回车
√ Do you want to add a .gitignore? (Y/n) · y  #回车You need to install these dependencies to run the sample project:npm install --save-dev "hardhat@^2.9.3" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0"Project created
See the README.md file for some example tasks you can run.

完成后,项目目录生成目录结构如下:

contracts目录: 存储合约源文件

scripts目录:     存储测试文件

test目录:         存储自动化脚本文件

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

(5)安装插件Ehter.js 与Waffle

上一步中启动项目提示信息中出现

You need to install these dependencies to run the sample project:
  npm install --save-dev "hardhat@^2.9.3" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0"

这需要安装相应的插件,直接拷贝上面命令,执行时间有些长,而且有时会报错,可再次执行尝试

F:\hardhatdemo>npm install --save-dev "hardhat@^2.9.3" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0"
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated testrpc@0.0.1: testrpc has been renamed to ganache-cli, please use this package from now on.
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated @ensdomains/ens@0.4.5: Please use @ensdomains/ens-contracts
npm WARN deprecated @ensdomains/resolver@0.2.4: Please use @ensdomains/ens-contractsadded 1107 packages, and audited 1409 packages in 17m115 packages are looking for fundingrun `npm fund` for details54 vulnerabilities (11 moderate, 42 high, 1 critical)To address issues that do not require attention, run:npm audit fixSome issues need review, and may require choosing
a different dependency.Run `npm audit` for details.

(6)编译合约

F:\hardhatdemo>npx hardhat compile
Downloading compiler 0.8.4
Compiled 2 Solidity files successfully

此时项目根目录生成2个目录

(7)测试(ether.js&waffle)

F:\hardhatdemo>npx hardhat testGreeter
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Hola, mundo!'√ Should return the new greeting once it's changed (3728ms)1 passing (4s)

编译及测试自己的合约

(1)创建并进入目录

F:\hardhatdemo>cd ..    #返回上一级目录F:>mkdir hardhatmyself  #创建自己合约目录F:>cd hardhatmyself     #进入目录

(2) 初始化项目

F:\hardhatmyself>npm init -yes
Wrote to F:\hardhatmyself\package.json:{"name": "hardhatmyself","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC"
}

项目目录生成文件package.json

(3)安装hardhat

F:\hardhatmyself>npm install --save-dev hardhatadded 298 packages, and audited 299 packages in 16m53 packages are looking for fundingrun `npm fund` for detailsfound 0 vulnerabilities

安装时间较长,有时10+分钟,有时1小时,超过半小时建议,重新尝试。安装完一次以后可以一直使用。

(4)启动项目

F:\hardhatmyself>npx hardhat
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888Welcome to Hardhat v2.9.3? What do you want to do? ...Create a basic sample projectCreate an advanced sample projectCreate an advanced sample project that uses TypeScript
> Create an empty hardhat.config.jsQuit

通过键盘上的上下键选择”Create an empty hardhat.config.js",回车即可。

F:\hardhatmyself>npx hardhat
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888Welcome to Hardhat v2.9.3√ What do you want to do? · Create an empty hardhat.config.js
Config file created

发现项目目录中增加配置文件 hardhat.config.js

(5)安装插件

F:\hardhatmyself>npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated testrpc@0.0.1: testrpc has been renamed to ganache-cli, please use this package from now on.
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated @ensdomains/ens@0.4.5: Please use @ensdomains/ens-contracts
npm WARN deprecated @ensdomains/resolver@0.2.4: Please use @ensdomains/ens-contractsadded 1107 packages, and audited 1409 packages in 3m115 packages are looking for fundingrun `npm fund` for details54 vulnerabilities (11 moderate, 42 high, 1 critical)To address issues that do not require attention, run:npm audit fixSome issues need review, and may require choosing
a different dependency.Run `npm audit` for details.

安装时间较长,若半小时还没反应,重新执行安装命令。

(6)编写合约

创建合约目录contracts

F:\hardhatmyself>mkdir contracts

创建合约文件hello.sol,文件名与合约名相同,内容为:

// SPDX-License-Identifier: Apache-2.0
pragma solidity^0.8.0;/*** @title hello* @dev test in Remix env* @custom:dev-run-script prictice/hello.js*/
contract hello {string public message;constructor(string memory _msg) {message = _msg;}function getMsg() public view returns (string memory) {return message;}function setMsg(string memory _msg) public {message = _msg;}
}

(7)编译合约

查看配置文件中的编译版本,调整与合约中编译版本匹配

/*** @type import('hardhat/config').HardhatUserConfig*/
module.exports = {solidity: "0.8.4",
};

编译合约尝试了一下0.8.0至0.8.7,只有0.8.3与0.8.4版本编译通过,其它版本都报错,所以建议先在0.8.3与0.8.4版本编译

编译命令npx hardhat compile默认编译contracts目录下的合约文件(.sol)

# 0.8.0 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.0
Error HH501: Couldn't download compiler version 0.8.0. Please check your connection.For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces# 0.8.1 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.1
DocstringParsingError: Documentation tag @custom:dev-run-script not valid for contracts.--> contracts/hello.sol:4:2:|
4 |  /**|  ^ (Relevant source part starts here and spans across multiple lines).Error HH600: Compilation failedFor more info go to https://hardhat.org/HH600 or run Hardhat with --show-stack-traces# 0.8.2 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.2
Error HH501: Couldn't download compiler version 0.8.2. Please check your connection.For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces# 0.8.3 (成功)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.3
Compiled 1 Solidity file successfully# 0.8.4 (成功)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.4
Compiled 1 Solidity file successfully# 0.8.5 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.5
Error HH501: Couldn't download compiler version 0.8.5. Please check your connection.For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces# 0.8.6 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.6
Error HH501: Couldn't download compiler version 0.8.6. Please check your connection.For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces# 0.8.7 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.7
Error HH501: Couldn't download compiler version 0.8.7. Please check your connection.For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces

项目目录生成 artifacts与cache

(8)测试合约

配置文件hardhat.config.j中添加引用插件waffle,如下所示(第一行):

require("@nomiclabs/hardhat-waffle");
/*** @type import('hardhat/config').HardhatUserConfig*/
module.exports = {solidity: "0.8.4",
};

创建测试目录test

创建测试脚本文件sample-test-hello.js,编写测试脚本内容如下:

const { expect } = require("chai");
const { ethers } = require("hardhat");describe("Hello", function(){it("Should return the new hello once it's changed", async function(){const Hello = await ethers.getContractFactory("hello");const hello = await Hello.deploy("hello ,tracy, I am testing");await hello.deployed();console.log("hell address:", hello.address);//expect(await hello.getMsg());console.log("origin getMsg:", await hello.getMsg());const setMsg = await hello.setMsg("new Msg, hello, I am here");await setMsg.wait();//expect(await hello.getMsg());console.log("new getMsg:", await hello.getMsg());});
});

测试命令npx hardhat test默认执行test目录下的js脚本文件

F:\hardhatmyself>npx hardhat testHello
hell address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
origin getMsg: hello ,tracy, I am testing
new getMsg: new Msg, hello, I am here√ Should return the new hello once it's changed (700ms)1 passing (709ms)

折腾许久,终于成功了,按这个思路继续进行其它合约脚本测试。

参考文档:概述 | Hardhat | 为专业人士开发的以太坊开发环境 by Nomic Labs

Hardhat创建、编译、测试智能合约相关推荐

  1. 【区块链】Truffle 部署 编译 测试 智能合约 的 完整实践操作

    本文首发自我的CSDN博客,原文链接如下 blog.csdn.net/diandianxiy- 目标 搭建开发环境 创建一个Truffle项目 编写智能合约 编译转移智能合约 测试智能合约 创建用户界 ...

  2. 使用hardhat开发以太坊智能合约-测试合约

    Web3工具网站[点我访问] 现已上线,欢迎使用,如有好的意见和建议也欢迎反馈. 本系列课程: 第一节:使用hardhat开发以太坊智能合约-搭建环境 第二节:使用hardhat开发以太坊智能合约-测 ...

  3. 以太坊源码linux下如何编译,以太坊教程:搭建环境、编写编译一个智能合约

    本以太坊教程主要是介绍:搭建一个开发环境.编写编译一个智能合约. 以太坊是什么 以太坊(Ethereum)是一个开源的有智能合约功能的公共区块链平台.通过其专用加密货币以太币(Ether)提供去中心化 ...

  4. Truffle - 2 利用Truffle编写、测试智能合约并将其部署到不同的测试网络

    2 利用Truffle编写.测试智能合约并将其部署到不同的测试网络 2.1创建项目 建一个文件夹 mkdir truffle-project truffle init //初始化qinjianquan ...

  5. 使用hardhat 开发以太坊智能合约-验证合约

    Web3工具网站[点我访问] 现已上线,欢迎使用,如有好的意见和建议也欢迎反馈. 本系列课程: 第一节:使用hardhat开发以太坊智能合约-搭建环境 第二节:使用hardhat开发以太坊智能合约-测 ...

  6. 以太坊教程:搭建环境、编写编译一个智能合约

    本以太坊教程主要是介绍:搭建一个开发环境.编写编译一个智能合约. 以太坊是什么 以太坊(Ethereum)是一个开源的有智能合约功能的公共区块链平台.通过其专用加密货币以太币(Ether)提供去中心化 ...

  7. 区块链: 编译发布智能合约

    什么是智能合约?? 智能合约与平时的代码其实没有什么区别,只是运行于一个以太坊这样的分布式平台上而已.这个运行的平台,赋予了这些代码不可变,确定性,分布式和可自校验状态等特点.代码运行过程中状态的存储 ...

  8. 智能合约场景下的模糊测试——智能合约基本介绍

    智能合约场景下的模糊测试--智能合约基本介绍 前言 一 基本概念 1.1 智能合约 1.2 图灵完全 二 智能合约特性 2.1 运行环境 2.2 生命周期 1)开发 2)编译 3)部署 4)调用 5) ...

  9. 蚂蚁区块链第12课 如何使用命令行编译工具solcjs编译Solidity智能合约?

    1,摘要 蚂蚁区块链合约平台支持 Solidity 智能合约,针对合约源代码的编译,可以直接通过蚂蚁区块链 Cloud IDE 合约开发环境进行合约编译.部署.测试和调试. 本文介绍由蚂蚁区块链平台提 ...

  10. [测试智能合约]ganache+metamask+remix

    1.谷歌浏览器 下载metamask 2.设置metamask 增加一个7545端口的网络 3.导入账户 从ganache的accounts里面随便找个账户的私钥传进去 4.打开remix 修改env ...

最新文章

  1. UOJ#7. 【NOI2014】购票 | 线段树 凸包优化DP
  2. ftp主动和被动模式_【扫盲】FTP基础知识详解
  3. opencv 正脸和侧脸检测
  4. mysql-mha高可用
  5. 实践出真知之Spring Cloud之基于Eureka、Ribbon、Feign的真实案例
  6. 阿里程序员转行公务员,工资少了40万,只留一句话惊醒众人
  7. 实验8:Problem A: 立体空间中的点(I)
  8. 数独的优化回朔算法(二)
  9. 模式识别 - 名词解释整理
  10. Android框架揭秘-Android Service Framework
  11. PDF文档如何添加图片签名
  12. 【作业分享】Reverse Polish Notation | 数据结构·stack
  13. 发票核验API接口到底好用吗
  14. 思科模拟器(Cisco Packet Tracer7.2.1)安装过程
  15. wang zhe rong yao
  16. ms 真空层_Materials Studio学习
  17. 3款开源软件帮你缩短链接
  18. 外贸数字化发展,进出口新业态新空间丨汇信
  19. TextBox 单行文本框,多行文本框
  20. 一个人的朝圣深度感悟_朝圣之末找到更强大的WordWrap函数

热门文章

  1. 计算机cpu任务管理器,任务管理器里CPU使用率过高问题
  2. 求解Ax=b的克莱默法则
  3. 计算机efs加密,我的电脑文件efs加密了,现在从做系统打不开了怎么处理啊
  4. Python - 怎么将一个数字拆分成多个随机数字
  5. u盘数据恢复的原理_U盘数据恢复其实很简单
  6. 使用mysql语句进行多表联查(以三个表为例)
  7. java零基础Ⅰ-- 1.java 概述
  8. 寒假实践之—大奖赛计分系统
  9. vue随笔之二类型判断一
  10. u3d联机斗地主(1):出牌规则