Search code examples
ethereumsoliditychai

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


I have the following contract in Solidity that was working up until I added the line

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;
        }

}

I have the following chai test that fails saying not enough ether, but the address I am using is a hardhat address with tons of ether in it

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');

 }); 
})

I am running npx hardhat test --network localhost

Not quite sure why I am getting this error, any help would be greatly appreciated.

Thanks ahead of time.


Solution

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

    This JS snippet doesn't specify any value of the transaction, so it's sent with 0 value by default.

    And since the contract is expecting the value to equal mintPrice (0.025 ether), it fails the require() condition, effectively reverting the transaction.

    You can specify the value in the overrides param (docs).

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