How can I make an ERC721 NFT such that the token owner can edit an attribute in the metadata? Where can they edit the metadata?

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?

Answers 2

  • 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.


  • To my knowledge, none of the marketplaces have this functionality of the owner being able to edit the metadata. But with that being said, there is nothing stopping you from including this functionality in your token contract.

    But it also depends on what you mean with metadata. Usually, with metadata, we are referring to the name and symbol of the NFT. That's probably not what you are referring to. If someone should be able to edit that, it's probably the contract creator.

    You probably mean other metadata tied to individual NFTs, like other properties or images. The most common way to include these is through a URI in the smart contract, which points to some JSON file somewhere which contain properties, images etc.

    But there is nothing stopping you from having some of these properties in your smart contract, and letting the token owner edit them. Or you can let the token owner edit the token URI.

    Then in order to make this usable for a normal NFT buyer, maybe you would have to build your own user interface to interact with these functions and change the properties. Because this is not standard functionality included in the ERC721 standard, so exchanges and marketplaces would not have this implemented.


Related Questions