hardhat入门教程

2024-03-11 08:59
文章标签 入门教程 hardhat

本文主要是介绍hardhat入门教程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 概述
  • 安装
  • 快速开始
  • 运行任务
  • 编译合约
  • 测试合约
  • 部署合约
  • 连接钱包或Dapp到Hardhat网络
  • 部署主网/测试网

概述

Hardhat是一个编译、部署、测试和调试以太坊应用的开发环境。它可以帮助开发人员管理和自动化构建智能合约和dApps过程中固有的重复性任务,并围绕这一工作流程轻松引入更多功能。这意味着hardhat在最核心的地方是编译、运行和测试智能合约。

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

Hardhat Runner是与Hardhat交互的CLI命令,是一个可扩展的任务运行器。它是围绕任务和插件的概念设计的。每次你从CLI运行Hardhat时,你都在运行一个任务。例如,npx hardhat compile运行的是内置的compile任务。任务可以调用其他任务,允许定义复杂的工作流程。用户和插件可以覆盖现有的任务,从而定制和扩展工作流程。

Hardhat的很多功能都来自于插件,而作为开发者,你可以自由选择想使用的插件。Hardhat不限制使用什么工具的,但它确实有一些内置的默认值。所有这些都可以覆盖。

安装

Hardhat是通过本地安装在项目中使用的。这样你的环境就可以重现,也可以避免未来的版本冲突。

要安装它,你需要创建一个npm项目,进入一个空文件夹,运行npm init。 并遵循其指示操作。项目初始化之后,运行:

npm install --save-dev hardhat

快速开始

通过在项目文件夹中运行npx hardhat来创建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  "Y888Welcome to Hardhat v2.19.4? What do you want to do? ...
> Create a JavaScript projectCreate a TypeScript projectCreate a TypeScript project (with Viem)Create an empty hardhat.config.jsQuit

我们选择创建JavaScript项目

Hardhat会让提示你如何安装,但是,如果你错过了,你可以用npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers来安装它们。

运行任务

$ npx hardhat
Hardhat version 2.0.8Usage: hardhat [GLOBAL OPTIONS] <TASK> [TASK OPTIONS]GLOBAL OPTIONS:--config              A Hardhat config file.--emoji               Use emoji in messages.--help                Shows this message, or a task's help if its name is provided--max-memory          The maximum amount of memory that Hardhat can use.--network             The network to connect to.--show-stack-traces   Show stack traces.--tsconfig            Reserved hardhat argument -- Has no effect.--verbose             Enables Hardhat verbose logging--version             Shows hardhat's version.AVAILABLE TASKS:accounts      Prints the list of accountscheck         Check whatever you needclean         Clears the cache and deletes all artifactscompile       Compiles the entire project, building all artifactsconsole       Opens a hardhat consoleflatten       Flattens and prints contracts and their dependencieshelp          Prints this messagenode          Starts a JSON-RPC server on top of Hardhat Networkrun           Runs a user-defined script after compiling the projecttest          Runs mocha testsTo get help for a specific task run: npx hardhat help [task]

编译合约

合约目录contracts/
要编译它,只需运行:

npx hardhat compile

测试合约

test/目录下,可以找到Lock.js文件:

