智能合约开发框架-Hardhat
?
简介
Hardhat是一个编译、部署、测试和调试以太坊应用的开发环境。
Hardhat内置了Hardhat网络,这是一个专为开发设计的本地以太坊网络。主要功能有Solidity调试,跟踪调用堆栈、 console.log() 和交易失败时的明确错误信息提示等。
环境
安装
npm install --global --production windows-build-tools
npm install -g hardhat
安装中如果出现这样的报错
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version was set from command line or npm config
npm ERR! gyp ERR! find VS - looking for Visual Studio version 2017
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS checking VS2017 (15.9.28307.1927) found at:
npm ERR! gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community"
npm ERR! gyp ERR! find VS - found "Visual Studio C++ core features"
npm ERR! gyp ERR! find VS - found VC++ toolset: v141
npm ERR! gyp ERR! find VS - missing any Windows SDK
npm ERR! gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS valid versions for msvs_version:
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp
下载visual studio 2017以上版本,勾选使用C++的桌面开发即可
生成项目
创建项目目录,并在项目目录下执行命令初始化
创建项目目录
PS C:\Users\Administrator\Desktop> mkdir test_hardhat
PS C:\Users\Administrator\Desktop> cd .\test_hardhat\
项目初始化
PS C:\Users\Administrator\Desktop\test_hardhat> 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 # 默认选择 basic sample project, 直接回车
Create an advanced sample project
Create an advanced sample project that uses TypeScript
Create an empty hardhat.config.js
Quit
?
? Hardhat project root: · C:\Users\Administrator\Desktop\test_hardhat # 默认不用修改, 直接回车
? Do you want to add a .gitignore? (Y/n) ? y # 默认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.
安装依赖
Hardhat会提示你如何安装依赖
copy 并执行上面的命令即可
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"
目录结构
存放开发的智能合约
存放用于调试或部署脚本的脚本文件(建议单独创建文件夹deploy用于存放部署脚本)
存放测试用例的脚本文件
hardhat.config.js是框架配置文件
Hardhat配置
通过修改hardhat.config.js完成框架的配置,其中常用的主要是 networks 和 solidity compiler 配置,
module.exports = {
defaultNetwork: "hardhat", // 指定默认网络
networks: { // 配置可能用到的所有网络连接信息
hardhat: { // hardhat框架集成了 hardhat node,不需要配置url等信息
},
rinkeby: { // 定义其他网络
url: "https://rinkeby.infura.io/v3/...",
accounts: [privateKey1, privateKey2, ...]
}
},
?
solidity: "0.8.4",
};
module.exports = {
defaultNetwork: "hardhat",
networks: {
// ......
// 忽略networks配置
},
?
solidity: {
version: "0.8.1", // compiler(编译器)版本需要 >= 合约文件版本号
settings: {
optimizer: { // 优化器是一个特殊配置,当合约文件过于复杂,合约编译不能通过,编译器有 最大合约字节码 的限制
enabled: false, // 如果需要开启优化,则为 true
runs: 200 // runs 默认不用修改
}
}
},
};
合约编译
合约.sol文件放在 /contracts 目录下
PS C:\Users\Administrator\Desktop\test_hardhat> npx hardhat compile
Downloading compiler 0.8.4
Compiled 2 Solidity files successfully
编译成功后,可在 /artifacts/contracts (新生成的)目录下找到对应合约的 <合约名>.json 文件中找到合约对应的 abi 和 bytecode
合约测试
合约测试.js文件放在 /test 目录下
PS C:\Users\Administrator\Desktop\test_hardhat> 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 (1132ms)
?
?
1 passing (1s)
合约部署
合约部署.js文件放在 /scripts 目录下
?
PS C:\Users\Administrator\Desktop\test_hardhat> npx hardhat run --network hardhat scripts/sample-script.js
Deploying a Greeter with greeting: Hello, Hardhat!
Greeter deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
参考文献
官方文档: https://hardhat.org/getting-started/
中文文档: https://learnblockchain.cn/docs/hardhat/getting-started/
?
|