Loop in openzeppelin function

I'm wrinting my firt smart contract.. I'm using the Wizard to help me. I finish edit my contract, now time to test. I dont know how to write complete tests, so I'm using Remix.org to help.
I got some of the following errors:

Gas requirement of function vjkNFT.pause is infinite: If the gas requirement of a function is higher than the block gas limit, it cannot be executed. Please avoid loops in your functions or actions that modify large areas of storage (this includes clearing or copying arrays in storage)
Pos: 22:4 :

I believe those loopings are coming from OpenZepplin function, I tested them alone and I got the same error.
I'm using those function:

    function pause() public onlyOwner {
        _pause();
    }
    function unpause() public onlyOwner {
        _unpause();
    }
    function safeMint(string memory uri) external payable {

        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();

        _safeMint(msg.sender, tokenId);
        _setTokenURI(tokenId, uri);
    }

Some one have some idea about what is happing?

Hi @Bruno_Raphael_Rocha, I tried the above in Remix and don't see any error. Can you share the rest of your code or how you are testing it?

1 Like

I cant finger it out:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract vjkNFT is ERC721, ERC721Enumerable, ERC721URIStorage, Pausable, Ownable, ERC721Burnable {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIdCounter;

    uint256 public mintPrice = 0.05 ether;
    uint256 public maxSupply = 9999;
    mapping(address => uint) public mintedWallets;

    constructor() payable ERC721("vjkNFT", "VJK") { }

    function pause() public onlyOwner {
        _pause();
    }

    function unpause() public onlyOwner {
        _unpause();
    }

    function setMaxSupply(uint256 _maxSupply) external onlyOwner{
        maxSupply = _maxSupply;
    }

    function withdraw() public payable onlyOwner {
        payable(owner()).transfer(address(this).balance);
    }

    function safeMint(string memory uri) external payable {
        uint256 tokenId = _tokenIdCounter.current();

        require(mintedWallets[msg.sender] < 10, "exceeds max per wallet");
        require(msg.value == mintPrice, "wrong value");
        require(maxSupply > tokenId, "sold out");

        mintedWallets[msg.sender]++;
        _tokenIdCounter.increment();

        _safeMint(msg.sender, tokenId);
        _setTokenURI(tokenId, string(abi.encodePacked("data:application/json;base64,", uri)));
    }

    function _beforeTokenTransfer(address from, address to, uint256 tokenId)
        internal
        whenNotPaused
        override(ERC721, ERC721Enumerable)
    {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    // The following functions are overrides required by Solidity.

    function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }

    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }
}

I am here for the same reason. I am pretty sure it is because it is enumerable. Enumerable costs gas that in extreme conditions could become excessive?

1 Like

I just tried removing enumerable and I am still getting the error so it wasn't that. I am using the open zeppelin contract wizard, I doubt the contact changed much since I used it last month without issues. I am not concerned moving forward without worrying about it, but it would be cool if somebody knew why.

1 Like

Apparently strings are potentially infinite since strings are dynamic arrays in solidity. Some function containing a string as a parameter is causing this technically correct error. Should be fine.

1 Like

Awesome.. I'll test here too.. thank you so much

That is the problem, the error is also in this function, with no string parameters:

function pause() public onlyOwner {
    _pause();
}

Or no? I'm new here, so I'm not sure

@ MetalGearSolidity I got the same anwser from others that I consulted.
"
You can safely ignore this warning. The problem is your string variable in the parameters string uri . Because a string has no fixed size, it is theoretically possible to require an infinite amount of gas to fill it with an infinite amount of characters.

Your code will still compile and work if no other errors are being shown.
"

1 Like