7

我正在编写一份关于solidity 0.8.3 的合同,但我收到了这个奇怪的错误,_setTokenURI()尽管该方法是在 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;
  } }

在此处输入图像描述

4

1 回答 1

14

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

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

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

contract NFTB is ERC721URIStorage { // changed parent
于 2021-03-24T23:36:11.627 回答