MAKE NFTS WITH ON CHAIN METADATA
This week, students are taught how to create a dynamic NFT. Metadata for an NFT can be stored either off or on chain. Off chain storage would mean that the metadata could not be updated directly from the smart contract. However, storing metadata on the Ethereum blockchain would be gas intensive. Students will learn about gas efficient layer 2 blockchains, how to store metadata on chain, mapping, how to process and store SVG images, and how to modify metadata.
LAYER 2
This project will be deployed on the Polygon test network blockchain. Polygon is a layer2 blockchain which means that it is built on top of another blockchain. In Polygon’s case, it is built on top of the Ethereum blockchain. The reasons to use Polygon are because layer2 blockchains are generally faster in terms of transactions and far more gas efficient. Keeping NFT metadata on the Ethereum block chain would be very gas intensive. For this reason, a layer 2 blockchain like Polygon is far better suited for the task of housing NFT metadata.
The first step for this project is to add Polygon Mumbai to Metamask. Clicking the ‘Add Mumbai Network’ link will create a ‘Switch Network’ confirmation with Metamask:
STORING METADATA ON CHAIN
Hardhat was introduced to students in week 2, and it is an integral part of this project. The ‘hardhatconfig’ file that is created is replaced with the following code:
This configuration file contains all the information necessary to connect the student’s Metamask wallet and the Polygon test network. As students learned last week, sensitive information will be placed in a .env file which will never leave the local environment as the .env file will be placed on the gitignore list.
MAPPING
From here, a solidity file is created with the initial boilerplate information that will eventually become the smart contract. Upon initializing the contract, students are introduced to the topic of mapping. The Medium article by Jean Cvllr defines mapping. In the simplest terms, mappings are key-value pairs that make it easy to access data.
SCALABLE VECTOR GRAPHICS
Another focal point for this lesson is the SVG (scalable vector graphics) image format. In week 1, students were taught to store metadata off chain for NFT marketplaces to pull image data. Utilizing the SVG image format allows the use of images on chain within the smart contract. A function is created within the smart contract which holds the SVG code:
The SVG code will eventually become the NFT image. Through a group of functions, various aspects of the NFT will be created when the NFT is minted.
MODIFY METADATA
With all the code set up, the final step is to deploy the contract.
Once the contract is deployed, the contract address is viewable through polygon testnet block explorer.
In order to interact with the contract, it must be verified.
After contract verification, Metamask is able to interact with the contract through the Polygon block explorer.
After minting the NFT, it can be viewed on the OpenSea Testnet.
Metamask can also interact with the contract through the train function which will update the NFT metadata.
CONCLUSION
As students progress through the Road to Web3, each week becomes more challenging. Week 3 teaches how to to store NFT metadata on chain. The lesson introduces the idea of layer2 blockchains and influences students to be conscious of gas fees associated with smart contracts. Building on the previous week, students use Hardhat to create the smart contract that will hold metadata than can be changed. Students are introduced to the idea of mapping with further research necessary to complete the challenge found at the end of the lesson. A key take away from this lesson is the usage of SVG image files as they can exist within a smart contract. Perhaps the most important concept taught in this lesson is the idea that a front end is only necessary for mass adoption. Developers can interact with smart contract right from the block explorer.