智能合约开发框架-Hardhat

简介

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

Hardhat内置了Hardhat网络,这是一个专为开发设计的本地以太坊网络。主要功能有Solidity调试,跟踪调用堆栈、 console.log() 和交易失败时的明确错误信息提示等。

环境

  • node.js
  • python

安装

npm install --global --production windows-build-tools
npm install -g hardhat

安装中如果出现这样的报错

npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version was set from command line or npm config
npm ERR! gyp ERR! find VS - looking for Visual Studio version 2017
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS checking VS2017 (15.9.28307.1927) found at:
npm ERR! gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community"
npm ERR! gyp ERR! find VS - found "Visual Studio C++ core features"
npm ERR! gyp ERR! find VS - found VC++ toolset: v141
npm ERR! gyp ERR! find VS - missing any Windows SDK
npm ERR! gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS valid versions for msvs_version:
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows

下载visual studio 2017以上版本,勾选使用C++的桌面开发即可

生成项目

创建项目目录,并在项目目录下执行命令初始化

创建项目目录

PS C:\Users\Administrator\Desktop> mkdir test_hardhat
PS C:\Users\Administrator\Desktop> cd .\test_hardhat\

项目初始化

PS C:\Users\Administrator\Desktop\test_hardhat> 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         # 默认选择 basic sample project, 直接回车Create an advanced sample projectCreate an advanced sample project that uses TypeScriptCreate an empty hardhat.config.jsQuit
​
? Hardhat project root: · C:\Users\Administrator\Desktop\test_hardhat  # 默认不用修改, 直接回车
? Do you want to add a .gitignore? (Y/n) » y  # 默认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.

安装依赖

Hardhat会提示你如何安装依赖

copy 并执行上面的命令即可

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"

目录结构

  • /contracts

存放开发的智能合约

  • /scripts

存放用于调试或部署脚本的脚本文件(建议单独创建文件夹deploy用于存放部署脚本)

  • /test

存放测试用例的脚本文件

  • hardhat.config.js

hardhat.config.js是框架配置文件

Hardhat配置

通过修改hardhat.config.js完成框架的配置,其中常用的主要是 networks 和 solidity compiler 配置,

  • networks
module.exports = {defaultNetwork: "hardhat",    // 指定默认网络networks: {                   // 配置可能用到的所有网络连接信息hardhat: {                  // hardhat框架集成了 hardhat node,不需要配置url等信息},rinkeby: {                  // 定义其他网络url: "https://rinkeby.infura.io/v3/...",accounts: [privateKey1, privateKey2, ...] }},
​solidity: "0.8.4",
};
  • solidity compiler
