solidity实现ERC1155多代币标准

2023-12-04 17:44

本文主要是介绍solidity实现ERC1155多代币标准,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1、NFT - 维基百科
  • 2、IERC1155MetadataURI
  • 3、IERC1155Receiver
  • 4、IERC1155
  • 5、ERC1155
  • 6、NFT1155
  • 7、开源地址

1、NFT - 维基百科

ERC-1155 标准于2018年6月由Witek Radomski、Andrew Cooke、Philippe Castonguay、James Therien、Eric Binet及Ronan Sandford提出。此标准可针对单一应用合约,提供同质代币、非同质代币(用ERC-1155建构ERC-721资产)及其他结构类型(例如半同质代币,semi-fungible tokens)的多元组合。相较于ERC-721每创立一个代币ID就对应一个单独智能合约资产,在ERC-1155多元代币标准下,每个代币ID可以代表一系列不同类型的资产,并附加量化区块以标示钱包中各类型资产的数量;同类型资产可以互换,要转让多少数量也可以。
ERC-1155 标准下的NFT结构上更有弹性:拥有元数据、供应量与其他属性;新功能包括:可一次传输多种代币类型以节省交易成本、也可同时进行多个代币交易(托管/原子交换)而无需逐一批准每个合约、可在单一应用合约中注明并混搭多种FT、NFT及SFT。

2、IERC1155MetadataURI

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;// ERC1155的可选接口,加入了uri()函数查询元数据
interface IERC1155MetadataURI  {// 返回第`id`种类代币的URIfunction uri(uint256 id) external view returns (string memory);
}

3、IERC1155Receiver

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;import "./IERC165.sol";interface IERC1155Receiver is IERC165 {// 接受ERC1155安全转账`safeTransferFrom`     function onERC1155Received(address operator,address from,uint256 id,uint256 value,bytes calldata data) external returns (bytes4);// 接受ERC1155批量安全转账`safeBatchTransferFrom` function onERC1155BatchReceived(address operator,address from,uint256[] calldata ids,uint256[] calldata values,bytes calldata data) external returns (bytes4);
}

4、IERC1155

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;import "./IERC165.sol";// https://eips.ethereum.org/EIPS/eip-1155[EIP].
interface IERC1155 is IERC165 {// 单类代币转账事件event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);// 批量代币转账事件, ids和values为转账的代币种类和数量数组event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values);// 批量授权事件, 当`account`将所有代币授权给`operator`时释放event ApprovalForAll(address indexed account, address indexed operator, bool approved);// 当`id`种类的代币的URI发生变化时释放,`value`为新的URIevent URI(string value, uint256 indexed id);// 持仓查询,返回`account`拥有的`id`种类的代币的持仓量function balanceOf(address account, uint256 id) external view returns (uint256);// 批量持仓查询,`accounts`和`ids`数组的长度要想等。function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids) external view returns (uint256[] memory);// 批量授权,将调用者的代币授权给`operator`地址。释放{ApprovalForAll}事件.function setApprovalForAll(address operator, bool approved) external;// 批量授权查询,如果授权地址`operator`被`account`授权,则返回`true`function isApprovedForAll(address account, address operator) external view returns (bool);// 安全转账,将`amount`单位`id`种类的代币从`from`转账给`to`.  释放{TransferSingle}事件.function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes calldata data) external;// 批量安全转账, 释放{TransferBatch}事件function safeBatchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata amounts, bytes calldata data) external;
}

