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 开源地址。


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/195737.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从微软官网下载系统镜像重装的方法

一、制作系统镜像介质U盘 1、在一台能够正常进入系统的电脑中登录以下网址&#xff1a;https://www.microsoft.com/zh-cn/software-download/windows10&#xff0c; 点击立即下载工具。 2、在下载完成后&#xff0c;双击打开&#xff0c;选择为另一台电脑创建安装介质&#xf…

controller能接收到数据有数据但是前端无法显示数据

又是制作系统时遇到的问题。只是想做个查询商品的页面&#xff0c;结果弄了一天&#xff0c;在网上各种查问题&#xff0c;各种解决办法用在我的代码上&#xff0c;换了无数种关键词搜索终于找到了一条成功解决了问题。 问题描述&#xff1a; 事情是这样的&#xff1a;我要写一…

云原生之深入解析Kubernetes策略引擎对比:OPA/Gatekeeper与Kyverno

一、前言 ① Kubernetes 策略 Kubernetes 的 Pod Security Policy&#xff0c;正如其名字所暗示的&#xff0c;仅是针对 Pod 工作的&#xff0c;是一种用来验证和控制 Pod 及其属性的机制。另外 PSP 只能屏蔽非法 Pod 的创建&#xff0c;无法执行任何补救/纠正措施。而 Gatek…

如何将Java条码Dynamsoft Barcode Reader集成到命令行、GUI和Web应用程序中

Dynamsoft Barcode Reader SDK一款多功能的条码读取控件&#xff0c;只需要几行代码就可以将条码读取功能嵌入到Web或桌面应用程序。这可以节省数月的开发时间和成本。能支持多种图像文件格式以及从摄像机或扫描仪获取的DIB格式。使用Dynamsoft Barcode Reader SDK&#xff0c;…

实验案例二:多表查询

1、表联接类型。 表联接类型可以分为内联接&#xff0e;外联接和交叉联接等。 1&#xff0e;内联接。 内联接〈 inner join&#xff09;是最常用的-一-种联接方式&#xff0c;只返回两个数据集合之间匹配关系的行&#xff0c;将位于两个互相交叉的数据集合中重叠部分以内的数…

ROS第一个程序——helloworld

目录 一、工作空间的创建 1.创建工作空间并初始化 2.进入 src 创建 ros 包并添加依赖 二、C实现helloworld C源码实现 编辑 ros 包下的 Cmakelist.txt文件 进入工作空间目录并编译 执行 三、python实现helloworld 进入 ros 包添加 scripts 目录并编辑 python 文件 …

推荐6个AI相关开源项目

1.GPT 学术优化 - 一个专注优化论文的 AI 工具 &#x1f310;开源地址&#xff1a;https://github.com/binary-husky/gpt_academic &#x1f5a5;️体验地址&#xff1a;https://huggingface.co/spaces/qingxu98/gpt-academic &#x1f4c4;该工具可以为你的论文提供一键润色…

flink安装与配置-脚本一键安装(超简单)

文章目录 前言使用shell脚本一键安装1. 复制脚本2. 增加执行权限3. 执行脚本4. 加载用户环境变量5. 浏览器访问 总结 前言 本文介绍了使用shell脚本一键安装和配置Apache Flink单机版的方法。通过复制并执行提供的安装脚本&#xff0c;可以自动下载、安装和配置Flink。脚本会检…

ZYNQ_project:HDMI

实验目标&#xff1a;先显示彩条&#xff0c;通过uart串口传输100x100 大小图片像素信息&#xff0c;然后开始弹跳显示该图片在显示器上。 HDMI 是新一代的多媒体接口标准&#xff0c; 英文全称是 High-Definition Multimedia Interface&#xff0c; 即高清多媒体接口。 它能够…

录视频人不在电脑旁,怎么设置定时关机

如果你平常工作比较忙&#xff0c;或者要录制的视频/音频文件需要很长时间&#xff0c;最好选择预约录制&#xff0c;这样可使录屏软件自动开始和停止录制&#xff0c;并且定时关机。此外&#xff0c;你还可以设置保存录制文件、关闭录屏软件。是不是听起来很感兴趣&#xff1f…

