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

基于OpenZeppelin ERC721URIStorage的合约设置mint价格后测试报错

问题根因

你的mint函数为payable类型,合约逻辑要求调用时必须随交易发送和mintPrice(0.025 ETH)等值的ETH。当前测试代码调用nft.mint时没有指定随交易发送的ETH数量,导致交易携带的msg.value为0,触发了校验报错,该错误和测试账户的总余额充足与否无关。

修复方案

修改测试代码中mint方法的调用逻辑,添加value参数指定发送的ETH数量,用ethers.js内置工具将0.025 ETH转换为wei单位传入即可,同时建议增加交易等待逻辑,避免未上链就查询tokenURI导致报错。
修改后的测试代码如下:

describe("NFT", function () {
 it("Should return a transaction hash, async function", async function () {
   const NFT = await ethers.getContractFactory("NFT");
   const nft = await NFT.deploy();
   await nft.deployed();
   
   // 调用mint时新增value参数,传入0.025 ETH对应的wei值
   const mintTx = await nft.mint(
    '0xf3...fFb92266', 
    "/Users/.../web3/nft-next-minter/public/test.json",
    { value: ethers.utils.parseEther("0.025") }
   );
   // 等待交易确认上链
   await mintTx.wait();
   
   expect(mintTx).to.have.any.keys('hash');
   expect(await nft.tokenURI(1)).to.equal('/Users/.../web3/nft-next-minter/public/test.json');
 }); 
})

可选优化建议

你可以将合约中的mintPrice变量可见性改为public,测试时直接读取链上配置值,避免硬编码数值和合约配置不一致的问题:

// 合约中修改mintPrice的定义
uint256 public mintPrice = 0.025 ether;

修改后测试代码可以直接读取合约配置:

const mintPrice = await nft.mintPrice();
const mintTx = await nft.mint(
  '0xf3...fFb92266', 
  "/Users/.../web3/nft-next-minter/public/test.json",
  { value: mintPrice }
);

内容的提问来源于stack exchange,提问作者Anders Kitson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 09:18:00