---
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/ko/eip-5216/
lang: ko
tldr: ERC-1155에 ERC-20 방식의 approve(operator, id, amount)와 allowance()를 추가해, 마켓플레이스가 모든 토큰이 아니라 하나의 토큰 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()를 추가해, 마켓플레이스가 모든 토큰이 아니라 하나의 토큰 ID 중 제한된 수량만 옮기도록 허용할 수 있게 한다.

## 어떤 문제를 해결하나요?

[ERC-1155](/eip-1155/)의 승인 메커니즘은 `setApprovalForAll(operator, true)` 하나뿐입니다. 이는 오퍼레이터가 그 컨트랙트에서 보유한 **모든** 토큰(모든 ID, 모든 수량)을 옮길 수 있게 합니다. 마켓플레이스에 아이템 하나를 올리려 해도 컬렉션 전체에 대한 무제한 접근 권한을 줘야 합니다. 버그나 탈취된 오퍼레이터가 있으면 전부 털릴 수 있습니다.

ERC-5216은 [ERC-20](/eip-20/)의 allowance 모델을 가져와, 소유자가 딱 필요한 만큼, 즉 특정 `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에 대한 오퍼레이터의 allowance를 (더하는 것이 아니라) 설정하고 `Approval`을 발생시킵니다. 오퍼레이터가 호출자 자신일 수는 없습니다.
- `safeTransferFrom`은 호출자가 해당 id에 대해 충분한 allowance를 가진 경우 성공해야 하며, 전송한 수량을 **차감**해야 합니다. 다만 호출자가 `setApprovalForAll`로 승인된 경우는 예외이며, 이 경우는 여전히 무제한을 뜻합니다.
- `safeBatchTransferFrom`은 배치에 있는 모든 id의 allowance를 확인해야 합니다.
- `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별 allowance를 요청해야만 이점이 있습니다. 많은 곳이 여전히 `setApprovalForAll`을 요구합니다.
- ERC-20과 마찬가지로, 0이 아닌 allowance를 다른 0이 아닌 값으로 바꾸면 프런트러닝당할 수 있습니다. 먼저 0으로 설정하고, 쓰지 않는 allowance는 철회하세요.
- `setApprovalForAll`은 여전히 모든 것을 덮어씁니다. 오퍼레이터가 무엇을 옮길 수 있는지 감사할 때는 `isApprovedForAll`을 확인하세요.


---

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