hardhat集成开发环境

系统为macos bigsur ,已经安装过nodejs

安装yarn

curl -o- -L https://yarnpkg.com/install.sh | bash 

> Extracting to ~/.yarn...
> Adding to $PATH...
> We've added the following to your /Users/jiangwujie/.zshrc
> If this isn't the profile of your current shell then please add the following to your correct profile:

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

> Successfully installed Yarn 1.22.19! Please open another terminal where the `yarn` command will now be available.

安装 hardhat

# 创建目录
% mkdir -p ~/VscodeWeb3Projects/hhproject/chain
# 进入目录
% cd ~/VscodeWeb3Projects/hhproject/chain
# yarn初始化
chain % yarn init -y 
# 安装hardhat
chain % yarn add hardhat
yarn add v1.22.19
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
# 这里报错,node 版本不对
error @nomicfoundation/ethereumjs-blockchain@6.0.0: The engine "node" is incompatible with this module. Expected version ">=14". Got "12.15.0"
error Found incompatible module.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

解决,参考文档 https://www.jianshu.com/p/6d869ae26714

# ignore-engines true是用于修复node版本不兼容的命令配置
chain % yarn config set ignore-engines true
chain % yarn add hardhat                   
yarn add v1.22.19
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
success Saved 201 new dependencies.
info Direct dependencies
└─ hardhat@2.12.5
info All dependencies
├─ @ethersproject/abi@5.7.0
……省略部分……
├─ yargs-unparser@2.0.0
├─ yargs@16.2.0
└─ yocto-queue@0.1.0
✨  Done in 43.20s.
# hardhat安装成功

初始化一个项目

# 初始化项目命令
chain % yarn hardhat
yarn run v1.22.19
$ ~/VscodeWeb3Projects/hhproject/chain/node_modules/.bin/hardhat
~/VscodeWeb3Projects/hhproject/chain/node_modules/hardhat/internal/cli/cli.js:129
        let taskName = parsedTaskName ?? task_names_1.TASK_HELP;
                                       ^

SyntaxError: Unexpected token '?'
    at Module._compile (internal/modules/cjs/loader.js:891:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

# 再次报错

参考文档:https://stackoverflow.com/questions/73622631/npx-hardhat-compile-unexpected-token 应该是nodejs版本不一样的问题,需要升级nodejs版本.

升级nodejs版本参考文档https://www.jianshu.com/p/acd316dceeb8

~ % sudo npm cache clean -f
Password:
npm WARN using --force I sure hope you know what you are doing.


   ╭───────────────────────────────────────────────────────────────╮
   │                                                               │
   │      New major version of npm available! 6.13.4 → 9.2.0       │
   │   Changelog: https://github.com/npm/cli/releases/tag/v9.2.0   │
   │               Run npm install -g npm to update!               │
   │                                                               │
   ╰───────────────────────────────────────────────────────────────╯

~ % sudo npm install -g n
/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
+ n@9.0.1
added 1 package from 2 contributors in 2.451s
~ % npm view node versions
# sudo n xx.xx.x是 使用n插件将node更改为某个版本
# 如果sudo n latest则为安装最新版本,这里建议 v18.8.0
# 不但版本号好看,在后面hardhat 编译等等时也比较方便
~ % sudo n 18.8.0
  installing : node-v18.8.0
       mkdir : /usr/local/n/versions/node/v18.8.0
       fetch : https://nodejs.org/dist/v18.8.0/node-v18.8.0-darwin-x64.tar.xz
     copying : node/18.8.0
   installed : v18.8.0 (with npm 9.2.0)

然后重新执行 yarn hardhat命令

chain % yarn hardhat
yarn run v1.22.19
warning package.json: No license field
$ /Users/jiangwujie/VscodeWeb3Projects/hhproject/chain/node_modules/.bin/hardhat
You are using a version of Node.js that is not supported by Hardhat, and it may work incorrectly, or not work at all.

Please, make sure you are using a supported version of Node.js.

To learn more about which versions of Node.js are supported go to https://hardhat.org/nodejs-versions
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.12.5 👷‍
# 选择 TypeScript项目
✔ What do you want to do? · Create a TypeScript project

✨ Project created ✨

See the README.md file for some example tasks you can run

Give Hardhat a star on Github if you're enjoying it! 💞✨

     https://github.com/NomicFoundation/hardhat

Please take a moment to complete the 2022 Solidity Survey: https://hardhat.org/solidity-survey-2022
✨  Done in 332.94s.

可以看到hardhat项目已经创建

#可以运行命令进行测试
# 执行部署脚本
chain % yarn hardhat run scripts/deploy.ts
yarn run v1.22.19
$ /Users/jiangwujie/VscodeWeb3Projects/hhproject/chain/node_modules/.bin/hardhat run scripts/deploy.ts
Lock with 1 ETH and unlock timestamp 1704036407 deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3
✨  Done in 4.86s.
jiangwujie@localhost chain % yarn hardhat node
yarn run v1.22.19
$ /Users/jiangwujie/VscodeWeb3Projects/hhproject/chain/node_modules/.bin/hardhat node
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/

Accounts
========

WARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.

…………


# 执行单元测试
chain % yarn hardhat test
yarn run v1.22.19
$ /Users/jiangwujie/VscodeWeb3Projects/hhproject/chain/node_modules/.bin/hardhat test


  Lock
    Deployment
      ✔ Should set the right unlockTime (1655ms)
      ✔ Should set the right owner
      ✔ Should receive and store the funds to lock
      ✔ Should fail if the unlockTime is not in the future
    Withdrawals
      Validations
        ✔ Should revert with the right error if called too soon
        ✔ Should revert with the right error if called from another account
        ✔ Shouldn't fail if the unlockTime has arrived and the owner calls it
      Events
        ✔ Should emit an event on withdrawals
      Transfers
        ✔ Should transfer the funds to the owner


  9 passing (2s)

✨  Done in 3.39s.

# 编译、部署到本地测试网
chain % yarn hardhat compile
chain % yarn hardhat run scripts/deploy.ts


# 启动本地测试网 yarn hardhat node
# 对应助记词 
# test test test test test test
# test test test test test junk

jiangwujie@localhost chain % yarn hardhat node
yarn run v1.22.19
$ /Users/jiangwujie/VscodeWeb3Projects/hhproject/chain/node_modules/.bin/hardhat node
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/

Accounts
========

WARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.

Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000 ETH)
Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
………… 省略 …………


私钥可以通过metamask 在localhost:8545网络进行导入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值