The Value of On-Chain Blockchain Storage in Relation to NFTs

Alex Gieson
In Writing
5 min readApr 11, 2022

--

TokenURI function from In Writing NFT project

NFTs are non-fungible tokens that exist on blockchains like ethereum or polygon. What makes an NFT valuable? It’s all in the metadata, specifically the tokenURI of the NFT. The tokenURI is a json file that lists different attributes of the NFT, which are unique to that specific NFT. Without the tokenURI, the NFT is worthless as it has no metadata. This article will go into the specifics of how to store tokenURI, and the pros and cons of on-chain and off-chain storage.

If you’re new to blockchain, check out this article I wrote on this same topic, but explains things in simpler terms before coming back to this article!

In this project, we’ll be analyzing how 3 projects store their tokenURI:

Background

Lets first examine a popular art-based NFT, to get a basic grasp of their underlying metadata. Take a look at Bored Ape #2180. If we scroll to the “Details” tab on OpenSea and click on “2180” in the “Token ID” field, OpenSea automatically opens the tokenURI of the NFT (or click here to view). The tokenURI is what makes an NFT valuable, and is where all the data is stored that gives the NFT its uniqueness.

TokenURI for Bored Ape #2180

The tokenURI is in json format, which is how the NFT’s metadata is displayed. Here we can see the image as well as the various attributes that this particular NFT has. Interestingly, both the tokenURI and image of this NFT are stored off chain on IPFS. This is done to reduce the amount of data that is stored on the blockchain, as on-chain storage is very expensive. For example, to store the image (122KB) for this NFT on the ethereum blockchain, it would cost thousands of dollars at the time of writing.

What are the downsides of hosting metadata off-chain?

Off-chain metadata storage is cheaper, but it comes at the risk of breaking the NFT. Metadata hosted on centralized servers/websites runs the greatest risk of making an NFT worthless, as centralized websites can go down for maintenance, or even stop running for any number of reasons. If this happens, the NFT’s metadata is not retrievable, meaning it has no unique data.

A better solution than centralized severs or websites would be storing data on IPFS. Inter Planetary File System is a decentralized file storage system, which provides redundancy, immutability, and permanent storage — so long as there are nodes to store your data. IPFS, which is exemplified above in the Bored Ape NFT example, is considered best practice if the metadata contains images or files that are too large to store on chain.

Hosting metadata on chain

In the case that metadata is small enough, hosting it on chain is the best solution, as it is stored directly on the ethereum blockchain. This is considered best practice because the ethereum blockchain will presumably outlast any website or server or hosting service that exists today.

How can hosting metadata on chain be done?

Let’s take a look at OnChainMonkey as an example. Navigating to the tokenURI is a little more difficult for this project, but it works the same way. It’s worth noting that this method of getting the tokenURI should be universal for all NFT projects with a verified contract on etherscan. Navigate to the OnChainMonkey smart contract on etherscan here, click on “Contract” near the bottom of the screen, then “Read Contract.” This is a list of all the functions you can call from the smart contract that do not require you to pay ethereum gas fees. Scrolling to the bottom of this page, you can see the function “tokenURI.” After clicking on the function, notice there is a field called “tokenId,” which is where you enter the token ID of the specific NFT’s metadata to request.

After pressing “Query,” the function returns a string. Copy everything after “string:” and paste it into a browser. Wait! Etherscan has a weird quirk where it prints commas as newline characters, so make sure you add in a comma after “base64” before pasting into your browser! You should now see the image below in your browser.

TokenURI of OnChainMonkey #0

What’s happening here? “data:application/json;base64,” says that the following data is encoded in base64 format. Your browser is smart enough to know how to decode base64, so it does that automatically for you. Next, looking at the image field, we can see that it is also encoded in base64 (but it’s an svg image this time, not a json)! This shows that the tokenURI as well as the image of this NFT are stored on chain, meaning they exist as long as the ethereum blockchain does!

Note: how can OnChainMonkey store images on chain, while Bored Ape can’t? This is because OnChainMonkey’s image is an SVG, meaning it is a vector image. Vector images are smaller, and therefore the cost of storing them on chain is cheaper and more viable.

The hybrid approach

Lastly, we’ll take a look at the project In Writing. In Writing is a text based NFT project, which features on-chain storage of strings (text), including unicode characters, as NFTs. Let’s take a look at In Writing #0’s tokenURI below, which is stored on chain using the same method as OnChainMonkey in the previous example.

TokenURI of In Writing #0

Although the tokenURI is stored on chain, we can see that the field “image_data” is not stored on chain because it is a link to a hosted website. Why has this been done? Because the value of an In Writing NFT is in the “string” field (which you’ll notice is base64 encoded), the other fields are simply extra. There is no need to over-complicate a smart contract by creating the image on chain, unless, of course, your project relies on images (like OnChainMonkey). In Writing stores what is needed on chain (the string), while outsourcing heavier workloads (image generation) off-chain to conserve contract size.

Conclusion

There are many ways to store metadata for NFTs, but on-chain storage offers the most stable long-term storage solution. Sometimes, though, it is necessary to resort to off-chain storage when storing large files, like jpg and png file formats. When deciding for yourself, consider what data is essential to your NFT, as In Writing did, to minimize NFT costs while still having a product that performs as expected. Don’t let your NFT project die with your website, allow it to live as long as the blockchain does by using on-chain storage!

--

--