Hey everyone, I’m stuck with a problem while trying to mint NFTs on the Polygon testnet. I’ve set up a smart contract that’s supposed to give users an NFT when they send some MATIC tokens. I’m using Remix IDE with Metamask as the provider.
Here’s what’s happening:
- The MATIC token transaction goes through fine
- But no NFT shows up in my Metamask account
- When I try to check the owner using the ‘ownerOf’ function, I get this error:
call to ERC721.ownerOf errored: Returned error: execution reverted: ERC721: invalid token ID
I’ve tried two different contracts, but I’m getting the same error with both. Here’s a simplified version of one of them:
pragma solidity ^0.8.9;
import '@openzeppelin/contracts@4.8.3/token/ERC721/ERC721.sol';
contract MyNFT is ERC721 {
uint256 public MINT_COST = 0.005 ether;
uint256 public tokenCount;
constructor() ERC721('MyNFT', 'MNFT') {
tokenCount = 0;
}
function mintToken(string memory tokenURI) public payable returns (uint256) {
require(msg.value >= MINT_COST, 'Insufficient payment');
uint256 newTokenId = tokenCount;
_safeMint(msg.sender, newTokenId);
tokenCount++;
return newTokenId;
}
}
Any ideas on what might be causing this and how to fix it? I’m really scratching my head here. Thanks in advance for any help!
I’ve encountered a similar issue before, and it sounds like there might be a problem with your token ID increment. In your current implementation, you’re incrementing the tokenCount after minting, which could lead to a mismatch.
Try modifying your mintToken function like this:
function mintToken(string memory tokenURI) public payable returns (uint256) {
require(msg.value >= MINT_COST, 'Insufficient payment');
uint256 newTokenId = tokenCount++;
_safeMint(msg.sender, newTokenId);
return newTokenId;
}
This way, you’re incrementing tokenCount before using it as the new token ID. Also, double-check that tokenCount is properly initialized in your constructor. If the issue persists, you might want to add some logging or events to track the token IDs being minted and queried.
yo elias, maybe try using the tokenURI you’re passing in? ur not doing anything with it rn. also, double check ur contract address on polygonscan. sometimes the issue is simpler than we think lol. gl with ur project bro!
hey there! i’ve been playing around with nfts too and ran into some weird stuff. have you tried checking if the transaction actually went through on the blockchain explorer? sometimes metamask can be a bit funky and not show things right away.
also, i’m curious - are you able to see the total supply of your nft contract? that might give you a clue if the tokens are actually being minted or not.
oh, and random thought - could it be something to do with gas fees on polygon? i remember having issues when i didn’t set them high enough.
keep us posted on what you find out! this stuff can be frustrating but its so cool when you finally get it working 