IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 区块链 -> EIP1155 erc1155 多代币标准介绍 -> 正文阅读

[区块链]EIP1155 erc1155 多代币标准介绍

作者:recommend-item-box-tow

参考链接

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 _operator  The address which initiated the transfer (i.e. msg.sender)
        @param _from      The address which previously owned the token
        @param _id        The ID of the token being transferred
        @param _value     The amount of tokens being transferred
        @param _data      Additional 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 _operator  The address which initiated the batch transfer (i.e. msg.sender)
        @param _from      The address which previously owned the token
        @param _ids       An array containing ids of each token being transferred (order and length must match _values array)
        @param _values    An array containing amounts of each token being transferred (order and length must match _ids array)
        @param _data      Additional 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"
		}
	}
}

也可以加上国际化… 略

附上星鲨的数据
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"
    }
}
  区块链 最新文章
盘点具备盈利潜力的几大加密板块,以及潜在
阅读笔记|让区块空间成为商品,打造Web3云
区块链1.0-比特币的数据结构
Team Finance被黑分析|黑客自建Token“瞒天
区块链≠绿色?波卡或成 Web3“生态环保”标
期货从入门到高深之手动交易系列D1课
以太坊基础---区块验证
进入以太坊合并的五个数字
经典同态加密算法Paillier解读 - 原理、实现
IPFS/Filecoin学习知识科普(四)
上一篇文章      下一篇文章      查看所有文章
加:2022-02-24 15:20:19  更:2022-02-24 15:21:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/25 22:41:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码