ERC-721 Token with on chain metadata
Ethereum non fungible tokens (NFTs) have been standardized by ERC-721. The standard emphasises the possibility to track token metadata, i.e. any kind of property of the asset, through a tokenURI, or Uniform Resource Identifier. The URI for NFTs can be a traditional Internet address, but this choice is poor in terms of decentralization because, if the underlying web infrastructure is discontinued, hosted metadata can be lost. Another possible choice is to use decentralized file system, like IPFS or Swarm, but this requires to master two different technology and still data on a decentralized file system can be lost if the data is not properly “pinned”.
Why not to store metadata on chain, together with the other relevant information? Of course, costs will be higher, depending on the amount of information that we may want to store, but that can be a very meaningful option is some situation. This can allow also to build smart contract functions that allow to safely evolve asset status, depending on participant interactions.
To exemplify this configuration, I developed this small snippet of code, adapting the GameItem example in the Open Zeppelin ERC-721 documentation, to allow on chain metadata (the color property in this example, but this is easily extensible). The contract may be straightforward for some of the readers, but I thought it would make sense to document as the question is often asked and I would greatly appreciate any comment.
The contract (NFT) takes about 831,000 gas to create an item with a text corresponding to the first Lorem Ipsum paragraph — see below(corresponding to 0.05 ETH at an average Gas Price of 62 Gwei, or $12 at today ETH conversion rate.) This is about about 868 bytes in size or less 1.4 ¢/byte, so YES data is expensive on the mainnet, but this could still make sense for highly valuable information (and you know how much truly secure application platforms may cost!). Also, this can make lot of sense on permissioned Ethereum networks like JPMorgan Chase Quorum or Hyperledger Besu, where transaction cost is not a problem. Of course running and securing these networks has a cost as well.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque dapibus eu ante in bibendum. Donec ut enim convallis sem sodales viverra at sed massa. Nunc sit amet bibendum libero, faucibus egestas sem. Proin tortor nunc, venenatis iaculis consectetur non, bibendum vel urna. Cras vel erat vel elit bibendum lobortis. Mauris a sem fermentum, posuere dui vel, placerat nulla. Fusce molestie varius nunc, eu venenatis tellus semper a. Suspendisse suscipit, massa sed pellentesque lacinia, diam dui blandit turpis, facilisis consequat neque urna ac dui. Pellentesque efficitur justo sed neque maximus imperdiet. Proin eget odio ex. Aenean suscipit libero sed feugiat volutpat. Pellentesque consectetur sed sem vel elementum. Ut eget suscipit sem. Phasellus dolor nisl, finibus sit amet velit at, fermentum cursus eros. Etiam vel facilisis nulla, in lobortis risus.