部署环境
操作系统: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:\Golang\sl\TalentChainGit\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 hardhat
added 298 packages in 44s
53 packages are looking for funding
run `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 "Y888
Welcome to Hardhat v2.9.3
? What do you want to do? ...
> Create a basic sample project
Create an advanced sample project
Create an advanced sample project that uses TypeScript
Create an empty hardhat.config.js
Quit
通过键盘上的上下键选择”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 "Y888
Welcome 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-contracts
added 1107 packages, and audited 1409 packages in 17m
115 packages are looking for funding
run `npm fund` for details
54 vulnerabilities (11 moderate, 42 high, 1 critical)
To address issues that do not require attention, run:
npm audit fix
Some issues need review, and may require choosing
a different dependency.
Run `npm audit` for details.
编译合约
F:\hardhatdemo>npx hardhat compile
Downloading compiler 0.8.4
Compiled 2 Solidity files successfully
此时项目根目录生成2个目录
?测试(ether.js&waffle)
F:\hardhatdemo>npx hardhat test
Greeter
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:\Golang\sl\TalentChainGit\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 hardhat
added 298 packages, and audited 299 packages in 16m
53 packages are looking for funding
run `npm fund` for details
found 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 "Y888
Welcome to Hardhat v2.9.3
? What do you want to do? ...
Create a basic sample project
Create an advanced sample project
Create an advanced sample project that uses TypeScript
> Create an empty hardhat.config.js
Quit
通过键盘上的上下键选择”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 "Y888
Welcome 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-contracts
added 1107 packages, and audited 1409 packages in 3m
115 packages are looking for funding
run `npm fund` for details
54 vulnerabilities (11 moderate, 42 high, 1 critical)
To address issues that do not require attention, run:
npm audit fix
Some 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 failed
For 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 test
Hello
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
|