How to Create and Deploy an ERC-1155 NFT Bundle Collection

How to Create and Deploy an ERC-1155 NFT Bundle Collection

Play this article

In this Tutorial, we will build NFT Bundle Collection using ERC-1155. Instead of Minting all the NFTs in one go. We allow users to mint NFT from any NFT bundle Collection one by one. We also set a maximum supply of NFTs; one user can mint only one NFT.

Overview

ERC-1155 has emerged as a gold standard for creating NFTs; every major marketplace lists new tokens as an ERC-1155 standard. In this guide, we will learn about the ERC-1155 token standard and how to create an ERC-1155 NFT Bundle Collection.

D_D Newsletter CTA

Steps

  1. Create 7 NFT collections
  2. Create and deploy an ERC-1155 contract
  3. Update the contract to be compatible with OpenSea
  4. Deploy our NFT collections

Requirements

  • Image assets to create NFTs
  • Metadata of NFTs
  • MetaMask and some Mumbai test MATIC
  • Knowledge of the ERC-20 and ERC-721 token standards

What is ERC-1155?

ERC1155 is a multi-token standard that allows the creation of fungible, non-fungible, and semi-fungible tokens all in one contract. Before ERC-1155, if a use case needed both ERC-20 (fungible) and ERC-721 (non-fungible) tokens, you would need to create separate contracts to achieve this. ERC1155 also allows for multiple NFT collections to be launched in just one smart contract instead of creating a different contract for each collection; this increases efficiency in smart contract construction and minimizes the transaction count, which is very important as it consumes less blockchain space. With ERC-1155, batch transfer of tokens is possible instead of transferring a token to a single address in previous standards.

A prevalent example of the ERC-1155 application is blockchain-based decentralized games, which need coins and collectibles, so ERC-1155 has become a standard there. ERC1155 has also become a standard in the NFT space.

The previous ERC-721 had a one-to-one mapping of token id with the address. ERC-1155 has a rather complex mapping where the address in a combination of token ID is mapped to the balance of the token.

Creating Metadata URI

We will create 7 NFT Bundle collections (Agra, Delhi, Goa, and so on) with a single NFT. We will store the Metadata of NFT to the decentralized storage IPFS because we are building decentralized applications (DApp). We will upload them through PINATA. You can choose some other decentralized storage IPFS of your choice.

Sign in to Pinata and upload your image files for Agra, Delhi, Goa, etc. You should see something like this once you have uploaded them successfully:

Pinata.png

Copy the IPFS URL of the image; we will need it for the metadata of each collection.

Pinata 2.png

We will create three JSON metadata files to store information about our NFT collections.

  • 0.json: Agra collection
  • 1.json: Delhi collection
  • 2.json: Goa collection

Our 0.json file will look something like this:

3.png

name: Has the name of the NFT.

description: Has the description of the NFT.

image: Has the link to the image we got earlier (IPFS URL).

properties: Has the various properties of NFT

Create the remaining JSON files, 1.json and 2.json, for Delhi and Goa collections, respectively, and so on.

Now Similarly, we will upload our metadata folder in Pinata. Once successfully uploaded, It will look like this:

4.png

You should be able to access JSON files by just entering the file name at the end of the URL, for example:

https://ipfs.io/ipfs/QmSCFe5vvoPsSpyHZpGAW78GJ4bAuDcySCV9UnMm7B69iS/0.json

5.png

Creating & Deploying the ERC1155 Contract

We will use the OpenZeppelin contracts library to create our ERC1155 contract and deploy it using Ethereum Remix IDE on the Mumbai testnet. Make sure you have some Mumbai test MATIC which you can also get from MUMBAI Faucet.

Please create a new file, traveler.sol, in REMIX and paste the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract TravelQuest is ERC1155, Ownable { 
    uint256[] public supplies = [50,50,50,50,50,50,150];
    uint256[] public minted = [0,0,0,0,0,0,0];
    mapping(uint256 => mapping(address => bool)) public member;

    constructor() ERC1155("ipfs://QmSCFe5vvoPsSpyHZpGAW78GJ4bAuDcySCV9UnMm7B69iS/{id}.json")
        {
        }

    // to Put NFT to Opensea
     function uri(uint256 _tokenId) override public view returns (string memory) {
        require(_tokenId <= supplies.length-1,"NFT does not exist");
        return string(
        abi.encodePacked(
            "ipfs://QmSCFe5vvoPsSpyHZpGAW78GJ4bAuDcySCV9UnMm7B69iS/",
            Strings.toString(_tokenId),
            ".json"
        )
        );
    }

     function mint(uint256 _tokenId) 
        public
        {
         require(
            !member[_tokenId][msg.sender],
            "You have already claimed this NFT."
        );    
        require(_tokenId <= supplies.length-1,"NFT does not exist");
        uint256 index = _tokenId;

        require (minted[index] + 1 <= supplies[index], "All the NFT have been minted");
        _mint(msg.sender, _tokenId, 1, "");
        // "" is data which is set empty
        minted[index] += 1;
        member[_tokenId][msg.sender] = true;
    }

    function totalNftMinted(uint256 _tokenId) public view returns(uint256){
        return minted[_tokenId];
    }
}

Line 1: Specifying SPDX license type, which is added after Solidity version ^0.6.8. Whenever the source code of a smart contract is made available to the public, these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special value UNLICENSED or skip the whole comment (it will not result in an error, just a warning).

Line 2: Declaring the Solidity version.

Line 4: Importing the OpenZeppelin ERC-1155 contract.

Line 10: supplies is an array that keeps track of the Maximum number of NFT available to mint.

Line 11: minted is an array that keeps track of how many NFT already been minted.

Line 21: function URI to put metadata of NFT to Opensea. We use metadata stored in IPFS.

Line 32: function mint to allow users to mint NFT from our NFT Bundle Collection. One user can only mint 1 NFT of each NFT collection.

Line 49: function totalNftMinted gives the total number of NFTs minted from each NFT Collection.

  • Address on which tokens will be minted to, msg.sender here means the deployer of the contract.
  • Token id, we have already assigned names to token id, so using names here.
  • Quantity of each token.

Compiling the Smart Contract

Compile the contract, go to the third tab on the left menu, select Injected Web3 as environment and deploy it by choosing the correct contract name:

6.png

Approve the transaction from MetaMask. Once the transaction is complete, it will deploy your contract.

Now you can perform functions like mint NFT by entering token id. We can also retrieve the URI of the token by entering the token id.

7.png

When you query the contract for URI, it will return a format supported by OpenSea.

8.png

Minting an NFT

Enter the token ID of the NFT Bundle Collection you want to mint.

9.png

After some time of minting, you can see the NFT on OpenSea.

10.jfif

You will also see the metadata of the NFT.

11.jfif

Conclusion

Congratulations on deploying your ERC-1155 tokens. If you made it here now, you know about the ERC-1155 multi-token standard and how to create and deploy the ERC1155 bundle NFT Collection.

I hope you learned something new or maybe even solved a problem. Thanks for reading, and have fun!

D_D Newsletter CTA

You can follow me on Twitter and LinkedIn. Keep your suggestions/comments coming!