IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Hardhat -> 正文阅读

[游戏开发]Hardhat

部署环境

操作系统:Win10

前提条件

安装Nodejs(版本在v14+),可参考:安装 Nodejs (CentOS7 与 Windows)_ling1998的博客-CSDN博客

安装hardhat并展示样例

(1)创建并进入目录

F:\>mkdir hardhatdemo
F:\>cd hardhatdemo

(2)初始化项目

F:\hardhatdemo>npm init --yes  #初始化项目,--yes跳过提示信息
Wrote to F:\Golang\sl\TalentChainGit\hardhatdemo\package.json:

{
  "name": "hardhatdemo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

此时项目根目录中生成package.json文件

?

(3)安装hardhat

F:\hardhatdemo>npm install --save-dev hardhat

added 298 packages in 44s

53 packages are looking for funding
  run `npm fund` for details

安装时间较长,耐心等待吧。

安装成功生成node-modules依赖包目录,里面包含许多依赖包

此安装并非顺利,出现了许多问题,但当时未解决,第二天再试突然就成功了,出现的错误可参见:Hardhat安装与相关插件报错记录_ling1998的博客-CSDN博客

既然成功了,那就继续吧。

(4)启动项目

F:\hardhatdemo>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 a basic sample project
  Create an advanced sample project
  Create an advanced sample project that uses TypeScript
  Create an empty hardhat.config.js
  Quit

通过键盘上的上下键选择”Create a basic sample project“ ,确认项目根目录及确认增加.gitignore,连续回车确认即可。

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 a basic sample project
√ Hardhat project root: · F:\hardhatdemo      #回车
√ 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"

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

完成后,项目目录生成目录结构如下:?

contracts目录: 存储合约源文件

scripts目录:? ? ?存储测试文件

test目录:? ? ? ? ?存储自动化脚本文件

hardhat.config.js文件:配置文件

(5)安装插件Ehter.js 与Waffle

上一步中启动项目提示信息中出现

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"

这需要安装相应的插件,直接拷贝上面命令,执行时间有些长,而且有时会报错,可再次执行尝试

F:\hardhatdemo>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"
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated testrpc@0.0.1: testrpc has been renamed to ganache-cli, please use this package from now on.
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated @ensdomains/ens@0.4.5: Please use @ensdomains/ens-contracts
npm WARN deprecated @ensdomains/resolver@0.2.4: Please use @ensdomains/ens-contracts

added 1107 packages, and audited 1409 packages in 17m

115 packages are looking for funding
  run `npm fund` for details

54 vulnerabilities (11 moderate, 42 high, 1 critical)

To address issues that do not require attention, run:
  npm audit fix

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.

编译合约

F:\hardhatdemo>npx hardhat compile
Downloading compiler 0.8.4
Compiled 2 Solidity files successfully

此时项目根目录生成2个目录

?测试(ether.js&waffle)

F:\hardhatdemo>npx hardhat test


  Greeter
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Hola, mundo!'
    √ Should return the new greeting once it's changed (3728ms)


  1 passing (4s)

编译及测试自己的合约

(1)创建并进入目录

F:\hardhatdemo>cd ..    #返回上一级目录

F:>mkdir hardhatmyself  #创建自己合约目录

F:>cd hardhatmyself     #进入目录

(2)?初始化项目

F:\hardhatmyself>npm init -yes
Wrote to F:\Golang\sl\TalentChainGit\hardhatmyself\package.json:

{
  "name": "hardhatmyself",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

项目目录生成文件package.json

?

(3)安装hardhat

F:\hardhatmyself>npm install --save-dev hardhat

added 298 packages, and audited 299 packages in 16m

53 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

安装时间较长,有时10+分钟,有时1小时,超过半小时建议,重新尝试。安装完一次以后可以一直使用。

?

(4)启动项目

F:\hardhatmyself>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 a basic sample project
  Create an advanced sample project
  Create an advanced sample project that uses TypeScript
> Create an empty hardhat.config.js
  Quit

通过键盘上的上下键选择”Create an empty hardhat.config.js",回车即可。?

F:\hardhatmyself>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 empty hardhat.config.js
Config file created

发现项目目录中增加配置文件 hardhat.config.js

?

(5)安装插件

F:\hardhatmyself>npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details
npm WARN deprecated testrpc@0.0.1: testrpc has been renamed to ganache-cli, please use this package from now on.
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated @ensdomains/ens@0.4.5: Please use @ensdomains/ens-contracts
npm WARN deprecated @ensdomains/resolver@0.2.4: Please use @ensdomains/ens-contracts

added 1107 packages, and audited 1409 packages in 3m

115 packages are looking for funding
  run `npm fund` for details

54 vulnerabilities (11 moderate, 42 high, 1 critical)

To address issues that do not require attention, run:
  npm audit fix

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.

安装时间较长,若半小时还没反应,重新执行安装命令。?

(6)编写合约

创建合约目录contracts

F:\hardhatmyself>mkdir contracts

创建合约文件hello.sol,文件名与合约名相同,内容为:

// SPDX-License-Identifier: Apache-2.0
pragma solidity^0.8.0;

 /**
   * @title hello
   * @dev test in Remix env
   * @custom:dev-run-script prictice/hello.js
   */
contract hello {
    string public message;

    constructor(string memory _msg) {
        message = _msg;
    }

    function getMsg() public view returns (string memory) {
        return message;
    }

    function setMsg(string memory _msg) public {
        message = _msg;
    }
}

(7)编译合约

查看配置文件中的编译版本,调整与合约中编译版本匹配?

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.4",
};

编译合约尝试了一下0.8.0至0.8.7,只有0.8.3与0.8.4版本编译通过,其它版本都报错,所以建议先在0.8.3与0.8.4版本编译

编译命令npx hardhat compile默认编译contracts目录下的合约文件(.sol)

# 0.8.0 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.0
Error HH501: Couldn't download compiler version 0.8.0. Please check your connection.

For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces

# 0.8.1 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.1
DocstringParsingError: Documentation tag @custom:dev-run-script not valid for contracts.
 --> contracts/hello.sol:4:2:
  |
4 |  /**
  |  ^ (Relevant source part starts here and spans across multiple lines).


Error HH600: Compilation failed

For more info go to https://hardhat.org/HH600 or run Hardhat with --show-stack-traces

# 0.8.2 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.2
Error HH501: Couldn't download compiler version 0.8.2. Please check your connection.

For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces

# 0.8.3 (成功)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.3
Compiled 1 Solidity file successfully

# 0.8.4 (成功)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.4
Compiled 1 Solidity file successfully

# 0.8.5 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.5
Error HH501: Couldn't download compiler version 0.8.5. Please check your connection.

For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces

# 0.8.6 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.6
Error HH501: Couldn't download compiler version 0.8.6. Please check your connection.

For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces

# 0.8.7 (失败)
F:\hardhatmyself>npx hardhat compile
Downloading compiler 0.8.7
Error HH501: Couldn't download compiler version 0.8.7. Please check your connection.

For more info go to https://hardhat.org/HH501 or run Hardhat with --show-stack-traces

项目目录生成 artifacts与cache

(8)测试合约

?配置文件hardhat.config.j中添加引用插件waffle,如下所示(第一行):?

require("@nomiclabs/hardhat-waffle");
/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.4",
};

创建测试目录test

创建测试脚本文件sample-test-hello.js,编写测试脚本内容如下:

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Hello", function(){
  it("Should return the new hello once it's changed", async function(){
    const Hello = await ethers.getContractFactory("hello");
    const hello = await Hello.deploy("hello ,tracy, I am testing");
    await hello.deployed();
    console.log("hell address:", hello.address);

    //expect(await hello.getMsg());

    console.log("origin getMsg:", await hello.getMsg());
    const setMsg = await hello.setMsg("new Msg, hello, I am here");

    await setMsg.wait();
    //expect(await hello.getMsg());
    console.log("new getMsg:", await hello.getMsg());
  });
});

测试命令npx hardhat test默认执行test目录下的js脚本文件

F:\hardhatmyself>npx hardhat test


  Hello
hell address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
origin getMsg: hello ,tracy, I am testing
new getMsg: new Msg, hello, I am here
    √ Should return the new hello once it's changed (700ms)


  1 passing (709ms)

折腾许久,终于成功了,按这个思路继续进行其它合约脚本测试。

参考文档:概述 | Hardhat | 为专业人士开发的以太坊开发环境 by Nomic Labs

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-04-27 11:37:15  更:2022-04-27 11:38:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 14:50:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码