在正文的第一句加入“我正在参加「码上掘金挑战赛」详情请看:码上掘金挑战赛来了!
首先环境搭建
需要安装nodejs,建议版本用16以上
创建项目
新建一个项目文件
-
执行初始化指令: yarn init
-
安装Hardhat: yarn add --dev hardhat
-
进入安装目录执行: npx hardhat
-
选择创建一个空的项目
-
安装插件(可以不安装)
yarn add --dev @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-network-helpers @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers @nomiclabs/hardhat-etherscan chai ethers hardhat-gas-reporter solidity-coverage @typechain/hardhat typechain @typechain/ethers-v5 @ethersproject/abi @ethersproject/providers
6.在根目录新建一个contracts文件夹用了存放合约文件
7.新建一个Token.sol文件
//SPDX-License-Identifier: UNLICENSED
// Solidity files have to start with this pragma.
// It will be used by the Solidity compiler to validate its version.
pragma solidity ^0.8.17;
// This is the main building block for smart contracts.
contract Token {
// Some string type variables to identify the token.
string public name = "My Hardhat Token";
string public symbol = "MHT";
// The fixed amount of tokens, stored in an unsigned integer type variable.
uint256 public totalSupply = 1000000;
// An address type variable is used to store ethereum accounts.
address public owner;
// A mapping is a key/value map. Here we store each account's balance.
mapping(address => uint256) balances;
// The Transfer event helps off-chain applications understand
// what happens within your contract.
event Transfer(address indexed _from, address indexed _to, uint256 _value);
/**
* Contract initialization.
*/
constructor() {
// The totalSupply is assigned to the transaction sender, which is the
// account that is deploying the contract.
balances[msg.sender] = totalSupply;
owner = msg.sender;
}
function transfer(address to, uint256 amount) external {
// Check if the transaction sender has enough tokens.
// If `require`'s first argument evaluates to `false` then the
// transaction will revert.
require(balances[msg.sender] >= amount, "Not enough tokens");
// Transfer the amount.
balances[msg.sender] -= amount;
balances[to] += amount;
// Notify off-chain applications of the transfer.
emit Transfer(msg.sender, to, amount);
}
function balanceOf(address account) external view returns (uint256) {
return balances[account];
}
}
-
编译token合约 命令:npx hardhat compile
运行之后发现报错,报错信息
查看hardhat的错误文档
需要安装 solc yarn global add solc
9.再次执行: npx hardhat compile
编译成功
10.在项目的根目录新建一个test文件和Token.js的文件
token.js的代码如下
const { expect } = require("chai");
describe("Token contract", function () {
it("Deployment should assign the total supply of tokens to the owner", async function () {
const [owner] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
const hardhatToken = await Token.deploy();
const ownerBalance = await hardhatToken.balanceOf(owner.address);
expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
});
});
11.运行测试用例 npx hardhat test
添加 ethers
const { ethers } = require("hardhat");
发现还是报错
修改导入方式
还是报错
说是要引入另一插件 babel-cli(www.dvy.com.cn/2021/01/24/…
yarn global add babel-cli
安装了还是报一样的错误,感觉找错的路径错了,仔细检查发现没有引入
require("@nomiclabs/hardhat-ethers");
引入之后,执行:npx hardhat test
发现还是报错
这个报错事是断言库里面的逻辑有问题
最后修改为
最后终于成功
总结
终于成功使用hardhat创建了一个项目,虽然历时一个下午,但是还是很欣慰的。同时也深感自己js方面的知识有点缺乏,以后会好好加油!!!