'loops in my smart contract that I cant find
I'm testing my contract and I got the following error:
Gas costs: Gas requirement of function vjkNFT.safeMint 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: 32:4:
Here the function that contains the error:
// 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);
}
}
Someone know what this is about? Thank you
Solution 1:[1]
Okay, now I understand.
This is a duplicate of: https://ethereum.stackexchange.com/questions/41851/solidity-function-gas-requirement-is-infinite
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Uriel Chami |
