I’m building a website where users can mint their own ERC721 tokens. Each user should be able to set a unique name and symbol for their NFT. But I’m running into an issue where only the first user’s name and symbol stick, and all subsequent mints use those same values.\n\nHere’s a simplified version of my contract:\n\nsolidity\npragma solidity ^0.7.0;\nimport "@openzeppelin/contracts/token/ERC721/ERC721.sol";\n\ncontract CustomNFT is ERC721 {\n uint256 private _tokenIds;\n\n constructor() ERC721("Initial", "INIT") {\n _tokenIds = 0;\n }\n\n function mintToken(string memory tokenURI, string memory newName, string memory newSymbol) public returns (uint256) {\n _tokenIds++;\n uint256 newItemId = _tokenIds;\n _mint(msg.sender, newItemId);\n _setTokenURI(newItemId, tokenURI);\n \n // Attempt to update name and symbol\n _name = newName;\n _symbol = newSymbol;\n \n return newItemId;\n }\n}\n\n\nWhen I check the transaction on PolygonScan, it always shows the initial name and symbol. How can I make sure each NFT gets its own unique name and symbol?
hey strummingmelody! i get what ur trying to do, it’s a cool idea. but like others said, ERC721 doesn’t really work that way. have u thought about maybe using attributes instead? ![]()
u could do something like this:
mapping(uint256 => string) private _customNames;
mapping(uint256 => string) private _customSymbols;
function mintToken(string memory tokenURI, string memory customName, string memory customSymbol) public returns (uint256) {
_tokenIds++;
uint256 newItemId = _tokenIds;
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
_customNames[newItemId] = customName;
_customSymbols[newItemId] = customSymbol;
return newItemId;
}
this way, each token gets its own name n symbol, but it doesn’t mess with the ERC721 standard. u could even add functions to get these custom values later. what do u think? would that work for ur project?
hey there, i’ve run into this before. the issue is that _name and _symbol are contract-level variables, not token-specific. you’d need to store unique names/symbols per token ID, maybe in a mapping. also, consider using OpenZeppelin’s ERC721URIStorage for better token URI management. good luck with ur project!
You’re approaching this from the wrong angle. ERC721 contracts have a single name and symbol for the entire collection, not for individual tokens. What you’re trying to do isn’t standard practice.
Instead, consider storing custom metadata for each token. You can use the tokenURI function to point to off-chain metadata (JSON files) that contain custom ‘name’ and ‘symbol’ fields for each token. This way, you maintain ERC721 compatibility while allowing for token-specific customization.
Alternatively, if on-chain storage is crucial, create a separate mapping in your contract to store custom data per token ID. But remember, this isn’t part of the ERC721 standard and may not be recognized by all platforms or wallets.