DEV Community 👩‍💻👨‍💻

Aayush Gupta
Aayush Gupta

Posted on

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

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

Overview

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

What we will do:

  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

What you will need:

  • Image assets to create NFTs
  • Metadata of NFTs
  • MetaMask and some Mumbai test MATIC.
  • Knowledge of ERC20 and ERC721 (NFT) token standards.

What is ERC1155?

ERC1155 is a multi-token standard that allows the creation of fungible, non-fungible, and semi-fungible tokens all in one contract. Before ERC1155, if a use case needed both ERC20 (fungible) and ERC721 (non-fungible) tokens, then separate contracts were required 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 ERC1155, batch transfer of tokens is also possible instead of transferring a token to a single address in previous standards.

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

The previous ERC721 had a one-to-one mapping of token id with the address. ERC1155 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 in each one. To upload our files to the decentralized storage IPFS, 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 and so on. You should see something like this once they've been uploaded successfully:

Image description

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

Image description

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:

Image description

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:

Image description

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

Image description

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 MUMABI Faucet.
Create a new file, traveler.sol, in REMIX and paste the following code into it.

// 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 claim 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];
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation of the code above:

Line 1: Specifying SPDX license type, which is added after Solidity ^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 simply 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 ERC1155 contract.

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

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

Line 21: function URI to put metadata of NFT to opensea. Metadata which is stored in IPFS is used.

Line 32: function mint to allow user 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 NFT which have been 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.

Compile 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 proper contract name:

Image description

Approve the transaction from MetaMask. Once the transaction is complete, your contract will be deployed.

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.

Image description

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

Image description

Mint Nft

Image description

NFT display on OpenSea

Image description

Showing NFT with all the metadata.

Image description

Conclusion

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

I hope you learnt something new or maybe even solved a problem. Thanks for reading, have fun!
You can follow me on Twitter and Linkedin. Keep your suggestions/comments coming!

Top comments (0)

🌖🌗🌘 Turn on dark mode in Settings