EIPs Explained

ERC-5216 · Last Call · ERC · 2022-07-11

ERC-5216: ERC-1155 Allowance Extension

Adds ERC-20-style approve(operator, id, amount) and allowance() to ERC-1155, so you can let a marketplace move a limited number of one token id instead of everything.

What problem does it solve?

ERC-1155 has only one approval mechanism: setApprovalForAll(operator, true), which lets the operator move every token you hold in that contract, of every id, in any amount. To list a single item on a marketplace you must give the marketplace unlimited access to the whole collection. A bug or a compromised operator can then drain everything.

ERC-5216 borrows the allowance model from ERC-20 so owners can approve exactly what is needed: a given amount of a given id.

How it works

The contract implements IERC5216 on top of ERC-1155:

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 sets (not adds to) the operator's allowance for one id and emits Approval. The operator cannot be the caller.
  • safeTransferFrom must succeed when the caller has a sufficient allowance for that id, and must deduct the transferred amount, unless the caller is approved via setApprovalForAll (which still means unlimited).
  • safeBatchTransferFrom must check the allowance for every id in the batch.
  • supportsInterface(0x1be07d74) returns true (ERC-165).

Sample code

Core logic layered on OpenZeppelin's ERC-1155 (v5), for illustration:

// 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)
}

Things to know

  • Status: Last Call (deadline in 2022, never moved to Final). The interface may still be considered unsettled.
  • Marketplaces only benefit if they actually request per-id allowances; many still ask for setApprovalForAll.
  • Like ERC-20, changing a non-zero allowance to another non-zero value can be front-run. Set it to 0 first, and revoke unused allowances.
  • setApprovalForAll still overrides everything. Check isApprovedForAll when auditing what an operator can move.

#erc-1155 #approvals #allowance #nft #security

Details

Authors
Iván Mañús (@ivanmmurciaua), Juan Carlos Cantó (@EscuelaCryptoES)
Status
Last Call
Type
Standards Track / ERC
Created
2022-07-11
Requires
Required by

Machine-readable: Markdown · JSON