首页 > 其他分享 >7.区块链系列之hardhat框架部署合约

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

时间:2022-10-29 15:57:41浏览次数:86  
标签:run 框架 deploy blockchain yarn js 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

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

标签:run,框架,deploy,blockchain,yarn,js,hardhat,区块
From: https://www.cnblogs.com/shenjian-online/p/16838900.html

相关文章

  • 6.区块链系列之本地智能合约部署至测试网与主网
    1.注册登录alchemyhttps://www.alchemy.com/在面板中我们看到Goerli网络,然后点击viewkey,如下图所示拷贝http替换.env中的RPC_URL=http://127.0.0.1:7545配置2.导出......
  • 5.区块链系列之私钥管理
    本文讲解如何对私钥加密处理以及部署后需注意的事项1.新建encryptKey.jsconstethers=require("ethers");constfs=require("fs-extra");require("dotenv").config......
  • 9.区块链系列之hardhat框架测试合约
    先前我们讲解了如何部署智能合约,今天我们来对合约进行测试,这是非常重要的一部分,毕竟一旦部署后不可变,如果测试不充分,那么黑客就不会客气了1.单元测试在test目录下新建te......
  • 8.区块链系列之hardhat框架部署合约(二)
    现在我们来实践hardhat部署合约中的其他更多技术要点1.代码方式验证合约注册https://etherscan.io/,如下图添加拷贝API_KEY在.env文件中新增ETHERSCAN_API_KEYE......
  • 10.区块链系列之hardhat部署抵押赎回Fund合约
    本文继续通过笔者学习到的抵押赎回智能合约Fund来进一步学习solidity语言,加深对开发的理解,其中通过storage节省gas是需要重点实践的,毕竟涉及到资产代码已提交至https://gi......
  • GF(Go Frame)开发框架
    ​​GF(GoFrame)​​是一款模块化、松耦合、生产级的Go应用开发框架。提供了常用的核心开发组件,如:缓存、日志、文件、时间、队列、数组、集合、字符串、定时器、命令行、文......
  • robotframework自动化测试框架实战教程:库文档工具(Libdoc)
    Libdoc是RobotFramework内置的工具,用来为测试库和资源文件生成关键字的文档.文档分为HTML和XML格式,前者供人阅读,后者供RIDE和其它工具使用. 文档文件被指定为库/......
  • #打卡不停更#【FFH】浅析Ability框架中Stage模型与FA模型的差异
    (#打卡不停更#【FFH】浅析Ability框架中Stage模型与FA模型的差异)Aility框架概述Ability是应用所具备能力的抽象,也是应用程序的基本组成单元。OpenHarmony与HarmonyOS的应......
  • Model/View框架简介-Qt
    一.Model/View框架简介1.Model/View框架核心思想    Model/View框架的核心思想是模型(数据)与视图(显示)相分离,模型对外提供标准接口存取数据,不关心数据如何显示,视图自定......
  • 分布式定时任务设计及其框架
    在很多应用系统中,我们常常要定时或周期性执行一些任务。比如,订单系统的超时状态判断、缓存数据的定时更新、定时给用户发邮件,甚至是一些定期计算的报表等。单机程序中常见的......