【问题标题】:DeclarationError: Undeclared identifier - although it's present in ERC721.solDeclarationError: Undeclared identifier - 尽管它存在于 ERC721.sol 中
【发布时间】:2021-03-24 20:59:36
【问题描述】:

我正在编写一份关于solidity 0.8.3 的合同,我收到_setTokenURI() 的这个奇怪错误,尽管方法是defined in OpenZeppelin 4.X

pragma solidity ^0.8.3;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract NFTB is ERC721 {

  using Counters for Counters.Counter;
  Counters.Counter private _tokenIds;
  mapping(string => uint8) hashes;

  constructor() public ERC721("NFTB", "NFTB") {}

  function awardItem(address recipient, string memory hash, string memory metadata) public returns (uint256) {
    require(hashes[hash] != 1);
    hashes[hash] = 1;
    _tokenIds.increment();
    uint256 newItemId = _tokenIds.current();
    _setTokenURI(newItemId, metadata);
    _mint(recipient, newItemId);
    return newItemId;
  } }

【问题讨论】:

    标签: ethereum solidity truffle openzeppelin


    【解决方案1】:

    函数_setTokenURI()@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol (source on GitHub) 中定义,但您的代码不会导入此协定(包括嵌套导入)。这就是函数未声明的原因。

    由于ERC721URIStorage 扩展了ERC721,您可以直接从ERC721URIStorage 扩展您的NFTB

    import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; // changed import
    import "@openzeppelin/contracts/utils/Counters.sol";
    
    contract NFTB is ERC721URIStorage { // changed parent
    

    【讨论】:

    • 等我下个月赚几百万美元,我一定会给你捐款的。
    • 我也试过了,但它在我的机器上不起作用,仍然未声明。我正在使用固化 0.8.0
    • @jalapina 确保将 ERC721 的 .sol 中的所有引用更新为 ERC721URIStorage
    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多