I learn solidity hardhat followed this course Patrick Collins (https://www.youtube.com/watch?v=gyMwXuJrbJQ ) At Lesson7 Hardhat Fund Me section Mocking / tags in hardhat 10:49:02,deploy a smart contract (Chainlink MockV3Aggregator).
run this command in terminal yarn hardhat deploy --tags mocks
information in terminal
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 ✨ Done in 2.34s.
it seems the smart contract isn't deployed.
code below
00-deploy-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-deploy-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 并不一样
},
},
}
helper-hardhat-config.js
const networkConfig = {
31337: {
name: "localhost",
},
5: {
name: "goerli",
ethUsdPriceFeed: "0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e",
},
}
// 声明develomentChains
const developmentChains = ["localhost", "hardhat"]
// export config可以让其他文件来交互
module.exports = {
networkConfig,
developmentChains,
}