You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hardhat部署脚本报错:VRFCoordinatorV2Mock等相关问题求助

问题解决方案

问题1:deployments.get获取合约后调用createSubscription报错

deployments.get()返回的是合约的部署元数据(包含地址、ABI等),并非可直接调用的合约实例。你需要基于这些元数据创建合约实例:

// 获取部署信息
const vrfDeployment = await deployments.get("VRFCoordinatorV2Mock");
// 创建可调用的合约实例
const vrfCoordinatorV2Mock = await ethers.getContractAt(
  "VRFCoordinatorV2Mock",
  vrfDeployment.address
);
// 现在可以正常调用方法
await vrfCoordinatorV2Mock.createSubscription();

或者如果使用hardhat-deploy插件,也可以直接用deployments.getContract()获取实例:

const vrfCoordinatorV2Mock = await deployments.getContract("VRFCoordinatorV2Mock");
await vrfCoordinatorV2Mock.createSubscription();

问题2:ethers.getContract报Cannot read properties of undefined

这是因为ethers对象未被正确引入到当前作用域。解决方式:

  • 如果是hardhat-deploy的脚本,确保在导出函数时解构ethers:
module.exports = async ({ deployments, ethers }) => {
  // 在这里可以直接使用ethers.getContract
  const vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock");
};
  • 或者通过Hardhat Runtime Environment(hre)获取:
const hre = require("hardhat");
const vrfCoordinatorV2Mock = await hre.ethers.getContract("VRFCoordinatorV2Mock");

问题3:ethers.utils.parseEther报Cannot read properties of undefined

同样是ethers对象未正确引入的问题,解决方法:

  • 在hardhat-deploy脚本中,确保解构出ethers后使用:
module.exports = async ({ ethers }) => {
  const amount = ethers.utils.parseEther("1");
};
  • 或者直接导入ethers工具类:
const { utils } = require("ethers");
const amount = utils.parseEther("1");
  • 也可以通过hre访问:
const hre = require("hardhat");
const amount = hre.ethers.utils.parseEther("1");

内容的提问来源于stack exchange,提问作者Muhammad Adil Yaseen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:52:08