EIPs Explained

ERC-5216 · Last Call(최종 검토) · ERC · 2022-07-11

ERC-5216: ERC-1155 Allowance Extension

ERC-1155에 ERC-20 방식의 approve(operator, id, amount)와 allowance()를 추가해, 마켓플레이스가 모든 토큰이 아니라 하나의 토큰 ID 중 제한된 수량만 옮기도록 허용할 수 있게 한다.

어떤 문제를 해결하나요?

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

ERC-5216은 ERC-20의 allowance 모델을 가져와, 소유자가 딱 필요한 만큼, 즉 특정 id의 특정 amount만 승인할 수 있게 합니다.

어떻게 동작하나요?

컨트랙트는 ERC-1155 위에 IERC5216을 구현합니다.

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).

샘플 코드

OpenZeppelin의 ERC-1155(v5) 위에 얹은 핵심 로직으로, 설명용입니다.

// 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을 확인하세요.

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

상세 정보

저자
Iván Mañús (@ivanmmurciaua), Juan Carlos Cantó (@EscuelaCryptoES)
상태
Last Call(최종 검토)
유형
Standards Track / ERC
작성일
2022-07-11
의존하는 제안
이 제안에 의존하는 제안

기계 판독용 데이터: Markdown · JSON