• 7.区块链系列之hardhat框架部署合约


    先前讲解的本地部署只能合约的方式编码较多,现在我们介绍目前比较流行的智能合约框架hardhat

    1.环境准备
    yarn init
    yarn add --dev hardhat
    yarn hardhat
    npm install --save-dev @nomicfoundation/hardhat-toolbox
    
    • 1
    • 2
    • 3
    • 4
    2. 新建并编译SimpleStorage.sol
    • 在hardhat框架conracts目录下新建SimpleStorage.sol
    // SPDX-License-Identifier: MIT
    pragma solidity 0.8.8;
    
    contract SimpleStorage {
        uint256 favoriteNumber;
    
        struct People {
            uint256 favoriteNumber;
            string name;
        }
    
        // uint256[] public anArray;
        People[] public people;
    
        mapping(string => uint256) public nameToFavoriteNumber;
    
        function store(uint256 _favoriteNumber) public {
            favoriteNumber = _favoriteNumber;
        }
    
        function retrieve() public view returns (uint256) {
            return favoriteNumber;
        }
    
        function addPerson(string memory _name, uint256 _favoriteNumber) public {
            people.push(People(_favoriteNumber, _name));
            nameToFavoriteNumber[_name] = _favoriteNumber;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 修改配置文件solidity版本为0.8.8
    module.exports = {
      solidity: "0.8.8"
    };
    
    • 1
    • 2
    • 3
    • 开始编译yarn hardhat compile
    (base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat compile
    yarn run v1.22.19
    $ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat compile
    Downloading compiler 0.8.8
    Compiled 1 Solidity file successfully
    Done in 5.70s.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 安装代码美化依赖
    yarn add --dev prettier prettier-plugin-solidity
    
    • 1

    新建.prettierrc文件

    {
      "tabWidth": 4,
      "useTabs": false,
      "semi": false,
      "singleQuote": false
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    新建.prettierignore文件

    node_modules
    package.json
    img
    artifacts
    cache
    coverage
    .env
    .*
    README.md
    coverage.json
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    3.修改deploy.js并部署

    将以下代码覆盖deploy.js内容

    // imports
    const { ethers, run, network } = require("hardhat")
    
    // async main
    async function main() {
      const SimpleStorageFactory = await ethers.getContractFactory("SimpleStorage")
      console.log("Deploying contract...")
      const simpleStorage = await SimpleStorageFactory.deploy()
      await simpleStorage.deployed()
      console.log(`Deployed contract to: ${simpleStorage.address}`)
    }
    
    // main
    main()
      .then(() => process.exit(0))
      .catch((error) => {
        console.error(error)
        process.exit(1)
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    执行 yarn hardhat run scripts/deploy.js部署合约

    (base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat run scripts/deploy.js
    yarn run v1.22.19
    $ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat run scripts/deploy.js
    Deploying contract...
    Deployed contract to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
    Done in 3.67s.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在hardhat.config.js中,我们添加defaultNetwork: "hardhat",这是未指定网络时默认的配置

    module.exports = {
      defaultNetwork: "hardhat",
      ...
    };
    
    • 1
    • 2
    • 3
    • 4

    指定具体网络为hardhat

    (base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat run scripts/deploy.js --network hardhat
    yarn run v1.22.19
    $ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat run scripts/deploy.js --network hardhat
    Deploying contract...
    Deployed contract to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
    Done in 3.51s.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    4. 部署至GOERLI测试网络
    • 新建.env文件

    配置如下内容为测试网信息,详见之前文章

    GOERLI_RPC_URL=XXXXX
    PRIVATE_KEY=XXXXXXX
    
    • 1
    • 2

    覆盖deploy.js内容如下

    require("@nomicfoundation/hardhat-toolbox");
    require("dotenv").config()
    
    const GOERLI_RPC_URL = process.env.GOERLI_RPC_URL
    const PRIVATE_KEY = process.env.PRIVATE_KEY
    
    /** @type import('hardhat/config').HardhatUserConfig */
    module.exports = {
      defaultNetwork: "hardhat",
      networks: {
        hardhat: {},
        goerli: {
          url: GOERLI_RPC_URL,
          accounts: [PRIVATE_KEY],
          // https://chainlist.org/zh
          chainId: 5
        }
      },
      solidity: "0.8.8"
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    // 安装依赖
    npm install dotenv --save
    
    • 1
    • 2
    (base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat run scripts/deploy.js --network goerli
    yarn run v1.22.19
    $ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat run scripts/deploy.js --network goerli
    Deploying contract...
    Deployed contract to: 0x4fC6FAe2C80adFd7beF5F6AeF2d8E59F2f3e1265
    Done in 30.15s.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    访问该地址发现部署成功https://goerli.etherscan.io/address/0x4fC6FAe2C80adFd7beF5F6AeF2d8E59F2f3e1265

    1
    欢迎关注公众号算法小生获取完整代码

  • 相关阅读:
    html静态商城网页制作 基于HTML+CSS+JavaScript在线服装商城店铺商城设计毕业论文源码
    [附源码]SSM计算机毕业设计校园新闻管理系统JAVA
    K8S对外服务之Ingress
    安全漏洞分类之CNNVD漏洞分类指南
    软件测试的方法总结
    Fabric.js在vue2中使用
    Form组件
    SetProxy.bat 设置代理
    西门子HMI切换页面时的指示功能
    Java数据结构与算法---稀疏数组(二)
  • 原文地址:https://blog.csdn.net/SJshenjian/article/details/127460356