---
eip: 5216
label: ERC-5216
title: ERC-1155 Allowance Extension
status: Last Call
type: Standards Track
category: ERC
created: 2022-07-11
requires: 20, 165, 1155
official: https://eips.ethereum.org/EIPS/eip-5216
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5216.md
canonical: https://eips.huatuo.xyz/zh/eip-5216/
lang: zh
tldr: 为 ERC-1155 增加 ERC-20 风格的 approve(operator, id, amount) 和 allowance()，让你可以只允许市场转移某个 token id 的有限数量，而不是全部代币。
tags: [erc-1155, approvals, allowance, nft, security]
---

# ERC-5216: ERC-1155 Allowance Extension

> Extension for ERC-1155 secure approvals

**TL;DR:** 为 ERC-1155 增加 ERC-20 风格的 approve(operator, id, amount) 和 allowance()，让你可以只允许市场转移某个 token id 的有限数量，而不是全部代币。

## 它解决什么问题？

[ERC-1155](/eip-1155/) 只有一种授权机制：`setApprovalForAll(operator, true)`，它让操作者可以转移你在该合约中持有的**所有**代币，无论 id 是什么、数量多少。要在市场上架一件物品，你必须把整个合集的无限访问权限交给市场。一旦出现漏洞或操作者被攻破，你的全部资产都可能被转空。

ERC-5216 借用了 [ERC-20](/eip-20/) 的额度模型，让所有者只授权所需的部分：某个 `id` 的指定 `amount`。

## 它如何运作

合约在 ERC-1155 之上实现 `IERC5216`：

```solidity
event Approval(address indexed account, address indexed operator, uint256 id, uint256 amount);
function approve(address operator, uint256 id, uint256 amount) external;
function allowance(address account, address operator, uint256 id) external view returns (uint256);
```

- `approve` 为某一个 id 设置（而非累加）操作者的额度并触发 `Approval`。操作者不能是调用者本人。
- 当调用者对该 id 拥有足够额度时，`safeTransferFrom` 必须成功，并且必须**扣减**已转移的数量；通过 `setApprovalForAll` 获得授权的调用者除外（这仍然意味着无限额度）。
- `safeBatchTransferFrom` 必须检查批次中每个 id 的额度。
- `supportsInterface(0x1be07d74)` 返回 `true`（[ERC-165](/eip-165/)）。

## 示例代码

基于 OpenZeppelin 的 ERC-1155（v5）实现的核心逻辑，仅作演示：

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

import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract ERC1155Allowance is ERC1155 {
    event Approval(address indexed account, address indexed operator, uint256 id, uint256 amount);

    mapping(address => mapping(address => mapping(uint256 => uint256))) private _allowances;

    constructor() ERC1155("https://example.invalid/{id}.json") {}

    function approve(address operator, uint256 id, uint256 amount) external {
        require(operator != msg.sender, "self approval");
        _allowances[msg.sender][operator][id] = amount;
        emit Approval(msg.sender, operator, id, amount);
    }

    function allowance(address account, address operator, uint256 id) public view returns (uint256) {
        return _allowances[account][operator][id];
    }

    function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes memory data)
        public override
    {
        if (from != msg.sender && !isApprovedForAll(from, msg.sender)) {
            uint256 a = _allowances[from][msg.sender][id];
            require(a >= value, "insufficient allowance");
            _allowances[from][msg.sender][id] = a - value;
        }
        _safeTransferFrom(from, to, id, value, data);
    }

    function supportsInterface(bytes4 iid) public view override returns (bool) {
        return iid == 0x1be07d74 || super.supportsInterface(iid);
    }
    // safeBatchTransferFrom must apply the same check per id (omitted)
}
```

## 需要注意

- 状态：**Last Call**（截止日期在 2022 年，从未进入 Final）。该接口可能仍被视为尚未定型。
- 只有当市场真正请求按 id 授权额度时才有好处；许多市场仍然要求 `setApprovalForAll`。
- 与 ERC-20 一样，把非零额度改为另一个非零值可能被抢跑。请先设为 0，并撤销未使用的额度。
- `setApprovalForAll` 仍然凌驾于一切之上。审计操作者能转移什么时，请检查 `isApprovedForAll`。


---

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