Mint 100s of NFTs in just 5 MINUTES using ERC1155 😊😊
Published: 2022-05-10 13:27:18


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](https://medium.com/uv-labs/dude-i-just-deployed-my-own-nft-wt-f-9ef91c778af)

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****🛠️

1. **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.
2. **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
3. **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](https://blog.polysynth.com/how-to-connect-polygon-testnet-to-metamask-wallet-472bca410d64)* so that you are connected to Mumbai Matic Testnet. Once that is done, you should fetch test tokens; for that head 👉 *[here](https://faucet.polygon.technology/)*

🏋️*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](https://www.youtube.com/watch?v=bZKVfXmzRDw) 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 👇

```jsx
// 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. 

```jsx
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](https://gateway.pinata.cloud/ipfs/QmXLdCoTPVZ8Vf5PrEZR1awPKR8PvxmyEakaR5ummPCPnh) 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](https://medium.com/uv-labs/dude-i-just-deployed-my-own-nft-wt-f-9ef91c778af) 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 👇

```jsx
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 👇


![Pinata](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2awd7vy6pv3edimvqir2.png)

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.

```jsx
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Strings.sol";
```

And then add the following code inside your contract:

```jsx
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👇 


![Smart Contract](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tdnqwku5yo4m3gcd0gt.png)

Therefore, now it's TIME to deploy our contract and view our NFTs on Opensea.  🤯

For your ease, you can use this ➡️ [link](https://remix.ethereum.org/#url=https://github.com/UV-Labs/Tutorials/blob/main/GameofThronesNFT/GameofThrones.sol&optimize=false&runs=200&evmVersion=null&version=soljson-v0.8.7+commit.e28d00a7.js) 🔗 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](https://faucet.polygon.technology/)*


![Metamask Screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljbynrnw3znan1svad6e.png)

- 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.


![Remix Wallets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysan4cxgscv4tuh8elz7.png)
- 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](https://testnets.opensea.io/paritomar)

Showcase your NFTs to your friends and don’t forget to tag us on Twitter at [@uv_labs](https://twitter.com/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](https://medium.com/u/9cd26cbd96b9?source=post_page-----5311a13d173-----------------------------------) and [Pari Tomar](https://medium.com/u/130345445971?source=post_page-----5311a13d173-----------------------------------)