为扩展开放的 zeplin ERC721URIStorage 合约的合约设置铸造价格

Setting a mint price for a contract extending a open zeplin ERC721URIStorage contract

我在 Solidity 中有以下合同,在我添加行之前一直在工作

require(msg.value == mintPrice, "Not Enough Ether");

// contracts/NFT.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract NFT is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    uint256 mintPrice = 0.025 ether;

    constructor() ERC721("NFT", "ITM"){}

    function mint(address user, string memory tokenURI)
        public
        payable
        returns (uint256)
        {
            require(msg.value == mintPrice, "Not Enough Ether");
            _tokenIds.increment();

            uint256 newItemId = _tokenIds.current();
            _mint(user, newItemId);
            _setTokenURI(newItemId, tokenURI);

            return newItemId;
        }

}

我有以下 chai 测试失败说没有足够的以太币,但我使用的地址是一个安全帽地址,里面有大量以太币

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();
   
   expect(await nft.mint('0xf3...fFb92266', "/Users/.../web3/nft-next-minter/public/test.json")).to.have.any.keys('hash');

   expect(await nft.tokenURI(1)).to.equal('/Users/.../web3/nft-next-minter/public/test.json');

 }); 
})

我是运行npx hardhat test --network localhost

不太清楚为什么会出现此错误,将不胜感激任何帮助。

提前致谢。

await nft.mint(
    '0xf3...fFb92266',
    "/Users/.../web3/nft-next-minter/public/test.json"
)

此 JS 代码段未指定任何 value 交易,因此默认发送 0 值。

并且由于合约期望 value 等于 mintPrice(0.025 以太币),它不符合 require() 条件,有效地恢复了交易。

您可以在 overrides 参数 (docs) 中指定值。

await nft.mint(
    '0xf3...fFb92266',
    "/Users/.../web3/nft-next-minter/public/test.json",
    {value: ethers.utils.parseEther('0.025')}
)