进程程序替换和shell实现

先前fork说创建子进程执行代码&#xff0c;如何让子进程执行和父进程完全不一样的代码?程序替换。 一 单进程替换演示 1 execl函数使用 最近转到在vs code下写代码&#xff0c;之前也在xhell下用过execl函数&#xff0c;所以才想写篇博客总结总结&#xff0c;没想到在vs code…

Docker Compose简单入门

Docker Compose 简介 Docker Compose 是一个编排多容器发布式部署的工具&#xff0c;提供命令集管理容器化应用的完整开发周期&#xff0c;包括服务构建&#xff0c;启动和停止。 Docker Compose 真正的作用是在一个文件&#xff08;docker-compose.yml&#xff09;中定义并运…

Arthas安装及简单使用

一. 背景介绍 Arthas 是 Alibaba 在 2018 年 9 月开源的 Java 诊断工具。支持 JDK6&#xff0c; 采用命令行交互模式&#xff0c;提供 Tab 自动补全&#xff0c;可以方便的定位和诊断线上程序运行问题。得益于 Arthas 强大且丰富的功能&#xff0c;让 Arthas 能做很多的事情&a…

超声波眼镜清洗机是智商税吗?2023年超声波清洗机比较不错推荐

随着科技的日新月异&#xff0c;我们生活中充满了各种各样的智能设备。其中&#xff0c;超声波清洗机以其独特的清洗能力&#xff0c;逐渐走进我们的生活。然而&#xff0c;对于很多人来说&#xff0c;超声波清洗机还是一个相对陌生的概念。那么&#xff0c;超声波清洗机到底是…

P9 链表 清空链表|销毁链表

目录 前言 01销毁链表 02 清空链表 测试代码 前言 &#x1f3ac; 个人主页&#xff1a;ChenPi &#x1f43b;推荐专栏1: 《C_ChenPi的博客-CSDN博客》✨✨✨ &#x1f525; 推荐专栏2: 《Linux C应用编程&#xff08;概念类&#xff09;_ChenPi的博客-CSDN博客》✨✨✨ …

Markdown语法入门与进阶指南

一、Markdown简介 Markdown是一种轻量级标记语言&#xff0c;创始人为约翰格鲁伯&#xff08;john Gruber&#xff09;。它允许人们使用易读易写的纯文本格式编写文档&#xff0c;然后转换成有效的XHTML&#xff08;或者HTML&#xff09;文档。这种语言吸收了很多在电子邮件中…

Spring Session介绍

Spring SessionSession储存到Redis1&#xff1a;添加依赖2&#xff1a;配置信息3:Spring Session存String3:Spring Session存对象 项目改造-Redis储存Session Spring Session Spring Session储存在Redis和取的执行流程&#xff1a; 1&#xff1a;request.getSession() 方法时&…

地隔离放大器集成电路芯片D3121,低噪声低失真双通道且外接电容小,能有效消除由线 路电阻所引起的问题及噪声

D3121 是一块对地能动冲放大器集成电路&#xff0c;该电路能有效消除由线 路电阻所引起的问题及噪声。所需外围电容小&#xff0c;便于设计时小型化的同 时可靠性不降低。广泛应用于车载音响系统内。 D3121 系列采用 DIP8 、 SOP8 、 SIP8 的封装形式封装。 主要特点&#x…

Qt应用开发(Quick篇)——矩形模块 Rectangle

一、前言 矩形模块用于用纯色或渐变填充区域&#xff0c;或者提供一个矩形边框。 二、外观 每个矩形项都可以使用使用color属性指定的纯填充色、使用gradient类型定义并使用gradient属性设置的渐变来绘制。如果同时指定了颜色和渐变效果&#xff0c;则只会生效渐变效果。 通过…

Python:核心知识点整理大全2-笔记

在本章中&#xff0c;你将学习可在Python程序中使用的各种数据&#xff0c;还将学 习如何将数据存储到变量中&#xff0c;以及如何在程序中使用这些变量。 2.1 运行 hello_world.py 时发生的情况 运行hello_world.py时&#xff0c;Python都做了些什么呢&#xff1f;下面来深入…