---
eip: 2135
label: ERC-2135
title: Consumable Interface (Tickets, etc)
status: Final
type: Standards Track
category: ERC
created: 2019-06-23
requires: 165, 721, 1155
official: https://eips.ethereum.org/EIPS/eip-2135
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2135.md
canonical: https://eips.huatuo.xyz/zh/eip-2135/
lang: zh
tldr: 一个把 NFT 或多代币标记为可消耗（例如活动门票）的最小接口，包含标准的 consume() 调用、预检查和 OnConsumption 事件。
tags: [nft, tickets, erc721, erc1155, interface]
---

# ERC-2135: Consumable Interface (Tickets, etc)

> An interface extending ERC-721 and ERC-1155 for consumability, supporting use case such as an event ticket.

**TL;DR:** 一个把 NFT 或多代币标记为可消耗（例如活动门票）的最小接口，包含标准的 consume() 调用、预检查和 OnConsumption 事件。

## 它解决什么问题？

许多数字资产本来就是用完即止的：演唱会门票在入口被"消耗"，代金券在兑换时被消耗，通行证在扫码时被消耗。没有标准的话，每个票务合约都会自创 `redeem`、`use` 或 `checkIn` 函数，扫码设备、钱包、市场和其他合约就无法通用地处理它们。

ERC-2135 定义了一个小型的消耗接口，位于 [ERC-721](/eip-721/) 或 [ERC-1155](/eip-1155/) 代币之上。

## 它如何运作

兼容的合约需要实现：

| 成员 | 用途 |
| --- | --- |
| `consume(consumer, assetId, amount, data)` | 代表 `consumer` 消耗 `amount` 数量的 `assetId`；返回 `bool` |
| `isConsumableBy(consumer, assetId, amount)` | view 检查：这次消耗能否成功？ |
| `event OnConsumption(consumer, assetId, amount, data)` | 每次消耗成功时触发 |

规则：

- `supportsInterface(0xdd691946)` 必须返回 `true`（[ERC-165](/eip-165/)）。
- 如果合约是 ERC-721 或 ERC-1155 代币，成功的 `consume` 还必须触发 `Transfer` / `TransferSingle`，就像代币从持有者转到了零地址一样，这样索引器能看到它消失。
- `consumer` 不必是交易发送者，因此场馆的闸机合约或中继者可以代表他人进行消耗。
- `data` 是自由格式，可用于签名背书等扩展。

该标准有意不规定谁可以消耗、在什么条件下消耗、可消耗品如何创建，或它们携带什么元数据。这些都留给应用自行决定。

## 示例代码

一个只有场馆闸机才能消耗的 ERC-721 门票（OpenZeppelin v5）：

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

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

interface IERC2135 {
    function consume(address consumer, uint256 assetId, uint256 amount, bytes calldata data)
        external returns (bool);
    function isConsumableBy(address consumer, uint256 assetId, uint256 amount)
        external view returns (bool);
    event OnConsumption(address indexed consumer, uint256 indexed assetId, uint256 amount, bytes data);
}

contract Ticket is ERC721, IERC2135 {
    address public immutable gate;

    constructor(address gate_) ERC721("Concert Ticket", "TIX") { gate = gate_; }

    function mint(address to, uint256 id) external {
        require(msg.sender == gate, "only gate");
        _mint(to, id);
    }

    function isConsumableBy(address consumer, uint256 id, uint256 amount) public view returns (bool) {
        return amount == 1 && _ownerOf(id) == consumer;
    }

    function consume(address consumer, uint256 id, uint256 amount, bytes calldata data)
        external returns (bool)
    {
        require(msg.sender == gate, "only gate");
        require(isConsumableBy(consumer, id, amount), "not consumable");
        _burn(id); // emits Transfer(owner, address(0), id) as the spec requires
        emit OnConsumption(consumer, id, amount, data);
        return true;
    }

    function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
        return interfaceId == type(IERC2135).interfaceId || super.supportsInterface(interfaceId);
    }
}
```

## 需要注意

- 访问控制是最主要的安全决策。请明确决定持有者、发行方还是闸机合约可以调用 `consume`，并对其进行测试。
- 让消耗与转账限制保持一致：如果转账被暂停或用户受到限制，消耗也应表现相同。
- 对 ERC-721 和 ERC-1155 代币来说，必须触发的转向零地址事件意味着消耗实际上会把代币从持有者的余额中移除，因此销毁（burn）是自然的实现方式。
- 作者指出它可以改造用于 [ERC-20](/eip-20/) 之类的同质化代币，但该接口是以 NFT 为出发点设计的。


---

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