[Ethereum] Undeclared identifier _setTokenURI in ERC721

erc-721soliditytruffle

I'm using

pragma solidity ^0.8.0;

I heard that _setTokenURI was removed in pragma 0.8.0

I tried these options
https://forum.openzeppelin.com/t/function-settokenuri-in-erc721-is-gone-with-pragma-0-8-0/5978

like using

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

but that didn't work either, has anyone else figured out a work around for this ?

Strange they still have it in their docs https://docs.openzeppelin.com/contracts/4.x/erc721

Code:

pragma solidity ^0.8.0;

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

contract myNFT is ERC721{

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

    constructor() public ERC721("myNFT", "MNFT") {}

    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();
        _mint(recipient, newItemId);
        _setTokenURI(newItemId, metadata);

        return newItemId;
    }
}

Best Answer

Yes, you are right, the function _setTokenURI() was removed in pragma 0.8.0; and you have imported the updated ERC721URIStorage.sol.

But, you need to use that in your smart contract by using inheritance. So, you have to inherit ERC721URIStorage not ERC721.

contract myNFT is ERC721URIStorage{
              
}

hope this help you!!

Related Topic