On-Chain NFTs: How Do They Work?
What exactly is a fully on-chain NFT and why is it so cool? We dig into the nitty-gritty.
Over the past few weeks, I’ve experimented a lot with on-chain NFTs, culminating with the release of Polymorphic Pride Flags, which let users choose one of 12 pride flag morphs at any time. But what are on-chain NFTs? The basic level explanation is simple enough: rather than relying on images stored anywhere, everything is rendered directly with code stored in the “smart contract” — the on-chain code. The mechanics of this might seem complex on the surface but are decipherable.
When determining what metadata is associated with a given NFT, the
tokenURI() function on its smart contract is called. This can be a simple HTTP URL — though that is non-ideal from a decentralization standpoint. IPFS or Arweave enable the metadata to be stored in a properly decentralized manner, but still not on Ethereum itself. However, we can circumvent this entire lookup process and pass back the data directly from the
NFT metadata is stored as JSON, a simple format for storing data as key/value pairs. For example, a standard key for NFT JSON is “name,” which then, for its value, contains the name of the NFT. We can simply assemble this JSON data ourselves with smart contract code! However, in order for the systems calling
tokenURI() to properly parse it, we then must encode it as Base64 data.
This is similar to how, if embedding images directly in HTML without calling an external image file, you supply the image data in Base64. This is especially common with SVGs, which are typically smaller than most images due to relying on drawing shapes through math rather than recreating a complex image pixel by pixel. In fact, if you want to do everything fully on-chain, you need to encode the image data as Base64 as well, so that after decoding the enclosing JSON, the image itself is still Base64 — a double layer of encoding. Given the constraints of smart contracts, the leanness of SVGs is ideal — not to mention they are more able to be layered on top of one another.
In the case of Polymorphic Pride Flags, there is no on-the-fly layering. However, there is the interactive functionality of being able to switch to a different flag at any time. To accomplish this, we have an integer variable associated with each NFT stored on-chain that can only be updated by the owner. When
tokenURI() is called, it maps that integer value to one of the dozen flag morphs. This enables it to pass back all the necessary data — including the SVG of the flag — directly through the
tokenURI() function. For instance, if the integer associated with an NFT is 0, then it returns a classic rainbow flag.
Updating this integer is handled through a
morphFlag() function on the smart contract that will exist on the Ethereum blockchain forever, no matter what happens to this project. This ensures that, no matter what, these NFTs continue to be renderable — and morphable — entirely through Ethereum itself. Of course, for the ideal user experience, it’s preferable to morph the flag through the project’s website instead. However, it is merely a UI built over fully on-chain functionality.
Optimization of on-chain data is key due to the limit of 24 KB of compiled code per smart contract. Given how expensive it is to purchase this kind of space during deployment, even if you split the code between multiple smart contracts — as is the approach for some complex, popular on-chain projects like Shields — it is a major money saver to be able to pack your on-chain image code as densely as possible.
Utilizing a fully on-chain approach is not the right approach for every project, but it is an exciting, innovative approach to NFT development that reduces external dependencies. However, those dependencies are ones that can be decentralized in ways that will more than suffice for most creators — there is nothing wrong with art stored on IPFS or Arweave. While I’m skeptical we will see a future where fully on-chain NFTs become the norm, they should at least be demystified so that they are seen as an option. They certainly are sleek from a technological standpoint, and I hope to help drive them forward.
More Polymorphic Pride Flags 🏳️🌈
Thanks for Reading!
If you enjoyed this article, please consider liking, sharing, and subscribing to smol farm gazette.
Most of our articles are free forever. For just $5/month, you can unlock the rest and post comments. Proceeds support independent lesbian creators & farmers.