In one of our previous articles, we minted our own NFT where we used the ERC721 contract, which INDEED was an awesome experience for us! Link to that article: ๐ here
Now! We will challenge it to make it more fun and faster!
In this article, we will use the ERC-1155 standard in order to mint our NFT.
Not only this, we will also try to mint our NFTs in less than 5 minutes.
You might be wondering, why use the ERC-1155 for minting our NFTs instead of the standard ERC-721, right?
There are some key differences that should address this question:
- One Smart Contract for all: ERC-721 standard produces NFTs solely and forces developers to create a smart contract for each new token. On the other hand, ERC-1155 allows developers to develop a single smart contract that can be used to mint either fungible tokens or NFTs. Yes, this means, multiple of each of these tokens under one contract. It has the ability to support multiple states on the same address and contract. In simpler words, this means you can make in-game payments using a fungible token on that address and simultaneously transfer unique NFT assets as well.
-
Efficiency: ERC-721 allows a single operation for each transaction. It is expensive and time-consuming. Whereas, ERC-1155 allows multiple operations in a single transaction. Therefore, the transactions are cheaper and more efficient.
Unlike ERC-721, which utilizes significant space, ERC-1155 uses less storage space on a blockchain network. This is because ERC-1155 has the features of:
- Batch Transfer: Transfer multiple assets in a single call.
- Batch Approval: Approve all tokens to an address
This results in less space consumption on the blockchain network.
Now that we have seen some of the benefits of ERC-1155, letโs begin ๐๐ป
*Tools we will be using*๐ ๏ธ
- Remix IDE: It is a powerful open-source tool that helps you write Solidity contracts straight from the browse and speeds-up the whole development process. Though, in our opinion, this should be in no-manner your go-to strategy for working on projects.
- IPFS: IPFS is a distributed system for storing and accessing files, websites, applications, and data. We will be using Pinata client to interact with IPFS.ย Pinataย gives users the ability to upload, manage, and share their content whenever, wherever, and, with whomever they want
- MetaMask: MetaMask is a chrome wallet that lets us interact with the blockchain. We will need our wallet details from Metamask. You have many other options, but we are just sticking to this for ease.
Please note, you will have to connect your Metamask to the Mumbai Matic Testnet network for the purpose of this Tuts. Read ๐ย thisย so that you are connected to Mumbai Matic Testnet. Once that is done, you should fetch test tokens; for that head ๐ย here
๐๏ธNow letโs get to the fun part ๐๏ธโโ๏ธ
1. Write our Smart Contract
In order to write our smart contract, we are going to use Remix IDE to mint our NFTs faster than ever before! ๐
Therefore, if you are new to Remix IDE we highly recommend you to go through this video.
First of all, we are going to create a solidity file where weโll be defining our contract and mint some Game of Thronesโ charactersโ NFTs. ๐ฎ
Therefore, we are going to name our file as GameofThrones.sol
Second, we will import the ERC-1155 contract from Openzeppelin to use its functions in our contract.
Third, we will define our smart contract to mint our NFT that also inherits from Openzeppelinโs ERC1155 contract.
Your code should look similar to this ๐
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
contract GameofThrones is ERC1155 { }
Then, we are going to specify some names and IDs for the NFTs, which we could use further for testing references.
For example, here we have assigned the IDs to the various character named variables.
By this we could say that the NFT with the ID 1 is JonSnow and so on.
uint256 public constant JonSnow = 1;
uint256 public constant CerseiLannister = 2;
uint256 public constant DaenerysTargaryen = 3;
uint256 public constant NightKing = 4;
uint256 public constant TyrionLannister = 5;
uint256 public constant AryaStark = 6;
Now, we will define the constructor where we are going to use the _mint function from the ERC1155 contract in order to mint our NFT.
Inside the _mint function, we are going to define some values:
- The senderโs address
- The tokens that weโre going to mint
- The amount of tokens we want to mint
- And the data of the token
And thereโs one interesting thing, we have already uploaded our NFT Metadata to the IPFS to save our time.
IPFS is decentralized storage. And here we have used the Pinata IPFS which gives users the ability to store their data.
This is the folder storing the Metadata that weโre going to use for our NFTs. Therefore, here we have 6 different Metadata for the respective NFTs.
If youโre new to Pinata, we recommend you go through our previous article based on NFT, where we have covered how to upload the metadata of your NFT on Pinata IPFS.
Your constructor should look similar to this ๐
constructor() ERC1155("https://gateway.pinata.cloud/ipfs/QmXLdCoTPVZ8Vf5PrEZR1awPKR8PvxmyEakaR5ummPCPnh/{id}.json") {
_mint(msg.sender, JonSnow, 100, "");
_mint(msg.sender, CerseiLannister, 100, "");
_mint(msg.sender, DaenerysTargaryen, 100, "");
_mint(msg.sender, NightKing, 1, "");
_mint(msg.sender, TyrionLannister, 100, "");
_mint(msg.sender, AryaStark, 100, "");
}
Note that for Game of Thrones characters, all the characters are fungible tokens instead of the NightKing token, which is a non-fungible token as we minted only one of it.
The ERC1155 contract includes the optional extension IERC1155MetaDataURI. Thatโs where the URI function comes from: we will use it to retrieve the metadata URI.
Thus, if we replace the id with token ID 1 inside the MetadataURI we provided, we could get the metadata of the first NFT.
Result would look similar to this ๐
But OpenSea does not understand the way we encoded the id inside the MetadataURI.
Therefore we have to overwrite the URI function so it works a bit differently. And in order to do that, we are going to need some helper functions to work with the strings.
Therefore, we will import the Strings contract from Openzeppelinโs contract.
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Strings.sol";
And then add the following code inside your contract:
function uri(uint256 _tokenId) override public view returns (string memory) {
return string(
abi.encodePacked(
"https://gateway.pinata.cloud/ipfs/QmXLdCoTPVZ8Vf5PrEZR1awPKR8PvxmyEakaR5ummPCPnh/",
Strings.toString(_tokenId),
".json"
)
);
}
So here, we are writing our own implementation of the URI function and we want to take the token ID and then we want to concatenate the base URL and the required token ID (which is an integer) into the string.
By doing this, we will have our ERC-1155 contract ready to deploy!! ๐
Your smart contract should look like this๐
Therefore, now it's TIME to deploy our contract and view our NFTs on Opensea. ๐คฏ
For your ease, you can use this โก๏ธ link ๐ to directly load this contract in remix on your browser.
2. Deploy the Smart Contract on Mumbai Matic Testnet
Opensea supports various networks like Rinkeby, Mumbai Matic, etc. Thus, weโre going to use the Mumbai Matic Test network to mint our NFTs.
We will follow some steps to deploy our contract:
- To deploy the contract, weโre going to use the Metamask wallet on the Mumbai Matic test network, so that once the contract is deployed, Opensea can grab it easily.
- Switch to Mumbai Matic Test Network in your Metamask wallet and make sure you have some test Matic to perform the transaction. If you donโt have test Matic go ๐ย here
- Then, update the Remix IDE environment to Injected Web3 which you could find under the Deploy & Run Transactions section.
- Once done, you can see that the account address shown in the Remix IDE is similar to your Metamask account.
- Pick the contract which you want to deploy. In our case, we have the GameofThrones.sol
- Now, once youโll hit the Deploy button, you can see a Metamask window pop-up that asks us to confirm our transaction.
Hit the confirm button to mint our NFT on Opensea.
And there you gooo!!
You have minted your own NFTs. ๐ฒ๐ฒ๐ฒ
Without wasting any time, we will use this link to see our minted NFT on Opensea
https://testnets.opensea.io/<your-account-address>?tab=created
You can see mine here
Showcase your NFTs to your friends and donโt forget to tag us on Twitter atย @uv_labs
We will surely retweet your NFT ๐ฅณ
And donโt worry, if you get any error messages, you can ask us too by tagging usย on Twitter.
Do give us a star, clap if you liked our work.
Authors (open to feedback): ๐
Amateur-Devย andย Pari Tomar
Top comments (0)