调用Chainlink VRFCoordinatorMockV2的fundSubscription触发BigNumber错误
问题解决:调用VRFCoordinatorMockV2.fundSubscription触发BigNumber无效错误
问题原因
你遇到的invalid BigNumber value错误,核心是参数类型/值不符合合约方法要求:
- 合约
fundSubscription的第二个参数要求是uint96类型,但你的代码中用ethers.parseInt("1")生成FUND_AMOUNT存在写法错误(从输出看实际需要的是1 ETH对应的wei值,应该用parseEther而非parseInt)。 - 未显式声明
subscriptionId变量,存在潜在的作用域风险(虽日志显示有值,但显式声明能避免未定义问题)。
解决方案
1. 修正资金量生成方式
替换错误的数值生成方法,确保得到符合要求的wei值:
- Ethers v6:用
ethers.parseEther("1")生成1 ETH对应的wei值(1000000000000000000n),完全符合uint96范围要求。 - Ethers v5:用
ethers.utils.parseEther("1")生成对应BigNumber值。
2. 调整脚本代码,确保参数匹配
修改部署脚本如下:
// Ethers v6版本示例 const FUND_AMOUNT = ethers.parseEther("1"); const transactionReceipt = await transactionResponse.wait(); // 显式声明变量,避免作用域问题 const subscriptionId = transactionReceipt.logs[0].args.subId; console.log(subscriptionId); console.log(FUND_AMOUNT); // 按合约要求传递参数 await vrfCoordinatorV2MockContract.fundSubscription(subscriptionId, FUND_AMOUNT);
3. 额外验证步骤
- 提前确认订阅有效性:调用前检查订阅是否存在,避免无效订阅触发的隐性错误:
const subscription = await vrfCoordinatorV2MockContract.getSubscription(subscriptionId); if (subscription.owner === ethers.ZeroAddress) { throw new Error("无效的订阅ID"); } - 确认合约ABI正确性:使用Chainlink官方VRFCoordinatorMockV2.sol生成的ABI,避免方法签名不匹配导致的参数解析错误。
内容的提问来源于stack exchange,提问作者nimish
相关产品推荐
相关产品推荐

