---
eip: 5615
label: ERC-5615
title: ERC-1155 Supply Extension
status: Final
type: Standards Track
category: ERC
created: 2023-05-25
requires: 1155
official: https://eips.ethereum.org/EIPS/eip-5615
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5615.md
canonical: https://eips.huatuo.xyz/zh/eip-5615/
lang: zh
tldr: 为 ERC-1155 多代币合约增加 totalSupply(id) 和 exists(id)，把 OpenZeppelin 早已提供的供应量跟踪辅助函数标准化。
tags: [token, multi-token, nft, supply, extension]
---

# ERC-5615: ERC-1155 Supply Extension

> A simple mechanism to fetch token supply data from ERC-1155 tokens

**TL;DR:** 为 ERC-1155 多代币合约增加 totalSupply(id) 和 exists(id)，把 OpenZeppelin 早已提供的供应量跟踪辅助函数标准化。

## 它解决什么问题？

[ERC-1155](/eip-1155/) 允许一个合约持有多种代币类型，每种由一个 `id` 标识。基础标准能告诉你某个账户持有多少该 `id` 的代币，却不能告诉你该 `id` 的代币总共存在多少，也不能告诉你这个 `id` 是否真的是一种代币类型。

市场、浏览器和其他合约经常需要这些答案："这是 100 份的版本，还是 1 份的？""这件物品已经铸造了吗？"许多实现，尤其是 OpenZeppelin 的 `ERC1155Supply`，早已为此提供了辅助函数。ERC-5615 把这种既有做法写成标准，让调用方可以在任何地方依赖同样的两个函数签名。

## 它如何运作

该扩展增加两个 view 函数：

| 函数 | 返回值 |
| --- | --- |
| `totalSupply(uint256 id)` | 该 `id` 的流通代币数量；如果该 id 不存在则为 `0` |
| `exists(uint256 id)` | 该 id 是否存在、曾经存在或可能存在 |

`totalSupply` 对未知 id 刻意不回滚，只返回零。这就留下了歧义，因为供应量为零既可能表示"还没铸造任何代币"，也可能表示"这个 id 永远不会被使用"。`exists` 就是用来区分这两种情况的，所以它的措辞比较宽松（"存在、曾经存在或可能存在"）：对于已预留但尚未铸造的 id，或已被完全销毁的 id，实现可以返回 `true`。

这些函数名沿用了 [ERC-20](/eip-20/) 中的 `totalSupply` 以及 [ERC-721](/eip-721/) 合约中常见的存在性检查。

该 ERC 刻意不要求定义 [ERC-165](/eip-165/) 接口 ID。实现可以声明支持，但调用方不得依赖 `supportsInterface` 来检测，因为那样会破坏与标准撰写之前部署的众多合约的兼容性。

## 示例代码

接口，以及在 mint/burn 钩子中的最小跟踪模式：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IERC1155Supply {
    function exists(uint256 id) external view returns (bool);
    function totalSupply(uint256 id) external view returns (uint256);
}

abstract contract SupplyTracking is IERC1155Supply {
    mapping(uint256 => uint256) private _supply;

    function totalSupply(uint256 id) public view returns (uint256) {
        return _supply[id];          // 0 for unknown ids, never reverts
    }

    function exists(uint256 id) public view virtual returns (bool) {
        return _supply[id] > 0;      // simplest policy; may be widened
    }

    function _afterMint(uint256 id, uint256 amount) internal {
        _supply[id] += amount;
    }

    function _afterBurn(uint256 id, uint256 amount) internal {
        _supply[id] -= amount;
    }
}
```

在链下读取供应量（ethers v6）：

```ts
import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const multi = new Contract(contractAddress, [
  "function totalSupply(uint256) view returns (uint256)",
  "function exists(uint256) view returns (bool)",
], provider);

const id = 7n;
if (await multi.exists(id)) {
  console.log("edition size:", await multi.totalSupply(id));
}
```

## 需要注意

- 状态为 **Final**。它被设计为与 OpenZeppelin 的 `ERC1155Supply` 向后兼容，所以许多现有合约已经符合要求。
- 不要用 `supportsInterface` 探测该扩展；规范规定使用方在此处不得依赖 ERC-165。请直接调用这些函数，并在合约缺少它们时处理回滚。
- `exists` 返回 `false` 且 `totalSupply` 为 `0`，是该 id 未被使用的最明确信号；`exists` 返回 `true` 而供应量为零也是合法的（已预留或已被完全销毁的 id）。
- 供应量跟踪会在每次铸造和销毁时带来额外的存储写入成本。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-5615
