ERC721 NFT – How to Allow Token Owners to Edit Metadata Attributes

brownieerc-721nftsolidity

I've read that ERC721 tokens can be created such that the token owner can edit the on-chain metadata. I've been following tutorials for the brownie-nft mix and making NFTs on OpenSea, but I have yet to discover how the NFT owner can actually change the on-chain (or even off-chain) metadata. How can the token owner do this? Can the token owner do this in a marketplace such as OpenSea, or would they need to know solidity?

Best Answer

It depends on how you code the NFT. But basically you'd have a function that updates the on-chain metadata and/or updates the tokenURI to reflect the changes, and you'd make it in such a way that only the token owner can call the function.

Yes, they would need to know solidity.

If you added a function to the NFT contract like:

function changeAttributes(uint256 newStat, uint256 tokenId, string memory newTokenURI) public {
    require(
            _isApprovedOrOwner(_msgSender(), tokenId),
            "ERC721: caller is not owner nor approved"
        );
    tokenIdToStat[tokenId] = newStats;
    _setTokenURI(tokenId, newTokenURI);
}

This could be a function that could change the stats and metadata of an NFT.

  • The require statement has a function called _isApprovedOrOwner which can be defined that only the owner of the NFT can change the stats.
  • The tokenIdToStat[tokenId] = newStat; is a mapping of on-chain attributes that make a tokenId to the "stats" or "attributes" that it has.
  • The _setTokenURI(tokenId, newTokenURI); line then updates the metadata tokenURI. This last part you'd have to update some tokenURI to reflect the new on-chain metadata.

On-Chain vs Off-Chain metadata

In order to have your NFTs interact with each other on-chain, you need to have on-chain metadata. As of right now, NFT marketplaces don't have a standard for reading on-chain metadata, so they all refer to the off-chain metadata found in the tokenURI.

If the NFT has only on-chain metadata, then the function like the one above would be sufficient for them to change metadata. If they have off-chain metadata, they would then additionally need to update the tokenURI with the new metadata. Ideally, you have on-chain metadata too, so that the attributes of the token are immortalized on-chain and not susceptible to centralized manipulation.

Related Topic