赞
踩
Hardhat是一个编译、部署、测试和调试以太坊应用的开发环境。
先安装nodejs环境;
然后打开命令行执行以下命令, 在项目目录pj_220509下安装hardhat环境:
- mkdir pj_220509
-
- cd pj_220509
-
- npm install --save-dev hardhat
pj_220509目录下, 执行命令 npx hardhat
- D:\01steven-disk\blockchain\workspace\pj220509>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 advanced sample project
- √ Hardhat project root: · D:\01steven-disk\blockchain\workspace\pj220509
- √ 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" "@nomiclabs/hardhat-etherscan@^3.0.0" "dotenv@^10.0.0" "eslint@^7.29.0" "eslint-config-prettier@^8.3.0" "eslint-config-standard@^16.0.3" "eslint-plugin-import@^2.23.4" "eslint-plugin-node@^11.1.0" "eslint-plugin-prettier@^3.4.0" "eslint-plugin-promise@^5.1.0" "hardhat-gas-reporter@^1.0.4" "prettier@^2.3.2" "prettier-plugin-solidity@^1.0.0-beta.13" "solhint@^3.3.6" "solidity-coverage@^0.7.16"
- Project created
- See the README.md file for some example tasks you can run.
然后按提示安装相关的nodejs包,如下;完成安装;
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" "@nomiclabs/hardhat-etherscan@^3.0.0" "dotenv@^10.0.0" "eslint@^7.29.0" "eslint-config-prettier@^8.3.0" "eslint-config-standard@^16.0.3" "eslint-plugin-import@^2.23.4" "eslint-plugin-node@^11.1.0" "eslint-plugin-prettier@^3.4.0" "eslint-plugin-promise@^5.1.0" "hardhat-gas-reporter@^1.0.4" "prettier@^2.3.2" "prettier-plugin-solidity@^1.0.0-beta.13" "solhint@^3.3.6" "solidity-coverage@^0.7.16"
装完后呀,可以在本地启动一个区块链本地环境节点,厉害吧;
装完后,可以编译你的智能合约,测试你的智能合约,部署你的智能合约到本地网、测试网、或主网。
还有提供一个控制台,即命令窗口,你可以用来和区块链、智能合约 “对话聊天”
下图即是项目的目录结构,主要目录简单说明下:
contracts 合约源文件目录
scripts 部署脚本、交互脚本存放目录
test 测试程序目录
artifacts 编译后生成的目录
hardhat.config.js 配置文件
在pj_220509目录下,在命令行中执行
- D:\01steven-disk\blockchain\workspace\pj220509>npx hardhat node
- Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
在pj_220509目录下,在命令行中执行: npx hardhat compile
将会编译项目中所有的合约;
在pj_220509目录下,在命令行中执行: npx hardhat test
将会执行test目录所有的测试程序;
在scripts目录下,写好部署脚本 deploy.js , 然后使用以下命令执行脚本进行部署,
--network localhost 参数用来指定连接到哪个网,这里是本地环境;(即上面用 npx hardhat node启动的本地环境)
- npx hardhat run --network localhost scripts/deploy.js
- Greeter deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。