const {time,loadFixture,
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { expect } = require("chai");describe("Lock", function () {// We define a fixture to reuse the same setup in every test.// We use loadFixture to run this setup once, snapshot that state,// and reset Hardhat Network to that snapshot in every test.async function deployOneYearLockFixture() {const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;const ONE_GWEI = 1_000_000_000;const lockedAmount = ONE_GWEI;const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;// Contracts are deployed using the first signer/account by defaultconst [owner, otherAccount] = await ethers.getSigners();const Lock = await ethers.getContractFactory("Lock");const lock = await Lock.deploy(unlockTime, { value: lockedAmount });return { lock, unlockTime, lockedAmount, owner, otherAccount };}describe("Deployment", function () {it("Should set the right unlockTime", async function () {const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);expect(await lock.unlockTime()).to.equal(unlockTime);});it("Should set the right owner", async function () {const { lock, owner } = await loadFixture(deployOneYearLockFixture);expect(await lock.owner()).to.equal(owner.address);});it("Should receive and store the funds to lock", async function () {const { lock, lockedAmount } = await loadFixture(deployOneYearLockFixture);expect(await ethers.provider.getBalance(lock.target)).to.equal(lockedAmount);});it("Should fail if the unlockTime is not in the future", async function () {// We don't use the fixture here because we want a different deploymentconst latestTime = await time.latest();const Lock = await ethers.getContractFactory("Lock");await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith("Unlock time should be in the future");});});describe("Withdrawals", function () {describe("Validations", function () {it("Should revert with the right error if called too soon", async function () {const { lock } = await loadFixture(deployOneYearLockFixture);await expect(lock.withdraw()).to.be.revertedWith("You can't withdraw yet");});it("Should revert with the right error if called from another account", async function () {const { lock, unlockTime, otherAccount } = await loadFixture(deployOneYearLockFixture);// We can increase the time in Hardhat Networkawait time.increaseTo(unlockTime);// We use lock.connect() to send a transaction from another accountawait expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith("You aren't the owner");});it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);// Transactions are sent using the first signer by defaultawait time.increaseTo(unlockTime);await expect(lock.withdraw()).not.to.be.reverted;});});describe("Events", function () {it("Should emit an event on withdrawals", async function () {const { lock, unlockTime, lockedAmount } = await loadFixture(deployOneYearLockFixture);await time.increaseTo(unlockTime);await expect(lock.withdraw()).to.emit(lock, "Withdrawal").withArgs(lockedAmount, anyValue); // We accept any value as `when` arg});});describe("Transfers", function () {it("Should transfer the funds to the owner", async function () {const { lock, unlockTime, lockedAmount, owner } = await loadFixture(deployOneYearLockFixture);await time.increaseTo(unlockTime);await expect(lock.withdraw()).to.changeEtherBalances([owner, lock],[lockedAmount, -lockedAmount]);});});});
});

可以使用npx hardhat test来运行测试:

$npx hardhat testLockDeployment✔ Should set the right unlockTime (1438ms)✔ Should set the right owner✔ Should receive and store the funds to lock✔ Should fail if the unlockTime is not in the future (56ms)WithdrawalsValidations✔ 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 itEvents✔ Should emit an event on withdrawalsTransfers✔ Should transfer the funds to the owner9 passing (2s)

部署合约

我们使用Hardhat脚本部署合约。 在 scripts/里面,你会发现 Lock.js的代码如下:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;// Uncomment this line to use console.log
// import "hardhat/console.sol";contract Lock {uint public unlockTime;address payable public owner;event Withdrawal(uint amount, uint when);constructor(uint _unlockTime) payable {require(block.timestamp < _unlockTime,"Unlock time should be in the future");unlockTime = _unlockTime;owner = payable(msg.sender);}function withdraw() public {// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);require(block.timestamp >= unlockTime, "You can't withdraw yet");require(msg.sender == owner, "You aren't the owner");emit Withdrawal(address(this).balance, block.timestamp);owner.transfer(address(this).balance);}
}

npx hardhat run .\scripts\deploy.js运行它:

$npx hardhat run .\scripts\deploy.js
Lock with 0.001ETH and unlock timestamp 1705461023 deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3

连接钱包或Dapp到Hardhat网络

Hardhat在启动时,默认情况下总会启动一个Hardhat Network的内存实例,你也可以以独立的方式运行Hardhat Network,以便外部客户(可能是MetaMask,你的Dapp前端,或者一个脚本)可以连接到它。

要以独立的方式运行Hardhat Network,运行npx hardhat node:

