When i burn a NFT and then look up if the address still contains the token using tokenOfOwnerByIndex the returned token is still listed in the _ownedTokens mapping.
here is my code on the client side to look up tokenOfOwnerByIndex:
const getNfts = async () => {
let balanceOf = await contract.balanceOf(account);
console.log('balanceOf: ', balanceOf.toString());
for (let i = 0; i < balanceOf; i++) {
let id = await contract.tokenOfOwnerByIndex(account, i);
console.log('id: ', id.toString());
let tokenURI = await contract.tokenURI(id);
console.log('tokenURI: ', tokenURI);
}
};
I am using the standard openzepplin contract wizard contracts:
// 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/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC721, ERC721Enumerable, ERC721URIStorage, ERC721Burnable, Ownable {
constructor() ERC721("MyToken", "MTK") {}
function safeMint(address to, uint256 tokenId, string memory uri)
public
onlyOwner
{
_safeMint(to, tokenId);
_setTokenURI(tokenId, uri);
}
// The following functions are overrides required by Solidity.
function _beforeTokenTransfer(address from, address to, uint256 tokenId)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId);
}
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);
}
}
so when tokenURI
function is called then it reverts with: "ERC721URIStorage: URI query for nonexistent token"
Here is my console output:
First i get the balanceOf then i burn tokenId 0 and then when i check the balanceOf again it says i have id 0, 1 even thought i have burnt 0
the transfer functions works fine. the token ids are correct once i have sent them to another address.
Please can anyone assist with this issue?