Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article explains ERC-5615, an extension to ERC-1155.

The following specification is the primary reference.

Overview

ERC-5615 adds two functions to ERC-1155 tokens: totalSupply, which returns the number of tokens with a given ID, and exists, which checks whether a given token ID exists.

For an introduction to ERC-1155, see the following article.

https://chaldene.net/erc1155

Specification

An implementation may support the ERC-165 interface, but callers must not depend on that support.

javascript
interface ERC1155Supply is ERC1155 {
  function exists(uint256 id) external view returns (bool);

  function totalSupply(uint256 id) external view returns (uint256);
}

exists

This function indicates whether a particular token ID exists, has existed, or may exist. An ID that may exist can represent a token that has not yet been minted but is recognized by the contract.

Parameters
  • id: Token ID.
Return Value

A boolean indicating whether the specified ID exists, has existed, or may exist under the contract's definition of existence.

totalSupply

Returns the number of tokens with a particular ID. It returns zero if the specified ID does not exist.

Parameters
  • id: Token ID.
Return Value

The total supply of the specified token ID.

Additional Considerations

ERC-165 support is not required because the interface is sufficiently simple and requiring it could break compatibility with existing implementations.

The totalSupply and exists functions draw on ERC-721 and ERC-20.

totalSupply does not revert for a nonexistent ID. Callers should use exists to check existence rather than relying on a zero supply alone.

exists can distinguish the following two situations in which totalSupply returns zero.

  • Tokens with the given ID have not yet been minted.
  • Tokens with the given ID will never be minted.

Backward Compatibility

The interface is designed to be backward compatible with OpenZeppelin's ERC1155Supply.

Implementation

The following code is the OpenZeppelin implementation reproduced in the original article. It represents the version of the example used by the author, not necessarily the current code at the linked branch.

javascript
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC1155/extensions/ERC1155Supply.sol)

pragma solidity ^0.8.19;

import {ERC1155} from "../ERC1155.sol";

abstract contract ERC1155Supply is ERC1155 {
    mapping(uint256 => uint256) private _totalSupply;
    uint256 private _totalSupplyAll;

    function totalSupply(uint256 id) public view virtual returns (uint256) {
        return _totalSupply[id];
    }

    function totalSupply() public view virtual returns (uint256) {
        return _totalSupplyAll;
    }

    function exists(uint256 id) public view virtual returns (bool) {
        return totalSupply(id) > 0;
    }

    /**
     * @dev See {ERC1155-_update}.
     */
    function _update(
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory values
    ) internal virtual override {
        super._update(from, to, ids, values);

        if (from == address(0)) {
            uint256 totalMintValue = 0;
            for (uint256 i = 0; i < ids.length; ++i) {
                uint256 value = values[i];
                // Overflow check required: The rest of the code assumes that totalSupply never overflows
                _totalSupply[ids[i]] += value;
                totalMintValue += value;
            }
            // Overflow check required: The rest of the code assumes that totalSupplyAll never overflows
            _totalSupplyAll += totalMintValue;
        }

        if (to == address(0)) {
            uint256 totalBurnValue = 0;
            for (uint256 i = 0; i < ids.length; ++i) {
                uint256 value = values[i];

                unchecked {
                    // Overflow not possible: values[i] <= balanceOf(from, ids[i]) <= totalSupply(ids[i])
                    _totalSupply[ids[i]] -= value;
                    // Overflow not possible: sum_i(values[i]) <= sum_i(totalSupply(ids[i])) <= totalSupplyAll
                    totalBurnValue += value;
                }
            }
            unchecked {
                // Overflow not possible: totalBurnValue = sum_i(values[i]) <= sum_i(totalSupply(ids[i])) <= totalSupplyAll
                _totalSupplyAll -= totalBurnValue;
            }
        }
    }
}

Conclusion

ERC-5615 exposes supply and existence information for individual ERC-1155 token IDs. Further execution examples may be added in the future.