Marketplace has launched, further enabling blockchain developers! Learn more

How to Create and Deploy an ERC-1155 NFT

September 23, 2022

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

What we will do:
  1. Create 3 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:

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 3 NFT collections (rock, paper, and scissors) with a single NFT in each one. To upload our files to the decentralized storage IPFS, we can upload them through CLI or use this very easy-to-use tool NFT Storage.

We will be using the second option, NFT Storage. Sign in to NFT Storage and upload your image files for rock, paper, and scissors. You should see something like this once they've been uploaded successfully:

NFT Storage UI showing table of uploaded files.


Click on "Actions" and copy the IPFS URL of each image; we will need it for the metadata of each collection.

Example of option to copy IPFS URL from NFT Storage UI.

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

  • 1.json: Rock collection
  • 2.json: Paper collection
  • 3.json: Scissors collection

Our 1.json file will look something like this:

creating metadata uri

Copy
{ //1.

    "name": "Rocks",

    "description": "This is a collection of Rock NFTs.",

    "image": "https://ipfs.io/ipfs/bafkreifvhjdf6ve4jfv6qytqtux5nd4nwnelioeiqx5x2ez5yrgrzk7ypi",

}


  • 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). 
  • If a collection has multiple images, which is usually the case, an extra parameter id is added to differentiate tokens amongst the collection.

Create the remaining JSON files, 2.json and 3.json, for paper and scissors collections, respectively.

To efficiently upload all the JSON files to IPFS, we will archive them in content-addressed format. https://car.ipfs.io/ helps archive files in IPFS compatible content-addressed archive (.car) format.

Head over to IPFS CAR and upload all three JSON files. Once uploaded, download the .car file and upload it to NFT Storage. All our JSON files are now stored on IPFS in an archived manner. Copy the IPFS URL of the uploaded .car file, and you should be able to access JSON files by just entering the file name at the end of the URL, for example:

The JSON for the Rock collection displayed on a webpage.

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 Ropsten testnet. Make sure you have some Ropsten test ETH which you can also get from Ropsten Faucet.

Create a new file, token.sol, in REMIX and paste the following code into it.

creating deploying the erc1155 contract

Copy
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract rockPaperScissors is ERC1155 {
    uint256 public constant Rock = 1;
    uint256 public constant Paper = 2;
    uint256 public constant Scissors = 3;

    constructor() ERC1155("https://ipfs.io/ipfs/bafybeihjjkwdrxxjnuwevlqtqmh3iegcadc32sio4wmo7bv2gbf34qs34a/{id}.json") {
        _mint(msg.sender, Rock, 1, "");
        _mint(msg.sender, Paper, 1, "");
        _mint(msg.sender, Scissors, 1, "");
    }
}

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.

Lines 6-9: Creating our contract named rockPaperScissors and creating three variables Rock, Paper, and Scissors; then assigning proper id to each one of them.

Lines 11-15: Initializing constructor with the link to our car file as a parameter, minting different NFT collections with parameters:

  • 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.
  • The last is the data field which is kept empty here.

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:

REMIX window with deploying options.


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

Now you can perform functions like getting the balance of the token by entering the address and token id. We can also retrieve the URI of the token by entering the token id.

REMIX window with deployed contract.


OpenSea does not support the returned URI format. So we will need to overwrite the URI function to return the file name as a string:

creating deploying the erc1155 contract

Copy
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

contract rockPaperScissors is ERC1155 {
    uint256 public constant Rock = 1;
    uint256 public constant Paper = 2;
    uint256 public constant Scissors = 3;

    constructor() ERC1155("https://ipfs.io/ipfs/bafybeihjjkwdrxxjnuwevlqtqmh3iegcadc32sio4wmo7bv2gbf34qs34a/{id}.json") {
        _mint(msg.sender, Rock, 1, "");
        _mint(msg.sender, Paper, 1, "");
        _mint(msg.sender, Scissors, 1, "");
    }

    function uri(uint256 _tokenid) override public pure returns (string memory) {
        return string(
            abi.encodePacked(
                "https://ipfs.io/ipfs/bafybeihjjkwdrxxjnuwevlqtqmh3iegcadc32sio4wmo7bv2gbf34qs34a/",
                Strings.toString(_tokenid),".json"
            )
        );
    }
}

