Launching an NFT from Scratch 🎉
What is an NFT? đź’ˇ
A Non-Fungible Token is a unique token that lives on a blockchain. Each NFT has an identifier that can be tracked as the token is minted, transferred, or staked. Under the hood here is what makes an NFT:
The two main pieces that make an NFT are:
- The smart contract which contains the logic
- A decentralized storage (IPFS) to store the metadata and assets that the smart contract points to.
In this tutorial, we will be creating a very simple smart contract based on the OpenZeppelin ERC721 standard and storing image assets on Pinata, an IPFS service.
Housekeeping đźŹ
The first step is to ensure you have Metamask installed in your browser and have created an account. For this tutorial, we’ll strictly only be using the Sepolia test net so make sure to switch networks.
If you don’t have Metamask, head over to https://metamask.io/ and install the browser extension. Go through the onboarding screens and follow best practices for securing your account.
Deploying smart contracts cost gas, so we’ll need some Sepolia ether before we get started. Thanks to our friends at Alchemy we can request some for free! Head over to https://sepoliafaucet.com/ and top up your account to 0.5 Sepolia ETH. You will need to create a free Alchemy account first for validation purposes.
After those transactions go through you should see 0.5 Sepolia ETH in your Metamask. To add your own external RPC provider like Alchemy or Moralis instead of using Metamask’s default provider, go to “Add a network”, select your network, and add in your corresponding RPC API key. You’ll use this RPC endpoint in Remix to deploy your smart contract.
Metadata đź’ľ
Smart contracts don’t actually store any images, they simply hold a URL to a location where metadata and images live. While you could technically have this URL point to any source, to preserve the decentralized nature of NFTs this data should be stored on decentralized storage. We will use Pinata to help us, which is a service on top of IPFS (https://ipfs.io/). Head over to https://www.pinata.cloud/ and create a free account.
Before we start storing data we need to prepare our assets:
- Images
For this tutorial let’s just pick 5 random images from https://unsplash.com/. You’ll want to add each of these images to a folder somewhere on your computer and label them. For ease, you can name them 0.png, 1.png, 2.png, 3.png, 4.png
. Next, go ahead and upload (pin) the folder in Pinata.
Once the folder is uploaded, you should find a CID (content identifier) that looks something like:
QmRUxK8c69BjcpnecmkytyxS6dAcooqNwsBMvLJv9er1EF
Keep track of this.
2. Metadata
Now that we have stored our images on IPFS, we need to generate the metadata which has information about our NFT. This is what the smart contract will point to and what marketplaces like OpenSea use to read NFTs from the blockchain. You can read more about the metadata standard here: https://docs.opensea.io/docs/metadata-standards.
In a text editor of choice, you’ll need to create 5
metadata files to go with the images you just uploaded. The format of each file will look like this:
Here is where we can have some fun and give our NFT some life. Each file will have the following fields:
name
: the name of the NFT collection
description
: the description of the NFT collection
image
: this will follow the formatipfs://{CID}/{Image#}.png
. You’ll use the CID generated from the uploaded images followed by the token ID.e.g. using the above CID for file
0
the image field should beipfs://QmRUxK8c69BjcpnecmkytyxS6dAcooqNwsBMvLJv9er1EF/0.png
attributes
: this is what collections use for rarity. You can add these or leave them blank for simplicity.
*Note the only fields changing in each file are the image
and attributes
(if set) fields.
Once you have all 5
files in a folder, name them in order e.g. 0,1,2,3,4
with no extension for simplicity, and upload them to Pinata. You should now have a new CID
that looks like QmRJUSwnQ7zYMa1jzYFkLPcYBmfe7SCHyW6u1qEVvnjfx8
. This is what will form the URL for the smart contract to use.
The full URL for the above CID will be https://gateway.pinata.cloud/ipfs/QmRJUSwnQ7zYMa1jzYFkLPcYBmfe7SCHyW6u1qEVvnjfx8/.
Make sure you keep the /
in there, it’s important for the smart contract.
Smart Contract ⛓️
Now let’s get our solidity skills up! We’ll be using the remix IDE and a pre-baked NFT contract to get started (you can always customize this).
Copy the code from https://gist.github.com/chadlohrli/3b2600431aa0ef699042ccef002298b6 and paste it into a new file on remix: https://remix.ethereum.org/. You can name this file simpleERC721.sol
.
*ERC721 is the NFT standard, you can read more about it here: https://eips.ethereum.org/EIPS/eip-721
Go ahead and try compiling the code, click on Compile simpleERC721.sol
and make sure you’ve set the compiler version to 0.8.0
for this contract.
Next, let’s get ready to deploy. This is where the Rinkeby Eth will come in handy.
First, make sure the following are set in the Remix IDE:
Environment:
Injected Web3 (Rinkeby (4) network)
Account:
Your account with 0.3 Rinkeby ETH
Contract:
simpleERC721 — simpleERC721.sol
If all of this is good, under the DEPLOY
tag is where you will add all of the NFT details:
Name
: the name of your NFT, same that was used for metadata
Symbol
: the symbol for your NFT
BaseURI
: the metadata URI from above (remember the trailing /)
TokenPrice
: the token price in wei. You can use this handy calculator to convert from eth to wei https://eth-converter.com/. For this I used10000000000000000
wei which is equivalent0.01
eth.
MaxTokens
: in our case 5
MaxMints
: the number of tokens one can mint per transaction, just set this to 1
Double-check everything and go ahead and hit the transact
button. This should open up your Metamask to confirm the transaction.
Once the contract is deployed, you should see the contract under Deployed Contracts
Mint đź”®
Time to Mint! Hit the down arrow under deployed contracts to display all of the smart contract functions.
First set the right wei amount in the value box in Remix. (scroll all the way back up Remix for this).
Next enter 1
next to the mintTokens
function and make sure you set the right wei amount in the value box (scroll all the way back up Remix for this).
Next, click on mintTokens
and accept the Metamask transaction. Once this confirms you’ve officially just minted your first NFT on your own contract 🎉
OpenSea 🖼️
You can now view the NFT on https://testnets.opensea.io/ under your profile (same ethereum address used to mint). Note this may take a few minutes while OpenSea indexes the transaction
Etherscan đź‘“
You can also check out the transaction on https://rinkeby.etherscan.io/ under the contract address, in my case the contract address is 0xF057b6A4253E36C2a07C52F99170a8E150856EaD
Yay 🎉
Congrats on making it to the end! Throughout this tutorial you learned:
- How to create metadata and upload to IPFS
- How to deploy a simple smart contract on Rinkeby with Remix IDE
- How to mint on Remix IDE
- How to view your smart contract on Etherscan and OpenSea
Services Used 🔨
- https://remix.ethereum.org/ — Ethereum IDE (integrated development environment)
- https://metamask.io/ — Ethereum Wallet
- https://faucets.chain.link/rinkeby — Free Rinkeby faucet
- https://www.pinata.cloud/ — IPFS service for storing assets
- https://rinkeby.etherscan.io/ — Rinkeby network block explorer
- https://testnets.opensea.io/ — NFT marketplace
- https://docs.opensea.io/docs/metadata-standards — OpenSea Metadata Standard
- https://eth-converter.com/ — Eth to Wei converter
- https://unsplash.com/ — Royalty free images
Reach Out 📱
If you have any questions feel free to leave comments or follow on Twitter at https://twitter.com/ultrasoundchad.
This post is also part of the ETHSD meetup. Check us out at https://www.meetup.com/eth-sd/ 🔥