文章前言
本篇文章将对ERC-1155标准规范进行简单介绍,在介绍之前我们先来看一下之前的ERC-20、ERC-721、ERC-777都解决了什么问题,主要应用与那些场景:
ERC-20
基于ERC-20标准开发的代币合约也被统称为\\”同质化代币(Fungible token,缩写为FT)\\”,其主要的一个特性就是可以无限细分为10^18份,目前大多数ICO使用的代币都是基于ERC-20开发的代币。
ERC-721
基于ERC-721标准开发的代币合约被统称为\\”非同质化代币(Non-Fungible Tokens,缩写为NFT)\\”,ERC-721代币相较于ERC-20代币最大的区别就是不可分割性和唯一性,其Token的最小单位为1,且每个Token对不同的用户都有不同的价值含义。
ERC-721代表了对资产的所有权,为物品归属性记录提供的可能性,例如现实世界中的文学作品,收藏品等等,还有金融交易中的借贷交易记录等信息。
ERC-777
ERC-777是对ERC-20协议的升级,本质上还是同质化代币(Fungible token,缩写为FT),ERC-777引入了运营商的概念来解决授权给第三方账户或智能合约进行转账操作的问题,同时采用接口send(dest,value,data)发送代币,先前兼容,同时在转账时为用户提供了可以携带额外信息的功能,也提供了转账通知机制。
ERC-1155
ERC-1155标准规范可以看做是ERC-721标准的一个升级版本,ERC-1155与ERC-721协议对比有以下方面的升级:
-
ERC-721仅限于不可替代品,而ERC-1155允许替代品和非替代品共存,同时允许可替代token可以被\\”转换\\”为不可替代Token,反之亦然
-
ERC-721需要为每种新型Token部署新的智能合约,而ERC-1155可以在单个智能合约中部署无限代币类型
-
ERC-721将代币发送到不支持ERC-721协议的合约时易导致代币丢失,ERC-1155中如果接收方不支持ERC-1152协议将立刻归还代币
-
ERC-721仅支持静态元数据,每个代币ID必须具有由智能合约或管理的元数据URI,而ERC-1155可以指向无数个代币URI,且无需再链上存储任何其他数据,甚至可以用于执行Web服务,该服务为数据库中的每个代币托管动态生成的代币JSON。
-
……
交易过程
ERC-20代币交易过程:
现有的ERC-20代币交易都需要一个多步骤流程来实现从一个Token到另一个token的转换,从下图中可以看到仅2个代币的交换就需要4次交易(ERC-20代币和ERC-721代币都是独立的合同,因此添加到交易中的每个额外代币类型都需要额外的approve步骤),更多的代币互换就要更多的步骤:
ERC-1155代币交易过程:
ERC-1155只需要两个简单的步骤就可以交换任意数量的token:
差异对比分析:
为了更好的理解交易差异性,我们打一个比方:假定你去超市购买了100块钱的多种商品,之后再结算时对于基于ERC-20与ERC-721的柜员她会拿起一件商品扫描一次,之后请你支付一次,之后再次扫描另一件商品,之后再请你支付一次,依次类推直到最后一个商品结算完,而ERC-1155的柜员她会拿起一件商品扫描一次,之后再拿起其余的商品不断扫描(类似于打包交易信息),最后算出一个总数请你支付一次即可!可以看到通过ERC-1155,仅需要使用一次交易就能一次性把多个物品发送给一个或者多个接收方,这能有效的减少Gas费用和以太坊的拥堵,从上的几点来看ERC-1155可以说是融合了ERC-20和ERC-721各自的特性和优点,而开发者可以给自己的应用创建几千种不同类型的产品,每一件产品都有自己独立的ID或者像ERC-20一样可以同质化。
transfer, approve, melt和其他交易功能都以数组为参数,这使您可以在单个事务中执行100-200个这样的操作,要转移单个项目,只需为每个数组提供一个元素,要转移两个项目,使用两个元素,依此类推。
接口列表
下面我们看一下IERC-1155中规定的ERC-1155需要实现的接口有哪些:
#FUNCTIONS
balanceOf(account, id)
balanceOfBatch(accounts, ids)
setApprovalForAll(operator, approved)
isApprovedForAll(account, operator)
safeTransferFrom(from, to, id, amount, data)
safeBatchTransferFrom(from, to, ids, amounts, data)
#EVENTS
TransferSingle(operator, from, to, id, value)
TransferBatch(operator, from, to, ids, values)
ApprovalForAll(account, operator, approved)
URI(value, id)
IERC-1155源代码如下所示:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import \\\"../../utils/introspection/IERC165.sol\\\";
/**
* @dev Required interface of an ERC1155 compliant contract, as defined in the
* https://eips.ethereum.org/EIPS/eip-1155[EIP].
*
* _Available since v3.1._
*/
interface IERC1155 is IERC165 {
/**
* @dev Emitted when `value` tokens of token type `id` are transferred from `from` to `to` by `operator`.
*/
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);
/**
* @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
* transfers.
*/
event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values);
/**
* @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
* `approved`.
*/
event ApprovalForAll(address indexed account, address indexed operator, bool approved);
/**
* @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
*
* If an {URI} event was emitted for `id`, the standard
* https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
* returned by {IERC1155MetadataURI-uri}.
*/
event URI(string value, uint256 indexed id);
/**
* @dev Returns the amount of tokens of token type `id` owned by `account`.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function balanceOf(address account, uint256 id) external view returns (uint256);
/**
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
*
* Requirements:
*
* - `accounts` and `ids` must have the same length.
*/
function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids) external view returns (uint256[] memory);
/**
* @dev Grants or revokes permission to `operator` to transfer the caller\\\'s tokens, according to `approved`,
*
* Emits an {ApprovalForAll} event.
*
* Requirements:
*
* - `operator` cannot be the caller.
*/
function setApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns true if `operator` is approved to transfer ``account``\\\'s tokens.
*
* See {setApprovalForAll}.
*/
function isApprovedForAll(address account, address operator) external view returns (bool);
/**
* @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
*
* Emits a {TransferSingle} event.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - If the caller is not `from`, it must be have been approved to spend ``from``\\\'s tokens via {setApprovalForAll}.
* - `from` must have a balance of tokens of type `id` of at least `amount`.
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
* acceptance magic value.
*/
function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes calldata data) external;
/**
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
*
* Emits a {TransferBatch} event.
*
* Requirements:
*
* - `ids` and `amounts` must have the same length.
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
* acceptance magic value.
*/
function safeBatchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata amounts, bytes calldata data) external;
}
源码解读
下面我们从OpenZeppelin官方提供的ERC-1155.sol源码进行分析ERC-1155的具体功能实现:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import \\\"./IERC1155.sol\\\";
import \\\"./IERC1155Receiver.sol\\\";
import \\\"./extensions/IERC1155MetadataURI.sol\\\";
import \\\"../../utils/Address.sol\\\";
import \\\"../../utils/Context.sol\\\";
import \\\"../../utils/introspection/ERC165.sol\\\";
/**
*
* @dev Implementation of the basic standard multi-token.
* See https://eips.ethereum.org/EIPS/eip-1155
* Originally based on code by Enjin: https://github.com/enjin/erc-1155
*
* _Available since v3.1._
*/
contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
using Address for address;
// Mapping from token ID to account balances
mapping (uint256 => mapping(address => uint256)) private _balances;
// Mapping from account to operator approvals
mapping (address => mapping(address => bool)) private _operatorApprovals;
// Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
string private _uri;
/**
* @dev See {_setURI}.
*/
constructor (string memory uri_) {
_setURI(uri_);
}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
return interfaceId == type(IERC1155).interfaceId
|| interfaceId == type(IERC1155MetadataURI).interfaceId
|| super.supportsInterface(interfaceId);
}
/**
* @dev See {IERC1155MetadataURI-uri}.
*
* This implementation returns the same URI for *all* token types. It relies
* on the token type ID substitution mechanism
* https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
*
* Clients calling this function must replace the `\\\\{id\\\\}` substring with the
* actual token type ID.
*/
function uri(uint256) public view virtual override returns (string memory) {
return _uri;
}
/**
* @dev See {IERC1155-balanceOf}.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
require(account != address(0), \\\"ERC1155: balance query for the zero address\\\");
return _balances[id][account];
}
/**
* @dev See {IERC1155-balanceOfBatch}.
*
* Requirements:
*
* - `accounts` and `ids` must have the same length.
*/
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;
}
/**
* @dev See {IERC1155-setApprovalForAll}.
*/
function setApprovalForAll(address operator, bool approved) public virtual override {
require(_msgSender() != operator, \\\"ERC1155: setting approval status for self\\\");
_operatorApprovals[_msgSender()][operator] = approved;
emit ApprovalForAll(_msgSender(), operator, approved);
}
/**
* @dev See {IERC1155-isApprovedForAll}.
*/
function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
return _operatorApprovals[account][operator];
}
/**
* @dev See {IERC1155-safeTransferFrom}.
*/
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
)
public
virtual
override
{
require(to != address(0), \\\"ERC1155: transfer to the zero address\\\");
require(
from == _msgSender() || isApprovedForAll(from, _msgSender()),
\\\"ERC1155: caller is not owner nor approved\\\"
);
address operator = _msgSender();
_beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);
uint256 fromBalance = _balances[id][from];
require(fromBalance >= amount, \\\"ERC1155: insufficient balance for transfer\\\");
_balances[id][from] = fromBalance - amount;
_balances[id][to] += amount;
emit TransferSingle(operator, from, to, id, amount);
_doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
}
/**
* @dev See {IERC1155-safeBatchTransferFrom}.
*/
function safeBatchTransferFrom(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
)
public
virtual
override
{
require(ids.length == amounts.length, \\\"ERC1155: ids and amounts length mismatch\\\");
require(to != address(0), \\\"ERC1155: transfer to the zero address\\\");
require(
from == _msgSender() || isApprovedForAll(from, _msgSender()),
\\\"ERC1155: transfer caller is not owner nor approved\\\"
);
address operator = _msgSender();
_beforeTokenTransfer(operator, from, to, ids, amounts, data);
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\\\");
_balances[id][from] = fromBalance - amount;
_balances[id][to] += amount;
}
emit TransferBatch(operator, from, to, ids, amounts);
_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
}
/**
* @dev Sets a new URI for all token types, by relying on the token type ID
* substitution mechanism
* https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
*
* By this mechanism, any occurrence of the `\\\\{id\\\\}` substring in either the
* URI or any of the amounts in the JSON file at said URI will be replaced by
* clients with the token type ID.
*
* For example, the `https://token-cdn-domain/\\\\{id\\\\}.json` URI would be
* interpreted by clients as
* `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
* for token type ID 0x4cce0.
*
* See {uri}.
*
* Because these URIs cannot be meaningfully represented by the {URI} event,
* this function emits no events.
*/
function _setURI(string memory newuri) internal virtual {
_uri = newuri;
}
/**
* @dev Creates `amount` tokens of token type `id`, and assigns them to `account`.
*
* Emits a {TransferSingle} event.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - If `account` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
* acceptance magic value.
*/
function _mint(address account, uint256 id, uint256 amount, bytes memory data) internal virtual {
require(account != address(0), \\\"ERC1155: mint to the zero address\\\");
address operator = _msgSender();
_beforeTokenTransfer(operator, address(0), account, _asSingletonArray(id), _asSingletonArray(amount), data);
_balances[id][account] += amount;
emit TransferSingle(operator, address(0), account, id, amount);
_doSafeTransferAcceptanceCheck(operator, address(0), account, id, amount, data);
}
/**
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
*
* Requirements:
*
* - `ids` and `amounts` must have the same length.
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
* acceptance magic value.
*/
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 = _msgSender();
_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
for (uint 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);
}
/**
* @dev Destroys `amount` tokens of token type `id` from `account`
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens of token type `id`.
*/
function _burn(address account, uint256 id, uint256 amount) internal virtual {
require(account != address(0), \\\"ERC1155: burn from the zero address\\\");
address operator = _msgSender();
_beforeTokenTransfer(operator, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), \\\"\\\");
uint256 accountBalance = _balances[id][account];
require(accountBalance >= amount, \\\"ERC1155: burn amount exceeds balance\\\");
_balances[id][account] = accountBalance - amount;
emit TransferSingle(operator, account, address(0), id, amount);
}
/**
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
*
* Requirements:
*
* - `ids` and `amounts` must have the same length.
*/
function _burnBatch(address account, uint256[] memory ids, uint256[] memory amounts) internal virtual {
require(account != address(0), \\\"ERC1155: burn from the zero address\\\");
require(ids.length == amounts.length, \\\"ERC1155: ids and amounts length mismatch\\\");
address operator = _msgSender();
_beforeTokenTransfer(operator, account, address(0), ids, amounts, \\\"\\\");
for (uint i = 0; i < ids.length; i++) {
uint256 id = ids[i];
uint256 amount = amounts[i];
uint256 accountBalance = _balances[id][account];
require(accountBalance >= amount, \\\"ERC1155: burn amount exceeds balance\\\");
_balances[id][account] = accountBalance - amount;
}
emit TransferBatch(operator, account, address(0), ids, amounts);
}
/**
* @dev Hook that is called before any token transfer. This includes minting
* and burning, as well as batched variants.
*
* The same hook is called on both single and batched variants. For single
* transfers, the length of the `id` and `amount` arrays will be 1.
*
* Calling conditions (for each `id` and `amount` pair):
*
* - When `from` and `to` are both non-zero, `amount` of ``from``\\\'s tokens
* of token type `id` will be transferred to `to`.
* - When `from` is zero, `amount` tokens of token type `id` will be minted
* for `to`.
* - when `to` is zero, `amount` of ``from``\\\'s tokens of token type `id`
* will be burned.
* - `from` and `to` are never both zero.
* - `ids` and `amounts` have the same, non-zero length.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
)
internal
virtual
{ }
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(to).onERC1155Received.selector) {
revert(\\\"ERC1155: ERC1155Receiver rejected tokens\\\");
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert(\\\"ERC1155: transfer to non ERC1155Receiver implementer\\\");
}
}
}
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(to).onERC1155BatchReceived.selector) {
revert(\\\"ERC1155: ERC1155Receiver rejected tokens\\\");
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert(\\\"ERC1155: transfer to non ERC1155Receiver implementer\\\");
}
}
}
function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
uint256[] memory array = new uint256[](1);
array[0] = element;
return array;
}
}
合约继承
从合约的开头我们可以看到当前合约名称为ERC1155且继承自合约Context, ERC165, IERC1155, IERC1155MetadataURI:
contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
全局变量
ERC-1155定义了以下全局变量:
using Address for address;
// Mapping from token ID to account balances
mapping (uint256 => mapping(address => uint256)) private _balances;
// Mapping from account to operator approvals
mapping (address => mapping(address => bool)) private _operatorApprovals;
// Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
string private _uri;
构造函数
ERC-1155标准规范中构造函数的实现如下所示:
/**
* @dev See {_setURI}.
*/
constructor (string memory uri_) {
_setURI(uri_);
}
这里的_setURI示意为所有token类型设置新的URI:
/**
* @dev Sets a new URI for all token types, by relying on the token type ID
* substitution mechanism
* https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
*
* By this mechanism, any occurrence of the `\\\\{id\\\\}` substring in either the
* URI or any of the amounts in the JSON file at said URI will be replaced by
* clients with the token type ID.
*
* For example, the `https://token-cdn-domain/\\\\{id\\\\}.json` URI would be
* interpreted by clients as
* `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
* for token type ID 0x4cce0.
*
* See {uri}.
*
* Because these URIs cannot be meaningfully represented by the {URI} event,
* this function emits no events.
*/
function _setURI(string memory newuri) internal virtual {
_uri = newuri;
}
基本查询
ERC-1155协议提供了以下URI查询、账户资产查询、授权转账权限查询的方法:
/**
* @dev See {IERC1155MetadataURI-uri}.
*
* This implementation returns the same URI for *all* token types. It relies
* on the token type ID substitution mechanism
* https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
*
* Clients calling this function must replace the `\\\\{id\\\\}` substring with the
* actual token type ID.
*/
function uri(uint256) public view virtual override returns (string memory) {
return _uri;
}
/**
* @dev See {IERC1155-balanceOf}.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
require(account != address(0), \\\"ERC1155: balance query for the zero address\\\");
return _balances[id][account];
}
/**
* @dev See {IERC1155-balanceOfBatch}.
*
* Requirements:
*
* - `accounts` and `ids` must have the same length.
*/
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;
}
/**
* @dev See {IERC1155-isApprovedForAll}.
*/
function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
return _operatorApprovals[account][operator];
}
授权转账
ERC-1155合约提供了以下授权转账的函数,在这里要求授权转账的账户地址和当前账户地址不能为同一地址,即自己不能授权给自己转自己的代币(因为自己本身就可以转,无需多此一举),之后进行授权转账操作,不顾这里也应该多一个地址非零的检查才更加合理一些:
/**
* @dev See {IERC1155-setApprovalForAll}.
*/
function setApprovalForAll(address operator, bool approved) public virtual override {
require(_msgSender() != operator, \\\"ERC1155: setting approval status for self\\\");
_operatorApprovals[_msgSender()][operator] = approved;
emit ApprovalForAll(_msgSender(), operator, approved);
}
转账操作
ERC-1155提供了safeTransferFrom函数用于进行转账操作:
/**
* @dev See {IERC1155-safeTransferFrom}.
*/
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
)
public
virtual
override
{
require(to != address(0), \\\"ERC1155: transfer to the zero address\\\");
require(
from == _msgSender() || isApprovedForAll(from, _msgSender()),
\\\"ERC1155: caller is not owner nor approved\\\"
);
address operator = _msgSender();
_beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);
uint256 fromBalance = _balances[id][from];
require(fromBalance >= amount, \\\"ERC1155: insufficient balance for transfer\\\");
_balances[id][from] = fromBalance - amount;
_balances[id][to] += amount;
emit TransferSingle(operator, from, to, id, amount);
_doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
}
从上述代码可以看到这里会首先检查代币接受地址是否为空,之后检查token来源账户是否是当前函数调用者或者是否是代币来源账户的授权地址,之后根据id去检索对应的token,之后根据地址去检索该token的数量,之后再检查要转移的token数量amount是否小于token持有者所拥有的token的数量,之后更新token持有者以及token接受者所拥有的token数量,最后通过emit触发交易,然后调用_doSafeBatchTransferAcceptanceCheck来检查交易是否完成,可以看到这里会校验接受token的地址是否是一个合约地址如果是一个合约地址且没有成功接收token则会通过revert进行回滚交易,这也就是之前所说的当ERC-11155向合约发送token失败后可以回滚的原因所在:
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(to).onERC1155BatchReceived.selector) {
revert(\\\"ERC1155: ERC1155Receiver rejected tokens\\\");
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert(\\\"ERC1155: transfer to non ERC1155Receiver implementer\\\");
}
}
}
当然,ERC-1155也提供了批量转账的功能——safeBatchTransferFrom
/**
* @dev See {IERC1155-safeBatchTransferFrom}.
*/
function safeBatchTransferFrom(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
)
public
virtual
override
{
require(ids.length == amounts.length, \\\"ERC1155: ids and amounts length mismatch\\\");
require(to != address(0), \\\"ERC1155: transfer to the zero address\\\");
require(
from == _msgSender() || isApprovedForAll(from, _msgSender()),
\\\"ERC1155: transfer caller is not owner nor approved\\\"
);
address operator = _msgSender();
_beforeTokenTransfer(operator, from, to, ids, amounts, data);
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\\\");
_balances[id][from] = fromBalance - amount;
_balances[id][to] += amount;
}
emit TransferBatch(operator, from, to, ids, amounts);
_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
}
该函数首先会检索传入的tokenid的数组上传ids.length是否和账户数组长度一致,之后检索token的接受地址是否为空,之后检索代币来源地址和当前转账操作的函数调用者地址是否一致或者代币来源地址有授权给当前函数调用者地址,之后通过一个for循环来遍历数组进行转账,在单一循环中首先通过tokenid来检索对应的token地址,之后查询对应form地址的balance账户余额,之后检查可用余额是否足够,之后进行转账操作,之后遍历完数组内所有的地址之后通过emit来触发事件,之后调用_doSafeBatchTransferAcceptanceCheck检查转账是否成功:
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(to).onERC1155BatchReceived.selector) {
revert(\\\"ERC1155: ERC1155Receiver rejected tokens\\\");
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert(\\\"ERC1155: transfer to non ERC1155Receiver implementer\\\");
}
}
}
铸币操作
ERC-1155中对于铸币操作提供了以下参考规范:
/**
* @dev Creates `amount` tokens of token type `id`, and assigns them to `account`.
*
* Emits a {TransferSingle} event.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - If `account` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
* acceptance magic value.
*/
function _mint(address account, uint256 id, uint256 amount, bytes memory data) internal virtual {
require(account != address(0), \\\"ERC1155: mint to the zero address\\\");
address operator = _msgSender();
_beforeTokenTransfer(operator, address(0), account, _asSingletonArray(id), _asSingletonArray(amount), data);
_balances[id][account] += amount;
emit TransferSingle(operator, address(0), account, id, amount);
_doSafeTransferAcceptanceCheck(operator, address(0), account, id, amount, data);
}
在以上_mint函数中首先会检查铸币后的代币归属地址是否为空,之后根据token的id序列查找对应的token,之后检索token下地址账户所具备的token数量,之后更新token总量,之后通过emit来触发事件。
当然ERC-1155还提供了批量铸币的功能——_mintBatch
/**
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
*
* Requirements:
*
* - `ids` and `amounts` must have the same length.
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
* acceptance magic value.
*/
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 = _msgSender();
_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
for (uint 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);
}
在_mintBatch函数中首先会检查代币的接受地址是否为空,之后会检查token的Id数组长度是否和账户数组长度一致,之后通过for循环批量增发token,之后通emit触发事件。
代币销毁
ERC-1155中提供了以下用于代币销毁的功能函数:
/**
* @dev Destroys `amount` tokens of token type `id` from `account`
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens of token type `id`.
*/
function _burn(address account, uint256 id, uint256 amount) internal virtual {
require(account != address(0), \\\"ERC1155: burn from the zero address\\\");
address operator = _msgSender();
_beforeTokenTransfer(operator, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), \\\"\\\");
uint256 accountBalance = _balances[id][account];
require(accountBalance >= amount, \\\"ERC1155: burn amount exceeds balance\\\");
_balances[id][account] = accountBalance - amount;
emit TransferSingle(operator, account, address(0), id, amount);
}
这里的_burn函数会首先检查account地址是否为空,之后通过token的ID序列来检索对应的token,之后根据地址account检索当前token内用户所持有的token的数量,之后检查要销毁的token的数量是否大于用户持有该币种的token的数量,之后进行销毁操作,最后通过emit触发事件。
当然,REC-1155中也提供了批量转账的功能函数——_burnBatch
/**
* @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
*
* Requirements:
*
* - `ids` and `amounts` must have the same length.
*/
function _burnBatch(address account, uint256[] memory ids, uint256[] memory amounts) internal virtual {
require(account != address(0), \\\"ERC1155: burn from the zero address\\\");
require(ids.length == amounts.length, \\\"ERC1155: ids and amounts length mismatch\\\");
address operator = _msgSender();
_beforeTokenTransfer(operator, account, address(0), ids, amounts, \\\"\\\");
for (uint i = 0; i < ids.length; i++) {
uint256 id = ids[i];
uint256 amount = amounts[i];
uint256 accountBalance = _balances[id][account];
require(accountBalance >= amount, \\\"ERC1155: burn amount exceeds balance\\\");
_balances[id][account] = accountBalance - amount;
}
emit TransferBatch(operator, account, address(0), ids, amounts);
}
_burnBatch函数的逻辑与_burn类似,只是多了一个数组长度的检查以及通过for循环来遍历销毁的代币的操作,这里不再赘述
文末小结
通过对上面ERC-1155的源码分析,最大的感受就是ERC-1155协议兼容NFT与FT两种类型,同时对于接收方是合约但是转币失败归还代币的功能实现,以及合约管理中的元数据URI的体验。
参考链接
https://eips.ethereum.org/EIPS/eip-1155
https://github.com/ethereum/EIPs/issues/1155
https://docs.openzeppelin.com/contracts/4.x/api/token/erc1155
https://blog.enjincoin.io/erc-1155-the-crypto-item-standard-ac9cf1c5a226
原创文章,作者:七芒星实验室,如若转载,请注明出处:https://www.sudun.com/ask/34308.html