当前位置:   article > 正文

智能合约开发笔记-hardhat入门_hardhat 生成advanced项目

hardhat 生成advanced项目

Hardhat是什么?

Hardhat是一个编译、部署、测试和调试以太坊应用的开发环境。

怎么安装?

安装nodejs环境;

然后打开命令行执行以下命令, 在项目目录pj_220509下安装hardhat环境:

  1. mkdir  pj_220509
  2. cd    pj_220509
  3. npm install --save-dev hardhat

怎么创建项目?

pj_220509目录下, 执行命令 npx hardhat

  1. D:\01steven-disk\blockchain\workspace\pj220509>npx hardhat
  2. 888 888 888 888 888
  3. 888 888 888 888 888
  4. 888 888 888 888 888
  5. 8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
  6. 888 888 "88b 888P" d88" 888 888 "88b "88b 888
  7. 888 888 .d888888 888 888 888 888 888 .d888888 888
  8. 888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.
  9. 888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888
  10. Welcome to Hardhat v2.9.3
  11. √ What do you want to do? · Create an advanced sample project
  12. √ Hardhat project root: · D:\01steven-disk\blockchain\workspace\pj220509
  13. √ Do you want to add a .gitignore? (Y/n) · y
  14. You need to install these dependencies to run the sample project:
  15. 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"
  16. Project created
  17. 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目录下,在命令行中执行

  1. D:\01steven-disk\blockchain\workspace\pj220509>npx hardhat node
  2. 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启动的本地环境)

  1. npx hardhat run --network localhost scripts/deploy.js
  2. Greeter deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/150094
推荐阅读
相关标签
  

闽ICP备14008679号