---
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/ja/eip-5216/
lang: ja
tldr: ERC-1155 に ERC-20 風の approve(operator, id, amount) と allowance() を追加し、マーケットプレイスにすべてではなく、1 つのトークン 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() を追加し、マーケットプレイスにすべてではなく、1 つのトークン ID の限られた数量だけを動かす権限を与えられるようにする。

## 何を解決するのか

[ERC-1155](/eip-1155/) の承認の仕組みは `setApprovalForAll(operator, true)` だけです。これは、そのコントラクトで保有する**すべて**のトークン（全 ID、任意の数量）をオペレーターが動かせるようにします。マーケットプレイスに 1 つのアイテムを出品するだけでも、コレクション全体への無制限のアクセスを与えなければなりません。バグや侵害されたオペレーターがいれば、すべてを抜き取られかねません。

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` は、1 つの ID に対するオペレーターのアローワンスを（加算ではなく）設定し、`Approval` を発行します。オペレーターを呼び出し元自身にすることはできません。
- `safeTransferFrom` は、呼び出し元がその ID について十分なアローワンスを持っていれば成功しなければならず、転送した量を**差し引か**なければなりません。ただし、呼び出し元が `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 以外のアローワンスを別の 0 以外の値に変更する操作はフロントランされる可能性があります。先に 0 に設定し、使っていないアローワンスは取り消してください。
- `setApprovalForAll` は依然としてすべてに優先します。オペレーターが何を動かせるかを監査するときは、`isApprovedForAll` を確認してください。


---

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