Creating Your Own ERC-721 NFT: A Step-by-Step Guide using NFT.Storage & Remix IDE
Non-Fungible Tokens, or NFTs, are controlled by a standard on-chain smart contract called ERC-721. Typically, an on-chain ERC-721 contract generates token IDs by associating each token ID with a URL that points to off-chain digital assets.
As the expectation for content permanence and the Web3 philosophy means hosting NFT metadata, images, and other assets on a long-term decentralized off-chain storage, this adds additional (but necessary) layers to factor in the capabilities of the underlying storage hardware.
This article will guide you on uploading files to NFT.Storage (a free, decentralized off-chain storage), creating your NFT’s metadata, and minting an NFT on-chain. Note that this article is written for those with a basic understanding of JSON and familiarity with Remix IDE.
How to Upload Files to NFT.Storage
NFT.Storage is a free, decentralized storage platform powered by IPFS and Filecoin. InterPlanetary File System (IPFS) addresses all files you upload, while Filecoin provides consensus mechanisms for long-term content persistence with Proof-of-Spacetime and Proof-of-Replication.
IPFS generates an IPFS URL pointing to any file uploaded to NFT.Storage. Each IPFS URL consists of an IPFS scheme (ipfs://) followed by a unique content identifier (CID). You can use IPFS URLs in your NFTs metadata to ensure your NFT always refers to the intended asset.
To upload files to NFT.Storage, follow these steps:
- Go to NFT.Storage and log in.
- If you are not on the files page, click on Files to go to your files page.
- Click on the Upload button to load the file upload page.
- Click on the Choose File button to select a file from your device.
- Click on Upload to upload your files.
- Once your files are uploaded, click on the Action button to copy the following information:
- IPFS URL (ipfs://<cid>)
- IPFS hash of the uploaded files known as CID.
In practice, you will first upload an NFT’s digital asset to get its IPFS URL and then insert the resulting URL in your NFT’s metadata before uploading the metadata to NFT.Storage. From here, you’ll use your metadata’s IPFS URL for minting on-chain.
How to create NFT metadata
Metadata provides information about NFTs, usually in JSON format.
NFT marketplaces use your JSON metadata to display information for buyers. Therefore, your metadata should be compatible with the official NFT metadata standard or Enjin’s suggestions.
To create basic NFT metadata in JSON format, follow these steps:
- Create a file and name it as you wish by adding the .json extension.
- Copy and paste the following basic recommendation for NFT metadata JSON Schema:
{ "title": "Asset Metadata", "type": "object", "properties": { "name": { "type": "string", "description": "Identifies the asset to which this NFT represents" }, "description": { "type": "string", "description": "Describes the asset to which this NFT represents" }, "image": { "type": "string", "description": "A URI pointing to a resource with mime type image/* representing the asset to which this NFT represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive." } } } Edit it as you wish by implementing your NFT's image's IPFS URL as follows: "image": { "description": "IPFS URL HERE" }
- Save the file with your edited metadata and upload it to NFT.Storage.
- Once the metadata is uploaded, click on “Action” to copy the IPFS URL (ipfs://<cid>) of the metadata. In the ERC-721 standard, this URL behaves as the token URI.
Once you have the metadata IPFS URL, you can mint your NFT on-chain following the ERC-721 standard.
How to mint an NFT (ERC-721)
In this section, we will walk you through how to mint a one-of-one (i.e., unique) NFT for demonstration purposes. We will follow the ERC-721 standards and use Remix IDE along with the IPFS URL of the metadata uploaded to NFT.Storage.
Here’s how to mint an ERC-721 NFT using Remix IDE:
- Create a .sol file.
- Copy and paste the ERC-721 code into your Remix editor:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.9; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract MyToken is ERC721, ERC721URIStorage, Ownable { constructor() ERC721("MyToken", "MTK") {} 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 _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) { super._burn(tokenId); } function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) { return super.tokenURI(tokenId); } }
- Change the name “MyToken” and the symbol “MTK” to your desired name and symbol.
- Compile the code by clicking the Solidity Compiler tab, followed by the Compile button.
- Go to the Deploy & Run Transactions tab and select “MyToken” from the dropdown.
- Click the Deploy button.
- Once deployed, expand the deployed contract and copy the contract address.
- Go back to the Solidity Compiler tab, and select the “MyToken” contract from the dropdown.
- Click on the “safeMint” function.
- Fill in the “to” field with your Ethereum address, the “tokenId” field with the desired ID of your NFT, and the “uri” field with the metadata IPFS URL you copied from NFT.Storage.
- Click on the “transact” button to mint your NFT.
Congratulations – you have successfully minted your own ERC-721 NFT!