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

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

1.环境准备

yarn init
yarn add --dev hardhat
yarn hardhat
npm install --save-dev @nomicfoundation/hardhat-toolbox

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;
    }
}
  • 修改配置文件solidity版本为0.8.8
module.exports = {
  solidity: "0.8.8"
};
  • 开始编译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.
  • 安装代码美化依赖
yarn add --dev prettier prettier-plugin-solidity

新建.prettierrc文件

{
  "tabWidth": 4,
  "useTabs": false,
  "semi": false,
  "singleQuote": false
}

新建.prettierignore文件

node_modules
package.json
img
artifacts
cache
coverage
.env
.*
README.md
coverage.json

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

执行 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.

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

module.exports = {
  defaultNetwork: "hardhat",
  ...
};

指定具体网络为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.

4. 部署至GOERLI测试网络

  • 新建.env文件

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

GOERLI_RPC_URL=XXXXX
PRIVATE_KEY=XXXXXXX

覆盖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"
};
// 安装依赖
npm install dotenv --save
(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.

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

1

所有代码已提交至https://gitee.com/SJshenjian/blockchain/tree/master/hardhat-simple-storage-fcc

欢迎关注公众号算法小生沈健的技术博客查看最新文章

posted @ 2022-10-29 15:47  算法小生  阅读(381)  评论(0编辑  收藏  举报