$npx 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: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80Account #1: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 (10000 ETH)
Private Key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690dAccount #2: 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC (10000 ETH)
Private Key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365aAccount #3: 0x90F79bf6EB2c4f870365E785982E1f101E93b906 (10000 ETH)
Private Key: 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6Account #4: 0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65 (10000 ETH)
Private Key: 0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926aAccount #5: 0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc (10000 ETH)
Private Key: 0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffbaAccount #6: 0x976EA74026E726554dB657fA54763abd0C3a0aa9 (10000 ETH)
Private Key: 0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564eAccount #7: 0x14dC79964da2C08b23698B3D3cc7Ca32193d9955 (10000 ETH)
Private Key: 0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356Account #8: 0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f (10000 ETH)
Private Key: 0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97Account #9: 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720 (10000 ETH)
Private Key: 0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6Account #10: 0xBcd4042DE499D14e55001CcbB24a551F3b954096 (10000 ETH)
Private Key: 0xf214f2b2cd398c806f84e317254e0f0b801d0643303237d97a22a48e01628897Account #11: 0x71bE63f3384f5fb98995898A86B02Fb2426c5788 (10000 ETH)
Private Key: 0x701b615bbdfb9de65240bc28bd21bbc0d996645a3dd57e7b12bc2bdf6f192c82Account #12: 0xFABB0ac9d68B0B445fB7357272Ff202C5651694a (10000 ETH)
Private Key: 0xa267530f49f8280200edf313ee7af6b827f2a8bce2897751d06a843f644967b1Account #13: 0x1CBd3b2770909D4e10f157cABC84C7264073C9Ec (10000 ETH)
Private Key: 0x47c99abed3324a2707c28affff1267e45918ec8c3f20b8aa892e8b065d2942ddAccount #14: 0xdF3e18d64BC6A983f673Ab319CCaE4f1a57C7097 (10000 ETH)
Private Key: 0xc526ee95bf44d8fc405a158bb884d9d1238d99f0612e9f33d006bb0789009aaaAccount #15: 0xcd3B766CCDd6AE721141F452C550Ca635964ce71 (10000 ETH)
Private Key: 0x8166f546bab6da521a8369cab06c5d2b9e46670292d85c875ee9ec20e84ffb61Account #16: 0x2546BcD3c84621e976D8185a91A922aE77ECEc30 (10000 ETH)
Private Key: 0xea6c44ac03bff858b476bba40716402b03e41b8e97e276d1baec7c37d42484a0Account #17: 0xbDA5747bFD65F08deb54cb465eB87D40e51B197E (10000 ETH)
Private Key: 0x689af8efa8c651a91ad287602527f3af2fe9f6501a7ac4b061667b5a93e037fdAccount #18: 0xdD2FD4581271e230360230F9337D5c0430Bf44C0 (10000 ETH)
Private Key: 0xde9be858da4a475276426320d5e9262ecfc3ba460bfac56360bfa6c4c28b4ee0Account #19: 0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199 (10000 ETH)
Private Key: 0xdf57089febbacf7ba0bc227dafbffa9fc08a93fdc68e1e42411a14efcf23656eWARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.

这将暴露一个JSON-RPC接口链接到Hardhat网络。只要将钱包或应用程序连接到http://localhost:8545就可以使用它。

如果要把Hardhat连接到这个节点上,例如,要在这个网络上运行一个部署脚本,只需要使用--network localhost来运行脚本。

你可以试一试,先用npx hardhat node启动一个节点,并使用network参数重新运行示例脚本:

$npx hardhat run .\scripts\deploy.js --network localhost

部署成功后,节点中会有对应执行日志。
在这里插入图片描述

部署主网/测试网

可以--network在运行任何任务时使用该参数,如下所示:

$npx hardhat run scripts/deploy.js --network <network-name>

deploy.jsmain方法中第一行添加签名

const [deployer] = await ethers.getSigners();

hardhat.config.js文件中添加新的网络。我们将在本示例中使用 Sepolia:

require("@nomicfoundation/hardhat-toolbox");// Go to https://infura.io, sign up, create a new API key
// in its dashboard, and replace "KEY" with it
const INFURA_API_KEY = "f12202b025d24f9e8ebc5d0a786";// Replace this private key with your Sepolia account private key
// To export your private key from Coinbase Wallet, go to
// Settings > Developer Settings > Show private key
// To export your private key from Metamask, open Metamask and
// go to Account Details > Export Private Key
// Beware: NEVER put real Ether into testing accounts
const SEPOLIA_PRIVATE_KEY = "钱包私钥";module.exports = {solidity: "0.8.23",networks: {sepolia: {url: `https://sepolia.infura.io/v3/${INFURA_API_KEY}`,accounts: [SEPOLIA_PRIVATE_KEY]}}
};

最后运行:

$npx hardhat run scripts/deploy.js --network sepolia

Hardhat官网

这篇关于hardhat入门教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/797342

相关文章

Weex入门教程之4,获取当前全局环境变量和配置信息(屏幕高度、宽度等)

