Skip to content

Commit

Permalink
Merge pull request #21 from gnosis/replace-1155
Browse files Browse the repository at this point in the history
Replace 1155
  • Loading branch information
cag committed May 31, 2019
2 parents 57cdcff + d8225ae commit 5743009
Show file tree
Hide file tree
Showing 26 changed files with 9,908 additions and 2,542 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
docs/
!.eslintrc.js
16 changes: 13 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
module.exports = {
"extends": "plugin:prettier/recommended",
"parserOptions": { "ecmaVersion": 8 }
};
extends: ["eslint:recommended", "plugin:prettier/recommended"],
env: {
node: true,
mocha: true
},
globals: {
artifacts: true,
web3: true,
contract: true,
assert: true
},
parserOptions: { ecmaVersion: 8 }
};
4 changes: 2 additions & 2 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ IFS=$'\n\t'

# ESLint staged changes only
git diff --diff-filter=d --cached --name-only -z -- '*.js' '*.jsx' \
| xargs -0 -I % sh -c 'git show ":%" | ./node_modules/.bin/eslint --stdin --stdin-filename "%";'
| xargs -0 -I % sh -c 'git show ":%" | npx eslint --stdin --stdin-filename "%";'
eslint_exit=$?

if [ ${eslint_exit} -eq 0 ]; then
Expand All @@ -25,7 +25,7 @@ fi

# Solhint staged changes only
git diff --diff-filter=d --cached --name-only -z -- '*.sol' \
| xargs -0 -I % sh -c 'git show ":%" | ./node_modules/.bin/solhint stdin;'
| xargs -0 -I % sh -c 'git show ":%" | npx solium --stdin;'
solhint_exit=$?

if [ ${solhint_exit} -eq 0 ]; then
Expand Down
6 changes: 0 additions & 6 deletions .solhint.json

This file was deleted.

2 changes: 2 additions & 0 deletions .soliumignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
contracts/ERC1820Registry.sol
6 changes: 6 additions & 0 deletions .soliumrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "solium:recommended",
"plugins": [
"security"
]
}
234 changes: 234 additions & 0 deletions contracts/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
pragma solidity ^0.5.0;

import "./IERC1155.sol";
import "./IERC1155TokenReceiver.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/utils/Address.sol";
import "openzeppelin-solidity/contracts/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 owner balances
mapping (uint256 => mapping(address => uint256)) private _balances;

// Mapping from owner to operator approvals
mapping (address => mapping(address => bool)) private _operatorApprovals;

constructor()
public
{
_registerInterface(
this.safeTransferFrom.selector ^
this.safeBatchTransferFrom.selector ^
this.balanceOf.selector ^
this.balanceOfBatch.selector ^
this.setApprovalForAll.selector ^
this.isApprovedForAll.selector
);
}

/**
@dev Get the specified address' balance for token with specified ID.
@param owner The address of the token holder
@param id ID of the token
@return The owner's balance of the token type requested
*/
function balanceOf(address owner, uint256 id) public view returns (uint256) {
require(owner != address(0), "ERC1155: balance query for the zero address");
return _balances[id][owner];
}

/**
@dev Get the balance of multiple account/token pairs
@param owners The addresses of the token holders
@param ids IDs of the tokens
@return Balances for each owner and token id pair
*/
function balanceOfBatch(
address[] memory owners,
uint256[] memory ids
)
public
view
returns (uint256[] memory)
{
require(owners.length == ids.length, "ERC1155: owners and IDs must have same lengths");

uint256[] memory batchBalances = new uint256[](owners.length);

for (uint256 i = 0; i < owners.length; ++i) {
require(owners[i] != address(0), "ERC1155: some address in batch balance query is zero");
batchBalances[i] = _balances[ids[i]][owners[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
* @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 {
_operatorApprovals[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}

/**
@notice Queries the approval status of an operator for a given owner.
@param owner The owner of the Tokens
@param operator Address of authorized operator
@return True if the operator is approved, false if not
*/
function isApprovedForAll(address owner, address operator) external view returns (bool) {
return _operatorApprovals[owner][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
{
require(to != address(0), "ERC1155: target address must be non-zero");
require(
from == msg.sender || _operatorApprovals[from][msg.sender] == true,
"ERC1155: need operator approval for 3rd party transfers."
);

_balances[id][from] = _balances[id][from].sub(value);
_balances[id][to] = value.add(_balances[id][to]);

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
{
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 || _operatorApprovals[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);
_balances[id][to] = value.add(_balances[id][to]);
}

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 {
require(to != address(0), "ERC1155: mint to the zero address");

_balances[id][to] = value.add(_balances[id][to]);
emit TransferSingle(msg.sender, address(0), to, id, value);

_doSafeTransferAcceptanceCheck(msg.sender, address(0), to, id, value, data);
}

/**
* @dev Internal function to burn an amount of a token with the given ID
* @param owner 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 owner, uint256 id, uint256 value) internal {
_balances[id][owner] = _balances[id][owner].sub(value);
emit TransferSingle(msg.sender, owner, address(0), id, value);
}

function _doSafeTransferAcceptanceCheck(
address operator,
address from,
address to,
uint256 id,
uint256 value,
bytes memory data
)
internal
{
if(to.isContract()) {
require(
IERC1155TokenReceiver(to).onERC1155Received(operator, from, id, value, data) ==
IERC1155TokenReceiver(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
{
if(to.isContract()) {
require(
IERC1155TokenReceiver(to).onERC1155BatchReceived(operator, from, ids, values, data) == IERC1155TokenReceiver(to).onERC1155BatchReceived.selector,
"ERC1155: got unknown value from onERC1155BatchReceived"
);
}
}
}
29 changes: 29 additions & 0 deletions contracts/ERC1155/IERC1155.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pragma solidity ^0.5.0;

import "openzeppelin-solidity/contracts/introspection/IERC165.sol";

/**
@title ERC-1155 Multi Token Standard basic interface
@dev See https://eips.ethereum.org/EIPS/eip-1155
*/
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 owner, address indexed operator, bool approved);

event URI(string value, uint256 indexed id);

function balanceOf(address owner, uint256 id) public view returns (uint256);

function balanceOfBatch(address[] memory owners, uint256[] memory ids) public view returns (uint256[] memory);

function setApprovalForAll(address operator, bool approved) external;

function isApprovedForAll(address owner, address operator) external view returns (bool);

function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes calldata data) external;

function safeBatchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata values, bytes calldata data) external;
}
57 changes: 57 additions & 0 deletions contracts/ERC1155/IERC1155TokenReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
pragma solidity ^0.5.0;

/**
@title ERC-1155 Multi Token Receiver
@dev See https://eips.ethereum.org/EIPS/eip-1155
*/
interface IERC1155TokenReceiver {
/**
@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);

/**
@dev Indicates whether a contract implements the `ERC1155TokenReceiver`
functions and so can accept ERC1155 token types. Will return
`bytes4(keccak256("isERC1155TokenReceiver()"))`
(i.e. 0x0d912442 or its own function selector).
*/
function isERC1155TokenReceiver() external view returns (bytes4);
}
12 changes: 12 additions & 0 deletions contracts/ERC1155/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
sections:
- title: Core
contracts:
- IERC1155
- ERC1155
- IERC1155TokenReceiver
---

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 `IERC1155TokenReceiver`. Only `IERC1155` is required for a contract to be ERC1155 compliant. The basic functionality is implemented in `ERC1155`.

0 comments on commit 5743009

Please sign in to comment.