以太坊|EIP1155 erc1155 多代币标准介绍


文章目录

      • 参考链接
      • erc1155介绍,以及和erc721的区别
      • erc1155 标准
      • ERC-1155 TokenReceiver
      • Metadata uri
        • ERC-1155 元数据 URI JSON 模式

参考链接
EIP-1155:多代币标准, https://eips.ethereum.org/EIPS/eip-1155
erc1155介绍,以及和erc721的区别
erc721是非同质化代币,每个id都是一个单一的不可替代的索引
erc1155是非同质化代币和同质化代币混合的, 可兼容的。
erc1155兼容721
先看转账方法
function safeTransferFrom( address from, address to, uint256 id, uint256 amount,//amount, 下面的标准中对应的是_value bytes memory data ) public

转账中除了id,还有一个 amount/value, 这个实际是对应一个数量,也就是同一个id可以有多个数量(对等erc20的同质化代币)
引用Harry的说明
1155相当于一个游戏道具生成器,可以生成20把斧头,100个剑 斧头和斧头之间是同质化的 斧头和剑是非同质化的

如果斧头和斧头都有各自属性,就使用不同的id, id对应的amount一直是1,就又是非同质化代币了
除了以上的说明
在eip-1155官方标准介绍里面,最下面还有一段关于拆分ID的策略
_id任何 ERC-1155 函数中uint256参数的前 128 位可能代表基本令牌 ID,而后 128 位可能代表不可替代的索引以使其唯一。
具体查看官方链接中 Non-Fungible Tokens
其次1155相比721增加了批量功能
erc1155 标准
  • 实现 ERC-1155 标准的智能合约必须实现ERC1155接口中的所有功能。
  • 实现 ERC-1155 标准的智能合约必须实现 ERC-165 supportsInterface函数,并且返回 0xd9b67a26 表示 true, 否则不支持
pragma solidity ^0.5.9; /** @title ERC-1155 Multi Token Standard @dev See https://eips.ethereum.org/EIPS/eip-1155 Note: The ERC-165 identifier for this interface is 0xd9b67a26. */ interface ERC1155 /* is ERC165 */ { event TransferSingle(address indexed _operator, address indexed _from, address indexed _to, uint256 _id, uint256 _value); event TransferBatch(address indexed _operator, address indexed _from, address indexed _to, uint256[] _ids, uint256[] _values); event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved); event URI(string _value, uint256 indexed _id); //转单个,对应的是事件是 TransferSingle function safeTransferFrom(address _from, address _to, uint256 _id, uint256 _value, bytes calldata _data) external; //转多个,对应的事件是 TransferBatch function safeBatchTransferFrom(address _from, address _to, uint256[] calldata _ids, uint256[] calldata _values, bytes calldata _data) external; function balanceOf(address _owner, uint256 _id) external view returns (uint256); function balanceOfBatch(address[] calldata _owners, uint256[] calldata _ids) external view returns (uint256[] memory); function setApprovalForAll(address _operator, bool _approved) external; function isApprovedForAll(address _owner, address _operator) external view returns (bool); }

ERC-1155 TokenReceiver
如果接收erc1155代币的是 合约地址,该合约必须实现ERC1155TokenReceiver的接口,常见场景(NFT挖矿,NFT交易)
bytes4 constant RECEIVED_1155 = 0xf23a6e61; //`bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` bytes4 constant RECEIVED_1155BATCH = 0xbc197c81; //`bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`

pragma solidity ^0.5.9; /** Note: The ERC-165 identifier for this interface is 0x4e2312e0. */ interface ERC1155TokenReceiver { /** @notice Handle the receipt of a single ERC1155 token type. @dev An ERC1155-compliant smart contract MUST call this function on the token recipient contract, at the end of a `safeTransferFrom` after the balance has been updated. This function MUST return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` (i.e. 0xf23a6e61) if it accepts the transfer. This function MUST revert if it rejects the transfer. Return of any other value than the prescribed keccak256 generated value MUST result in the transaction being reverted by the caller. @param _operatorThe address which initiated the transfer (i.e. msg.sender) @param _fromThe address which previously owned the token @param _idThe ID of the token being transferred @param _valueThe amount of tokens being transferred @param _dataAdditional data with no specified format @return`bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` */ function onERC1155Received(address _operator, address _from, uint256 _id, uint256 _value, bytes calldata _data) external returns(bytes4); /** @notice Handle the receipt of multiple ERC1155 token types. @dev An ERC1155-compliant smart contract MUST call this function on the token recipient contract, at the end of a `safeBatchTransferFrom` after the balances have been updated. This function MUST return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` (i.e. 0xbc197c81) if it accepts the transfer(s). This function MUST revert if it rejects the transfer(s). Return of any other value than the prescribed keccak256 generated value MUST result in the transaction being reverted by the caller. @param _operatorThe address which initiated the batch transfer (i.e. msg.sender) @param _fromThe address which previously owned the token @param _idsAn array containing ids of each token being transferred (order and length must match _values array) @param _valuesAn array containing amounts of each token being transferred (order and length must match _ids array) @param _dataAdditional data with no specified format @return`bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` */ function onERC1155BatchReceived(address _operator, address _from, uint256[] calldata _ids, uint256[] calldata _values, bytes calldata _data) external returns(bytes4); }

  • _operator 对应的是msg.sender
  • _from 对应的是 这个代币的前一个所有者
  • _to 对应的是接收地址
Metadata uri
建议格式{id}
  • id是[0-9a-f]中的数据,不要前缀0x
  • 如果有必要的话,还可以统一使用十六进制,长度64位,不足的前面补0,类似事件中的数据
例子
如果客户端指的令牌ID 314592 / 0x4CCE0
https://token-cdn-domain/{id}.json
将被替换
https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json
pragma solidity ^0.5.9; /** Note: The ERC-165 identifier for this interface is 0x0e89341c. */ interface ERC1155Metadata_URI { /** @notice A distinct Uniform Resource Identifier (URI) for a given token. @dev URIs are defined in RFC 3986. The URI MUST point to a JSON file that conforms to the "ERC-1155 Metadata URI JSON Schema". @return URI string */ function uri(uint256 _id) external view returns (string memory); }

ERC-1155 元数据 URI JSON 模式
{ "title": "Token Metadata", "type": "object", "properties": { "name": { "type": "string", "description": "Identifies the asset to which this token represents" }, "decimals": { "type": "integer", "description": "The number of decimal places that the token amount should display - e.g. 18, means to divide the token amount by 1000000000000000000 to get its user representation." }, "description": { "type": "string", "description": "Describes the asset to which this token represents" }, "image": { "type": "string", "description": "A URI pointing to a resource with mime type image/* representing the asset to which this token represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive." }, "properties": { "type": "object", "description": "Arbitrary properties. Values may be strings, numbers, object or arrays." } } }

下面是 ERC-1155 元数据 JSON 文件的示例。properties 数组为特定于令牌的显示属性和元数据提出了一些 SUGGESTED 格式。
{ "name": "Asset Name", "description": "Lorem ipsum...", "image": "https:\/\/s3.amazonaws.com\/your-bucket\/images\/{id}.png", "properties": { "simple_property": "example value", "rich_property": { "name": "Name", "value": "123", "display_value": "123 Example Value", "class": "emphasis", "css": { "color": "#ffffff", "font-weight": "bold", "text-decoration": "underline" } }, "array_property": { "name": "Name", "value": [1,2,3,4], "class": "emphasis" } } }

也可以加上国际化… 略
【以太坊|EIP1155 erc1155 多代币标准介绍】附上星鲨的数据
https://public.nftstatic.com/static/nft/BSC/BMBSTARSHARKS/207000003926
{ "image": "https://public.nftstatic.com/static/nft/res/7356086731594c0fb004870226e12965.jpeg", "name": "Level-2 SHARK Ticket", "description": "It can be withdrawn to the Starsharks Marketplace and exchanged for a Level-2 SHARK. Two level-1 sharks can be synthesized to make a level-2 shark.", "extend_info": { "videoUrl": "https://public.nftstatic.com/static/nft/res/0e94b132005d455cb86d675c7f368a87.mp4" } }

    推荐阅读