Solidity

安全帽部署 MockV3Aggregator 不起作用

  • October 28, 2022

我學習了 Solidity 安全帽,學習了這門課程 Patrick Collins ( https://www.youtube.com/watch?v=gyMwXuJrbJQ ) 在第 7 課 Hardhat Fund Me 部分 Mocking / tags in hardhat 10:49:02,部署智能合約 (Chainlink MockV3Aggregator )。

yarn hardhat deploy --tags mocks 在終端的終端資訊中執行此命令

yarn run v1.22.19 warning ../package.json: No license field $ /Users/liwei/hardhat-fund-me/node_modules/.bin/hardhat deploy –tags mocks Nothing to compile ✨ 在 2.34 秒內完成。

似乎沒有部署智能合約。

下面的程式碼

00-部署-mock.js

const { network } = require("hardhat")
const { developmentChains } = require("../helper-hardhat-config")

//声明chainId,MockV3Aggregator的构造函数参数

const DECIMALS = "8"
const INITIAL_PRICE = "200000000"

module.exports = async ({ getNamedAccounts, deployments }) => {
   //console.log(`chainId is ${chainId}`)
   const { deploy, log } = deployments
   // The deployments field itself contains the deploy function.欄位本身包含部署功能。
   const { deployer } = await getNamedAccounts()
   // Fetch the accounts.获取账户 These can be configured in hardhat.config.ts as explained above.
   const chainId = network.config.chainId

   /*
   是否在本地或hardhat网络上,chainId.name pull from hardhat config,
   developmentChains pull from helper-hardhat-config
   includes关键字:看哪些变量是否在数组中
   */
   if (developmentChains.includes(chainId.name)) {
       log("Local network detected! Deploying mocks ...")
       await deploy("MockV3Aggregator", {
           contract: "MockV3Aggregator",
           from: deployer,
           log: true,
           args: [DECIMALS, INITIAL_PRICE],
       })
       log("Mocks deployed!")
       log("----------------------------------------------")
   }
}
// 如何只部署deploy mock文件
module.exports.tags = ["all", "mocks"]

01-部署-funde-me.js

const { network } = require("hardhat")
const { networkConfig, developmentChains } = require("../helper-hardhat-config")
//const { verify } = require("../utils/verify")
require("dotenv").config()

module.exports = async ({ getNamedAccounts, deployments }) => {
   // we get the deployments and getNamedAccounts which are provided by hardhat-deploy. 也就是hre

   const { deploy, log } = deployments
   // The deployments field itself contains the deploy function.欄位本身包含部署功能。
   const { deployer } = await getNamedAccounts()
   // Fetch the accounts.获取账户 These can be configured in hardhat.config.ts as explained above.
   const chainId = network.config.chainId

   let ethUsdPriceFeedAddress = networkConfig[chainId]["ethUsdPriceFeed"] // 参数priceFeed

   const fundMe = await deploy("FundMe", {
       from: deployer,
       args: [ethUsdPriceFeedAddress],
       log: true,
   })
}

module.exports.tags = ["all", "fundme"]

harhat.config.js

module.exports = {
   // solidity: "0.8.17",
   solidity: {
       compilers: [{ version: "0.8.8" }, { version: "0.6.6" }],
   },
   defaultNetwork: "hardhat",
   networks: {
       hardhat: {
           chainId: 31337,
           // gasPrice: 130000000000,
       },
       goerli: {
           url: GOERLI_RPC_URL,
           accounts: [PRIVATE_KEY],
           chainId: 5,
           blockConfirmations: 6,
       },
   },

   namedAccounts: {
       deployer: {
           default: 0, // 这样会默认把第一个账户作为部署合约的账户
           1: 0, //在主网上也会把第一个账户作为部署账户,不管conifg怎么配置,不同网络上的account 0 并不一样
       },
   },
}

助手-hardhat-c​​onfig.js

const networkConfig = {
   31337: {
       name: "localhost",
   },

   5: {
       name: "goerli",
       ethUsdPriceFeed: "0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e",
   },
}
// 声明develomentChains

const developmentChains = ["localhost", "hardhat"]
// export config可以让其他文件来互動
module.exports = {
   networkConfig,
   developmentChains,
}

確保您的文件夾結構中有一個“部署”目錄,以便實際部署契約。00-deploy-mock.js01-deploy-funde-me.js文件應該在該目錄中。

從FundMe repo複製所有軟體包 ,然後重新安裝它們。

引用自:https://ethereum.stackexchange.com/questions/138306