From d9fa59f30a27f095c48b09555106fed0200654e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Tue, 9 Jun 2020 14:47:51 -0300 Subject: [PATCH] Address ERC1155 changes (#2267) * Make holder fns public * Add context, remove msg.sender from check * Fix location of Holder arguments * Add beforeTransfer hook * Minor test improvements * Add ERC1155Burnable and tests * Add ERC1155Pausable * Add ERC1155PresetMinterPauser.sol * Add uri constructors * Improved revert reasons * Initial docs improvements * Add missing docs * Improve acceptance checks revert reasons * Apply suggestions from code review Co-authored-by: Francisco Giordano * Remove note about 1155 preset uri in mint * Add rquirements to balanceOfBatch * Add note about URI and uri * Fix list in docs * Fix lint errors * Use natural sorting for API titles * Fix doc references * Escape {id} references to remove docgen warnings * Added intro docs, fixed links * Apply suggestions from code review Co-authored-by: Francisco Giordano * Add changelog entry Co-authored-by: Francisco Giordano --- CHANGELOG.md | 1 + contracts/mocks/ERC1155BurnableMock.sol | 13 + contracts/mocks/ERC1155PausableMock.sol | 31 ++ .../presets/ERC1155PresetMinterPauser.sol | 104 ++++++ contracts/presets/ERC20PresetMinterPauser.sol | 2 +- .../ERC721PresetMinterPauserAutoId.sol | 2 +- contracts/presets/README.adoc | 2 + contracts/token/ERC1155/ERC1155.sol | 318 ++++++++++-------- contracts/token/ERC1155/ERC1155Burnable.sol | 29 ++ contracts/token/ERC1155/ERC1155Holder.sol | 8 +- contracts/token/ERC1155/ERC1155Pausable.sol | 37 ++ contracts/token/ERC1155/IERC1155.sol | 78 ++++- .../token/ERC1155/IERC1155MetadataURI.sol | 6 + contracts/token/ERC1155/README.adoc | 17 +- contracts/token/ERC20/ERC20.sol | 2 +- contracts/token/ERC721/README.adoc | 2 - docs/modules/ROOT/nav.adoc | 1 + docs/modules/ROOT/pages/erc1155.adoc | 21 ++ docs/modules/ROOT/pages/tokens.adoc | 2 + scripts/gen-nav.js | 2 +- .../presets/ERC1155PresetMinterPauser.test.js | 136 ++++++++ ...=> ERC721PresetMinterPauserAutoId.test.js} | 0 test/token/ERC1155/ERC1155.behavior.js | 22 +- test/token/ERC1155/ERC1155.test.js | 59 ++-- test/token/ERC1155/ERC1155Burnable.test.js | 69 ++++ test/token/ERC1155/ERC1155Pausable.test.js | 110 ++++++ 26 files changed, 876 insertions(+), 198 deletions(-) create mode 100644 contracts/mocks/ERC1155BurnableMock.sol create mode 100644 contracts/mocks/ERC1155PausableMock.sol create mode 100644 contracts/presets/ERC1155PresetMinterPauser.sol create mode 100644 contracts/token/ERC1155/ERC1155Burnable.sol create mode 100644 contracts/token/ERC1155/ERC1155Pausable.sol create mode 100644 docs/modules/ROOT/pages/erc1155.adoc create mode 100644 test/presets/ERC1155PresetMinterPauser.test.js rename test/presets/{ERC721PresetMinterPauserAutoId.js => ERC721PresetMinterPauserAutoId.test.js} (100%) create mode 100644 test/token/ERC1155/ERC1155Burnable.test.js create mode 100644 test/token/ERC1155/ERC1155Pausable.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 98d9680a5..9465d42fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### New features * `SafeCast`: added functions to downcast signed integers (e.g. `toInt32`), improving usability of `SignedSafeMath`. ([#2243](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2243)) + * `ERC1155`: added support for a base implementation, non-standard extensions and a preset contract. ([#2014](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2014), [#2230](https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2230)) ### Improvements * `ReentrancyGuard`: reduced overhead of using the `nonReentrant` modifier. ([#2171](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2171)) diff --git a/contracts/mocks/ERC1155BurnableMock.sol b/contracts/mocks/ERC1155BurnableMock.sol new file mode 100644 index 000000000..cce25894b --- /dev/null +++ b/contracts/mocks/ERC1155BurnableMock.sol @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.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); + } +} diff --git a/contracts/mocks/ERC1155PausableMock.sol b/contracts/mocks/ERC1155PausableMock.sol new file mode 100644 index 000000000..d76fe8123 --- /dev/null +++ b/contracts/mocks/ERC1155PausableMock.sol @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ERC1155Mock.sol"; +import "../token/ERC1155/ERC1155Pausable.sol"; + +contract ERC1155PausableMock is ERC1155Mock, ERC1155Pausable { + constructor(string memory uri) public ERC1155Mock(uri) { } + + function pause() external { + _pause(); + } + + function unpause() external { + _unpause(); + } + + function _beforeTokenTransfer( + address operator, + address from, + address to, + uint256[] memory ids, + uint256[] memory amounts, + bytes memory data + ) + internal virtual override(ERC1155, ERC1155Pausable) + { + super._beforeTokenTransfer(operator, from, to, ids, amounts, data); + } +} diff --git a/contracts/presets/ERC1155PresetMinterPauser.sol b/contracts/presets/ERC1155PresetMinterPauser.sol new file mode 100644 index 000000000..078406ae5 --- /dev/null +++ b/contracts/presets/ERC1155PresetMinterPauser.sol @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "../access/AccessControl.sol"; +import "../GSN/Context.sol"; +import "../token/ERC1155/ERC1155.sol"; +import "../token/ERC1155/ERC1155Burnable.sol"; +import "../token/ERC1155/ERC1155Pausable.sol"; + +/** + * @dev {ERC1155} token, including: + * + * - ability for holders to burn (destroy) their tokens + * - a minter role that allows for token minting (creation) + * - a pauser role that allows to stop all token transfers + * + * This contract uses {AccessControl} to lock permissioned functions using the + * different roles - head to its documentation for details. + * + * The account that deploys the contract will be granted the minter and pauser + * roles, as well as the default admin role, which will let it grant both minter + * and pauser roles to other accounts. + */ +contract ERC1155PresetMinterPauser is Context, AccessControl, ERC1155Burnable, ERC1155Pausable { + bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); + bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE"); + + /** + * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE`, and `PAUSER_ROLE` to the account that + * deploys the contract. + */ + constructor(string memory uri) public ERC1155(uri) { + _setupRole(DEFAULT_ADMIN_ROLE, _msgSender()); + + _setupRole(MINTER_ROLE, _msgSender()); + _setupRole(PAUSER_ROLE, _msgSender()); + } + + /** + * @dev Creates `amount` new tokens for `to`, of token type `id`. + * + * See {ERC1155-_mint}. + * + * Requirements: + * + * - the caller must have the `MINTER_ROLE`. + */ + function mint(address to, uint256 id, uint256 amount, bytes memory data) public virtual { + require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint"); + + _mint(to, id, amount, data); + } + + /** + * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] variant of {mint}. + */ + function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public virtual { + require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint"); + + _mintBatch(to, ids, amounts, data); + } + + /** + * @dev Pauses all token transfers. + * + * See {ERC1155Pausable} and {Pausable-_pause}. + * + * Requirements: + * + * - the caller must have the `PAUSER_ROLE`. + */ + function pause() public virtual { + require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to pause"); + _pause(); + } + + /** + * @dev Unpauses all token transfers. + * + * See {ERC1155Pausable} and {Pausable-_unpause}. + * + * Requirements: + * + * - the caller must have the `PAUSER_ROLE`. + */ + function unpause() public virtual { + require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to unpause"); + _unpause(); + } + + function _beforeTokenTransfer( + address operator, + address from, + address to, + uint256[] memory ids, + uint256[] memory amounts, + bytes memory data + ) + internal virtual override(ERC1155, ERC1155Pausable) + { + super._beforeTokenTransfer(operator, from, to, ids, amounts, data); + } +} diff --git a/contracts/presets/ERC20PresetMinterPauser.sol b/contracts/presets/ERC20PresetMinterPauser.sol index f63974529..00175955f 100644 --- a/contracts/presets/ERC20PresetMinterPauser.sol +++ b/contracts/presets/ERC20PresetMinterPauser.sol @@ -20,7 +20,7 @@ import "../token/ERC20/ERC20Pausable.sol"; * * The account that deploys the contract will be granted the minter and pauser * roles, as well as the default admin role, which will let it grant both minter - * and pauser roles to other accounts + * and pauser roles to other accounts. */ contract ERC20PresetMinterPauser is Context, AccessControl, ERC20Burnable, ERC20Pausable { bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); diff --git a/contracts/presets/ERC721PresetMinterPauserAutoId.sol b/contracts/presets/ERC721PresetMinterPauserAutoId.sol index 11fc0d87d..9a9ce0dee 100644 --- a/contracts/presets/ERC721PresetMinterPauserAutoId.sol +++ b/contracts/presets/ERC721PresetMinterPauserAutoId.sol @@ -22,7 +22,7 @@ import "../token/ERC721/ERC721Pausable.sol"; * * The account that deploys the contract will be granted the minter and pauser * roles, as well as the default admin role, which will let it grant both minter - * and pauser roles to other accounts + * and pauser roles to other accounts. */ contract ERC721PresetMinterPauserAutoId is Context, AccessControl, ERC721Burnable, ERC721Pausable { using Counters for Counters.Counter; diff --git a/contracts/presets/README.adoc b/contracts/presets/README.adoc index e649928e4..160aeae09 100644 --- a/contracts/presets/README.adoc +++ b/contracts/presets/README.adoc @@ -11,3 +11,5 @@ TIP: Intermediate and advanced users can use these as starting points when writi {{ERC20PresetMinterPauser}} {{ERC721PresetMinterPauserAutoId}} + +{{ERC1155PresetMinterPauser}} diff --git a/contracts/token/ERC1155/ERC1155.sol b/contracts/token/ERC1155/ERC1155.sol index e28b8cb10..881502bc1 100644 --- a/contracts/token/ERC1155/ERC1155.sol +++ b/contracts/token/ERC1155/ERC1155.sol @@ -5,9 +5,10 @@ pragma solidity ^0.6.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"; -import "../../introspection/ERC165.sol"; /** * @title Standard ERC1155 token @@ -16,7 +17,7 @@ import "../../introspection/ERC165.sol"; * See https://eips.ethereum.org/EIPS/eip-1155 * Originally based on code by Enjin: https://github.com/enjin/erc-1155 */ -contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { +contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI { using SafeMath for uint256; using Address for address; @@ -67,7 +68,7 @@ contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { * on the token type ID substituion mechanism * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP]. * - * Clients calling this function must replace the `{id}` substring with the + * Clients calling this function must replace the `\{id\}` substring with the * actual token type ID. */ function uri(uint256) external view override returns (string memory) { @@ -75,13 +76,11 @@ contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { } /** - @dev Get the specified address' balance for token with specified ID. - - Attempting to query the zero account for a balance will result in a revert. - - @param account The address of the token holder - @param id ID of the token - @return The account's balance of the token type requested + * @dev See {IERC1155-balanceOf}. + * + * Requirements: + * + * - `account` cannot be the zero address. */ function balanceOf(address account, uint256 id) public view override returns (uint256) { require(account != address(0), "ERC1155: balance query for the zero address"); @@ -89,13 +88,11 @@ contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { } /** - @dev Get the balance of multiple account/token pairs. - - If any of the query accounts is the zero account, this query will revert. - - @param accounts The addresses of the token holders - @param ids IDs of the tokens - @return Balances for each account and token id pair + * @dev See {IERC1155-balanceOfBatch}. + * + * Requirements: + * + * - `accounts` and `ids` must have the same length. */ function balanceOfBatch( address[] memory accounts, @@ -106,12 +103,12 @@ contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { override returns (uint256[] memory) { - require(accounts.length == ids.length, "ERC1155: accounts and IDs must have same lengths"); + 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: some address in batch balance query is zero"); + require(accounts[i] != address(0), "ERC1155: batch balance query for the zero address"); batchBalances[i] = _balances[ids[i]][accounts[i]]; } @@ -119,110 +116,93 @@ contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { } /** - * @dev Sets or unsets the approval of a given operator. - * - * An operator is allowed to transfer all tokens of the sender on their behalf. - * - * Because an account already has operator privileges for itself, this function will revert - * if the account attempts to set the approval status for itself. - * - * @param operator address to set the approval - * @param approved representing the status of the approval to be set + * @dev See {IERC1155-setApprovalForAll}. */ function setApprovalForAll(address operator, bool approved) public virtual override { - require(msg.sender != operator, "ERC1155: cannot set approval status for self"); - _operatorApprovals[msg.sender][operator] = approved; - emit ApprovalForAll(msg.sender, operator, approved); + require(_msgSender() != operator, "ERC1155: setting approval status for self"); + + _operatorApprovals[_msgSender()][operator] = approved; + emit ApprovalForAll(_msgSender(), operator, approved); } /** - @notice Queries the approval status of an operator for a given account. - @param account The account of the Tokens - @param operator Address of authorized operator - @return True if the operator is approved, false if not - */ + * @dev See {IERC1155-isApprovedForAll}. + */ function isApprovedForAll(address account, address operator) public view override returns (bool) { return _operatorApprovals[account][operator]; } /** - @dev Transfers `value` amount of an `id` from the `from` address to the `to` address specified. - Caller must be approved to manage the tokens being transferred out of the `from` account. - If `to` is a smart contract, will call `onERC1155Received` on `to` and act appropriately. - @param from Source address - @param to Target address - @param id ID of the token type - @param value Transfer amount - @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver - */ + * @dev See {IERC1155-safeTransferFrom}. + */ function safeTransferFrom( address from, address to, uint256 id, - uint256 value, + uint256 amount, bytes memory data ) public virtual override { - require(to != address(0), "ERC1155: target address must be non-zero"); + require(to != address(0), "ERC1155: transfer to the zero address"); require( - from == msg.sender || isApprovedForAll(from, msg.sender) == true, - "ERC1155: need operator approval for 3rd party transfers" + from == _msgSender() || isApprovedForAll(from, _msgSender()), + "ERC1155: caller is not owner nor approved" ); - _balances[id][from] = _balances[id][from].sub(value, "ERC1155: insufficient balance for transfer"); - _balances[id][to] = _balances[id][to].add(value); + address operator = _msgSender(); - emit TransferSingle(msg.sender, from, to, id, value); + _beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data); - _doSafeTransferAcceptanceCheck(msg.sender, from, to, id, value, 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 Transfers `values` amount(s) of `ids` from the `from` address to the - `to` address specified. Caller must be approved to manage the tokens being - transferred out of the `from` account. If `to` is a smart contract, will - call `onERC1155BatchReceived` on `to` and act appropriately. - @param from Source address - @param to Target address - @param ids IDs of each token type - @param values Transfer amounts per token type - @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver - */ + * @dev See {IERC1155-safeBatchTransferFrom}. + */ function safeBatchTransferFrom( address from, address to, uint256[] memory ids, - uint256[] memory values, + uint256[] memory amounts, bytes memory data ) public virtual override { - require(ids.length == values.length, "ERC1155: IDs and values must have same lengths"); - require(to != address(0), "ERC1155: target address must be non-zero"); + require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch"); + require(to != address(0), "ERC1155: transfer to the zero address"); require( - from == msg.sender || isApprovedForAll(from, msg.sender) == true, - "ERC1155: need operator approval for 3rd party transfers" + 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 value = values[i]; + uint256 amount = amounts[i]; _balances[id][from] = _balances[id][from].sub( - value, - "ERC1155: insufficient balance of some token type for transfer" + amount, + "ERC1155: insufficient balance for transfer" ); - _balances[id][to] = _balances[id][to].add(value); + _balances[id][to] = _balances[id][to].add(amount); } - emit TransferBatch(msg.sender, from, to, ids, values); + emit TransferBatch(operator, from, to, ids, amounts); - _doSafeBatchTransferAcceptanceCheck(msg.sender, from, to, ids, values, data); + _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data); } /** @@ -230,11 +210,11 @@ contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { * substituion mechanism * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP]. * - * By this mechanism, any occurence of the `{id}` substring in either the - * URI or any of the values in the JSON file at said URI will be replaced by + * By this mechanism, any occurence 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 + * 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. @@ -249,93 +229,154 @@ contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { } /** - * @dev Internal function to mint an amount of a token with the given ID - * @param to The address that will own the minted token - * @param id ID of the token to be minted - * @param value Amount of the token to be minted - * @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver + * @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 to, uint256 id, uint256 value, bytes memory data) internal virtual { - require(to != address(0), "ERC1155: mint to the zero address"); + function _mint(address account, uint256 id, uint256 amount, bytes memory data) internal virtual { + require(account != address(0), "ERC1155: mint to the zero address"); - _balances[id][to] = _balances[id][to].add(value); - emit TransferSingle(msg.sender, address(0), to, id, value); + address operator = _msgSender(); - _doSafeTransferAcceptanceCheck(msg.sender, address(0), to, id, value, data); + _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 Internal function to batch mint amounts of tokens with the given IDs - * @param to The address that will own the minted token - * @param ids IDs of the tokens to be minted - * @param values Amounts of the tokens to be minted - * @param data Data forwarded to `onERC1155Received` if `to` is a contract receiver + * @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 values, bytes memory data) internal virtual { - require(to != address(0), "ERC1155: batch mint to the zero address"); - require(ids.length == values.length, "ERC1155: minted IDs and values must have same lengths"); + 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"); - for(uint i = 0; i < ids.length; i++) { - _balances[ids[i]][to] = values[i].add(_balances[ids[i]][to]); + 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(msg.sender, address(0), to, ids, values); + emit TransferBatch(operator, address(0), to, ids, amounts); - _doSafeBatchTransferAcceptanceCheck(msg.sender, address(0), to, ids, values, data); + _doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data); } /** - * @dev Internal function to burn an amount of a token with the given ID - * @param account Account which owns the token to be burnt - * @param id ID of the token to be burnt - * @param value Amount of the token to be burnt + * @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 value) internal virtual { - require(account != address(0), "ERC1155: attempting to burn tokens on zero account"); + 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), ""); _balances[id][account] = _balances[id][account].sub( - value, - "ERC1155: attempting to burn more than balance" + amount, + "ERC1155: burn amount exceeds balance" ); - emit TransferSingle(msg.sender, account, address(0), id, value); + + emit TransferSingle(operator, account, address(0), id, amount); } /** - * @dev Internal function to batch burn an amounts of tokens with the given IDs - * @param account Account which owns the token to be burnt - * @param ids IDs of the tokens to be burnt - * @param values Amounts of the tokens to be burnt + * @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 values) internal virtual { - require(account != address(0), "ERC1155: attempting to burn batch of tokens on zero account"); - require(ids.length == values.length, "ERC1155: burnt IDs and values must have same lengths"); + 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"); - for(uint i = 0; i < ids.length; i++) { + 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( - values[i], - "ERC1155: attempting to burn more than balance for some token" + amounts[i], + "ERC1155: burn amount exceeds balance" ); } - emit TransferBatch(msg.sender, account, address(0), ids, values); + 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 value, + uint256 amount, bytes memory data ) private { - if(to.isContract()) { - require( - IERC1155Receiver(to).onERC1155Received(operator, from, id, value, data) == - IERC1155Receiver(to).onERC1155Received.selector, - "ERC1155: got unknown value from onERC1155Received" - ); + 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"); + } } } @@ -344,17 +385,28 @@ contract ERC1155 is ERC165, IERC1155, IERC1155MetadataURI { address from, address to, uint256[] memory ids, - uint256[] memory values, + uint256[] memory amounts, bytes memory data ) private { - if(to.isContract()) { - require( - IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, values, data) == - IERC1155Receiver(to).onERC1155BatchReceived.selector, - "ERC1155: got unknown value from onERC1155BatchReceived" - ); + 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; + } } diff --git a/contracts/token/ERC1155/ERC1155Burnable.sol b/contracts/token/ERC1155/ERC1155Burnable.sol new file mode 100644 index 000000000..af18e2814 --- /dev/null +++ b/contracts/token/ERC1155/ERC1155Burnable.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.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. + */ +abstract contract ERC1155Burnable is ERC1155 { + function burn(address account, uint256 id, uint256 value) public virtual { + 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 virtual { + require( + account == _msgSender() || isApprovedForAll(account, _msgSender()), + "ERC1155: caller is not owner nor approved" + ); + + _burnBatch(account, ids, values); + } +} diff --git a/contracts/token/ERC1155/ERC1155Holder.sol b/contracts/token/ERC1155/ERC1155Holder.sol index f70f4da3c..052bdc728 100644 --- a/contracts/token/ERC1155/ERC1155Holder.sol +++ b/contracts/token/ERC1155/ERC1155Holder.sol @@ -5,15 +5,11 @@ pragma solidity ^0.6.0; import "./ERC1155Receiver.sol"; contract ERC1155Holder is ERC1155Receiver { - - function onERC1155Received(address, address, uint256, uint256, bytes calldata) external override virtual returns (bytes4) - { + function onERC1155Received(address, address, uint256, uint256, bytes memory) public virtual override returns (bytes4) { return this.onERC1155Received.selector; } - - function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata) external override virtual returns (bytes4) - { + function onERC1155BatchReceived(address, address, uint256[] memory, uint256[] memory, bytes memory) public virtual override returns (bytes4) { return this.onERC1155BatchReceived.selector; } } diff --git a/contracts/token/ERC1155/ERC1155Pausable.sol b/contracts/token/ERC1155/ERC1155Pausable.sol new file mode 100644 index 000000000..a83f1c8df --- /dev/null +++ b/contracts/token/ERC1155/ERC1155Pausable.sol @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.6.0; + +import "./ERC1155.sol"; +import "../../utils/Pausable.sol"; + +/** + * @dev ERC1155 token with pausable token transfers, minting and burning. + * + * Useful for scenarios such as preventing trades until the end of an evaluation + * period, or having an emergency switch for freezing all token transfers in the + * event of a large bug. + */ +abstract contract ERC1155Pausable is ERC1155, Pausable { + /** + * @dev See {ERC1155-_beforeTokenTransfer}. + * + * Requirements: + * + * - the contract must not be paused. + */ + function _beforeTokenTransfer( + address operator, + address from, + address to, + uint256[] memory ids, + uint256[] memory amounts, + bytes memory data + ) + internal virtual override + { + super._beforeTokenTransfer(operator, from, to, ids, amounts, data); + + require(!paused(), "ERC1155Pausable: token transfer while paused"); + } +} diff --git a/contracts/token/ERC1155/IERC1155.sol b/contracts/token/ERC1155/IERC1155.sol index 07c7f1693..55116c0be 100644 --- a/contracts/token/ERC1155/IERC1155.sol +++ b/contracts/token/ERC1155/IERC1155.sol @@ -5,27 +5,97 @@ pragma solidity ^0.6.2; import "../../introspection/IERC165.sol"; /** - @title ERC-1155 Multi Token Standard basic interface - @dev See https://eips.ethereum.org/EIPS/eip-1155 + * @dev Required interface of an ERC1155 compliant contract, as defined in the + * https://eips.ethereum.org/EIPS/eip-1155[EIP]. */ interface IERC1155 is IERC165 { + /** + * @dev Emitted when `value` tokens of token type `id` are transfered 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); - function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes calldata data) external; + /** + * @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; - function safeBatchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata values, 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; } diff --git a/contracts/token/ERC1155/IERC1155MetadataURI.sol b/contracts/token/ERC1155/IERC1155MetadataURI.sol index aff9ac96a..c437b8bbf 100644 --- a/contracts/token/ERC1155/IERC1155MetadataURI.sol +++ b/contracts/token/ERC1155/IERC1155MetadataURI.sol @@ -9,5 +9,11 @@ import "./IERC1155.sol"; * in the https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[EIP]. */ interface 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); } diff --git a/contracts/token/ERC1155/README.adoc b/contracts/token/ERC1155/README.adoc index 5e8dafa4c..ff3ea6246 100644 --- a/contracts/token/ERC1155/README.adoc +++ b/contracts/token/ERC1155/README.adoc @@ -2,11 +2,14 @@ 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`. +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. +{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. -`ERC1155Holder` implements the `IERC1155Receiver` interface for contracts that can receive (and hold) ERC1155 tokens. +Additionally there are multiple custom extensions, including: + +* designation of addresses that can pause token transfers for all users ({ERC1155Pausable}). +* destruction of own tokens ({ERC1155Burnable}). == Core @@ -18,4 +21,12 @@ The EIP consists of three interfaces which fulfill different roles, found here a {{IERC1155Receiver}} +== Extensions + +{{ERC1155Pausable}} + +{{ERC1155Burnable}} + +== Convenience + {{ERC1155Holder}} diff --git a/contracts/token/ERC20/ERC20.sol b/contracts/token/ERC20/ERC20.sol index 1c69521ef..a8ab97f27 100644 --- a/contracts/token/ERC20/ERC20.sol +++ b/contracts/token/ERC20/ERC20.sol @@ -12,7 +12,7 @@ import "../../utils/Address.sol"; * * This implementation is agnostic to the way tokens are created. This means * that a supply mechanism has to be added in a derived contract using {_mint}. - * For a generic mechanism see {ERC20MinterPauser}. + * For a generic mechanism see {ERC20PresetMinterPauser}. * * TIP: For a detailed writeup see our guide * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How diff --git a/contracts/token/ERC721/README.adoc b/contracts/token/ERC721/README.adoc index 0b95c48e8..a774ebed2 100644 --- a/contracts/token/ERC721/README.adoc +++ b/contracts/token/ERC721/README.adoc @@ -8,8 +8,6 @@ The EIP consists of three interfaces, found here as {IERC721}, {IERC721Metadata} Additionally, {IERC721Receiver} can be used to prevent tokens from becoming forever locked in contracts. Imagine sending an in-game item to an exchange address that can't send it back!. When using <>, the token contract checks to see that the receiver is an {IERC721Receiver}, which implies that it knows how to handle {ERC721} tokens. If you're writing a contract that needs to receive {ERC721} tokens, you'll want to include this interface. -Finally, some custom extensions are also included: - Additionally there are multiple custom extensions, including: * designation of addresses that can pause token transfers for all users ({ERC721Pausable}). diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index d155159eb..d9d4f182a 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -7,6 +7,7 @@ *** xref:erc20-supply.adoc[Creating Supply] ** xref:erc721.adoc[ERC721] ** xref:erc777.adoc[ERC777] +** xref:erc1155.adoc[ERC1155] * xref:gsn.adoc[Gas Station Network] ** xref:gsn-strategies.adoc[Strategies] diff --git a/docs/modules/ROOT/pages/erc1155.adoc b/docs/modules/ROOT/pages/erc1155.adoc new file mode 100644 index 000000000..863460ae8 --- /dev/null +++ b/docs/modules/ROOT/pages/erc1155.adoc @@ -0,0 +1,21 @@ += ERC1155 + +ERC1155 is a novel token standard that aims to take the best from previous standards to create a xref:tokens.adoc#different-kinds-of-tokens[*fungibility-agnostic*] and *gas-efficient* xref:tokens.adoc#but_first_coffee_a_primer_on_token_contracts[token contract]. + +TIP: ERC1155 draws ideas from all of xref:erc20.adoc[ERC20], xref:721.adoc[ERC721], and xref:erc777.adoc[ERC777]. If you're unfamiliar with those standards, head to their guides before moving on. + +[[multi-token-standard]] +== Multi Token Standard + +The distinctive feature of ERC1155 is that it uses a single smart contract to represent multiple tokens at once. This is why its xref:api:token/ERC1155.adoc#IERC1155-balanceOf-address-uint256-[`balanceOf`] function differs from ERC20's and ERC777's: it has an additional `id` argument for the identifier of the token that you want to query the balance of. + +This is similar to how ERC721 does things, but in that standard a token `id` has no concept of balance: each token is non-fungible and exists or doesn't. The xref:api:token/ERC721.adoc#IERC721-balanceOf-address-[`balanceOf`] function refers to _how many different tokens_ an account has, not how many of each. On the other hand, in ERC1155 accounts have a distinct balance for each token `id`, and non-fungible tokens are implemented by simply minting a single one of them. + +This approach leads to massive gas savings for projects that require multiple tokens. Instead of deploying a new contract for each token type, a single ERC1155 token contract can hold the entire system state, reducing deployment costs and complexity. + +[[batch-operations]] +== Batch Operations + +Because all state is held in a single contract, it is possible to operate over multiple tokens in a single transaction very efficiently. The standard provides two functions, xref:api:token/ERC1155.adoc#IERC1155-balanceOfBatch-address---uint256---[`balanceOfBatch`] and xref:api:token/ERC1155.adoc#IERC1155-safeBatchTransferFrom-address-address-uint256---uint256---bytes-[`safeBatchTransferFrom`], that make querying multiple balances and transferring multiple tokens simpler and less gas-intensive. + +In the spirit of the standard, we've also included batch operations in the non-standard functions, such as xref:api:token/ERC1155.adoc#ERC1155-_mintBatch-address-uint256---uint256---bytes-[`_mintBatch`]. diff --git a/docs/modules/ROOT/pages/tokens.adoc b/docs/modules/ROOT/pages/tokens.adoc index 3bbbda666..7f3253f48 100644 --- a/docs/modules/ROOT/pages/tokens.adoc +++ b/docs/modules/ROOT/pages/tokens.adoc @@ -4,6 +4,7 @@ Ah, the "token": blockchain's most powerful and most misunderstood tool. A token is a _representation of something in the blockchain_. This something can be money, time, services, shares in a company, a virtual pet, anything. By representing things as tokens, we can allow smart contracts to interact with them, exchange them, create or destroy them. +[[but_first_coffee_a_primer_on_token_contracts]] == But First, [strikethrough]#Coffee# a Primer on Token Contracts Much of the confusion surrounding tokens comes from two concepts getting mixed up: _token contracts_ and the actual _tokens_. @@ -28,3 +29,4 @@ You've probably heard of the ERC20 or ERC721 token standards, and that's why you * xref:erc20.adoc[ERC20]: the most widespread token standard for fungible assets, albeit somewhat limited by its simplicity. * xref:erc721.adoc[ERC721]: the de-facto solution for non-fungible tokens, often used for collectibles and games. * xref:erc777.adoc[ERC777]: a richer standard for fungible tokens, enabling new use cases and building on past learnings. Backwards compatible with ERC20. + * xref:erc1155.adoc[ERC1155]: a novel standard for multi-tokens, allowing for a single contract to represent multiple fungible and non-fungible tokens, along with batched operations for increased gas efficiency. diff --git a/scripts/gen-nav.js b/scripts/gen-nav.js index 14d5ce99a..7e35a544e 100644 --- a/scripts/gen-nav.js +++ b/scripts/gen-nav.js @@ -24,7 +24,7 @@ const links = files.map((file) => { // Case-insensitive sort based on titles (so 'token/ERC20' gets sorted as 'erc20') const sortedLinks = links.sort(function (a, b) { - return a.title.toLowerCase().localeCompare(b.title.toLowerCase()); + return a.title.toLowerCase().localeCompare(b.title.toLowerCase(), undefined, { numeric: true }); }); for (const link of sortedLinks) { diff --git a/test/presets/ERC1155PresetMinterPauser.test.js b/test/presets/ERC1155PresetMinterPauser.test.js new file mode 100644 index 000000000..d69675d72 --- /dev/null +++ b/test/presets/ERC1155PresetMinterPauser.test.js @@ -0,0 +1,136 @@ +const { accounts, contract, web3 } = require('@openzeppelin/test-environment'); + +const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers'); +const { ZERO_ADDRESS } = constants; + +const { expect } = require('chai'); + +const ERC1155PresetMinterPauser = contract.fromArtifact('ERC1155PresetMinterPauser'); + +describe('ERC1155PresetMinterPauser', function () { + const [ deployer, other ] = accounts; + + const firstTokenId = new BN('845'); + const firstTokenIdAmount = new BN('5000'); + + const secondTokenId = new BN('48324'); + const secondTokenIdAmount = new BN('77875'); + + const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000'; + const MINTER_ROLE = web3.utils.soliditySha3('MINTER_ROLE'); + const PAUSER_ROLE = web3.utils.soliditySha3('PAUSER_ROLE'); + + const uri = 'https://token.com'; + + beforeEach(async function () { + this.token = await ERC1155PresetMinterPauser.new(uri, { from: deployer }); + }); + + it('deployer has the default admin role', async function () { + expect(await this.token.getRoleMemberCount(DEFAULT_ADMIN_ROLE)).to.be.bignumber.equal('1'); + expect(await this.token.getRoleMember(DEFAULT_ADMIN_ROLE, 0)).to.equal(deployer); + }); + + it('deployer has the minter role', async function () { + expect(await this.token.getRoleMemberCount(MINTER_ROLE)).to.be.bignumber.equal('1'); + expect(await this.token.getRoleMember(MINTER_ROLE, 0)).to.equal(deployer); + }); + + it('deployer has the pauser role', async function () { + expect(await this.token.getRoleMemberCount(PAUSER_ROLE)).to.be.bignumber.equal('1'); + expect(await this.token.getRoleMember(PAUSER_ROLE, 0)).to.equal(deployer); + }); + + it('minter and pauser role admin is the default admin', async function () { + expect(await this.token.getRoleAdmin(MINTER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE); + expect(await this.token.getRoleAdmin(PAUSER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE); + }); + + describe('minting', function () { + it('deployer can mint tokens', async function () { + const receipt = await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer }); + expectEvent(receipt, 'TransferSingle', + { operator: deployer, from: ZERO_ADDRESS, to: other, value: firstTokenIdAmount, id: firstTokenId } + ); + + expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount); + }); + + it('other accounts cannot mint tokens', async function () { + await expectRevert( + this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: other }), + 'ERC1155PresetMinterPauser: must have minter role to mint' + ); + }); + }); + + describe('batched minting', function () { + it('deployer can batch mint tokens', async function () { + const receipt = await this.token.mintBatch( + other, [firstTokenId, secondTokenId], [firstTokenIdAmount, secondTokenIdAmount], '0x', { from: deployer } + ); + + expectEvent(receipt, 'TransferBatch', + { operator: deployer, from: ZERO_ADDRESS, to: other } + ); + + expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal(firstTokenIdAmount); + }); + + it('other accounts cannot batch mint tokens', async function () { + await expectRevert( + this.token.mintBatch( + other, [firstTokenId, secondTokenId], [firstTokenIdAmount, secondTokenIdAmount], '0x', { from: other } + ), + 'ERC1155PresetMinterPauser: must have minter role to mint' + ); + }); + }); + + describe('pausing', function () { + it('deployer can pause', async function () { + const receipt = await this.token.pause({ from: deployer }); + expectEvent(receipt, 'Paused', { account: deployer }); + + expect(await this.token.paused()).to.equal(true); + }); + + it('deployer can unpause', async function () { + await this.token.pause({ from: deployer }); + + const receipt = await this.token.unpause({ from: deployer }); + expectEvent(receipt, 'Unpaused', { account: deployer }); + + expect(await this.token.paused()).to.equal(false); + }); + + it('cannot mint while paused', async function () { + await this.token.pause({ from: deployer }); + + await expectRevert( + this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer }), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + it('other accounts cannot pause', async function () { + await expectRevert( + this.token.pause({ from: other }), + 'ERC1155PresetMinterPauser: must have pauser role to pause' + ); + }); + }); + + describe('burning', function () { + it('holders can burn their tokens', async function () { + await this.token.mint(other, firstTokenId, firstTokenIdAmount, '0x', { from: deployer }); + + const receipt = await this.token.burn(other, firstTokenId, firstTokenIdAmount.subn(1), { from: other }); + expectEvent(receipt, 'TransferSingle', + { operator: other, from: other, to: ZERO_ADDRESS, value: firstTokenIdAmount.subn(1), id: firstTokenId } + ); + + expect(await this.token.balanceOf(other, firstTokenId)).to.be.bignumber.equal('1'); + }); + }); +}); diff --git a/test/presets/ERC721PresetMinterPauserAutoId.js b/test/presets/ERC721PresetMinterPauserAutoId.test.js similarity index 100% rename from test/presets/ERC721PresetMinterPauserAutoId.js rename to test/presets/ERC721PresetMinterPauserAutoId.test.js diff --git a/test/token/ERC1155/ERC1155.behavior.js b/test/token/ERC1155/ERC1155.behavior.js index 8f4100326..fb25d175c 100644 --- a/test/token/ERC1155/ERC1155.behavior.js +++ b/test/token/ERC1155/ERC1155.behavior.js @@ -90,7 +90,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, [firstTokenHolder, secondTokenHolder, firstTokenHolder, secondTokenHolder], [firstTokenId, secondTokenId, unknownTokenId] ), - 'ERC1155: accounts and IDs must have same lengths' + 'ERC1155: accounts and ids length mismatch' ); }); @@ -100,7 +100,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, [firstTokenHolder, secondTokenHolder, ZERO_ADDRESS], [firstTokenId, secondTokenId, unknownTokenId] ), - 'ERC1155: some address in batch balance query is zero' + 'ERC1155: batch balance query for the zero address' ); }); @@ -168,7 +168,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, it('reverts if attempting to approve self as an operator', async function () { await expectRevert( this.token.setApprovalForAll(multiTokenHolder, true, { from: multiTokenHolder }), - 'ERC1155: cannot set approval status for self' + 'ERC1155: setting approval status for self' ); }); }); @@ -213,7 +213,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, '0x', { from: multiTokenHolder }, ), - 'ERC1155: target address must be non-zero' + 'ERC1155: transfer to the zero address' ); }); @@ -275,7 +275,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount, '0x', { from: proxy, }), - 'ERC1155: need operator approval for 3rd party transfers' + 'ERC1155: caller is not owner nor approved' ); }); }); @@ -391,7 +391,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, this.token.safeTransferFrom(multiTokenHolder, this.receiver.address, firstTokenId, firstAmount, '0x', { from: multiTokenHolder, }), - 'ERC1155: got unknown value from onERC1155Received' + 'ERC1155: ERC1155Receiver rejected tokens' ); }); }); @@ -450,7 +450,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, [firstAmount, secondAmount.addn(1)], '0x', { from: multiTokenHolder } ), - 'ERC1155: insufficient balance of some token type for transfer' + 'ERC1155: insufficient balance for transfer' ); }); @@ -462,7 +462,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, [firstAmount, secondAmount], '0x', { from: multiTokenHolder } ), - 'ERC1155: IDs and values must have same lengths' + 'ERC1155: ids and amounts length mismatch' ); }); @@ -474,7 +474,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, [firstAmount, secondAmount], '0x', { from: multiTokenHolder } ), - 'ERC1155: target address must be non-zero' + 'ERC1155: transfer to the zero address' ); }); @@ -538,7 +538,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, [firstAmount, secondAmount], '0x', { from: proxy } ), - 'ERC1155: need operator approval for 3rd party transfers' + 'ERC1155: transfer caller is not owner nor approved' ); }); }); @@ -658,7 +658,7 @@ function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, [firstAmount, secondAmount], '0x', { from: multiTokenHolder }, ), - 'ERC1155: got unknown value from onERC1155BatchReceived' + 'ERC1155: ERC1155Receiver rejected tokens' ); }); }); diff --git a/test/token/ERC1155/ERC1155.test.js b/test/token/ERC1155/ERC1155.test.js index 0debd9712..a9248830e 100644 --- a/test/token/ERC1155/ERC1155.test.js +++ b/test/token/ERC1155/ERC1155.test.js @@ -9,12 +9,12 @@ const { shouldBehaveLikeERC1155 } = require('./ERC1155.behavior'); const ERC1155Mock = contract.fromArtifact('ERC1155Mock'); describe('ERC1155', function () { - const [creator, tokenHolder, tokenBatchHolder, ...otherAccounts] = accounts; + const [operator, tokenHolder, tokenBatchHolder, ...otherAccounts] = accounts; const initialURI = 'https://token-cdn-domain/{id}.json'; beforeEach(async function () { - this.token = await ERC1155Mock.new(initialURI, { from: creator }); + this.token = await ERC1155Mock.new(initialURI); }); shouldBehaveLikeERC1155(otherAccounts); @@ -30,8 +30,8 @@ describe('ERC1155', function () { const data = '0xcafebabe'; - describe('_mint(address, uint256, uint256, bytes memory)', function () { - it('reverts with a null destination address', async function () { + 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' @@ -40,18 +40,12 @@ describe('ERC1155', function () { context('with minted tokens', function () { beforeEach(async function () { - ({ logs: this.logs } = await this.token.mint( - tokenHolder, - tokenId, - mintAmount, - data, - { from: creator } - )); + ({ 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: creator, + operator, from: ZERO_ADDRESS, to: tokenHolder, id: tokenId, @@ -60,26 +54,23 @@ describe('ERC1155', function () { }); it('credits the minted amount of tokens', async function () { - expect(await this.token.balanceOf( - tokenHolder, - tokenId - )).to.be.bignumber.equal(mintAmount); + expect(await this.token.balanceOf(tokenHolder, tokenId)).to.be.bignumber.equal(mintAmount); }); }); }); - describe('_mintBatch(address, uint256[] memory, uint256[] memory, bytes memory)', function () { - it('reverts with a null destination address', async function () { + describe('_mintBatch', function () { + it('reverts with a zero destination address', async function () { await expectRevert( this.token.mintBatch(ZERO_ADDRESS, tokenBatchIds, mintAmounts, data), - 'ERC1155: batch mint to the zero address' + '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: minted IDs and values must have same lengths' + 'ERC1155: ids and amounts length mismatch' ); }); @@ -90,17 +81,15 @@ describe('ERC1155', function () { tokenBatchIds, mintAmounts, data, - { from: creator } + { from: operator } )); }); it('emits a TransferBatch event', function () { expectEvent.inLogs(this.logs, 'TransferBatch', { - operator: creator, + operator, from: ZERO_ADDRESS, to: tokenBatchHolder, - // ids: tokenBatchIds, - // values: mintAmounts, }); }); @@ -117,18 +106,18 @@ describe('ERC1155', function () { }); }); - describe('_burn(address, uint256, uint256)', function () { + describe('_burn', function () { it('reverts when burning the zero account\'s tokens', async function () { await expectRevert( this.token.burn(ZERO_ADDRESS, tokenId, mintAmount), - 'ERC1155: attempting to burn tokens on zero account' + '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: attempting to burn more than balance' + 'ERC1155: burn amount exceeds balance' ); }); @@ -139,13 +128,13 @@ describe('ERC1155', function () { tokenHolder, tokenId, burnAmount, - { from: creator } + { from: operator } )); }); it('emits a TransferSingle event', function () { expectEvent.inLogs(this.logs, 'TransferSingle', { - operator: creator, + operator, from: tokenHolder, to: ZERO_ADDRESS, id: tokenId, @@ -162,25 +151,25 @@ describe('ERC1155', function () { }); }); - describe('_burnBatch(address, uint256[] memory, uint256[] memory)', function () { + describe('_burnBatch', function () { it('reverts when burning the zero account\'s tokens', async function () { await expectRevert( this.token.burnBatch(ZERO_ADDRESS, tokenBatchIds, burnAmounts), - 'ERC1155: attempting to burn batch of tokens on zero account' + '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: burnt IDs and values must have same lengths' + '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: attempting to burn more than balance for some token' + 'ERC1155: burn amount exceeds balance' ); }); @@ -191,13 +180,13 @@ describe('ERC1155', function () { tokenBatchHolder, tokenBatchIds, burnAmounts, - { from: creator } + { from: operator } )); }); it('emits a TransferBatch event', function () { expectEvent.inLogs(this.logs, 'TransferBatch', { - operator: creator, + operator, from: tokenBatchHolder, to: ZERO_ADDRESS, // ids: tokenBatchIds, diff --git a/test/token/ERC1155/ERC1155Burnable.test.js b/test/token/ERC1155/ERC1155Burnable.test.js new file mode 100644 index 000000000..5de1c88e5 --- /dev/null +++ b/test/token/ERC1155/ERC1155Burnable.test.js @@ -0,0 +1,69 @@ +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' + ); + }); + }); +}); diff --git a/test/token/ERC1155/ERC1155Pausable.test.js b/test/token/ERC1155/ERC1155Pausable.test.js new file mode 100644 index 000000000..502920296 --- /dev/null +++ b/test/token/ERC1155/ERC1155Pausable.test.js @@ -0,0 +1,110 @@ +const { accounts, contract } = require('@openzeppelin/test-environment'); + +const { BN, expectRevert } = require('@openzeppelin/test-helpers'); + +const { expect } = require('chai'); + +const ERC1155PausableMock = contract.fromArtifact('ERC1155PausableMock'); + +describe('ERC1155Pausable', function () { + const [ holder, operator, receiver, other ] = accounts; + + const uri = 'https://token.com'; + + beforeEach(async function () { + this.token = await ERC1155PausableMock.new(uri); + }); + + context('when token is paused', function () { + const firstTokenId = new BN('37'); + const firstTokenAmount = new BN('42'); + + const secondTokenId = new BN('19842'); + const secondTokenAmount = new BN('23'); + + beforeEach(async function () { + await this.token.setApprovalForAll(operator, true, { from: holder }); + await this.token.mint(holder, firstTokenId, firstTokenAmount, '0x'); + + await this.token.pause(); + }); + + it('reverts when trying to safeTransferFrom from holder', async function () { + await expectRevert( + this.token.safeTransferFrom(holder, receiver, firstTokenId, firstTokenAmount, '0x', { from: holder }), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + it('reverts when trying to safeTransferFrom from operator', async function () { + await expectRevert( + this.token.safeTransferFrom(holder, receiver, firstTokenId, firstTokenAmount, '0x', { from: operator }), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + it('reverts when trying to safeBatchTransferFrom from holder', async function () { + await expectRevert( + this.token.safeBatchTransferFrom(holder, receiver, [firstTokenId], [firstTokenAmount], '0x', { from: holder }), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + it('reverts when trying to safeBatchTransferFrom from operator', async function () { + await expectRevert( + this.token.safeBatchTransferFrom( + holder, receiver, [firstTokenId], [firstTokenAmount], '0x', { from: operator } + ), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + it('reverts when trying to mint', async function () { + await expectRevert( + this.token.mint(holder, secondTokenId, secondTokenAmount, '0x'), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + it('reverts when trying to mintBatch', async function () { + await expectRevert( + this.token.mintBatch(holder, [secondTokenId], [secondTokenAmount], '0x'), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + it('reverts when trying to burn', async function () { + await expectRevert( + this.token.burn(holder, firstTokenId, firstTokenAmount), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + it('reverts when trying to burnBatch', async function () { + await expectRevert( + this.token.burn(holder, [firstTokenId], [firstTokenAmount]), + 'ERC1155Pausable: token transfer while paused' + ); + }); + + describe('setApprovalForAll', function () { + it('approves an operator', async function () { + await this.token.setApprovalForAll(other, true, { from: holder }); + expect(await this.token.isApprovedForAll(holder, other)).to.equal(true); + }); + }); + + describe('balanceOf', function () { + it('returns the amount of tokens owned by the given address', async function () { + const balance = await this.token.balanceOf(holder, firstTokenId); + expect(balance).to.be.bignumber.equal(firstTokenAmount); + }); + }); + + describe('isApprovedForAll', function () { + it('returns the approval of the operator', async function () { + expect(await this.token.isApprovedForAll(holder, operator)).to.equal(true); + }); + }); + }); +});