$getConfig() 获取当前全局环境变量和配置信息。 Returns: config (object): 配置对象;bundleUrl (string): bundle 的 url;debug (boolean): 是否是调试模式;env (object): 环境对象; weexVersion (string): Weex sdk 版本;appName (string): 应用名字;

Weex入门教程之3,使用 Vue 开发 Weex 页面

环境安装 在这里简略地介绍下,详细看官方教程 Node.js 环境 Node.js官网 通常,安装了 Node.js 环境,npm 包管理工具也随之安装了。因此,直接使用 npm 来安装 weex-toolkit。 npm 是一个 JavaScript 包管理工具,它可以让开发者轻松共享和重用代码。Weex 很多依赖来自社区,同样,Weex 也将很多工具发布到社区方便开发者使用。

Weex入门教程之2,Android Studio安装Weex插件

插件位置及描述 https://plugins.jetbrains.com/idea/plugin/8460-weex 貌似对windows还不是很支持,先放着吧。 安装 插件功能 先预览下都有什么功能 安装完成Weex插件后,如果在main toolbar找不到这些功能图标,那么就需要手动添加到main toolbar 添加到main toolbar 红框内就是

Weex入门教程之1,了解Weex

【资料合集】Weex Conf回顾集锦:讲义PDF+活动视频! PDF分享:链接:http://pan.baidu.com/s/1hr8RniG 密码:fa3j 官方教程:https://weex-project.io/cn/v-0.10/guide/index.html 用意 主要是介绍Weex,并未涉及开发方面,好让我们开始开发之前充分地了解Weex到底是个什么。 以下描述主要摘取于

Python简单入门教程helloworld

Python 学习资源 推荐书籍: Python核心编程(第二版) (强烈推荐,建议有一定基础的看,或者看完简明Python教程再看) Python 基础教程 第二版 (入门,没有核心编程好,但也不错) 编写高质量代码:改善Python程序的91个建议 (进阶,有一定基础再看) 书籍下载: Python 教程(部分内容来源于网络, 历时一年多总结整理的,给刚刚入门的

【超级干货】2天速成PyTorch深度学习入门教程,缓解研究生焦虑

3、cnn基础 卷积神经网络 输入层 —输入图片矩阵 输入层一般是 RGB 图像或单通道的灰度图像,图片像素值在[0,255],可以用矩阵表示图片 卷积层 —特征提取 人通过特征进行图像识别,根据左图直的笔画判断X,右图曲的笔画判断圆 卷积操作 激活层 —加强特征 池化层 —压缩数据 全连接层 —进行分类 输出层 —输出分类概率 4、基于LeNet

浅谈NODE的NPM命令和合约测试开发工具HARDHAT

$ npm install yarn -g  # 将模块yarn全局安装 $ npm install moduleName # 安装模块到项目目录下 默认跟加参数 --save 一样 会在package文件的dependencies节点写入依赖。   $ npm install -g moduleName # -g 的意思是将模块安装到全局,具体安装到磁盘哪个位置,要看 npm root -g

【2024最新】Python入门教程(非常详细)从零基础入门到精通,看完这一篇就够了!

前言 本文罗列了了python零基础入门到精通的详细教程,内容均以知识目录的形式展开。 第一章:python基础之markdown Typora软件下载Typora基本使用Typora补充说明编程与编程语言计算机的本质计算机五大组成部分计算机三大核心硬件操作系统 第二章:编程语言的发展史和第一个Python程序 文件的概念计算机内部数据原理编程语言发展史编程语言的分类python解释器版

TestNG 入门教程

阅读目录 TestNG介绍在Eclipse中在线安装TestNG在Eclipse中离线安装TestngTestNG最简单的测试TestNG的基本注解TestNG中如何执行测试使用testtng.xml 文件执行 case TestNG按顺序执行CaseTestNG异常测试TestNG组测试TestNG参数化测试TestNG忽略测试TestNG 依赖测试TestNG测试结果报告   Test

Weex入门教程之,关注weex-toolkit命令变更

由于weex的版本处于快速迭代中,我们需要时刻关注其变动,因为weex可能修复了某些bug或新增了一些功能。 关注链接:  原码链接:https://github.com/weexteam/weex-toolkit  发布链接:https://www.npmjs.com/package/weex-toolkit 或者使用帮助命令查看 Microsoft Windows [版本 10.0.10