本地开发环境搭建
下载后进行安装,一路next就OK
打开终端输入node -v,npm -v测试是否安装成功
下载后进行安装,继续安装两个插件
3、创建一个开发文件夹 mkdir test,终端切换到该位置cd test
4、初始化npm项目,npm init回车,一路回车
5、安装 hardhat,npm install --save-dev hardhat
6、初始化hardhat,npx hardhat init 回车,选择创建一个js项目后回车
7、安装火狐浏览器,下载metamask插件
8、实现简单合约,编写测试用例
test/contracts/HelloWorld.sol
// SPDX-License-Identifier:GPL-3.0
pragma solidity ^0.8.9;
contract HelloWorld{
string _h="hello world!";
function getHi()public view returns(string memory){
return _h;
}
}
test/test/hw.js
const { expect } = require('chai');
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const HW = artifacts.require('HelloWorld');
contract("HelloWorld", function ([alice, bob, carol, david, erin]) {
let hw;
let result;
before(async function () {
hw = await HW.new({ from: alice });
console.log("contract address:",HW,"alice",alice);
});
describe("测试", function () {
it("getHi", async function () {
result=await hw.getHi();
console.log("hi:", result.toString());
});
});
});
test/harhat.config.js
require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-truffle5");
module.exports = {
solidity: "0.8.17",
hardhat: {
accounts: {
count: 5,
}
},
test: {
url: 'http://127.0.0.1:8545',
accounts: [`私钥`],
gasPrice: 5 * 10 ** 9
}
};
终端命令
npm i
npm install --save-dev @openzeppelin/test-helpers
npm install --save-dev @nomiclabs/hardhat-truffle5
npm install --save-dev "@nomiclabs/hardhat-web3@^2.0.0"
npx hardhat test/hw.js
9、运行结果
10、文件结构(Lock是创建的时候自动生成的文件)
|