ERC1155 feature pending tasks (#2014)
* Initial ERC1155 implementation with some tests (#1803) * Initial ERC1155 implementation with some tests * Remove mocked isERC1155TokenReceiver * Revert reason edit nit * Remove parameters associated with isERC1155TokenReceiver call * Add tests for approvals and single transfers * Add tests for transferring to contracts * Add tests for batch transfers * Make expectEvent.inTransaction tests async * Renamed "owner" to "account" and "holder" * Document unspecified balanceOfBatch reversion on zero behavior * Ensure accounts can't set their own operator status * Specify descriptive messages for underflow errors * Bring SafeMath.add calls in line with OZ style * Explicitly prevent _burn on the zero account * Implement batch minting/burning * Refactored operator approval check into isApprovedForAll calls * Renamed ERC1155TokenReceiver to ERC1155Receiver * Added ERC1155Holder * Fix lint issues * Migrate tests to @openzeppelin/test-environment * Port ERC 1155 branch to Solidity 0.6 (and current master) (#2130) * port ERC1155 to Solidity 0.6 * make ERC1155 constructor more similar to ERC721 one * also migrate mock contracts to Solidity 0.6 * mark all non-view functions as virtual Co-authored-by: Alan Lu <alanlu1023@gmail.com> Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com> Co-authored-by: Robert Kaiser <kairo@kairo.at>
This commit is contained in:
committed by
GitHub
parent
0c7b2ec09e
commit
956d6632d9
307
contracts/token/ERC1155/ERC1155.sol
Normal file
307
contracts/token/ERC1155/ERC1155.sol
Normal file
@ -0,0 +1,307 @@
|
||||
pragma solidity ^0.6.0;
|
||||
|
||||
import "./IERC1155.sol";
|
||||
import "./IERC1155Receiver.sol";
|
||||
import "../../math/SafeMath.sol";
|
||||
import "../../utils/Address.sol";
|
||||
import "../../introspection/ERC165.sol";
|
||||
|
||||
/**
|
||||
* @title Standard ERC1155 token
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
contract ERC1155 is ERC165, IERC1155
|
||||
{
|
||||
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;
|
||||
|
||||
/*
|
||||
* 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;
|
||||
|
||||
constructor() public {
|
||||
// register the supported interfaces to conform to ERC1155 via ERC165
|
||||
_registerInterface(_INTERFACE_ID_ERC1155);
|
||||
}
|
||||
|
||||
/**
|
||||
@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
|
||||
*/
|
||||
function balanceOf(address account, uint256 id) public view override returns (uint256) {
|
||||
require(account != address(0), "ERC1155: balance query for the zero address");
|
||||
return _balances[id][account];
|
||||
}
|
||||
|
||||
/**
|
||||
@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
|
||||
*/
|
||||
function balanceOfBatch(
|
||||
address[] memory accounts,
|
||||
uint256[] memory ids
|
||||
)
|
||||
public
|
||||
view
|
||||
override
|
||||
returns (uint256[] memory)
|
||||
{
|
||||
require(accounts.length == ids.length, "ERC1155: accounts and IDs must have same lengths");
|
||||
|
||||
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");
|
||||
batchBalances[i] = _balances[ids[i]][accounts[i]];
|
||||
}
|
||||
|
||||
return batchBalances;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
function setApprovalForAll(address operator, bool approved) external override virtual {
|
||||
require(msg.sender != operator, "ERC1155: cannot set approval status for self");
|
||||
_operatorApprovals[msg.sender][operator] = approved;
|
||||
emit ApprovalForAll(msg.sender, 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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
function safeTransferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256 id,
|
||||
uint256 value,
|
||||
bytes calldata data
|
||||
)
|
||||
external
|
||||
override
|
||||
virtual
|
||||
{
|
||||
require(to != address(0), "ERC1155: target address must be non-zero");
|
||||
require(
|
||||
from == msg.sender || isApprovedForAll(from, msg.sender) == true,
|
||||
"ERC1155: need operator approval for 3rd party transfers"
|
||||
);
|
||||
|
||||
_balances[id][from] = _balances[id][from].sub(value, "ERC1155: insufficient balance for transfer");
|
||||
_balances[id][to] = _balances[id][to].add(value);
|
||||
|
||||
emit TransferSingle(msg.sender, from, to, id, value);
|
||||
|
||||
_doSafeTransferAcceptanceCheck(msg.sender, from, to, id, value, 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
|
||||
*/
|
||||
function safeBatchTransferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256[] calldata ids,
|
||||
uint256[] calldata values,
|
||||
bytes calldata data
|
||||
)
|
||||
external
|
||||
override
|
||||
virtual
|
||||
{
|
||||
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(
|
||||
from == msg.sender || isApprovedForAll(from, msg.sender) == true,
|
||||
"ERC1155: need operator approval for 3rd party transfers"
|
||||
);
|
||||
|
||||
for (uint256 i = 0; i < ids.length; ++i) {
|
||||
uint256 id = ids[i];
|
||||
uint256 value = values[i];
|
||||
|
||||
_balances[id][from] = _balances[id][from].sub(
|
||||
value,
|
||||
"ERC1155: insufficient balance of some token type for transfer"
|
||||
);
|
||||
_balances[id][to] = _balances[id][to].add(value);
|
||||
}
|
||||
|
||||
emit TransferBatch(msg.sender, from, to, ids, values);
|
||||
|
||||
_doSafeBatchTransferAcceptanceCheck(msg.sender, from, to, ids, values, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
function _mint(address to, uint256 id, uint256 value, bytes memory data) internal virtual {
|
||||
require(to != 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);
|
||||
|
||||
_doSafeTransferAcceptanceCheck(msg.sender, address(0), to, id, value, 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
|
||||
*/
|
||||
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");
|
||||
|
||||
for(uint i = 0; i < ids.length; i++) {
|
||||
_balances[ids[i]][to] = values[i].add(_balances[ids[i]][to]);
|
||||
}
|
||||
|
||||
emit TransferBatch(msg.sender, address(0), to, ids, values);
|
||||
|
||||
_doSafeBatchTransferAcceptanceCheck(msg.sender, address(0), to, ids, values, 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
|
||||
*/
|
||||
function _burn(address account, uint256 id, uint256 value) internal virtual {
|
||||
require(account != address(0), "ERC1155: attempting to burn tokens on zero account");
|
||||
|
||||
_balances[id][account] = _balances[id][account].sub(
|
||||
value,
|
||||
"ERC1155: attempting to burn more than balance"
|
||||
);
|
||||
emit TransferSingle(msg.sender, account, address(0), id, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
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");
|
||||
|
||||
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"
|
||||
);
|
||||
}
|
||||
|
||||
emit TransferBatch(msg.sender, account, address(0), ids, values);
|
||||
}
|
||||
|
||||
function _doSafeTransferAcceptanceCheck(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256 id,
|
||||
uint256 value,
|
||||
bytes memory data
|
||||
)
|
||||
internal
|
||||
virtual
|
||||
{
|
||||
if(to.isContract()) {
|
||||
require(
|
||||
IERC1155Receiver(to).onERC1155Received(operator, from, id, value, data) ==
|
||||
IERC1155Receiver(to).onERC1155Received.selector,
|
||||
"ERC1155: got unknown value from onERC1155Received"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function _doSafeBatchTransferAcceptanceCheck(
|
||||
address operator,
|
||||
address from,
|
||||
address to,
|
||||
uint256[] memory ids,
|
||||
uint256[] memory values,
|
||||
bytes memory data
|
||||
)
|
||||
internal
|
||||
virtual
|
||||
{
|
||||
if(to.isContract()) {
|
||||
require(
|
||||
IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, values, data) ==
|
||||
IERC1155Receiver(to).onERC1155BatchReceived.selector,
|
||||
"ERC1155: got unknown value from onERC1155BatchReceived"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
17
contracts/token/ERC1155/ERC1155Holder.sol
Normal file
17
contracts/token/ERC1155/ERC1155Holder.sol
Normal file
@ -0,0 +1,17 @@
|
||||
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)
|
||||
{
|
||||
return this.onERC1155Received.selector;
|
||||
}
|
||||
|
||||
|
||||
function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata) external override virtual returns (bytes4)
|
||||
{
|
||||
return this.onERC1155BatchReceived.selector;
|
||||
}
|
||||
}
|
||||
13
contracts/token/ERC1155/ERC1155Receiver.sol
Normal file
13
contracts/token/ERC1155/ERC1155Receiver.sol
Normal file
@ -0,0 +1,13 @@
|
||||
pragma solidity ^0.6.0;
|
||||
|
||||
import "./IERC1155Receiver.sol";
|
||||
import "../../introspection/ERC165.sol";
|
||||
|
||||
abstract contract ERC1155Receiver is ERC165, IERC1155Receiver {
|
||||
constructor() public {
|
||||
_registerInterface(
|
||||
ERC1155Receiver(0).onERC1155Received.selector ^
|
||||
ERC1155Receiver(0).onERC1155BatchReceived.selector
|
||||
);
|
||||
}
|
||||
}
|
||||
29
contracts/token/ERC1155/IERC1155.sol
Normal file
29
contracts/token/ERC1155/IERC1155.sol
Normal file
@ -0,0 +1,29 @@
|
||||
pragma solidity ^0.6.0;
|
||||
|
||||
import "../../introspection/IERC165.sol";
|
||||
|
||||
/**
|
||||
@title ERC-1155 Multi Token Standard basic interface
|
||||
@dev See https://eips.ethereum.org/EIPS/eip-1155
|
||||
*/
|
||||
abstract contract IERC1155 is IERC165 {
|
||||
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);
|
||||
|
||||
event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values);
|
||||
|
||||
event ApprovalForAll(address indexed account, address indexed operator, bool approved);
|
||||
|
||||
event URI(string value, uint256 indexed id);
|
||||
|
||||
function balanceOf(address account, uint256 id) public view virtual returns (uint256);
|
||||
|
||||
function balanceOfBatch(address[] memory accounts, uint256[] memory ids) public view virtual returns (uint256[] memory);
|
||||
|
||||
function setApprovalForAll(address operator, bool approved) external virtual;
|
||||
|
||||
function isApprovedForAll(address account, address operator) external view virtual returns (bool);
|
||||
|
||||
function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes calldata data) external virtual;
|
||||
|
||||
function safeBatchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata values, bytes calldata data) external virtual;
|
||||
}
|
||||
56
contracts/token/ERC1155/IERC1155Receiver.sol
Normal file
56
contracts/token/ERC1155/IERC1155Receiver.sol
Normal file
@ -0,0 +1,56 @@
|
||||
pragma solidity ^0.6.0;
|
||||
|
||||
import "../../introspection/IERC165.sol";
|
||||
|
||||
/**
|
||||
@title ERC-1155 Multi Token Receiver Interface
|
||||
@dev See https://eips.ethereum.org/EIPS/eip-1155
|
||||
*/
|
||||
interface 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);
|
||||
}
|
||||
12
contracts/token/ERC1155/README.md
Normal file
12
contracts/token/ERC1155/README.md
Normal file
@ -0,0 +1,12 @@
|
||||
---
|
||||
sections:
|
||||
- title: Core
|
||||
contracts:
|
||||
- IERC1155
|
||||
- ERC1155
|
||||
- IERC1155Receiver
|
||||
---
|
||||
|
||||
This set of interfaces and contracts are all related to the [ERC1155 Multi Token Standard](https://eips.ethereum.org/EIPS/eip-1155).
|
||||
|
||||
The EIP consists of two interfaces which fulfill different roles, found here as `IERC1155` and `IERC1155Receiver`. Only `IERC1155` is required for a contract to be ERC1155 compliant. The basic functionality is implemented in `ERC1155`.
|
||||
Reference in New Issue
Block a user