Quickstart & Install Requirements
Before we get started, let's quickly go over the requirements for this tutorial and how to set up your development environment. We will need Hardhat, Node.js, and a code editor installed. We will also make use of the OpenZeppelin library for smart contract development. Follow the instructions provided in this section to install all the necessary tools and dependencies.
View Static SVG Deployed NFT on OpenSea
To kick off our tutorial, we will first learn how to deploy a static SVG-based NFT to the Polygon chain and view it on the OpenSea marketplace. We will walk through the steps of setting up the necessary smart contract, creating an SVG image, and deploying it to the blockchain. Finally, we will verify that our NFT is visible and tradable on OpenSea.
View RandomSVG on OpenSea
In this section, we will take our NFT art creation to the next level by introducing randomness. We will explore how to generate random SVGs using Chainlink VRF and create truly unique and scarce NFTs. We will modify our smart contract to incorporate the randomness functionality, deploy it to the Polygon chain, and verify that the randomly generated SVG NFTs can be viewed and traded on OpenSea.
Static SVG NFT Setup
Before we delve into the details of creating NFTs with random SVGs, let's first set up the foundation for our static SVG NFTs. We will define the required smart contract, SVGNFT.sol, and make use of the OpenZeppelin library for contract standardization. We will go through the contract code and explain the essential functions and variables needed for our static SVG NFTs.
Creating an NFT and SVG Intro
To create NFTs, we need to understand the basics of SVG image generation and its integration with our smart contracts. In this section, we will introduce the concept of SVGs and how they can be used to represent images in a scalable and programmable manner. We will also explain how to integrate SVG generation into our NFT smart contracts.
svgToImageURI (Base64 Encoding)
In this section, we will define a function to format the metadata URI for our NFTs. We will use this function to combine the SVG image URI and additional metadata details into a properly formatted JSON object. This formatted token URI will be stored on-chain and will provide all the necessary information about our NFTs.
Deploying to Rinkeby Testnet
With our deployment script ready, we will now execute the deployment process to the Rinkeby Testnet. We will use Hardhat to deploy our SVGNFT contract and ensure that everything is functioning correctly. We will also verify the contract on the Rinkeby Etherscan to make it publicly accessible.
Viewing on OpenSea
Once our SVGNFT contract is deployed and verified on the Rinkeby Testnet, we can proceed to view our NFTs on the OpenSea marketplace. We will connect our deployed contract to OpenSea and ensure that our static SVG NFTs are visible, with their metadata correctly displayed.
Now that we have successfully created and deployed static SVG NFTs, let's move on to the exciting part of generating random SVGs. We will introduce a new smart contract, RandomSVG.sol, which will handle the random SVG generation process. We will explain the structure and functions of this contract and how it integrates with Chainlink VRF.
Gameplan for Random SVGs
Before we dive into the implementation details, let's outline our gameplan for generating random SVGs. We will discuss the steps involved in creating unique SVG images using random elements, such as colors, shapes, and patterns. Understanding the overall process will help us tackle the implementation more effectively.
Create Function & Chainlink VRF
In this section, we will implement the create function in our RandomSVG contract. This function will initiate the request to Chainlink VRF, which will provide us with a random number used for generating the SVG image. We will explore how to integrate Chainlink VRF into our contract and handle the callback for random number generation.
After initiating the Chainlink VRF request, we need to handle the callback function, fulfillRandomness. This function will receive the random number generated by Chainlink and use it to generate a unique SVG image. We will implement the fulfillRandomness function and explore how to leverage the random number for SVG generation.
Once we have generated the random SVG image, we need to complete the minting process for our NFT. We will implement the finishMint function, which will finalize the minting process by assigning the SVG image to the NFT and updating the metadata. We will also explore how to update the token URI and make it accessible on-chain.
To generate the SVG image, we will implement the generateSvg function in our RandomSVG contract. This function will utilize the random number received from Chainlink VRF and combine it with various SVG elements to create a unique and visually appealing image. We will discuss the different components of the SVG and how to customize them based on the random number.
One crucial aspect of SVG generation is the path element, which defines the shape and outline of our image. We will implement the generatePath function to create random paths for our SVGs. We will explore different path commands and how to adjust them based on the random number.
Within the generatePath function, we will implement the generatePathCommand helper function. This function will generate a random path command based on the given parameters, such as direction and length. We will discuss the available path commands and how to incorporate them into our SVG generation process.
Grabbing the Final Functions
With all the necessary functions implemented, we will now grab the final versions of the create, fulfillRandomness, finishMint, generateSvg, generatePath, and generatePathCommand functions. We will ensure that all the components are working together seamlessly to generate random SVGs for our NFTs.
Before deploying our RandomSVG contract, we need to set up some mock contracts for testing purposes. We will create mock contracts for Chainlink VRF and OpenZeppelin's ERC721 implementation. These mock contracts will simulate the behavior of the actual contracts and allow us to test our RandomSVG contract in a controlled environment.
Finishing the Script
In this section, we will complete the deployment script (02_deploy_randomSVG.js) by adding the necessary code to deploy our RandomSVG contract and initialize it with the required parameters. We will also verify the contract on the Rinkeby Etherscan to make it publicly accessible.
Deploying to Rinkeby & OpenSea
With our deployment script ready, we can now execute the deployment process for our RandomSVG contract on the Rinkeby Testnet. Once deployed, we will verify that our RandomSVG NFTs are visible on the OpenSea marketplace and that their metadata is correctly displayed.
Deploy to Polygon Mainnet & Set Price
Now that we have successfully deployed and tested our NFTs on the Rinkeby Testnet, we can proceed to deploy them on the Polygon Mainnet. We will modify our deployment script to target the Polygon network and execute the deployment process. Additionally, we will set a price for our NFTs to enable trading and selling on the Polygon network.