区块链全栈以太坊(七)FoundMe之Hardhat版本.md

一、简介

源码 demo

快速开始

cd hardhat-fund-me-fcc
#安装依赖
yarn
#编译&部署到本地网络
yarn hardhat deploy
#

二、从零编写

(一)新建工程

mkdir xxx
cd xxx
yarn add --dev hardhat
# 查看 hardhat命令列表
yarn hardhat
#选择 
#Create an advanced sample project
#选择安装范例依赖 
#install this sample project's dependencies
#到这里,高级模板的项目创建完成

(二)去掉无用配置

1)去掉.eslintrc.js、.eslintignore

安装的依赖:
eslint: 发现并自动修复js代码问题。

2)去掉.npmignore

(三)solhint

solhint: 扫描分析solidity的代码,保证最佳的代码实践。

#扫描指定文件
yarn solhint contracts/*.sol

(四)prettier

从其它工程复制配置到 .prettierrc

(五)编写合约

FundMe.sol 、PriceConverter.sol

(六)编译

// 打开hardhat.config.js,修改solidity 编译器版本
solidity: "0.8.8"
yarn hardhat compile
#报错
#the library @chainlink/contracts,imported from contracts/FundMe.sol is not installed.
#这是因为,在remix中,会自动安装如下语句的依赖
#import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
#但是在vscode中,需要自己安装到node_modules
#这样才找得到依赖
yarn add --dev @chainlink/contracts
#再次编译
yarn hardhat compile
#编译成功

(七)部署

之前使用的是 deploy.js 脚本,现在改用 hardhat-deploy插件

用于 重复部署以及简便测试

1)安装

npm install -D hardhat-deploy
yarn add --dev hardhat-deploy

2)hardhat.config.js

require("hardhat-deploy")
#执行
yarn hardhat
#可以发现如下 task
deploy                Deploy contracts

3)hardhat-deploy-ethers

mkdir deploy
#用 hardhat-deploy-ethers 覆盖 hardhat-ethers
npm install --save-dev  @nomiclabs/hardhat-ethers hardhat-deploy-ethers ethers
#或是yarn
yarn add --dev @nomiclabs/hardhat-ethers hardhat-deploy-ethers ethers

4) 脚本编写

脚本编写注意:

1.仍然要 导入相关的库和包。
2.不需要main函数。
3.脚本文件名要加编号,便于按顺序执行。

新建01-deploy-fund-me.js。

定义函数,导出为默认。

//后续会有其它形式的函数。
function deployFunc(){
    console.log("Hi!")
}
module.exports.default = deployFunc

多种形式的函数

//hre: hardhat运行环境
module.exports = async (hre) => {
    //解包提取对象 ,类似hre.getNameAccounts
    const {getNameAccounts,deployments} = hre
    
}
//参数直接解包
module.exports = async ({getNameAccounts,deployments}) => {
    //解包提取对象 ,类似hre.getNameAccounts    
}

5)执行

# 执行 deploy文件下下所有脚本
yarn hardhat deploy 
# 可以看到正常输出Hi
# 执行 deploy文件夹下指定脚本
# 如下只会执行 00-deploy-mock.js,因为里面定义了#module.exports.tags = ["all", "mocks"]
yarn hardhat deploy  --tag mocks

6)本地节点自动执行

# 启动一个新的区块链节点,并且它已经包含我们已经部署的所有合约。
yarn hardhat node

(八)脚本编写

1)mock

注意,里面用到了mocking,当在hardhat执行环境时,做数据挡板。

const { deploy, log } = deployments
    const { deployer } = await getNamedAccounts()
    const chainId = network.config.chainId

    let ethUsdPriceFeedAddress
    if (chainId == 31337) {
        const ethUsdAggregator = await deployments.get("MockV3Aggregator")
        ethUsdPriceFeedAddress = ethUsdAggregator.address
    } else {
        ethUsdPriceFeedAddress = networkConfig[chainId]["ethUsdPriceFeed"]
    }
    log("----------------------------------------------------")
    log("Deploying FundMe and waiting for confirmations...")
    const fundMe = await deploy("FundMe", {
        from: deployer,
        args: [ethUsdPriceFeedAddress],
        log: true,
        // we need to wait if on a live network so we can verify properly
        waitConfirmations: network.config.blockConfirmations || 1,
    })
    log(`FundMe deployed at ${fundMe.address}`)

    if (
        !developmentChains.includes(network.name) &&
        process.env.ETHERSCAN_API_KEY
    ) {
        await verify(fundMe.address, [ethUsdPriceFeedAddress])
    }
//hardhat.config.js
 namedAccounts: {
        deployer: {
  //accounts种的第0个就是deployer
            default: 0, 
  //指定不同链上的第几个账户为deployer
  //如下指定 hardhat上,第1个账户为deployer
            31337: 1, 
        },
         //可以创建多个用户,如下
        user: {
             default: 1, 
        }    
    },

2)区分环境

https://github.com/aave/aave-v3-core

参考源码编写区分环境的代码 helper-hardhat-config.js

根据网络环境传入合约地址。

3)验证

新建utils目录,编写verify代码。
.env相关参数填充: privateKey, RPC_URL(alchemy),coinmarketcap_apikey(可选),etherscan_apikey。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容