Creating A Basic ERC721 Contract With Solidity and Viewing It On OpenSea
ERC721, also known as the Non-Fungible Token (NFT) standard, is a smart contract on the Ethereum blockchain that implements a standard interface for tokens. Each token is unique and not interchangeable with any other token; hence the term "non-fungible." This standard is widely used for creating digital collectibles, digital art, and more.
Let's Build ๐๐๐
This is the ERC721 contract we are going to be deploying:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract MyNFT is ERC721 {
uint256 private s_tokenCounter;
mapping(uint256 => string) private s_tokenIdToUri;
constructor() ERC721("Web3Bridge", "W3B") {
s_tokenCounter = 0;
}
function mintNft(string memory _tokenUri) external {
s_tokenIdToUri[s_tokenCounter] = _tokenUri;
_safeMint(msg.sender, s_tokenCounter);
s_tokenCounter++;
}
function tokenURI(
uint256 _tokenId
) public view override returns (string memory) {
return s_tokenIdToUri[_tokenId];
}
}
License and Solidity Version: The first line specifies the license for the contract, which is MIT. The second line indicates that the contract is written in Solidity version 0.8.20.
Importing OpenZeppelin ERC721: The contract imports the ERC721 implementation from the OpenZeppelin library, which is a widely used and audited library for Ethereum smart contracts.
Contract Declaration:
MyNFT
is declared as a contract that inherits fromERC721
.State Variables:
s_tokenCounter
is a private variable that keeps track of the total number of tokens minted.s_tokenIdToUri
is a mapping that associates each token ID with its URI.Constructor: The constructor initializes the ERC721 contract with a name and symbol.
Minting Function:
mintNft
allows an external account to mint a new NFT by providing a URI. The URI is stored in thes_tokenIdToUri
mapping, and the token is minted using the_safeMint
function from the ERC721 contract.Token URI Function:
tokenURI
is an overridden function that returns the URI associated with a given token ID.
Hosting TokenURI on IPFS
To host the tokenUri
on IPFS, you need to follow these steps:
- Upload Metadata to IPFS: Create a JSON file with the metadata for your NFT, including the name, description, image, and any other attributes. Upload this file to IPFS using a service like Pinata or directly through the IPFS command line interface.
- Get IPFS Hash: After uploading, you will receive an IPFS hash, which is a unique identifier for your file on the IPFS network.
- Use IPFS Hash as URI: Use the IPFS hash as the URI when minting your NFT. The URI should be in the format
ipfs://<IPFS_HASH>
.
Here's an example of what the metadata JSON might look like:
{
"name": "My First NFT",
"description": "A unique digital art piece",
"image": "ipfs://QmYz.../my-image.png"
}
Viewing on OpenSea
Once your contract is deployed and you have minted NFTs with their URIs pointing to IPFS, you can view and trade them on OpenSea:
- Deploy Contract: Deploy your contract to the Polygon Mumbai testnet using a tool like Remix, Truffle, or Hardhat.
- Mint NFTs: Mint NFTs using the
mintNft
function with the IPFS URIs.
- Verify Contract on OpenSea: Go to OpenSea, click on "Create," and then "Import Existing Contract." Enter your contract's address and follow the prompts to verify it.
- View NFTs: After verification, your NFTs should be visible on OpenSea. You can view them in your wallet or on the OpenSea marketplace.
Conclusion
Creating an ERC721 contract and hosting your NFTs on IPFS allows you to build a unique digital asset collection. OpenSea provides a platform to view and trade these assets, making it easy for users to discover and purchase your NFTs.
Subscribe to my newsletter
Read articles from favour aniogor directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by