I’m working on a Solidity smart contract for minting NFTs on the Polygon testnet using Remix IDE with Metamask. The MATIC payment goes through fine, but no NFT shows up in my wallet. When I call the tokenOwner function, I receive this error message:
call to ERC721.tokenOwner errored: execution reverted: ERC721: invalid token ID
Here’s my contract code:
pragma solidity ^0.8.9;
import "@openzeppelin/contracts@4.8.3/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@4.8.3/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts@4.8.3/security/Pausable.sol";
import "@openzeppelin/contracts@4.8.3/access/Ownable.sol";
import "@openzeppelin/contracts@4.8.3/utils/Counters.sol";
contract DigitalBadge is ERC721, ERC721Enumerable, Pausable, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
uint256 public TOKEN_COST = 0.005 ether;
uint256 public TOTAL_SUPPLY = 10000;
constructor() ERC721("DigitalBadge", "BADGE") {
_tokenIds.increment();
}
function withdrawFunds() public onlyOwner() {
require(address(this).balance > 0, "No balance available");
payable(owner()).transfer(address(this).balance);
}
function pauseContract() public onlyOwner {
_pause();
}
function unpauseContract() public onlyOwner {
_unpause();
}
function mintBadge(address recipient) public payable {
require(msg.value >= TOKEN_COST, "Insufficient payment");
require(totalSupply() <= TOTAL_SUPPLY, "Supply exhausted");
uint256 newTokenId = _tokenIds.current();
_tokenIds.increment();
_safeMint(recipient, newTokenId);
}
function _beforeTokenTransfer(address from, address to, uint256 tokenId, uint256 batchSize)
internal
whenNotPaused
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId, batchSize);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}
What could be causing this token ID error? Any help would be great!