即使存在于ERC721 Enumerable中,如何修复未宣布的标识符

发布于 2025-01-22 08:26:17 字数 2680 浏览 0 评论 0原文

我正在尝试准备一份坚固的智能合约,除了混音编译器上的单个未宣布错误外,一切看起来都很好。错误代码如下:

from solidity:
DeclarationError: Undeclared identifier.
  --> contracts/WTest.sol:66:22:
   |
66 |     uint256 supply = totalSupply();
   |                      ^^^^^^^^^^^

作为参考,我的代码如下:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.2;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract WTest is ERC721, ERC721Burnable, Ownable {
    using Strings for uint256;
    using SafeMath for uint256;


    uint256 public mintPrice;
    address public blackHoleAddress;
    
    ERC721 public crateContract;

    string public baseURI;
    string public baseExtension = ".json";
    mapping(uint256 => bool) private _crateProcessList;

    bool public paused = false;
    bool public revealed = false;

    uint256 public maxSupply = 5000;
    uint256 public maxPrivateSupply = 580;
    uint256 public maxMintAmount = 20;
    string public notRevealedUri;

    event OperationResult(bool result, uint256 itemId);
   

    constructor() ERC721("WTest", "WTST") {}

    function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

    function setBASEURI(string memory newuri) public onlyOwner {
        baseURI = newuri;
    }

     function setMintPrice(uint256 _mintPrice) public onlyOwner  returns(bool success) {
        mintPrice = _mintPrice;
        return true;
    }

    function getMintPrice() public view returns (uint256)
    {
    return mintPrice;
    }

    function setBlackHoleAddress(address _blackHoleAddress) public onlyOwner  returns(bool success) {
        blackHoleAddress = _blackHoleAddress;
        return true;
    }


    function setcrateContractAddress(ERC721 _crateContractAddress) public onlyOwner returns (bool success) {
        crateContract = _crateContractAddress;
        return true;
    }
function mint(uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    require(supply + _mintAmount <= maxSupply);

    if (msg.sender != owner()) {
      require(msg.value >= mintPrice * _mintAmount);
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(msg.sender, supply + i);
    }
  }

如您所见,我已经引用了ERC721Numerable.sol

在理解我出错的地方的任何帮助将不胜感激。

I am trying to prepare a solidity smart contract and everything appears fine except a single undeclared error on remix compiler. The error code is as follows:

from solidity:
DeclarationError: Undeclared identifier.
  --> contracts/WTest.sol:66:22:
   |
66 |     uint256 supply = totalSupply();
   |                      ^^^^^^^^^^^

For reference, my code is as follows:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.2;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract WTest is ERC721, ERC721Burnable, Ownable {
    using Strings for uint256;
    using SafeMath for uint256;


    uint256 public mintPrice;
    address public blackHoleAddress;
    
    ERC721 public crateContract;

    string public baseURI;
    string public baseExtension = ".json";
    mapping(uint256 => bool) private _crateProcessList;

    bool public paused = false;
    bool public revealed = false;

    uint256 public maxSupply = 5000;
    uint256 public maxPrivateSupply = 580;
    uint256 public maxMintAmount = 20;
    string public notRevealedUri;

    event OperationResult(bool result, uint256 itemId);
   

    constructor() ERC721("WTest", "WTST") {}

    function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

    function setBASEURI(string memory newuri) public onlyOwner {
        baseURI = newuri;
    }

     function setMintPrice(uint256 _mintPrice) public onlyOwner  returns(bool success) {
        mintPrice = _mintPrice;
        return true;
    }

    function getMintPrice() public view returns (uint256)
    {
    return mintPrice;
    }

    function setBlackHoleAddress(address _blackHoleAddress) public onlyOwner  returns(bool success) {
        blackHoleAddress = _blackHoleAddress;
        return true;
    }


    function setcrateContractAddress(ERC721 _crateContractAddress) public onlyOwner returns (bool success) {
        crateContract = _crateContractAddress;
        return true;
    }
function mint(uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    require(supply + _mintAmount <= maxSupply);

    if (msg.sender != owner()) {
      require(msg.value >= mintPrice * _mintAmount);
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(msg.sender, supply + i);
    }
  }

As you can see I have referenced ERC721Enumerable.sol

Any help in understanding where I am going wrong would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只等公子 2025-01-29 08:26:17

您必须扩展erc721enumerable,只有这样,您才能使用ptalsupply()函数。
您的智能合约应该与此相提并论:

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

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract WTest is ERC721, ERC721Enumerable, ERC721Burnable, Ownable {
    using Strings for uint256;
    using SafeMath for uint256;


    uint256 public mintPrice;
    address public blackHoleAddress;
    
    ERC721 public crateContract;

    string public baseURI;
    string public baseExtension = ".json";
    mapping(uint256 => bool) private _crateProcessList;

    bool public paused = false;
    bool public revealed = false;

    uint256 public maxSupply = 5000;
    uint256 public maxPrivateSupply = 580;
    uint256 public maxMintAmount = 20;
    string public notRevealedUri;

    event OperationResult(bool result, uint256 itemId);
   

    constructor() ERC721("WTest", "WTST") {}

    function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

    function setBASEURI(string memory newuri) public onlyOwner {
        baseURI = newuri;
    }

     function setMintPrice(uint256 _mintPrice) public onlyOwner  returns(bool success) {
        mintPrice = _mintPrice;
        return true;
    }

    function getMintPrice() public view returns (uint256)
    {
    return mintPrice;
    }

    function setBlackHoleAddress(address _blackHoleAddress) public onlyOwner  returns(bool success) {
        blackHoleAddress = _blackHoleAddress;
        return true;
    }


    function setcrateContractAddress(ERC721 _crateContractAddress) public onlyOwner returns (bool success) {
        crateContract = _crateContractAddress;
        return true;
    }

    function mint(uint256 _mintAmount) public payable {
        uint256 supply = totalSupply();
        require(!paused);
        require(_mintAmount > 0);
        require(_mintAmount <= maxMintAmount);
        require(supply + _mintAmount <= maxSupply);

        if (msg.sender != owner()) {
        require(msg.value >= mintPrice * _mintAmount);
        }

        for (uint256 i = 1; i <= _mintAmount; i++) {
        _safeMint(msg.sender, supply + i);
        }
    }

    // NOTE: Override ERC721Enumerable functions
    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
        super._beforeTokenTransfer(from, to, tokenId);
    }

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

}

You must extend ERC721Enumerable, and only then you can use totalSupply() function.
Your smart contract should be similtar to this:

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

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract WTest is ERC721, ERC721Enumerable, ERC721Burnable, Ownable {
    using Strings for uint256;
    using SafeMath for uint256;


    uint256 public mintPrice;
    address public blackHoleAddress;
    
    ERC721 public crateContract;

    string public baseURI;
    string public baseExtension = ".json";
    mapping(uint256 => bool) private _crateProcessList;

    bool public paused = false;
    bool public revealed = false;

    uint256 public maxSupply = 5000;
    uint256 public maxPrivateSupply = 580;
    uint256 public maxMintAmount = 20;
    string public notRevealedUri;

    event OperationResult(bool result, uint256 itemId);
   

    constructor() ERC721("WTest", "WTST") {}

    function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

    function setBASEURI(string memory newuri) public onlyOwner {
        baseURI = newuri;
    }

     function setMintPrice(uint256 _mintPrice) public onlyOwner  returns(bool success) {
        mintPrice = _mintPrice;
        return true;
    }

    function getMintPrice() public view returns (uint256)
    {
    return mintPrice;
    }

    function setBlackHoleAddress(address _blackHoleAddress) public onlyOwner  returns(bool success) {
        blackHoleAddress = _blackHoleAddress;
        return true;
    }


    function setcrateContractAddress(ERC721 _crateContractAddress) public onlyOwner returns (bool success) {
        crateContract = _crateContractAddress;
        return true;
    }

    function mint(uint256 _mintAmount) public payable {
        uint256 supply = totalSupply();
        require(!paused);
        require(_mintAmount > 0);
        require(_mintAmount <= maxMintAmount);
        require(supply + _mintAmount <= maxSupply);

        if (msg.sender != owner()) {
        require(msg.value >= mintPrice * _mintAmount);
        }

        for (uint256 i = 1; i <= _mintAmount; i++) {
        _safeMint(msg.sender, supply + i);
        }
    }

    // NOTE: Override ERC721Enumerable functions
    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
        super._beforeTokenTransfer(from, to, tokenId);
    }

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文