Commit 4b49242e by Francisco Giordano

add non-upgrade-safe erc1155

parent bf0277b3
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "../token/ERC1155/ERC1155Burnable.sol";
contract ERC1155BurnableMock is ERC1155Burnable {
constructor(string memory uri) public ERC1155(uri) { }
function mint(address to, uint256 id, uint256 value, bytes memory data) public {
_mint(to, id, value, data);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "../token/ERC1155/ERC1155.sol";
/**
* @title ERC1155Mock
* This mock just publicizes internal functions for testing purposes
*/
contract ERC1155Mock is ERC1155 {
constructor (string memory uri) public ERC1155(uri) {
// solhint-disable-previous-line no-empty-blocks
}
function setURI(string memory newuri) public {
_setURI(newuri);
}
function mint(address to, uint256 id, uint256 value, bytes memory data) public {
_mint(to, id, value, data);
}
function mintBatch(address to, uint256[] memory ids, uint256[] memory values, bytes memory data) public {
_mintBatch(to, ids, values, data);
}
function burn(address owner, uint256 id, uint256 value) public {
_burn(owner, id, value);
}
function burnBatch(address owner, uint256[] memory ids, uint256[] memory values) public {
_burnBatch(owner, ids, values);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "../token/ERC1155/IERC1155Receiver.sol";
import "./ERC165Mock.sol";
contract ERC1155ReceiverMock is IERC1155Receiver, ERC165Mock {
bytes4 private _recRetval;
bool private _recReverts;
bytes4 private _batRetval;
bool private _batReverts;
event Received(address operator, address from, uint256 id, uint256 value, bytes data, uint256 gas);
event BatchReceived(address operator, address from, uint256[] ids, uint256[] values, bytes data, uint256 gas);
constructor (
bytes4 recRetval,
bool recReverts,
bytes4 batRetval,
bool batReverts
)
public
{
_recRetval = recRetval;
_recReverts = recReverts;
_batRetval = batRetval;
_batReverts = batReverts;
}
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
)
external
returns(bytes4)
{
require(!_recReverts, "ERC1155ReceiverMock: reverting on receive");
emit Received(operator, from, id, value, data, gasleft());
return _recRetval;
}
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
)
external
returns(bytes4)
{
require(!_batReverts, "ERC1155ReceiverMock: reverting on batch receive");
emit BatchReceived(operator, from, ids, values, data, gasleft());
return _batRetval;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "./IERC1155.sol";
import "./IERC1155MetadataURI.sol";
import "./IERC1155Receiver.sol";
import "../../GSN/Context.sol";
import "../../introspection/ERC165.sol";
import "../../math/SafeMath.sol";
import "../../utils/Address.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 SafeMath for uint256;
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;
/*
* bytes4(keccak256('balanceOf(address,uint256)')) == 0x00fdd58e
* bytes4(keccak256('balanceOfBatch(address[],uint256[])')) == 0x4e1273f4
* bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465
* bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5
* bytes4(keccak256('safeTransferFrom(address,address,uint256,uint256,bytes)')) == 0xf242432a
* bytes4(keccak256('safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)')) == 0x2eb2c2d6
*
* => 0x00fdd58e ^ 0x4e1273f4 ^ 0xa22cb465 ^
* 0xe985e9c5 ^ 0xf242432a ^ 0x2eb2c2d6 == 0xd9b67a26
*/
bytes4 private constant _INTERFACE_ID_ERC1155 = 0xd9b67a26;
/*
* bytes4(keccak256('uri(uint256)')) == 0x0e89341c
*/
bytes4 private constant _INTERFACE_ID_ERC1155_METADATA_URI = 0x0e89341c;
/**
* @dev See {_setURI}.
*/
constructor (string memory uri) public {
_setURI(uri);
// register the supported interfaces to conform to ERC1155 via ERC165
_registerInterface(_INTERFACE_ID_ERC1155);
// register the supported interfaces to conform to ERC1155MetadataURI via ERC165
_registerInterface(_INTERFACE_ID_ERC1155_METADATA_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) external view returns (string memory) {
return _uri;
}
/**
* @dev See {IERC1155-balanceOf}.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function balanceOf(address account, uint256 id) public view 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
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) {
require(accounts[i] != address(0), "ERC1155: batch balance query for the zero address");
batchBalances[i] = _balances[ids[i]][accounts[i]];
}
return batchBalances;
}
/**
* @dev See {IERC1155-setApprovalForAll}.
*/
function setApprovalForAll(address operator, bool approved) public {
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 returns (bool) {
return _operatorApprovals[account][operator];
}
/**
* @dev See {IERC1155-safeTransferFrom}.
*/
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
)
public
{
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);
_balances[id][from] = _balances[id][from].sub(amount, "ERC1155: insufficient balance for transfer");
_balances[id][to] = _balances[id][to].add(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
{
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];
_balances[id][from] = _balances[id][from].sub(
amount,
"ERC1155: insufficient balance for transfer"
);
_balances[id][to] = _balances[id][to].add(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 {
_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 `to` 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 {
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] = _balances[id][account].add(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 {
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].add(_balances[ids[i]][to]);
}
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 {
require(account != address(0), "ERC1155: burn from the zero address");
address operator = _msgSender();
_beforeTokenTransfer(operator, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), "");
_balances[id][account] = _balances[id][account].sub(
amount,
"ERC1155: burn amount exceeds balance"
);
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 {
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++) {
_balances[ids[i]][account] = _balances[ids[i]][account].sub(
amounts[i],
"ERC1155: burn amount exceeds balance"
);
}
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
{ }
function _doSafeTransferAcceptanceCheck(
address operator,
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
)
private
{
if (to.isContract()) {
(bool success, bytes memory returndata) = to.call(abi.encodeWithSelector(
IERC1155Receiver(to).onERC1155Received.selector,
operator,
from,
id,
amount,
data
));
if (success) {
bytes4 response = abi.decode(returndata, (bytes4));
if (response != IERC1155Receiver(to).onERC1155Received.selector) {
revert("ERC1155: ERC1155Receiver rejected tokens");
}
} else {
if (returndata.length > 0) {
// solhint-disable-next-line no-inline-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
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()) {
(bool success, bytes memory returndata) = to.call(abi.encodeWithSelector(
IERC1155Receiver(to).onERC1155BatchReceived.selector,
operator,
from,
ids,
amounts,
data
));
if (success) {
bytes4 response = abi.decode(returndata, (bytes4));
if (response != IERC1155Receiver(to).onERC1155BatchReceived.selector) {
revert("ERC1155: ERC1155Receiver rejected tokens");
}
} else {
if (returndata.length > 0) {
// solhint-disable-next-line no-inline-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
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;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "./ERC1155.sol";
/**
* @dev Extension of {ERC1155} that allows token holders to destroy both their
* own tokens and those that they have been approved to use.
*
* _Available since v3.1._
*/
contract ERC1155Burnable is ERC1155 {
function burn(address account, uint256 id, uint256 value) public {
require(
account == _msgSender() || isApprovedForAll(account, _msgSender()),
"ERC1155: caller is not owner nor approved"
);
_burn(account, id, value);
}
function burnBatch(address account, uint256[] memory ids, uint256[] memory values) public {
require(
account == _msgSender() || isApprovedForAll(account, _msgSender()),
"ERC1155: caller is not owner nor approved"
);
_burnBatch(account, ids, values);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "./ERC1155Receiver.sol";
/**
* @dev _Available since v3.1._
*/
contract ERC1155Holder is ERC1155Receiver {
function onERC1155Received(address, address, uint256, uint256, bytes memory) public returns (bytes4) {
return this.onERC1155Received.selector;
}
function onERC1155BatchReceived(address, address, uint256[] memory, uint256[] memory, bytes memory) public returns (bytes4) {
return this.onERC1155BatchReceived.selector;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "./IERC1155Receiver.sol";
import "../../introspection/ERC165.sol";
/**
* @dev _Available since v3.1._
*/
contract ERC1155Receiver is ERC165, IERC1155Receiver {
constructor() public {
_registerInterface(
ERC1155Receiver(0).onERC1155Received.selector ^
ERC1155Receiver(0).onERC1155BatchReceived.selector
);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "../../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._
*/
contract 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;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "./IERC1155.sol";
/**
* @dev Interface of the optional ERC1155MetadataExtension interface, as defined
* in the https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[EIP].
*
* _Available since v3.1._
*/
contract IERC1155MetadataURI is IERC1155 {
/**
* @dev Returns the URI for token type `id`.
*
* If the `\{id\}` substring is present in the URI, it must be replaced by
* clients with the actual token type ID.
*/
function uri(uint256 id) external view returns (string memory);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "../../introspection/IERC165.sol";
/**
* _Available since v3.1._
*/
contract IERC1155Receiver is IERC165 {
/**
@dev Handles the receipt of a single ERC1155 token type. This function is
called at the end of a `safeTransferFrom` after the balance has been updated.
To accept the transfer, this must return
`bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
(i.e. 0xf23a6e61, or its own function selector).
@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)"))` if transfer is allowed
*/
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
)
external
returns(bytes4);
/**
@dev Handles the receipt of a multiple ERC1155 token types. This function
is called at the end of a `safeBatchTransferFrom` after the balances have
been updated. To accept the transfer(s), this must return
`bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
(i.e. 0xbc197c81, or its own function selector).
@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)"))` if transfer is allowed
*/
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
)
external
returns(bytes4);
}
= ERC 1155
[.readme-notice]
NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/token/erc1155
This set of interfaces and contracts are all related to the https://eips.ethereum.org/EIPS/eip-1155[ERC1155 Multi Token Standard].
The EIP consists of three interfaces which fulfill different roles, found here as {IERC1155}, {IERC1155MetadataURI} and {IERC1155Receiver}.
{ERC1155} implements the mandatory {IERC1155} interface, as well as the optional extension {IERC1155MetadataURI}, by relying on the substitution mechanism to use the same URI for all token types, dramatically reducing gas costs.
Additionally there are multiple custom extensions, including:
* designation of addresses that can pause token transfers for all users ({ERC1155Pausable}).
* destruction of own tokens ({ERC1155Burnable}).
NOTE: This core set of contracts is designed to be unopinionated, allowing developers to access the internal functions in ERC1155 (such as <<ERC1155-_mint-address-uint256-uint256-bytes-,`_mint`>>) and expose them as external functions in the way they prefer. On the other hand, xref:ROOT:erc1155.adoc#Presets[ERC1155 Presets] (such as {ERC1155PresetMinterPauser}) are designed using opinionated patterns to provide developers with ready to use, deployable contracts.
== Core
{{IERC1155}}
{{IERC1155MetadataURI}}
{{ERC1155}}
{{IERC1155Receiver}}
== Extensions
{{ERC1155Pausable}}
{{ERC1155Burnable}}
== Convenience
{{ERC1155Holder}}
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
"@openzeppelin/gsn-helpers": "^0.2.3", "@openzeppelin/gsn-helpers": "^0.2.3",
"@openzeppelin/gsn-provider": "^0.1.9", "@openzeppelin/gsn-provider": "^0.1.9",
"@openzeppelin/test-environment": "^0.1.2", "@openzeppelin/test-environment": "^0.1.2",
"@openzeppelin/test-helpers": "^0.5.4", "@openzeppelin/test-helpers": "^0.5.6",
"chai": "^4.2.0", "chai": "^4.2.0",
"concurrently": "^4.1.0", "concurrently": "^4.1.0",
"eslint": "^6.5.1", "eslint": "^6.5.1",
......
...@@ -27,6 +27,14 @@ const INTERFACES = { ...@@ -27,6 +27,14 @@ const INTERFACES = {
'symbol()', 'symbol()',
'tokenURI(uint256)', 'tokenURI(uint256)',
], ],
ERC1155: [
'balanceOf(address,uint256)',
'balanceOfBatch(address[],uint256[])',
'setApprovalForAll(address,bool)',
'isApprovedForAll(address,address)',
'safeTransferFrom(address,address,uint256,uint256,bytes)',
'safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)',
],
}; };
const INTERFACE_IDS = {}; const INTERFACE_IDS = {};
......
const { contract } = require('@openzeppelin/test-environment');
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { expect } = require('chai');
const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
const ERC1155ReceiverMock = contract.fromArtifact('ERC1155ReceiverMock');
function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, multiTokenHolder, recipient, proxy]) {
const firstTokenId = new BN(1);
const secondTokenId = new BN(2);
const unknownTokenId = new BN(3);
const firstAmount = new BN(1000);
const secondAmount = new BN(2000);
const RECEIVER_SINGLE_MAGIC_VALUE = '0xf23a6e61';
const RECEIVER_BATCH_MAGIC_VALUE = '0xbc197c81';
describe('like an ERC1155', function () {
describe('balanceOf', function () {
it('reverts when queried about the zero address', async function () {
await expectRevert(
this.token.balanceOf(ZERO_ADDRESS, firstTokenId),
'ERC1155: balance query for the zero address'
);
});
context('when accounts don\'t own tokens', function () {
it('returns zero for given addresses', async function () {
expect(await this.token.balanceOf(
firstTokenHolder,
firstTokenId
)).to.be.bignumber.equal('0');
expect(await this.token.balanceOf(
secondTokenHolder,
secondTokenId
)).to.be.bignumber.equal('0');
expect(await this.token.balanceOf(
firstTokenHolder,
unknownTokenId
)).to.be.bignumber.equal('0');
});
});
context('when accounts own some tokens', function () {
beforeEach(async function () {
await this.token.mint(firstTokenHolder, firstTokenId, firstAmount, '0x', {
from: minter,
});
await this.token.mint(
secondTokenHolder,
secondTokenId,
secondAmount,
'0x',
{
from: minter,
}
);
});
it('returns the amount of tokens owned by the given addresses', async function () {
expect(await this.token.balanceOf(
firstTokenHolder,
firstTokenId
)).to.be.bignumber.equal(firstAmount);
expect(await this.token.balanceOf(
secondTokenHolder,
secondTokenId
)).to.be.bignumber.equal(secondAmount);
expect(await this.token.balanceOf(
firstTokenHolder,
unknownTokenId
)).to.be.bignumber.equal('0');
});
});
});
describe('balanceOfBatch', function () {
it('reverts when input arrays don\'t match up', async function () {
await expectRevert(
this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder, firstTokenHolder, secondTokenHolder],
[firstTokenId, secondTokenId, unknownTokenId]
),
'ERC1155: accounts and ids length mismatch'
);
await expectRevert(
this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder],
[firstTokenId, secondTokenId, unknownTokenId]
),
'ERC1155: accounts and ids length mismatch'
);
});
it('reverts when one of the addresses is the zero address', async function () {
await expectRevert(
this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder, ZERO_ADDRESS],
[firstTokenId, secondTokenId, unknownTokenId]
),
'ERC1155: batch balance query for the zero address'
);
});
context('when accounts don\'t own tokens', function () {
it('returns zeros for each account', async function () {
const result = await this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder, firstTokenHolder],
[firstTokenId, secondTokenId, unknownTokenId]
);
expect(result).to.be.an('array');
expect(result[0]).to.be.a.bignumber.equal('0');
expect(result[1]).to.be.a.bignumber.equal('0');
expect(result[2]).to.be.a.bignumber.equal('0');
});
});
context('when accounts own some tokens', function () {
beforeEach(async function () {
await this.token.mint(firstTokenHolder, firstTokenId, firstAmount, '0x', {
from: minter,
});
await this.token.mint(
secondTokenHolder,
secondTokenId,
secondAmount,
'0x',
{
from: minter,
}
);
});
it('returns amounts owned by each account in order passed', async function () {
const result = await this.token.balanceOfBatch(
[secondTokenHolder, firstTokenHolder, firstTokenHolder],
[secondTokenId, firstTokenId, unknownTokenId]
);
expect(result).to.be.an('array');
expect(result[0]).to.be.a.bignumber.equal(secondAmount);
expect(result[1]).to.be.a.bignumber.equal(firstAmount);
expect(result[2]).to.be.a.bignumber.equal('0');
});
it('returns multiple times the balance of the same address when asked', async function () {
const result = await this.token.balanceOfBatch(
[firstTokenHolder, secondTokenHolder, firstTokenHolder],
[firstTokenId, secondTokenId, firstTokenId]
);
expect(result).to.be.an('array');
expect(result[0]).to.be.a.bignumber.equal(result[2]);
expect(result[0]).to.be.a.bignumber.equal(firstAmount);
expect(result[1]).to.be.a.bignumber.equal(secondAmount);
expect(result[2]).to.be.a.bignumber.equal(firstAmount);
});
});
});
describe('setApprovalForAll', function () {
let logs;
beforeEach(async function () {
({ logs } = await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder }));
});
it('sets approval status which can be queried via isApprovedForAll', async function () {
expect(await this.token.isApprovedForAll(multiTokenHolder, proxy)).to.be.equal(true);
});
it('emits an ApprovalForAll log', function () {
expectEvent.inLogs(logs, 'ApprovalForAll', { account: multiTokenHolder, operator: proxy, approved: true });
});
it('can unset approval for an operator', async function () {
await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
expect(await this.token.isApprovedForAll(multiTokenHolder, proxy)).to.be.equal(false);
});
it('reverts if attempting to approve self as an operator', async function () {
await expectRevert(
this.token.setApprovalForAll(multiTokenHolder, true, { from: multiTokenHolder }),
'ERC1155: setting approval status for self'
);
});
});
describe('safeTransferFrom', function () {
beforeEach(async function () {
await this.token.mint(multiTokenHolder, firstTokenId, firstAmount, '0x', {
from: minter,
});
await this.token.mint(
multiTokenHolder,
secondTokenId,
secondAmount,
'0x',
{
from: minter,
}
);
});
it('reverts when transferring more than balance', async function () {
await expectRevert(
this.token.safeTransferFrom(
multiTokenHolder,
recipient,
firstTokenId,
firstAmount.addn(1),
'0x',
{ from: multiTokenHolder },
),
'ERC1155: insufficient balance for transfer'
);
});
it('reverts when transferring to zero address', async function () {
await expectRevert(
this.token.safeTransferFrom(
multiTokenHolder,
ZERO_ADDRESS,
firstTokenId,
firstAmount,
'0x',
{ from: multiTokenHolder },
),
'ERC1155: transfer to the zero address'
);
});
function transferWasSuccessful ({ operator, from, id, value }) {
it('debits transferred balance from sender', async function () {
const newBalance = await this.token.balanceOf(from, id);
expect(newBalance).to.be.a.bignumber.equal('0');
});
it('credits transferred balance to receiver', async function () {
const newBalance = await this.token.balanceOf(this.toWhom, id);
expect(newBalance).to.be.a.bignumber.equal(value);
});
it('emits a TransferSingle log', function () {
expectEvent.inLogs(this.transferLogs, 'TransferSingle', {
operator,
from,
to: this.toWhom,
id,
value,
});
});
}
context('when called by the multiTokenHolder', async function () {
beforeEach(async function () {
this.toWhom = recipient;
({ logs: this.transferLogs } =
await this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount, '0x', {
from: multiTokenHolder,
}));
});
transferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
});
it('preserves existing balances which are not transferred by multiTokenHolder', async function () {
const balance1 = await this.token.balanceOf(multiTokenHolder, secondTokenId);
expect(balance1).to.be.a.bignumber.equal(secondAmount);
const balance2 = await this.token.balanceOf(recipient, secondTokenId);
expect(balance2).to.be.a.bignumber.equal('0');
});
});
context('when called by an operator on behalf of the multiTokenHolder', function () {
context('when operator is not approved by multiTokenHolder', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
});
it('reverts', async function () {
await expectRevert(
this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount, '0x', {
from: proxy,
}),
'ERC1155: caller is not owner nor approved'
);
});
});
context('when operator is approved by multiTokenHolder', function () {
beforeEach(async function () {
this.toWhom = recipient;
await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder });
({ logs: this.transferLogs } =
await this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount, '0x', {
from: proxy,
}));
});
transferWasSuccessful.call(this, {
operator: proxy,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
});
it('preserves operator\'s balances not involved in the transfer', async function () {
const balance1 = await this.token.balanceOf(proxy, firstTokenId);
expect(balance1).to.be.a.bignumber.equal('0');
const balance2 = await this.token.balanceOf(proxy, secondTokenId);
expect(balance2).to.be.a.bignumber.equal('0');
});
});
});
context('when sending to a valid receiver', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE, false,
RECEIVER_BATCH_MAGIC_VALUE, false,
);
});
context('without data', function () {
beforeEach(async function () {
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeTransferFrom(
multiTokenHolder,
this.receiver.address,
firstTokenId,
firstAmount,
'0x',
{ from: multiTokenHolder }
);
({ logs: this.transferLogs } = this.transferReceipt);
});
transferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
});
it('calls onERC1155Received', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'Received', {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
data: null,
});
});
});
context('with data', function () {
const data = '0xf00dd00d';
beforeEach(async function () {
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeTransferFrom(
multiTokenHolder,
this.receiver.address,
firstTokenId,
firstAmount,
data,
{ from: multiTokenHolder }
);
({ logs: this.transferLogs } = this.transferReceipt);
});
transferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
});
it('calls onERC1155Received', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'Received', {
operator: multiTokenHolder,
from: multiTokenHolder,
id: firstTokenId,
value: firstAmount,
data,
});
});
});
});
context('to a receiver contract returning unexpected value', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
'0x00c0ffee', false,
RECEIVER_BATCH_MAGIC_VALUE, false,
);
});
it('reverts', async function () {
await expectRevert(
this.token.safeTransferFrom(multiTokenHolder, this.receiver.address, firstTokenId, firstAmount, '0x', {
from: multiTokenHolder,
}),
'ERC1155: ERC1155Receiver rejected tokens'
);
});
});
context('to a receiver contract that reverts', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE, true,
RECEIVER_BATCH_MAGIC_VALUE, false,
);
});
it('reverts', async function () {
await expectRevert(
this.token.safeTransferFrom(multiTokenHolder, this.receiver.address, firstTokenId, firstAmount, '0x', {
from: multiTokenHolder,
}),
'ERC1155ReceiverMock: reverting on receive'
);
});
});
context('to a contract that does not implement the required function', function () {
it('reverts', async function () {
const invalidReceiver = this.token;
await expectRevert.unspecified(
this.token.safeTransferFrom(multiTokenHolder, invalidReceiver.address, firstTokenId, firstAmount, '0x', {
from: multiTokenHolder,
})
);
});
});
});
describe('safeBatchTransferFrom', function () {
beforeEach(async function () {
await this.token.mint(multiTokenHolder, firstTokenId, firstAmount, '0x', {
from: minter,
});
await this.token.mint(
multiTokenHolder,
secondTokenId,
secondAmount,
'0x',
{
from: minter,
}
);
});
it('reverts when transferring amount more than any of balances', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder, recipient,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount.addn(1)],
'0x', { from: multiTokenHolder }
),
'ERC1155: insufficient balance for transfer'
);
});
it('reverts when ids array length doesn\'t match amounts array length', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder, recipient,
[firstTokenId],
[firstAmount, secondAmount],
'0x', { from: multiTokenHolder }
),
'ERC1155: ids and amounts length mismatch'
);
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder, recipient,
[firstTokenId, secondTokenId],
[firstAmount],
'0x', { from: multiTokenHolder }
),
'ERC1155: ids and amounts length mismatch'
);
});
it('reverts when transferring to zero address', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder, ZERO_ADDRESS,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: multiTokenHolder }
),
'ERC1155: transfer to the zero address'
);
});
function batchTransferWasSuccessful ({ operator, from, ids, values }) {
it('debits transferred balances from sender', async function () {
const newBalances = await this.token.balanceOfBatch(new Array(ids.length).fill(from), ids);
for (const newBalance of newBalances) {
expect(newBalance).to.be.a.bignumber.equal('0');
}
});
it('credits transferred balances to receiver', async function () {
const newBalances = await this.token.balanceOfBatch(new Array(ids.length).fill(this.toWhom), ids);
for (let i = 0; i < newBalances.length; i++) {
expect(newBalances[i]).to.be.a.bignumber.equal(values[i]);
}
});
it('emits a TransferBatch log', function () {
expectEvent.inLogs(this.transferLogs, 'TransferBatch', {
operator,
from,
to: this.toWhom,
// ids,
// values,
});
});
}
context('when called by the multiTokenHolder', async function () {
beforeEach(async function () {
this.toWhom = recipient;
({ logs: this.transferLogs } =
await this.token.safeBatchTransferFrom(
multiTokenHolder, recipient,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: multiTokenHolder }
));
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
});
context('when called by an operator on behalf of the multiTokenHolder', function () {
context('when operator is not approved by multiTokenHolder', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
});
it('reverts', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder, recipient,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: proxy }
),
'ERC1155: transfer caller is not owner nor approved'
);
});
});
context('when operator is approved by multiTokenHolder', function () {
beforeEach(async function () {
this.toWhom = recipient;
await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder });
({ logs: this.transferLogs } =
await this.token.safeBatchTransferFrom(
multiTokenHolder, recipient,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: proxy },
));
});
batchTransferWasSuccessful.call(this, {
operator: proxy,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it('preserves operator\'s balances not involved in the transfer', async function () {
const balance1 = await this.token.balanceOf(proxy, firstTokenId);
expect(balance1).to.be.a.bignumber.equal('0');
const balance2 = await this.token.balanceOf(proxy, secondTokenId);
expect(balance2).to.be.a.bignumber.equal('0');
});
});
});
context('when sending to a valid receiver', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE, false,
RECEIVER_BATCH_MAGIC_VALUE, false,
);
});
context('without data', function () {
beforeEach(async function () {
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeBatchTransferFrom(
multiTokenHolder, this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: multiTokenHolder },
);
({ logs: this.transferLogs } = this.transferReceipt);
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it('calls onERC1155BatchReceived', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
operator: multiTokenHolder,
from: multiTokenHolder,
// ids: [firstTokenId, secondTokenId],
// values: [firstAmount, secondAmount],
data: null,
});
});
});
context('with data', function () {
const data = '0xf00dd00d';
beforeEach(async function () {
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeBatchTransferFrom(
multiTokenHolder, this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
data, { from: multiTokenHolder },
);
({ logs: this.transferLogs } = this.transferReceipt);
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it('calls onERC1155Received', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
operator: multiTokenHolder,
from: multiTokenHolder,
// ids: [firstTokenId, secondTokenId],
// values: [firstAmount, secondAmount],
data,
});
});
});
});
context('to a receiver contract returning unexpected value', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE, false,
RECEIVER_SINGLE_MAGIC_VALUE, false,
);
});
it('reverts', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder, this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: multiTokenHolder },
),
'ERC1155: ERC1155Receiver rejected tokens'
);
});
});
context('to a receiver contract that reverts', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE, false,
RECEIVER_BATCH_MAGIC_VALUE, true,
);
});
it('reverts', async function () {
await expectRevert(
this.token.safeBatchTransferFrom(
multiTokenHolder, this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: multiTokenHolder },
),
'ERC1155ReceiverMock: reverting on batch receive'
);
});
});
context('to a receiver contract that reverts only on single transfers', function () {
beforeEach(async function () {
this.receiver = await ERC1155ReceiverMock.new(
RECEIVER_SINGLE_MAGIC_VALUE, true,
RECEIVER_BATCH_MAGIC_VALUE, false,
);
this.toWhom = this.receiver.address;
this.transferReceipt = await this.token.safeBatchTransferFrom(
multiTokenHolder, this.receiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: multiTokenHolder },
);
({ logs: this.transferLogs } = this.transferReceipt);
});
batchTransferWasSuccessful.call(this, {
operator: multiTokenHolder,
from: multiTokenHolder,
ids: [firstTokenId, secondTokenId],
values: [firstAmount, secondAmount],
});
it('calls onERC1155BatchReceived', async function () {
await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
operator: multiTokenHolder,
from: multiTokenHolder,
// ids: [firstTokenId, secondTokenId],
// values: [firstAmount, secondAmount],
data: null,
});
});
});
context('to a contract that does not implement the required function', function () {
it('reverts', async function () {
const invalidReceiver = this.token;
await expectRevert.unspecified(
this.token.safeBatchTransferFrom(
multiTokenHolder, invalidReceiver.address,
[firstTokenId, secondTokenId],
[firstAmount, secondAmount],
'0x', { from: multiTokenHolder },
)
);
});
});
});
shouldSupportInterfaces(['ERC165', 'ERC1155']);
});
}
module.exports = {
shouldBehaveLikeERC1155,
};
const { accounts, contract } = require('@openzeppelin/test-environment');
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { expect } = require('chai');
const { shouldBehaveLikeERC1155 } = require('./ERC1155.behavior');
const ERC1155Mock = contract.fromArtifact('ERC1155Mock');
describe('ERC1155', function () {
const [operator, tokenHolder, tokenBatchHolder, ...otherAccounts] = accounts;
const initialURI = 'https://token-cdn-domain/{id}.json';
beforeEach(async function () {
this.token = await ERC1155Mock.new(initialURI);
});
shouldBehaveLikeERC1155(otherAccounts);
describe('internal functions', function () {
const tokenId = new BN(1990);
const mintAmount = new BN(9001);
const burnAmount = new BN(3000);
const tokenBatchIds = [new BN(2000), new BN(2010), new BN(2020)];
const mintAmounts = [new BN(5000), new BN(10000), new BN(42195)];
const burnAmounts = [new BN(5000), new BN(9001), new BN(195)];
const data = '0x12345678';
describe('_mint', function () {
it('reverts with a zero destination address', async function () {
await expectRevert(
this.token.mint(ZERO_ADDRESS, tokenId, mintAmount, data),
'ERC1155: mint to the zero address'
);
});
context('with minted tokens', function () {
beforeEach(async function () {
({ logs: this.logs } = await this.token.mint(tokenHolder, tokenId, mintAmount, data, { from: operator }));
});
it('emits a TransferSingle event', function () {
expectEvent.inLogs(this.logs, 'TransferSingle', {
operator,
from: ZERO_ADDRESS,
to: tokenHolder,
id: tokenId,
value: mintAmount,
});
});
it('credits the minted amount of tokens', async function () {
expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintAmount);
});
});
});
describe('_mintBatch', function () {
it('reverts with a zero destination address', async function () {
await expectRevert(
this.token.mintBatch(ZERO_ADDRESS, tokenBatchIds, mintAmounts, data),
'ERC1155: mint to the zero address'
);
});
it('reverts if length of inputs do not match', async function () {
await expectRevert(
this.token.mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts.slice(1), data),
'ERC1155: ids and amounts length mismatch'
);
await expectRevert(
this.token.mintBatch(tokenBatchHolder, tokenBatchIds.slice(1), mintAmounts, data),
'ERC1155: ids and amounts length mismatch'
);
});
context('with minted batch of tokens', function () {
beforeEach(async function () {
({ logs: this.logs } = await this.token.mintBatch(
tokenBatchHolder,
tokenBatchIds,
mintAmounts,
data,
{ from: operator }
));
});
it('emits a TransferBatch event', function () {
expectEvent.inLogs(this.logs, 'TransferBatch', {
operator,
from: ZERO_ADDRESS,
to: tokenBatchHolder,
});
});
it('credits the minted batch of tokens', async function () {
const holderBatchBalances = await this.token.balanceOfBatch(
new Array(tokenBatchIds.length).fill(tokenBatchHolder),
tokenBatchIds
);
for (let i = 0; i < holderBatchBalances.length; i++) {
expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i]);
}
});
});
});
describe('_burn', function () {
it('reverts when burning the zero account\'s tokens', async function () {
await expectRevert(
this.token.burn(ZERO_ADDRESS, tokenId, mintAmount),
'ERC1155: burn from the zero address'
);
});
it('reverts when burning a non-existent token id', async function () {
await expectRevert(
this.token.burn(tokenHolder, tokenId, mintAmount),
'ERC1155: burn amount exceeds balance'
);
});
it('reverts when burning more than available tokens', async function () {
await this.token.mint(
tokenHolder,
tokenId,
mintAmount,
data,
{ from: operator }
);
await expectRevert(
this.token.burn(tokenHolder, tokenId, mintAmount.addn(1)),
'ERC1155: burn amount exceeds balance'
);
});
context('with minted-then-burnt tokens', function () {
beforeEach(async function () {
await this.token.mint(tokenHolder, tokenId, mintAmount, data);
({ logs: this.logs } = await this.token.burn(
tokenHolder,
tokenId,
burnAmount,
{ from: operator }
));
});
it('emits a TransferSingle event', function () {
expectEvent.inLogs(this.logs, 'TransferSingle', {
operator,
from: tokenHolder,
to: ZERO_ADDRESS,
id: tokenId,
value: burnAmount,
});
});
it('accounts for both minting and burning', async function () {
expect(await this.token.balanceOf(
tokenHolder,
tokenId
)).to.be.bignumber.equal(mintAmount.sub(burnAmount));
});
});
});
describe('_burnBatch', function () {
it('reverts when burning the zero account\'s tokens', async function () {
await expectRevert(
this.token.burnBatch(ZERO_ADDRESS, tokenBatchIds, burnAmounts),
'ERC1155: burn from the zero address'
);
});
it('reverts if length of inputs do not match', async function () {
await expectRevert(
this.token.burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts.slice(1)),
'ERC1155: ids and amounts length mismatch'
);
await expectRevert(
this.token.burnBatch(tokenBatchHolder, tokenBatchIds.slice(1), burnAmounts),
'ERC1155: ids and amounts length mismatch'
);
});
it('reverts when burning a non-existent token id', async function () {
await expectRevert(
this.token.burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts),
'ERC1155: burn amount exceeds balance'
);
});
context('with minted-then-burnt tokens', function () {
beforeEach(async function () {
await this.token.mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts, data);
({ logs: this.logs } = await this.token.burnBatch(
tokenBatchHolder,
tokenBatchIds,
burnAmounts,
{ from: operator }
));
});
it('emits a TransferBatch event', function () {
expectEvent.inLogs(this.logs, 'TransferBatch', {
operator,
from: tokenBatchHolder,
to: ZERO_ADDRESS,
// ids: tokenBatchIds,
// values: burnAmounts,
});
});
it('accounts for both minting and burning', async function () {
const holderBatchBalances = await this.token.balanceOfBatch(
new Array(tokenBatchIds.length).fill(tokenBatchHolder),
tokenBatchIds
);
for (let i = 0; i < holderBatchBalances.length; i++) {
expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i].sub(burnAmounts[i]));
}
});
});
});
});
describe('ERC1155MetadataURI', function () {
const firstTokenID = new BN('42');
const secondTokenID = new BN('1337');
it('emits no URI event in constructor', async function () {
await expectEvent.notEmitted.inConstruction(this.token, 'URI');
});
it('sets the initial URI for all token types', async function () {
expect(await this.token.uri(firstTokenID)).to.be.equal(initialURI);
expect(await this.token.uri(secondTokenID)).to.be.equal(initialURI);
});
describe('_setURI', function () {
const newURI = 'https://token-cdn-domain/{locale}/{id}.json';
it('emits no URI event', async function () {
const receipt = await this.token.setURI(newURI);
expectEvent.notEmitted(receipt, 'URI');
});
it('sets the new URI for all token types', async function () {
await this.token.setURI(newURI);
expect(await this.token.uri(firstTokenID)).to.be.equal(newURI);
expect(await this.token.uri(secondTokenID)).to.be.equal(newURI);
});
});
});
});
const { accounts, contract } = require('@openzeppelin/test-environment');
const { BN, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const ERC1155BurnableMock = contract.fromArtifact('ERC1155BurnableMock');
describe('ERC1155Burnable', function () {
const [ holder, operator, other ] = accounts;
const uri = 'https://token.com';
const tokenIds = [new BN('42'), new BN('1137')];
const amounts = [new BN('3000'), new BN('9902')];
beforeEach(async function () {
this.token = await ERC1155BurnableMock.new(uri);
await this.token.mint(holder, tokenIds[0], amounts[0], '0x');
await this.token.mint(holder, tokenIds[1], amounts[1], '0x');
});
describe('burn', function () {
it('holder can burn their tokens', async function () {
await this.token.burn(holder, tokenIds[0], amounts[0].subn(1), { from: holder });
expect(await this.token.balanceOf(holder, tokenIds[0])).to.be.bignumber.equal('1');
});
it('approved operators can burn the holder\'s tokens', async function () {
await this.token.setApprovalForAll(operator, true, { from: holder });
await this.token.burn(holder, tokenIds[0], amounts[0].subn(1), { from: operator });
expect(await this.token.balanceOf(holder, tokenIds[0])).to.be.bignumber.equal('1');
});
it('unapproved accounts cannot burn the holder\'s tokens', async function () {
await expectRevert(
this.token.burn(holder, tokenIds[0], amounts[0].subn(1), { from: other }),
'ERC1155: caller is not owner nor approved'
);
});
});
describe('burnBatch', function () {
it('holder can burn their tokens', async function () {
await this.token.burnBatch(holder, tokenIds, [ amounts[0].subn(1), amounts[1].subn(2) ], { from: holder });
expect(await this.token.balanceOf(holder, tokenIds[0])).to.be.bignumber.equal('1');
expect(await this.token.balanceOf(holder, tokenIds[1])).to.be.bignumber.equal('2');
});
it('approved operators can burn the holder\'s tokens', async function () {
await this.token.setApprovalForAll(operator, true, { from: holder });
await this.token.burnBatch(holder, tokenIds, [ amounts[0].subn(1), amounts[1].subn(2) ], { from: operator });
expect(await this.token.balanceOf(holder, tokenIds[0])).to.be.bignumber.equal('1');
expect(await this.token.balanceOf(holder, tokenIds[1])).to.be.bignumber.equal('2');
});
it('unapproved accounts cannot burn the holder\'s tokens', async function () {
await expectRevert(
this.token.burnBatch(holder, tokenIds, [ amounts[0].subn(1), amounts[1].subn(2) ], { from: other }),
'ERC1155: caller is not owner nor approved'
);
});
});
});
const { accounts, contract } = require('@openzeppelin/test-environment');
const { BN } = require('@openzeppelin/test-helpers');
const ERC1155Holder = contract.fromArtifact('ERC1155Holder');
const ERC1155Mock = contract.fromArtifact('ERC1155Mock');
const { expect } = require('chai');
describe('ERC1155Holder', function () {
const [creator] = accounts;
const uri = 'https://token-cdn-domain/{id}.json';
const multiTokenIds = [new BN(1), new BN(2), new BN(3)];
const multiTokenAmounts = [new BN(1000), new BN(2000), new BN(3000)];
const transferData = '0x12345678';
beforeEach(async function () {
this.multiToken = await ERC1155Mock.new(uri, { from: creator });
this.holder = await ERC1155Holder.new();
await this.multiToken.mintBatch(creator, multiTokenIds, multiTokenAmounts, '0x', { from: creator });
});
it('receives ERC1155 tokens from a single ID', async function () {
await this.multiToken.safeTransferFrom(
creator,
this.holder.address,
multiTokenIds[0],
multiTokenAmounts[0],
transferData,
{ from: creator },
);
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[0]))
.to.be.bignumber.equal(multiTokenAmounts[0]);
for (let i = 1; i < multiTokenIds.length; i++) {
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[i])).to.be.bignumber.equal(new BN(0));
}
});
it('receives ERC1155 tokens from a multiple IDs', async function () {
for (let i = 0; i < multiTokenIds.length; i++) {
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[i])).to.be.bignumber.equal(new BN(0));
};
await this.multiToken.safeBatchTransferFrom(
creator,
this.holder.address,
multiTokenIds,
multiTokenAmounts,
transferData,
{ from: creator },
);
for (let i = 0; i < multiTokenIds.length; i++) {
expect(await this.multiToken.balanceOf(this.holder.address, multiTokenIds[i]))
.to.be.bignumber.equal(multiTokenAmounts[i]);
}
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment