New Here? Claim Your 30% Discount on Your First Blockchain Node Access!

banner image

How to Create ERC-1155 NFT on Ethereum

Vance Wood

Vance Wood

February 22, 2024

6 min read

article cover

ERC-1155 tokens appeared as a middle ground between ERC-721 and ERC-20, to take the best of and address the shortcomings of both.

H2: ERC-1155 introduction: Highlights

In contrast to ERC20 or ERC721, each helping create one or another type of tokens, ERC-1155 brings versatility.

  • The standard has existed since 2018 when the Enjin team suggested it as a more convenient way to deploy all kinds of assets using one Solidity contract;
  • Its key advantage lies in the possibility to mint and transfer not only multiple tokens within one transaction but different types of them;
  • ERC-1155 contract logic also facilitates managing semi-fungible assets;
  • Blockchain-based gaming projects are particularly enthusiastic about adopting this framework.

Now, let's examine the fundamental components of ERC1155 through examples before proceeding to our own smart contract.

What is ERC-1155?

ERC-1155, as a standard, was initially proposed by Witek Radomski, lead engineer at Enjin, a blockchain project focused on Web3 gaming. It aimed to simplify token management in gaming apps within a single code.

Today it guides on deploying and creating tokens that can represent fungible assets (otherwise knowns as ERC-20) and non-fungible assets (typically built as ERC-721).

Its hybrid nature is visible from the double mapping structure: from asset ID to the set of addresses owning it, to the balance of each address.

alance_of_each_address.

Image from GitHub

To unwrap: each asset is identified by an ID, with no limit on the number of tokens sharing the same ID, allowing for multiple owners.

An excellent example of this is the "Adidas Originals: Into the Metaverse" collection, which provides holders with benefits when purchasing the company products.

ERC-1155_as_Adidas_memebership_token

Image from OpenSea

Why use ERC-1155 for NFTs?

Let’s approach some reasons for using this standard for NFTs specifically by understanding why it was created in the first place and what limitations of ERC721 it solves.

N.B.: Discover more about standards in general and find a detailed ERC-1155 vs ERC-721 analysis from our dedicated blog post.

Meet semi-fungible assets

ERC-1155 tokens can show varying degrees of fungibility, allowing for semi-fungible tokens (SFTs).

Initially, they act like fungibles, allowing 1:1 exchanges. Later on, SFTs lose this feature and become one of a kind.

The event that triggers the transition could be any condition programmed into the smart contract.

Gas efficiency

ERCs 721 and 20 require running multiple transactions when dealing with different addresses and tokens.

With 1155, it’s possible to not only mint and send several assets at once but different types of them in a single transaction by providing two arrays, (‘ids’) and (‘values’).

batch_transfers_in_ERC1155

Image from GitHub

Safe transactions mechanism

The standard strictly enforces the use of the ‘safe’ prefix for all transfer functions, ensuring a higher level of reliability and elominating the risk of token loss.

safe_transfers_in_ERC1155

Image from GitHub

Transactions will revert if the receiving contract has not implemented the necessary interface to handle ERC1155 transfers.

With all the above in mind, the decision between ERC1155 vs ERC721 depends on whether NFTs require additional functionality.

This particular standard is especially applicable to Web3 gaming. Sandbox, a prominent metaverse project, leverages ERC1155 tokens to populate its virtual world with various items.

How to create an ERC-1155

Using OpenZeppelin libraries and Remix IDE for effortless editing, compilation, and deployment, we’ll craft the simplest contract to mint two versions of ERC-1155 tokens with predefined initial supplies.

  1. Begin by importing the necessary interface from OpenZeppelin to ensure all required libraries are available within the Remix workspace:
import “@openzeppelin/token/ERC1155/ERC1155.sol”;
  1. Inherit the contract and define the constructor to add the link to JSON metadata files uploaded to IPFS and packed in one folder:
contract MyCollection is ERC1155 {
    constructor() ERC1155("ipfs://QmRNSbStsC9wGBZ9WxuiLC1cDKj2wvQpCScHkBqjKodg7w/{id}.json")

{id} allows each token to have associated metadata corresponding with the ID.

uploading_NFT_metadata_to_IPFS

Image from IPFS

  1. Within the constructor, implement the minting function defined by OpenZeppelin's interface:
{
_mint(msg.sender, 0, 1000, ""); // Mints 1000 tokens with ID 0 
_mint(msg.sender, 1, 500, ""); // Mints 500 tokens with ID 1 
    }

The fourth argument is an optional data field that can be used for additional context.

Once the code is done, check that it compiles without errors by going to the Solidity compiler, which is the third tab in Remix.

compiling_ERC1155_with_Remix

Image from Remix

Context Banner

Deploying ERC-1155 NFT to Ethereum

Remix provides seamless deployment (creating) using MetaMask as a gateway. Leveraging this feature, we'll direct our communication with the Sepolia test network via Ethereum nodes provided by GetBlock.

Step 1: Connect to Ethereum

To do so, simply add a new network to MetaMask by pasting the endpoint URL retrieved from your GetBlock account as a new Chain ID, whether opting for testnet or mainnet.

adding_custom_RPC_URL_to_MetaMask

Image by GetBlock

Step 2: Deploying to Sepolia

Back in Remix, proceed with “Deployer”. Choose “Injected Provider” as an environment and ensure it fetches the selected network. Confirm the transaction from the wallet to pay a small portion of test ether as a fee for creating a new instance.

deploying_ERC1155_to_Ethereum_testnet”

Image from Remix

How to get ERC-1155 address

From Remix, we can navigate directly to the Etherescan to see deployment details or locate the contract address directly in the terminal.

finding_contract_address_in_Remix

Image from Remix

How to view ERC1155 NFT on OpenSea

Let’s copy the contract address (“0x2ea31817798e0b6a340427d0fecdab13432f245a”) to see how the collection is displayed on OpenSea, in our case testnets.opensea.io.

how_newly_added_ERC1155_collection_displays_on_OpenSea

Image from Testnets OpenSea

Just like that, with Remix, MetaMask, and GetBlock we deployed our contract to the Ethereum testnet. The process of token creation on the mainnet will be identical, except the real ETH will be spent.

N.B.: Alternatively, explore how to use OpenZepeplin Contract Wizard with Remix to simplify NFT contract writing in our dedicated guide.

H2: Final thoughts

While ERC 721 brings the elegance of simplicity, ERC 1155 can be viewed as a more advanced version with extended capabilities. As a relatively new phenomenon, more of its use cases are yet to be fully explored.

GetBlock, one of the top RPC node providers in the industry, manages over 55 blockchain nodes with access to mainnets and testnets, enabling developers to retrieve data directly from the blockchain and run full-scale decentralized applications.

All users enjoy the advantage of daily free access to 40,000 requests. Try now. Explore and experiment freely.

FAQ

  • What is the difference between ERC1155 and ERC721?

    plus
  • What are some popular dApps that use ERC-1155?

    plus
  • What are the advantages of using ERC-1155 for NFTs?

    plus
  • Is ERC-1155 the future of NFTs?

    plus
Vance Wood

Vance Wood

February 22, 2024

6 min read

twittertwittertelegramtelegramLinkedinLinkedin