Previously I have explained about creating a simple NFT smart contract. You can read this blog Creating a Simple NFT Smart Contract with Solidity and Hardhat.
This is the Remix IDE
that we use to create smart contracts and also deploy smart contracts with Injected Metamask
to the Sepolia
Testnet. You can access the Remix IDE web here: https://remix.ethereum.org
The web looks like this:
You can generate AI images from the websites below:
- https://stablediffusionweb.com
- https://rundiffusion.com/
- https://www.craiyon.com/
- https://www.freepik.com/ai/sketch-to-image
Once we have created an AI-generated NFT Image, we need to store the image accessible for NFT metadata on the blockchain. Optionally, we can store the NFT image from this website:
For this article we store an NFT to web Filebase
Get Faucet Sepolia Testnet
We must have ETH on the Sepolia Testnet Network. To claim the faucet you must have 0.001 ETH
on the Ethereum Mainnet. From this article, I claim the faucet from https://faucet.quicknode.com/drip
Creating NFT Token
For ease and security, we’ll use the OpenZeppelin ERC-721
contract to create our NFT. With OpenZeppelin
, we don’t need to write the whole ERC-721
interface. Instead, we can import the library contract and use its functions.
You can create a file with the name in the Sunset.sol
contract directory.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/[email protected]/token/ERC721/ERC721.sol";
import "@openzeppelin/[email protected]/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/[email protected]/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/[email protected]/access/Ownable.sol";
contract SunsetNFT is ERC721, ERC721URIStorage, ERC721Burnable, Ownable {
constructor(address initialOwner)
ERC721("The Sunset", "TSS")
Ownable(initialOwner)
{}
function safeMint(address to, uint256 tokenId, string memory uri)
public
onlyOwner
{
_safeMint(to, tokenId);
_setTokenURI(tokenId, uri);
}
// The following functions are overrides required by Solidity.
function tokenURI(uint256 tokenId)
public
view
override(ERC721, ERC721URIStorage)
returns (string memory)
{
return super.tokenURI(tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721URIStorage)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}
Explanation of the code above:
Line 1: Specifying SPDX license type as MIT. This indicates that the code is licensed under the MIT License, which is a permissive open-source license allowing the code to be used, modified, and distributed with very few restrictions.
Line 2: Declaring the Solidity version as ^0.8.20. This indicates that the code is written using Solidity version 0.8.20 or a compatible version.
Line 4-7: Importing necessary contracts from the OpenZeppelin library.
ERC721.sol
: This contract is imported from the OpenZeppelin library and represents the basic implementation of the ERC721 standard. ERC721 is the standard for non-fungible tokens (NFTs).ERC721URIStorage.sol
: This contract extends ERC721 and adds functionality for storing and managing metadata URIs associated with NFTs. Metadata URIs typically point to additional information about the NFT.ERC721Burnable.sol
: This contract also extends ERC721 and adds the ability to burn (destroy) NFTs. It provides a function for permanently removing NFTs from circulation, and only the owner can invoke it.Ownable.sol
: This contract is used for access control. It allows you to specify an owner who has special privileges within the contract. The owner can perform certain actions that other users cannot.
Line 9: Starting the contract named SunsetNFT
and mentioning that it extends the ERC721, ERC721URIStorage, and Ownable contracts. This means that the SunsetNFT
contract inherits the functionality and properties defined in these contracts.
Line 10: The constructor function begins here. Constructors are special functions in Solidity that are executed only once during contract deployment. This constructor takes one argument, initialOwner
, which is an Ethereum address.
Line 11: This line calls the constructor of the ERC721 contract with the arguments The Sunset
and TSS
. It initializes the NFT contract with a name of The Sunset
and a symbol of TSS
.
Line 12: This line calls the constructor of the Ownable contract, setting the initial owner of the MyToken contract to the address provided as initialOwner.
Line 15: Declaring the function safeMint
with three arguments: to (the address of the receiver of the NFT token), tokenId (the unique identifier for the token), and uri (the URI of the JSON file associated with the token). This function can only be called by the contract owner (specified by the onlyOwner
modifier).
Line 19: Minting a new token by calling the _safeMint
function inherited from the ERC721 contract. It creates a new token and assigns it to the specified receiver's address.
Line 20: Setting the token URI (metadata URI) associated with the token using the _setTokenURI
function inherited from the ERC721URIStorage
contract. The URI is set based on the provided tokenId and uri.
Line 25-32: Implementing overrides required by Solidity for the ERC721
and ERC721URIStorage
contracts. tokenURI
is a function that retrieves the metadata URI associated with a given tokenId. It's marked as public and view, indicating that it's a read-only function and can be called by anyone.
Line 34-41: Implementing the supportsInterface function, which is required by the ERC721
and ERC721URIStorage
contracts. This function checks whether a given interfaceId is supported by the contract and returns a boolean value accordingly.
By combining these functionalities and contracts, the code creates a custom ERC721 token contract named The Sunset
. This contract allows the contract owner to safely mint new tokens, associate metadata URIs with them, and supports the necessary interfaces defined by the ERC721 standard.
Now, take a minute to customize the smart contract with your own details if you'd like. You can update the token name and symbol by updating the following line - ERC721("The Sunset", "TSS")
.
When you're finished, compile the smart contract and deploy it using Injected Provider
(make sure to select Sepolia testnet on Metamask before compiling the contract). Then, paste your wallet address into the box just near the Deploy
button to define the initialOwner
parameter of the constructor function. Lastly, click Deploy
on Remix IDE
.
After deploying the smart contract, we can check the Block explorer
and the Contract Address
. We can interact with the contract from the contract address inside.
We can check the functions inside the Smart Contract, such as Name, Symbol, Balance, and many more.
Save to NFT Storage
We need to save the NFT Image to Filebase. If you don't have an account yet, you'll need to sign up with an email.
Step-by-Step Upload NFT Image:
- We can use
Filebase
for NFT Storage. - We need to create an account to log in to this website.
- Upload your NFT Image to
Filebase
and GetCID
to use asNFT Metadata
. - Write JSON file Metadata and we need to upload the JSON file again to
Filebase
. - Copy the
CID URL
to use when minting NFT withtoken_uri
. - After minting, we can access block transactions.
- Finally, we get NFT with Metadata, to check NFT, we can access
OpenSea Testnet
.
NFT Metadata
NFT metadata is very important for NFT Images and other NFTs. We need to configure metadata with JSON file. For example like this:
{
"name": "The Sunset Art #0",
"description": "As the sun sets behind the horizon, casting a golden glow over the water, people gather to surf the calm waves. The beauty of the sunset combined with the thrill of surfing creates an unforgettable experience that he cherishes.",
"image": "https://ipfs.filebase.io/ipfs/QmZBbictUcbH5GS8GZyPBsdZacwAoefhhhiWjgiPETiZki"
}
Minting NFT with Contract
After uploading the NFT Image, then uploading the NFT Metadata JSON file. We can mint NFT with a Contract.
Confirm the transaction with a Metamask Wallet
We can access the block transactions after minting from the smart contract. https://sepolia.etherscan.io
NFT Collections on OpenSea
Finally we can accessible NFT with OpenSea Testnet, you can check this link: https://testnets.opensea.io/collection/the-sunset-1
Conclusion
With Remix IDE, we can create Smart Contracts and also Deploy Smart Contracts with the Injected Metamask Wallet. And with Filebase, we can upload NFT Images to get CID URL for NFT Metadata that can describe NFT.
Thank you very much Filebase.