5、ERC1155

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;import "./IERC1155.sol";
import "./IERC1155Receiver.sol";
import "./IERC1155MetadataURI.sol";
import "./Address.sol";
import "./Strings.sol";
import "./IERC165.sol";// ERC1155多代币标准 https://eips.ethereum.org/EIPS/eip-1155
contract ERC1155 is IERC165, IERC1155, IERC1155MetadataURI {using Address for address; using Strings for uint256; // Token名称string public name;// Token代号string public symbol;// 代币种类id 到 账户account 到 余额balances 的映射mapping(uint256 => mapping(address => uint256)) private _balances;// address 到 授权地址 的批量授权映射mapping(address => mapping(address => bool)) private _operatorApprovals;// 构造函数,初始化`name` 和`symbol`, uri_constructor(string memory name_, string memory symbol_) {name = name_;symbol = symbol_;}function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {returninterfaceId == type(IERC1155).interfaceId ||interfaceId == type(IERC1155MetadataURI).interfaceId ||interfaceId == type(IERC165).interfaceId;}// 持仓查询 实现IERC1155的balanceOf,返回account地址的id种类代币持仓量。function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {require(account != address(0), "ERC1155: address zero is not a valid owner");return _balances[id][account];}// @dev 批量持仓查询   function balanceOfBatch(address[] memory accounts, uint256[] memory ids) public view virtual override returns (uint256[] memory) {require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");uint256[] memory batchBalances = new uint256[](accounts.length);for (uint256 i = 0; i < accounts.length; ++i) {batchBalances[i] = balanceOf(accounts[i], ids[i]);}return batchBalances;}//  批量授权,调用者授权operator使用其所有代币function setApprovalForAll(address operator, bool approved) public virtual override {require(msg.sender != operator, "ERC1155: setting approval status for self");_operatorApprovals[msg.sender][operator] = approved;emit ApprovalForAll(msg.sender, operator, approved);}// 查询批量授权.function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {return _operatorApprovals[account][operator];}// 安全转账,将`amount`单位的`id`种类代币从`from`转账到`to`function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) public virtual override {address operator = msg.sender;// 调用者是持有者或是被授权require(from == operator || isApprovedForAll(from, operator),"ERC1155: caller is not token owner nor approved");require(to != address(0), "ERC1155: transfer to the zero address");// from地址有足够持仓uint256 fromBalance = _balances[id][from];require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");// 更新持仓量unchecked {_balances[id][from] = fromBalance - amount;}_balances[id][to] += amount;// 释放事件emit TransferSingle(operator, from, to, id, amount);// 安全检查_doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);    }// 批量安全转账,将`amounts`数组单位的`ids`数组种类代币从`from`转账到`to`function safeBatchTransferFrom(address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public virtual override {address operator = msg.sender;// 调用者是持有者或是被授权require(from == operator || isApprovedForAll(from, operator),"ERC1155: caller is not token owner nor approved");require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");require(to != address(0), "ERC1155: transfer to the zero address");// 通过for循环更新持仓  for (uint256 i = 0; i < ids.length; ++i) {uint256 id = ids[i];uint256 amount = amounts[i];uint256 fromBalance = _balances[id][from];require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");unchecked {_balances[id][from] = fromBalance - amount;}_balances[id][to] += amount;}emit TransferBatch(operator, from, to, ids, amounts);// 安全检查_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);    }// 锻造function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal virtual {require(to != address(0), "ERC1155: mint to the zero address");address operator = msg.sender;_balances[id][to] += amount;emit TransferSingle(operator, address(0), to, id, amount);_doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, data);}// 批量铸造function _mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) internal virtual {require(to != address(0), "ERC1155: mint to the zero address");require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");address operator = msg.sender;for (uint256 i = 0; i < ids.length; i++) {_balances[ids[i]][to] += amounts[i];}emit TransferBatch(operator, address(0), to, ids, amounts);_doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);}// 销毁function _burn(address from, uint256 id, uint256 amount) internal virtual {require(from != address(0), "ERC1155: burn from the zero address");address operator = msg.sender;uint256 fromBalance = _balances[id][from];require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");unchecked {_balances[id][from] = fromBalance - amount;}emit TransferSingle(operator, from, address(0), id, amount);}// 批量销毁function _burnBatch(address from, uint256[] memory ids, uint256[] memory amounts) internal virtual {require(from != address(0), "ERC1155: burn from the zero address");require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");address operator = msg.sender;for (uint256 i = 0; i < ids.length; i++) {uint256 id = ids[i];uint256 amount = amounts[i];uint256 fromBalance = _balances[id][from];require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");unchecked {_balances[id][from] = fromBalance - amount;}}emit TransferBatch(operator, from, address(0), ids, amounts);}// 安全转账检查function _doSafeTransferAcceptanceCheck( address operator, address from, address to,uint256 id, uint256 amount, bytes memory data) private {if (to.isContract()) {try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {if (response != IERC1155Receiver.onERC1155Received.selector) {revert("ERC1155: ERC1155Receiver rejected tokens");}} catch Error(string memory reason) {revert(reason);} catch {revert("ERC1155: transfer to non-ERC1155Receiver implementer");}}}// ERC1155的批量安全转账检查function _doSafeBatchTransferAcceptanceCheck( address operator, address from, address to,uint256[] memory ids, uint256[] memory amounts, bytes memory data) private {if (to.isContract()) {try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (bytes4 response) {if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {revert("ERC1155: ERC1155Receiver rejected tokens");}} catch Error(string memory reason) {revert(reason);} catch {revert("ERC1155: transfer to non-ERC1155Receiver implementer");}}}// 返回id种类代币的uri,存储metadata,类似ERC721的tokenURI.function uri(uint256 id)  public view virtual override returns (string memory) {string memory baseURI = _baseURI();return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, id.toString())) : "";}// 计算{uri}的BaseURI,uri就是把baseURI和tokenId拼接在一起,需要开发重写.function _baseURI() internal view virtual returns (string memory) {return "";}
}

6、NFT1155

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;import "./ERC1155.sol"; contract NFT1155 is ERC1155 {uint256 constant MAX_ID = 10000;constructor() ERC1155("NFT1155", "NFT1155"){}function _baseURI() internal pure override returns (string memory) {return "ipfs://QmcPGA3kecSMfHcv8i6wpgB2RkPA1FSVqu6AZY3rKNmxuj/";}function mint(address to, uint256 id, uint256 amount) external {require(id < MAX_ID, "id overflow");_mint(to, id, amount, "");}function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts) external {for(uint256 i = 0; i < ids.length;i++){require(ids[i] < MAX_ID, "id overflow");}_mintBatch(to, ids, amounts, "");}
}

测试运行:
在这里插入图片描述

7、开源地址

gitee 开源地址。


这篇关于solidity实现ERC1155多代币标准的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/454345

相关文章

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

Nginx更新SSL证书的实现步骤

《Nginx更新SSL证书的实现步骤》本文主要介绍了Nginx更新SSL证书的实现步骤,包括下载新证书、备份旧证书、配置新证书、验证配置及遇到问题时的解决方法,感兴趣的了解一下... 目录1 下载最新的SSL证书文件2 备份旧的SSL证书文件3 配置新证书4 验证配置5 遇到的http://www.cppc

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

SpringBoot整合 Quartz实现定时推送实战指南

《SpringBoot整合Quartz实现定时推送实战指南》文章介绍了SpringBoot中使用Quartz动态定时任务和任务持久化实现多条不确定结束时间并提前N分钟推送的方案,本文结合实例代码给大... 目录前言一、Quartz 是什么?1、核心定位:解决什么问题?2、Quartz 核心组件二、使用步骤1