智能合约开发笔记-hardhat入门

Hardhat是什么?

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海阔天空_cn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值