环境搭建:Hardhat
创建项目
npm
mkdir HelloWorld
cd HelloWorld
npm init -y
npm install --save-dev hardhat
npx hardhat init
运行 npx hardhat init
之后,根据需要选择配置。
安装一个全局命令 hh
。这样就可以通过 hh
来代替 npx hardhat
:
npm
npm install -g hardhat-shorthand
当前文件夹新增了以下文件:
npm
contracts/
scripts/
test/
hardhat.config.js
contracts
用于存放智能合约scripts
用于存放各类脚本,主要是部署脚本test
用于存放测试脚本hardhat.config.js
是 hardhat 配置文件
编译合约
要编译合约,只需要运行:
npm
hh compile
运行测试
运行 test/
文件夹下的测试脚本,运行:
npm
hh test
部署合约
运行部署脚本 scripts/deploy.ts
:
npm
hh run scripts/deploy.ts
hardhat 本身会运行一个驻留在内存中的以太坊网络。也可以自己通过以下命令启用一个本地网络:
npm
hh node
使用以下命令将合约部署到本地网络
npm
hh run --network localhost scripts/deploy.ts