module.exports = {defaultNetwork: "hardhat",networks: {// ......// 忽略networks配置},
​solidity: {version: "0.8.1",   // compiler(编译器)版本需要 >= 合约文件版本号settings: {optimizer: {      // 优化器是一个特殊配置,当合约文件过于复杂,合约编译不能通过,编译器有 最大合约字节码 的限制enabled: false, // 如果需要开启优化,则为 trueruns: 200       // runs 默认不用修改}}},
};

合约编译

合约.sol文件放在 /contracts 目录下

# npx hardhat compile
PS C:\Users\Administrator\Desktop\test_hardhat> npx hardhat compile
Downloading compiler 0.8.4
Compiled 2 Solidity files successfully # 编译成功

编译成功后,可在 /artifacts/contracts (新生成的)目录下找到对应合约的 <合约名>.json 文件中找到合约对应的 abi 和 bytecode

合约测试

合约测试.js文件放在 /test 目录下

# npx hardhat test
PS C:\Users\Administrator\Desktop\test_hardhat> 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 (1132ms)
​
​1 passing (1s)

合约部署

合约部署.js文件放在 /scripts 目录下

# npx hardhat run --network <your-network> scripts/<deploy.js>
​
PS C:\Users\Administrator\Desktop\test_hardhat> npx hardhat run --network hardhat  scripts/sample-script.js
Deploying a Greeter with greeting: Hello, Hardhat!
Greeter deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3 # 部署得到的合约地址

参考文献

官方文档: https://hardhat.org/getting-started/

中文文档: https://learnblockchain.cn/docs/hardhat/getting-started/

简介智能合约开发框架-Hardhat相关推荐

  1. ApeWorX: 新的基于 Python 语言的智能合约开发框架

    Brownie 是 Python 开发人员经常使用智能合约框架. 现在出现了一个 Brownie 的继任者,可以让 Python Web3 开发人员获得更好的体验. 在本文中,我将讨论这个 Brown ...

  2. 手把手教你搭建智能合约测试环境、开发、编译、部署以及如何通过JS调用合约方法

    链客,专为开发者而生,有问必答! 此文章来自链客区块链技术问答社区,未经允许拒绝转载. 学习目标 了解智能合约 简单环境搭建 能够利用solidity编写Hello World合约 合约部署 和合约互 ...

  3. 智能合约的核心思想、语法重点、编程模式、示例、规范及架构

    目录 智能合约简介 智能合约例子 合约编程模式COP 合约语法 重难点 限制和规范 合约架构 什么是智能合约 一个智能合约是一套以数字形式定义的承诺(promises) ,包括合约参与方可以在上面执行 ...

  4. 区块链学习笔记21——ETH智能合约

    区块链学习笔记21--ETH智能合约 学习视频:北京大学肖臻老师<区块链技术与应用> 笔记参考:北京大学肖臻老师<区块链技术与应用>公开课系列笔记--目录导航页 智能合约简介 ...

  5. DAPP开发(三)——智能合约开发

    智能合约 Remix IDE 是开发以太坊智能合约的在线IDE工具,部署简单的智能合约非常方便. http://remix.ethereum.org truffle 一个世界级的智能合约开发框架,专为 ...

  6. 教你如何极简上手 Nervos CKB 上的智能合约开发

    Nervos CKB 是一条基于 PoW 的 Layer 1 公链,其 Cell 模型是比特币 UTXO 模型的泛化,因此它的智能合约开发有别于基于以太坊账户模型的智能合约开发.在本文中,Nervso ...

  7. 区块链学习(3) 以太坊测试环境编译并部署智能合约(mac版)

    选择编写智能合约的语言 Ethereum上的智能合约需要使用solidity语言来撰写.虽然还有其他能用来撰写智能合约的语言如Serpent(类Python).lll(类Fortran),但目前看到所 ...

  8. 区块链开发语言python_Python:不同区块链智能合约开发语言的选择

    链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载. 在本文中,将介绍比特币.超级账本Fabric和以太坊这三种区块链中,分别使用什么开发语言来进行智能合约的编程,并提供你进 ...

  9. 北京大学肖臻老师《区块链技术与应用》公开课笔记25——ETH智能合约篇1

    北京大学肖臻老师<区块链技术与应用>公开课笔记 以太坊智能合约,对应肖老师视频:click here 全系列笔记请见:click here 智能合约是以太坊的精髓所在,也是其与比特币系统最 ...

最新文章

  1. 机器学习狗太苦逼了!自动化调参哪家强?
  2. HBase Cassandra比较
  3. jsp中未登录用户也可以浏览页面的功能实现代码
  4. android 自定义分区,android 自定义预制APP分区
  5. opencv roberts算子_边缘检测 Roberts算子
  6. Node.js package.json
  7. 用邮件备份手机数据是必然趋势
  8. ext2文件系统之ext2_lookup函数源代码分析
  9. RTMP直播推流Video(视频)
  10. Windows 10 的快捷关机方式
  11. ubuntu 18.04 + SVO2.0
  12. A Survey on Knowledge Graph-Based Recommender
  13. nginx -s reopen 命令小解
  14. 分段三次埃尔米特插值
  15. VRP系统——路由器配置之信息中心基础
  16. 【基础知识】HTML5 Canvas小项目时钟的简单实现(图文、演示)
  17. 湖南计算机前十大学,湖南计算机专业学校排名
  18. 广州计算机专业分数线,广东人工智能专业分数线多少
  19. PB中OpenSheet与open的区别
  20. Data Analysis 软件(色谱质谱图分析软件)安装步骤

热门文章

  1. Kali模拟-在Android端窃取信息
  2. Mybatis多租户插件
  3. 浅谈linux的几种重启命令,linux用命令重启的两种方法(Linux重启关机命令经验之谈)...
  4. 华为Mate20将是你的专属翻译官,AI翻译或将迎来较大革新
  5. 网络工程师网络管理软件SNMPc软件的下载,安装和使用教程说明
  6. Selenium自动化测试面试题全家桶
  7. eoLinker chrome插件离线版安装
  8. 什么是 DNS DKIM 记录?
  9. AssistiveTouch热键按钮
  10. 国标GB28181入门