An NFT (Non-Fungible-Token) is a digital asset issued and controlled by a smart contract running in the blockchain. The uniqueness of an NFT is defined by the combination of the smart contract address and an identifier number called TokenId.Minting (or issuing) an NFT means that the smart contract creates a new unique tokenId and assigns it to an address (the owner of the NFT). The owner could be an externally owned account (EOA) or another smart contract.In addition to the minting functionality, the smart contract usually presents mechanisms to transfer the ownership of the NFT and also to destroy (burn) it.
The ERC721 standard defines the interfaces and behavior a smart contract managing NFTs must implements, which are the following:
- Let users get the count of NFTs an address owns in the smart contract.
- Let users know the owner of an NFT
- Let the owner transfer the ownership of an NFT to another address
- Let the owner approve another address to transfer an NFT on its behalf.
- Let the owner approve another address (operator) to be able to transfer any of its NFTs on its behalf.
- Let users get the approved address (allowed address to transfer on behalf of the owner) of a specific NFT
- Let users query if an address is an authorized operator for another address
Smart Contracts following the ERC721 standard also must emit events when:
- The ownership of any NFT changes by any mechanism.
- The approved address for an NFT is changed
- When an operator is enabled or disabled for an owner.
What is a Dynamic NFT?
Because an NFT whose only data is a tokenId, could not be very useful for certain applications, extra information attached to the NFT sometimes is needed. This attached information is called “Metadata” and could be stored in the blockchain or outside the blockchain
If we decide to store the metadata outside the blockchain, the only piece of information we would need to keep inside the blockchain is a link to where the stored metadata is. But storing metadata outside the blockchain (like in an AWS server) could result in losing the main blockchain benefits: decentralization, traceability, immutability, and no censorship.
On the other hand, storing data in the blockchain is very expensive. for example, at the moment of writing this article, storing 1kb in the Ethereum blockchain costs around USD 50
An intermediate solution is to store the metadata in a distributed file system, such as IPFS (Inter-planetary File system), which consists of a peer-to-peer network for storing and sharing data. In this case, we also need to store a link to where the metadata is but we will have the benefits of a distributed network. In a future blog, we will go deep on how to store NFT metadata in IPFS.
Another benefit of storing data in a distributed file system is that the data integrity and immutability are guaranteed through the use of cryptographic hashes.
The ERC721 also defines a standard to structure the metadata of an NFT in a form of a JSON file. In this way, an NFT marketplace like, for example, Opensea, can read the metadata and show the information associated with the NFT.
Possible use cases
NFTs could be used to represent characters in video games. As the game evolves, the character can change some of its characteristics or possessions. For example, the character could have a score, win weapons, change clothes, have custom traits, accumulate money, etc.
All the mutable characteristics of the characters could be attached to the NFT as metadata.
Cars, or houses, could be represented in the blockchain as an NFT. Some of its characteristics will be immutable (I.E. Chassis serial number for cars, or location for houses), but it will be possible to add permanent, transparent, and immutable data such as maintenance information, transfers, debt history, etc. Then, the attached information could be available to a possible buyer, making the market to be more transparent.
Some play-to-ear games offer to buy virtual assets in the form of NFT. This NFT will have mutable characteristics that make the user win more o fewer tokens as he/she plays. An example is Stepn where users own virtual sneakers that then use to earn money for walking, jogging, or running for a set time period, outdoors. Virtual sneakers can be customized, or sometimes fixed, in order to optimize the winnings.
A practical example
Let's say we have a smart contract that let users create an avatar. The avatar will have immutable characteristics (name, eye color, gender, age) and customizable characteristics (hairstyle, footwear type, pants type, t-shirt color)
In this example, the avatar represents the NFT and the characteristics of the avatar will be the associated metadata. Because of how the smart contract is designed, some characteristics are immutable and others are mutable. Mutable characteristics make our NFT a DynamicNFT.
As the metadata is pretty light, we will store it in the smart contract, since storing a link (string) will be much more expensive.
Every time a new avatar is created, the user will select the name, aye color, gender, and age. Hairstyle, footwear, pants, and t-shirt will be seted randomly.
The contract will let the owner of the avatar change the mutable characteristics.
To implement our avatar generator we will use the following libraries:
- Openzeppeling ERC721 library: Implementation of methods and interfaces for NFT management
- Openzeppeling Counters library: For TokenId generation
- Openzeppeling Ownable library: For access control utilities.
The concept of Dynamic NFT may sound a kind of contradictory but understanding that the only immutable data associated with an NFT is the tokenId, we can change the metadata (whenever is stored inside or outside the blockchain) according to the system needs.