Step-by-Step Guide to Developing an NFT Smart Contract (ERC721) Using Alchemy
Table of contents
INTRODUCTION:
Non-fungible tokens (NFTs) have revolutionized how digital assets are created, owned, and traded. ERC721, the most common standard for NFTs on the Ethereum blockchain, provides a framework for creating unique, indivisible tokens. In this guide, we'll explore how to develop an ERC721-compliant NFT smart contract using Alchemy, a blockchain development platform that simplifies interaction with Ethereum and other blockchains. You'll have a functional NFT smart contract ready for deployment by the end.
Here, you'll learn how to develop and deploy an ERC721 (NFT) smart contract using Alchemy, OpenZeppelin, Remix, and Ethereum's Sepolia testnet.
More precisely, you will learn:
How to write and modify the smart contract using OpenZeppelin and Remix
Deploy it on the Ethereum Sepolia testnet blockchain to save on gas fees.
Host the NFT tokens metadata on IPFS using Filebase.
Mint an NFT and visualize it on OpenSea.
Developing an ERC721 Smart Contract Using the OpenZeppelin Contract Wizard
In this guide, we'll use the OpenZeppelin Wizard to create our smart contract for two key reasons:
It ensures security.
It provides standard, widely-used smart contracts.
Security is critical when it comes to writing smart contracts. There are countless examples of vulnerabilities that have led to the loss of hundreds of millions of dollars due to poorly secured contracts.
You wouldn't want your valuable cryptocurrencies or NFTs to be stolen after deploying on the blockchain, right?
This is where OpenZeppelin shines. As one of the leading maintainers of widely adopted smart contract standards like ERC20 and ERC721, OpenZeppelin provides developers with thoroughly audited code to create secure and reliable contracts.
To begin developing your ERC721 NFT smart contract, the first step is to visit the OpenZeppelin Contract Wizard.
Once you're on the page, you'll see the following editor:
To get started, click the 'ERC721' button in the top left corner of the wizard. This allows you to select the ERC standard you want to use and specify the type of contract you wish to create:
With the contract standard selected, you'll see a variety of options in the left-hand menu.
Start by setting the name and symbol for your tokens. Click on the text box labelled 'MyToken' and enter your desired token name. Do the same for the Symbol. You can leave the base URI field blank for now, as the token name will be displayed on platforms like OpenSea and Rarible as the name of your collection.
Select the NFT (ERC721) Token Features
Now you'll need to select the features that you want to integrate into our Smart contract, right after the settings section, you’ll find the features section where you’ll be able to select the different modules to include in your smart contract.
For this contract, you'll want to select the following integrations:
Mintable: Adds a mint function that can only be called by privileged accounts.
Auto-increment IDs: Automatically assigns sequential IDs to your NFTs.
Enumerable: Enables on-chain enumeration of tokens and provides functions like totalSupply, which aren’t included in the default ERC721.
URI Storage: Allows you to associate URIs with each of your NFTs, linking them to metadata and images.
For this tutorial, we would leave the following unchecked:
Burnable: Allows tokens to be permanently destroyed.
Pausable: Enables pausing token transfers, sales, and other operations.
Votes: Adds governance-like features such as delegating votes and managing proposals.
Modify and Deploy Your ERC721 Contract Using Remix IDE
Now that you've created your ERC721 smart contract, it's time to modify and deploy it on the Sepolia Testnet. You'll use Remix IDE for this—an online, free integrated development environment specifically built for smart contract development with Solidity.
To get started, you'll notice a button at the top of the OpenZeppelin Wizard editor labelled 'Open in Remix':
Clicking on it will open REMIX IDE in a new tab of your browser.
Compile and Deploy the NFT Smart Contract on the Sepolia Testnet
Open the compiler menu on the left-hand side of the page and click the blue 'Compile' button
Next, click on the 'Deploy and Run Transactions' menu, open the Environment dropdown, and select 'injected Web3
Ensure that your MetaMask wallet is connected to the Sepolia network. Then, choose the NFT smart contract from the Contract dropdown and click 'Deploy.' A MetaMask pop-up will appear—click 'Sign' and confirm the payment of gas fees.
If everything goes smoothly, after about 10 seconds, you'll see the contract listed under 'Deployed Contracts.'
Now that the smart contract is deployed on the Sepolia testnet, it's time to mint our NFTs. However, before doing that, you'll need to create and upload the metadata to IPFS. Let’s first explore what we mean by 'metadata’.
What is NFTs Metadata?
NFT metadata refers to the detailed information that describes and provides context for a non-fungible token (NFT). Unlike the token itself, which is stored on the blockchain, the metadata contains essential data that brings meaning to the NFT, such as:
Name: The title of the NFT or the collection it belongs to.
Description: A brief explanation of the NFT, often detailing its purpose, origin, or background.
Image or Media URL: A link to the visual or digital asset associated with the NFT (like artwork, videos, music), which is usually stored off-chain on platforms like IPFS (InterPlanetary File System).
Attributes/Properties: Unique characteristics or traits that set the NFT apart from others in a collection (e.g., colour, rarity, edition number).
External Links: URLs to external websites or platforms that further describe or interact with the NFT.
For example, an NFT representing digital artwork would include metadata for the artist's name, the artwork's title, a description, and a link to the image file hosted off-chain.
Metadata is usually formatted in JSON and is crucial for platforms like OpenSea and Rarible, which use this information to display NFTs in their marketplace.
Creating and Uploading Metadata to IPFS
To get started, head over to filebase.com and create a new account.
After logging in, click the 'Bucket' button in the left-hand menu and create a new bucket:
Enter the newly created bucket, click the 'Upload' button, and upload the image you want to use for your NFT.
Once the upload is complete, click on the file and copy the IPFS Gateway URL:
Mint Your Sepolia NFT
Go back to Remix and in the "Deploy & Run Transactions" menu, locate the "Deployed Contracts" section. Click on the contract we just deployed, which will reveal a list of methods available in your smart contract:
Orange methods modify the blockchain.
Blue methods retrieve information from the blockchain.
To mint your NFT, click on the safeMint method’s dropdown. In the address field, paste your wallet address, and in the uri field, enter:
ipfs://<your_metadata_cid>
Click "Transact," which will trigger a MetaMask popup asking you to pay the gas fees.
Once you click "Sign," you'll mint your first NFT!
To verify the mint was successful, wait a few seconds, then copy and paste your wallet address into the balanceOf method input. Run it to check that it shows a balance of 1 NFT.
Next, use the tokenUri method, entering “0” as the token ID, to ensure your token URI is displayed.
Congratulations! You've minted your first NFT! 🎉
Visualize Your NFT on OpenSea
Head over to testnets.opensea.io and log in with your MetaMask wallet. Click on your profile picture, and you should see your newly minted NFT. If the image doesn’t appear yet, click on the NFT and use the “Refresh Metadata” button.
Congratulations! You’ve successfully created, modified, and deployed your first smart contract, minted your first NFT, and published your image on IPFS! 🔥
Conclusion
Developing an NFT smart contract using ERC721 is a fundamental step in creating unique digital assets on the Ethereum blockchain. This guide has provided a comprehensive step-by-step process for building and deploying an ERC721 NFT smart contract using Alchemy, OpenZeppelin, Remix, and IPFS. By following these steps, developers can ensure their NFTs are secure, easily mintable, and fully compliant with the most widely used NFT standard. Utilizing tools like the OpenZeppelin Contract Wizard simplifies the process of writing smart contracts, while platforms like Alchemy and Filebase make it easy to deploy and host NFT metadata. Deploying your NFT to a testnet such as Sepolia ensures cost efficiency, especially when experimenting with the development of blockchain projects.
This hands-on approach not only prepares you for deploying on the Ethereum mainnet but also offers a deeper understanding of how blockchain and NFTs operate. As NFT markets continue to expand, the skills gained through this tutorial empower you to participate in the creation, management, and innovation within the NFT space, contributing to a fast-growing digital economy.
References
Alchemy. (n.d.). Blockchain development made easy. Alchemy.com
OpenZeppelin. (n.d.). ERC721 contract wizard. OpenZeppelin.com
Filebase. (n.d.). Decentralized storage simplified. Filebase.com
Ethereum Foundation. (n.d.). Ethereum documentation. ethereum.org
OpenSea. (n.d.). Explore and trade NFTs. OpenSea.io
Subscribe to my newsletter
Read articles from Kehinde Sodiq directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Kehinde Sodiq
Kehinde Sodiq
Software Developer | | Blockchain Developer | | Web3 Technical Writer | | DLTAfrica | | Coding enthusiast | | Lifelong learner