Non-Fungible Tokens (NFTs) are unique digital assets stored on blockchains, representing ownership of items like digital art, collectibles, or in-game assets. Unlike cryptocurrencies such as Bitcoin, each NFT is distinct and non-interchangeable. This guide will walk you through creating a generative art NFT collection featuring dog breeds, powered by verifiable randomness from Chainlink VRF and hosted on IPFS.
Step 1: Clone the Repository and Set Up Dependencies
Begin by cloning the Chainlink Smart Contract Examples repository:
git clone https://github.com/smartcontractkit/smart-contract-examples.git
cd smart-contract-examples/ultimate-nft-repo
yarnEnvironment Variables
Configure these required variables in your project:
ETHERSCAN_API_KEY: Obtain from EtherscanSEPOLIA_URL: Use your Alchemy Sepolia endpointPRIVATE_KEY: Your wallet's private key (keep secure)
Step 2: Configure Chainlink VRF v2
- Visit the VRF subscription page, select Sepolia, and create a subscription.
- Fund the subscription with test LINK tokens from Chainlink’s faucet.
- Save your
subscriptionIdfor the smart contract constructor.
Step 3: Develop the NFT Smart Contract
Key Components:
- OpenZeppelin Contracts: Inherit
ERC721URIStorageandOwnable. - Chainlink VRF: Use
VRFConsumerBaseV2for randomness. - Custom Errors: Gas-efficient error handling with
revert.
Contract Structure:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
contract RandomIpfsNft is ERC721URIStorage, VRFConsumerBaseV2, Ownable {
enum Breed { PUG, SHIBA_INU, ST_BERNARD }
mapping(uint256 => Breed) private s_tokenIdToBreed;
string[] internal s_dogTokenUris;
// ... additional variables and functions
}Key Functions:
requestNft(): Requests randomness from Chainlink VRF.fulfillRandomWords(): Callback to mint NFTs with assigned breeds.withdraw(): Allows the owner to retrieve collected fees.
Step 4: Deploy Images to IPFS
- Upload dog breed images to Pinata.
- Store the IPFS URLs in the contract’s
s_dogTokenUrisarray.
Step 5: Deploy and Mint NFTs
- Deploy to Sepolia testnet using Hardhat or Remix.
- Add your contract as a VRF consumer under your subscription.
- Mint NFTs via Etherscan or a custom dApp interface.
👉 Explore NFT trading on OpenSea Sepolia
Step 6: Trade NFTs on OpenSea
After minting, view your collection on OpenSea’s Sepolia testnet.
FAQ Section
What is Chainlink VRF?
Chainlink Verifiable Random Function (VRF) provides tamper-proof randomness for smart contracts, ensuring fairness in generative art NFT traits.
How much does it cost to mint?
The minting fee is set in the constructor (e.g., 0.01 ETH). Adjustable by the contract owner.
Can I change the NFT artwork?
No, artwork is immutable once deployed to IPFS and linked to the token URI.
Summary
This tutorial covered:
- Creating generative NFTs with dynamic traits.
- Using Chainlink VRF for provable randomness.
- Hosting artwork on IPFS for decentralization.
- Deploying and trading NFTs on testnet marketplaces.
👉 Learn more about Chainlink integrations
For further exploration, check the Chainlink documentation or experiment with other smart contract examples.