Smart contracts are developed and tested using hardhat (MAC)

Online: http://remix.ethereum.org

Build a local development environment

1. Node.js installation official website address: https://nodejs.org/en/

After downloading and installing, all the way to next is OK

Open the terminal and enter node -v, npm -v to test whether the installation is successful

2. The official website address of vs code installation: https://code.visualstudio.com

After downloading and installing, continue to install the two plug-ins

plug-in

3. Create a development folder mkdir test, switch the terminal to this location cd test

4. Initialize the npm project, npm init press enter, press enter all the way

npm init

5、Install hardhat,npm install --save-dev hardhatinstall hardhat

6. Initialize hardhat, press Enter for npx hardhat init, choose to create a js project and press Enter

Initialize hardhat

7. Install the Firefox browser and download the metamask plug-in

8. Implement simple contracts and write test cases

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
    }
};

terminal command

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. Running results

operation result

10. File structure (Lock is a file that is automatically generated when it is created)

file structure

Guess you like

Origin blog.csdn.net/qq_37575994/article/details/127384163