avatarAndy Hartnett

Summary

The website content provides a tutorial on storing NFT metadata and SVGs directly on the blockchain to ensure immutability and permanence, which is crucial for the integrity of NFTs.

Abstract

The article discusses the importance of immutability and permanence in NFT metadata storage, highlighting the potential risks associated with relying on external servers for metadata. It outlines the OpenSea requirements for NFTs, emphasizing the need for a tokenURI() method that returns metadata URLs. The author argues that while IPFS services offer a solution for immutability, they do not fully address the permanence issue. The article then demonstrates how to store metadata on-chain using Solidity, ensuring that the data remains unchangeable and ever-present. It provides an example of how to return a data URL containing JSON metadata and an SVG image string within a smart contract. The article concludes by mentioning how to retrieve the metadata post-deployment using APIs from platforms like Alchemy or Moralis.

Opinions

  • The author believes that storing NFT metadata on-chain is the only way to guarantee its immutability and permanence.
  • There is a concern that relying on external servers for NFT metadata can lead to changes in the metadata, which undermines the integrity of the NFT.
  • IPFS services are recognized for providing immutability but are seen as insufficient for ensuring the long-term availability of NFT metadata without a dedicated gateway.
  • The author suggests that on-chain storage of metadata, including SVG images, is a superior approach for NFTs, despite the potential complexity and cost associated with on-chain data generation and storage.

Solidity Tutorial — How to Store NFT Metadata and SVG’s on the Blockchain

Recently I took a deep dive into the Crypto World. Especially on DeFi and Smart Contracts. So I did what probably every other developer has done that started working on the Block Chain. I minted my own NFT. Particularly an ERC-721 Token. This is an article I really wish I had a few weeks ago.

OpenSea Requirements For NFT’s

If you want your token to be available on Opensea you’ll need to follow the ERC721 standard. You can read full instructions on creating NFT’s to this standard on OpenSea’s Website . But the main requirement for these NFT’s is that your smart contract contains a tokenURI() method which returns the url containing your metadata.

Why Store On Chain

One of the biggest issues I’ve thought of when it comes to NFT’s is their immutability and permanence.

The immutability problem.

The smart contract on the chain always has the same tokenURI but there’s no guarantee that endpoint will always return the same thing. For example, say I have a webserver and I set up the webserver to return meta data based on the token number. Ex:

my-webserver-url.com/token/1

What happens when I decide to change what that endpoint does? There’s no guarantee to the owner of the token that the results from that API call will always be the exact same.

This problem has been mostly solved by IPFS services.(What is IPFS?) Which allow users to “Pin” files on the server. The files are stored with a hash that will never change. The uploader of the file cannot change what’s stored under that hash. This guarantees immutability but there’s another problem.

The Permanence Problem

Even if you use an IPFS, the file will only exist as long as it is “pinned”. And you still may need a dedicated gateway to serve your files with a decent speed, which may lead to your metadata requests timing out in the future.

The ONLY WAY to ensure your data is there forever is to store it directly on the blockchain. So how do we do that.

How to store Metadata on Chain

Take this example:

All tokenURI() does is return a Data Url that contains the JSON. The SVG is stored as a string and returned along with the rest of the metadata. In this example we are retrieving the Svg via it’s own method. Obviously you’ll need to do the generation of the SVG string on chain as well, but that’s outside of the scope of this article

The beauty is, once this has been deployed on the blockchain it will remain immutable and permanent forever.

Viewing the Metadata

Once the smart contract is deployed and an NFT is minted, you can retrieve its metadata using APIs from platforms like Alchemy or Moralis. For example, you can use the getNFTMetadata method with the Alchemy API.

Thanks for reading my article! If you love Crypto, NFTs, Programming, etc. Follow me on twitter!

Nft
Solidity Tutorial
Solidity
Opensea
Nft Development
Recommended from ReadMedium