实施步骤

官方文档

安装npm install --save-dev hardhat

  • 新建项目:npx hardhat init

  • 将所需合约放到contracts下,没有合约也没关系,有个默认合约在其中,只是熟悉流程的话建议改成以下:

    //SPDX-License-Identifier: Unlicensed
    pragma solidity ^0.8.0;
    
    contract Greeter {
      string greeting;
    
      event SetGreeting(
        address sender,     // msg.sender
        string greeting
      ); 
    
      constructor(string memory _greeting) {
        greeting = _greeting;
      }
    
      function greet() public view returns (string memory) {
        return greeting;
      }
    
      function setGreeting(string memory _greeting) public {
        greeting = _greeting;
        emit SetGreeting(msg.sender, _greeting);
      }
    }
  • 部署脚本,在scripts/deploy.js下:

    // We require the Hardhat Runtime Environment explicitly here. This is optional
    // but useful for running the script in a standalone fashion through `node <script>`.
    //
    // When running the script with `npx hardhat run <script>` you'll find the Hardhat
    // Runtime Environment's members available in the global scope.
    const hre = require("hardhat");
    
    async function main() {
      // Hardhat always runs the compile task when running scripts with its command
      // line interface.
      //
      // If this script is run directly using `node` you may want to call compile
      // manually to make sure everything is compiled
      // await hre.run('compile');
    
      // We get the contract to deploy
      const Greeter = await hre.ethers.getContractFactory("Greeter");
      const greeter = await Greeter.deploy("Hello, Hardhat!");
    
      await greeter.deployed();
    
      console.log("Greeter deployed to:", greeter.address);
    }
    
    // We recommend this pattern to be able to use async/await everywhere
    // and properly handle errors.
    main()
      .then(() => process.exit(0))
      .catch((error) => {
        console.error(error);
        process.exit(1);
      });
  • hardhat.config.js:

    require("@nomiclabs/hardhat-waffle");
    require('dotenv').config()
    
    // This is a sample Hardhat task. To learn how to create your own go to
    // https://hardhat.org/guides/create-task.html
    task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
      const accounts = await hre.ethers.getSigners();
    
      for (const account of accounts) {
        console.log(account.address);
      }
    });
    
    // You need to export an object to set up your config
    // Go to https://hardhat.org/config/ to learn more
    
    /**
     * @type import('hardhat/config').HardhatUserConfig
     */
    
    //This will default to ROLLUX_TANENBAUM_URL if you do not specify ANKR_API_KEY in .env
    const rolluxTanenbaumUrl = "https://rpc-tanenbaum.rollux.com"
    
    const words = "wealth void ski surge web vendor piano mass humble abstract dress require".match(/[a-zA-Z]+/g).length
    validLength = [12, 15, 18, 24]
    if (!validLength.includes(words)) {
       process.exit(-1)
    }
    
    module.exports = {
      solidity: "0.8.13",
      networks: {
        "rollux-tanenbaum": {
           url: rolluxTanenbaumUrl,
           chainId: 57000,
           accounts: { mnemonic: "YOUR MNEMONIC" }
        }
      }
    };

    可以参考https://github.com/SYS-Labs/rollux-tutorial/tree/main/getting-started#development-stacks,注意把env中的MNEMONIC和ANKR_API_KEY分别改成自己的助词和私钥

  • 按照上述网址的操作把合约部署好之后,可以去测试网上查看刚部署的合约:地址