# ERC-1155 Primer

By [0xNelli](https://paragraph.com/@0xnelli) · 2024-11-14

---

Overview
--------

The ERC-1155 standard specifies an interface that supports multiple tokens governed by a single contract. Additionally, tokens controlled by the contract can have different types of fungibility.

Supported fungibility types include:

*   Fungible (ERC-20 like)
    
*   Semi-fungible
    
*   Non-fungible (ERC-721 like)
    

It also introduces a number of QoL features, such as bulk approvals, where one approval applies to all token types held by the contract. This removes the need for users to approve an individual token each time they transact with it. Moreover, it introduces batched transactions. As the contract supports multiple tokens, it provides a mechanism to query balances for multiple tokens in one transaction or to transfer multiple token types in one transaction. Finally, it also implements safe transfers to ensure that the receiving entity supports the ERC-1155 token type.

Semi Fungibility
----------------

This fungibility type refers to a token that possesses unique metadata and properties but can be held by multiple addresses. For example, a game may require a key to gain access to a specific level or hidden content. The token representing the key is unique and would have metadata and properties related to the content it unlocks. However, many users of the game may hold the token.

This type of token has characteristics of both an ERC-20 and ERC-721. It represents a unique asset with its own properties and metadata, like an ERC-721. However, these tokens have multiple owners and are interchangeable with one another, as they represent the same asset, much like an ERC-20.

Batch Operations
----------------

Batch operations introduce significant gas savings as they permit multiple balance queries or transfers to be completed in a single transaction. This is only possible as the ERC-1155 standard maintains the state of multiple tokens in a single contract.

**Batch Query Balance:**

Queries an array of addresses (`accounts`) for the balance they hold of the token type `id`. Returns an array of token balances in the order of the `accounts` provided to the function call.

    balanceOfBatch(address[] accounts, uint256[] ids) -> uint256[]
    

*   The length of `accounts` must equal the length of `ids`
    
*   You can query multiple addresses for the balance of different tokens the contract holds
    
    *   E.g. query two addresses the balance of gold(fungible), potions(semi-fungible) and unique swords(non-fungible) the each own all in one transaction
        

**Batch Transfer:**

You can transfer multiple different types of tokens from one address to another in the same transaction.

    safeBatchTransferFrom(address from, address to, uint256[] ids, uint256[] amounts, bytes data);
    

*   `ids` and `amounts` must have the same length
    
*   `to` must implement `onERC1155BatchReceived`
    

### OpenZeppelin extended batch functionality:

OpenZeppelin extended the batch features included in the ERC-1155 standard, introducing a `mintBatch` and a `burnBatch`.

**Mint Batch:**

Mint multiple tokens in one transaction. The mint can also include multiple types of tokens, e.g. mint ERC-20 and ERC-721 tokens to the same address in a single transaction.

    _mintBatch(address to, uint256[] ids, uint256[] values, bytes data);
    

*   `ids` and `values` must have the same length
    
*   `to` cannot be the zero address
    
*   The `to` address must implement `onERC1155BatchReceived`
    

**Burn Batch:**

Burn multiple tokens in a single transaction.

    _burnBatch(address from, uint256[] ids, uint256[] values);
    

*   `from` cannot be the zero address
    
*   `from` must have at least the amount of tokens specified in the `values` parameter of the token represented by the array position
    
*   `ids` and `values` must have the same length
    

### Supply Extension

This is an additional extension that can be used to track the total supply of each of the tokens in the ERC-1155 contract. The only balance related functionality enshrined in the ERC-1155 standard is the `balanceOf` and `balanceOfBatch` functions, that only reveal information about the tokens an address holds.

This extension adds the following functions:

*   `totalSupply(id)` which returns the total amount of tokens with the id `id`.
    
*   `totalSupply()` which returns the number of tokens the ERC1155 contract holds.
    
*   `exists(id)` returns a `bool` as to whether the token of `id` exists
    

Sources
-------

1.  [https://eips.ethereum.org/EIPS/eip-1155](https://eips.ethereum.org/EIPS/eip-1155)
    
2.  [https://ethereum.org/en/developers/docs/standards/tokens/erc-1155/](https://ethereum.org/en/developers/docs/standards/tokens/erc-1155/)
    
3.  [https://docs.openzeppelin.com/contracts/5.x/erc1155](https://docs.openzeppelin.com/contracts/5.x/erc1155)
    
4.  [https://docs.openzeppelin.com/contracts/5.x/api/token/erc1155#ERC1155](https://docs.openzeppelin.com/contracts/5.x/api/token/erc1155#ERC1155)

---

*Originally published on [0xNelli](https://paragraph.com/@0xnelli/erc-1155-primer)*
