部署Hardhat合约时出现JsonRpcProvider未定义错误求助
部署Hardhat智能合约失败:Cannot read properties of undefined (reading 'JsonRpcProvider')
我跟着FreeCodeCamp的教程操作,已经重复两次部署Hardhat智能合约,但都失败了。我重建了项目目录,排除了安装/依赖问题,但还是遇到以下错误:
Error: ERROR processing skip func of /home/dylan/hh-fcc/hardhat-smartcontract-lottery-fcc/deploy/00-deploy-mocks.js:
TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
我搜索并尝试了相关解决方案,但没能解决问题,附上相关代码文件请求帮助。
00-deploy-mocks.js
const { developmentChains } = require("../helper-hardhat-config") const { network } = require("hardhat") const BASE_FEE = ethers.utils.parseEther("0.25") const GAS_PRICE_LINK = 1e9 module.exports = async function ({ getNamedAccounts, deployments }) { const { deploy, log } = deployments const { deployer } = await getNamedAccounts() const args = [BASE_FEE, GAS_PRICE_LINK] if (developmentChains.includes(network.name)) { log("local network detected! deploying mocks...") await deploy("VRFCoordinatorV2Mock", { from: deployer, log: true, args: args, }) log("Mocks Deployed!") log("----------------------------------------") } } module.exports.tags = ["all", "mocks"]
01-deploy-raffle.js
const { network } = require("hardhat") const { developmentChains, networkConfig } = require("../helper-hardhat-config") const { verify } = require("../helper-hardhat-config") const VRF_SUB_FUND_AMOUNT = ethers.utils.parseEther("2") module.exports = async function ({ getNamedAccounts, deployments }) { const { deploy, log } = deployments const { deployer } = await getNamedAccounts() const chainId = network.config.chainId let vrfCoordinatorV2Address, subscriptionId if (developmentChains.includes(network.name)) { const vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock") vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address const transactionResponse = await vrfCoordinatorV2Mock.createSubscription() const transactionReceipt = await transactionResponse.wait(1) subscriptionId = transactionReceipt.events[0].args.subId //Fund the sub //usually you'd need the link token await vrfCoordinatorV2Mock.fundSubscription(subscriptionId, VRF_SUB_FUND_AMOUNT) } else { vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"] subscriptionId = networkConfig[chainId]["subscriptionId"] } const entranceFee = networkConfig[chainId]["entranceFee"] const gasLane = networkConfig[chainId]["gasLane"] const callbackGasLimit = networkConfig[chainId]["callbackGasLimit"] const interval = networkConfig[chainId]["interval"] const args = [ vrfCoordinatorV2Address, entranceFee, gasLane, subscriptionId, callbackGasLimit, interval, ] const raffle = await deploy("Raffle", { from: deployer, args: args, log: true, waitConfirmations: network.config.blockConfirmations || 1, }) if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) { log("Verifying...") await verify(raffle.address, args) } log("----------------------------------") } module.exports.tags = ["all", "raffle"]
hardhat.config.js
require("@nomiclabs/hardhat-waffle") require("@nomiclabs/hardhat-etherscan") require("hardhat-deploy") require("solidity-coverage") require("hardhat-gas-reporter") require("hardhat-contract-sizer") require("dotenv").config() const GOERLI_RPC_URL = process.env.GOERLI_RPC_URL const PRIVATE_KEY = process.env.PRIVATE_KEY const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { defaultNetwork: "hardhat", networks: { hardhat: { chainId: 31337, blockConfirmations: 1, }, goerli: { chainId: 5, blockConfirmations: 6, url: GOERLI_RPC_URL, accounts: [PRIVATE_KEY], }, }, solidity: "0.8.7", namedAccounts: { deployer: { default: 0, }, player: { default: 1, }, }, }
helper-hardhat-config.js
const { ethers } = require("hardhat") const networkConfig = { 5: { name: "goerli", vrfCoordinatorV2: "0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D", entranceFee: ethers.utils.parseEther("0.01"), gasLane: "0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15", subscriptionId: "0", callbackGasLimit: "500000", interval: "30", }, 31337: { name: "hardhat", entranceFee: ethers.utils.parseEther("0.01"), gasLane: "0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15", callbackGasLimit: "500000", interval: "30", }, } const developmentChains = ["hardhat", "localhost"] module.exports = { networkConfig, developmentChains, }
问题根源
错误核心是部署脚本中未正确导入ethers对象,却直接使用了ethers.utils.parseEther等方法。此外还有两个次要问题:
01-deploy-raffle.js错误地从helper-hardhat-config导入verify,该文件未导出此函数helper-hardhat-config.js从hardhat导入ethers可能引发加载顺序问题
具体修复步骤
1. 修正00-deploy-mocks.js
在文件顶部添加ethers的导入:
const { developmentChains } = require("../helper-hardhat-config") const { network, ethers } = require("hardhat") // 新增ethers导入 const BASE_FEE = ethers.utils.parseEther("0.25") const GAS_PRICE_LINK = 1e9 // 剩余代码不变
2. 修正01-deploy-raffle.js
- 添加
ethers导入 - 修正
verify的导入路径(需确保存在对应的验证文件)
const { network, ethers } = require("hardhat") // 新增ethers导入 const { developmentChains, networkConfig } = require("../helper-hardhat-config") const { verify } = require("../utils/verify") // 修正到正确的verify文件路径 // 剩余代码不变
3. 修正helper-hardhat-config.js
改为直接导入ethers包,避免加载顺序问题:
const { ethers } = require("ethers") // 替换原导入方式 // 剩余代码不变
4. 创建utils/verify.js(若不存在)
实现合约验证功能:
const { run } = require("hardhat") const verify = async (contractAddress, args) => { console.log("Verifying contract...") try { await run("verify:verify", { address: contractAddress, constructorArguments: args, }) } catch (e) { if (e.message.toLowerCase().includes("already verified")) { console.log("Already verified!") } else { console.error(e) } } } module.exports = { verify }
验证修复
完成修改后重新运行部署命令:
npx hardhat deploy
内容的提问来源于stack exchange,提问作者LearningCoder
相关产品推荐
相关产品推荐

