在我找不到的智能合约中循环
loops in my smart contract that I cant find
我正在测试我的合约,但出现以下错误:
Gas 费用:函数vjkNFT.safeMint 的Gas 需求是无限的:如果函数的Gas 需求高于区块gas 限制,则无法执行。请避免在您的函数或操作中修改大面积存储的循环(这包括清除或复制存储中的数组)位置:32:4:
这里是包含错误的函数:
// 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);
}
}
有人知道这是怎么回事吗?谢谢
好的,我明白了。
这是以下内容的副本: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.
我正在测试我的合约,但出现以下错误:
Gas 费用:函数vjkNFT.safeMint 的Gas 需求是无限的:如果函数的Gas 需求高于区块gas 限制,则无法执行。请避免在您的函数或操作中修改大面积存储的循环(这包括清除或复制存储中的数组)位置:32:4:
这里是包含错误的函数:
// 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);
}
}
有人知道这是怎么回事吗?谢谢
好的,我明白了。
这是以下内容的副本: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.