Smart contract development framework-Hardhat
Introduction
Hardhat is a development environment for compiling, deploying, testing and debugging Ethereum applications.
Hardhat has built in the Hardhat Network, a native Ethereum network designed for development. The main functions include Solidity debugging, tracking call stack, console.log() and clear error message prompts when transactions fail, etc.
environment
- node.js
- python
Install
npm install --global --production windows-build-tools
npm install -g hardhat
If such an error occurs during installation
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#on-windows
Download visual studio 2017 or above, check the desktop development using C++
build project
Create a project directory and execute command initialization under the project directory
Create project directory
PS C:\Users\Administrator\Desktop> mkdir test_hardhat
PS C:\Users\Administrator\Desktop> cd .\test_hardhat\
Project initialization
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.
install dependencies
Hardhat will prompt you how to install dependencies
copy and execute the above command
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"
Directory Structure
- /contracts
Store developed smart contracts
- /scripts
Store script files for debugging or deploying scripts (it is recommended to create a separate folder deploy for storing deployment scripts)
- /test
Script files that store test cases
- hardhat.config.js
hardhat.config.js is the framework configuration file
Hard hat configuration
The configuration of the framework is completed by modifying hardhat.config.js, and the commonly used ones are mainly networks and solidity compiler configurations.
- networks
module.exports = {
defaultNetwork: "hardhat", // 指定默认网络
networks: {
// 配置可能用到的所有网络连接信息
hardhat: {
// hardhat框架集成了 hardhat node,不需要配置url等信息
},
rinkeby: {
// 定义其他网络
url: "https://rinkeby.infura.io/v3/...",
accounts: [privateKey1, privateKey2, ...]
}
},
solidity: "0.8.4",
};
- solidity compiler
module.exports = {
defaultNetwork: "hardhat",
networks: {
// ......
// 忽略networks配置
},
solidity: {
version: "0.8.1", // compiler(编译器)版本需要 >= 合约文件版本号
settings: {
optimizer: {
// 优化器是一个特殊配置,当合约文件过于复杂,合约编译不能通过,编译器有 最大合约字节码 的限制
enabled: false, // 如果需要开启优化,则为 true
runs: 200 // runs 默认不用修改
}
}
},
};
contract compilation
The contract .sol file is placed in the /contracts directory
# npx hardhat compile
PS C:\Users\Administrator\Desktop\test_hardhat> npx hardhat compile
Downloading compiler 0.8.4
Compiled 2 Solidity files successfully # 编译成功
After the compilation is successful, you can find the abi and bytecode corresponding to the contract in the <contract name>.json file of the corresponding contract in the /artifacts/contracts (newly generated) directory
contract test
The contract test.js file is placed in the /test directory
# npx hardhat 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)
contract deployment
The contract deployment.js file is placed in the /scripts directory
# npx hardhat run --network <your-network> scripts/<deploy.js>
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 # 部署得到的合约地址
references
Official documentation: https://hardhat.org/getting-started/
Chinese documentation: https://learnblockchain.cn/docs/hardhat/getting-started/