Additions:

Line 5: Importing an OpenZeppelin contract to convert Integer to String.

Lines 18-25: Overriding the URI function by creating a custom URI function and converting token if from integer to string, then returning the complete URI.

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

REMIX window with output as OpenSea compatible URI.

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

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you'll ever meet :)

Related articles 18

Solidity vs Vyper
Originally Published On: Aug 18, 2021
Updated On: Sep 23, 2022

With the introduction to smart... When we think of blockchains,... Introduction IntroductionVyper is... Now, the million dollar question...

Continue reading
How to write an Ethereum smart contract using Solidity
Originally Published On: Apr 5, 2020
Updated On: Sep 23, 2022

This article is intended for... Before getting started with... The smart contract term was... As we saw that smart contracts... Now, let's write a simple smart... After writing a smart contract... So we have successfully created...

Continue reading
How to Setup Local Development Environment for Solidity
Originally Published On: Jul 10, 2021
Updated On: Sep 23, 2022

The success story of blockchain... According to the Now that you are geared up with... Installation Now that our contract is ready... Now, let's write some test... The last thing, we need to do is... For deploying using hardhat, you... Here we saw how to work with...

Continue reading
How to Deploy a Smart Contract on MATIC/Polygon
Originally Published On: Oct 27, 2021
Updated On: Sep 23, 2022

Ethereum is a very lovely... With the rapid adoption of... We'll deploy our contract on... After creating your QuickNode... We will need some test MATIC to... Time to write our smart... Now click on the Solidity logo... A big thank you and kudos if you...

Continue reading
How to Create a BEP20 Token
Originally Published On: Jul 3, 2021
Updated On: Sep 23, 2022

BEP20 is the Binance Smart Chain... The first thing you will want to... The Next thing we will need to... Because BSC uses the EVM, the... With the initial BEP20.sol... In order to view this in your... Congratulations! You made it to...

Continue reading
What is an ABI?
Originally Published On: Mar 20, 2021
Updated On: Sep 23, 2022

While interacting with a smart... ABI (Application Binary... The JSON format of a contract’s... One of the most common ways is... Now that you know what ABI is,...

Continue reading
How to call another smart contract from your solidity code
Originally Published On: Jan 30, 2021
Updated On: Sep 23, 2022

Smart contracts are the most... Smart contracts are computer... We’ll deploy our contracts on... Now to achieve our goal we'll... Now you will see two contracts... Now click on count button under... Now let's update the value of... Let's check if the INTERFACE... Here we successfully saw how we...

Continue reading
How to create and deploy an ERC-721 (NFT)
Originally Published On: Mar 6, 2021
Updated On: Sep 23, 2022

Digital collectibles compatible... Fungible means to be the same or... ERC stands for Ethereum Request... Digital art (or... Now that we know what ERC-721... Before writing our NFT contract,... For ease and security, we’ll use... Congratulations on creating your...

Continue reading
How to deploy a smart contract with Brownie
Originally Published On: Jul 23, 2021
Updated On: Sep 23, 2022

Python is one of the most... Smart contract development is... Brownie is built on top of... First of all, lets cd into the... We will deploy our contract on... We need to set up our QuickNode... As our contract will be deployed... Before deploying the contract,... So, today we learned brownies...

Continue reading
How to Write a Secure Smart Contract in Vyper
Originally Published On: Feb 2, 2022
Updated On: Sep 23, 2022

Hey readers! If you are familiar... In this project we will be... First things first, if we are... We can begin by writing our... Here we will write the code... Congrats on making it to the...

Continue reading
Como crear un Token BEP20
Originally Published On: Jan 10, 2022
Updated On: Sep 23, 2022

BEP20 es en Binance Smart Chain... La primera cosa que hay que... El próximo paso que deberás... Como BSC usa la EVM, el código... Con el contrato BEP20.sol... Para poder ver esto en la... ¡Felicidades! Acabas de terminar...

Continue reading
How to Create a Dutch Auction Smart Contract
Originally Published On: Feb 11, 2022
Updated On: Sep 23, 2022

Often to perform any transaction... Auctions are platforms for... A Dutch auction, also known as... We will deploy our contract on... Before moving further, it is... Now that our Dutch auction... If you made it here,...

Continue reading