This commit is contained in:
Greg Hysen
2019-02-27 13:51:13 -08:00
parent b3106cd932
commit 0cac2d407b
4 changed files with 159 additions and 32 deletions

View File

@@ -2,23 +2,35 @@ pragma solidity ^0.5.3;
import "./lib/SafeMath.sol";
import "./ERC1155.sol";
import "./interfaces/IERC1155Mintable.sol";
/// @dev Mintable form of ERC1155
/// Shows how easy it is to mint new items
contract ERC1155Mintable is
IERC1155Mintable,
ERC1155
{
/// token nonce
uint256 internal nonce;
/// mapping from token to creator
mapping (uint256 => address) public creators;
/// mapping from token to max index
mapping (uint256 => uint256) public maxIndex;
/// asserts token is owned by msg.sender
modifier creatorOnly(uint256 _id) {
require(creators[_id] == msg.sender);
_;
}
// This function only creates the type.
/// @dev creates a new token
/// @param _uri URI of token
/// @param _isNF is non-fungible token
/// @return _type of token (a unique identifier)
function create(
string calldata _uri,
bool _isNF
@@ -44,8 +56,50 @@ contract ERC1155Mintable is
emit URI(_uri, _type);
}
function mintNonFungible(uint256 _type, address[] calldata _to) external creatorOnly(_type) {
/// @dev mints fungible tokens
/// @param _id token type
/// @param _to beneficiaries of minted tokens
/// @param _quantities amounts of minted tokens
function mintFungible(
uint256 _id,
address[] calldata _to,
uint256[] calldata _quantities
)
external
creatorOnly(_id)
{
require(isFungible(_id));
for (uint256 i = 0; i < _to.length; ++i) {
address to = _to[i];
uint256 quantity = _quantities[i];
// Grant the items to the caller
balances[_id][to] = safeAdd(quantity, balances[_id][to]);
// Emit the Transfer/Mint event.
// the 0x0 source address implies a mint
// It will also provide the circulating supply info.
emit TransferSingle(msg.sender, address(0x0), to, _id, quantity);
if (to.isContract()) {
require(IERC1155Receiver(to).onERC1155Received(msg.sender, msg.sender, _id, quantity, "") == ERC1155_RECEIVED);
}
}
}
/// @dev mints a non-fungible token
/// @param _type token type
/// @param _to beneficiaries of minted tokens
function mintNonFungible(
uint256 _type,
address[] calldata _to
)
external
creatorOnly(_type)
{
// No need to check this is a nf type rather than an id since
// creatorOnly() will only let a type pass through.
require(isNonFungible(_type));
@@ -71,27 +125,4 @@ contract ERC1155Mintable is
maxIndex[_type] = safeAdd(_to.length, maxIndex[_type]);
}
function mintFungible(uint256 _id, address[] calldata _to, uint256[] calldata _quantities) external creatorOnly(_id) {
require(isFungible(_id));
for (uint256 i = 0; i < _to.length; ++i) {
address to = _to[i];
uint256 quantity = _quantities[i];
// Grant the items to the caller
balances[_id][to] = safeAdd(quantity, balances[_id][to]);
// Emit the Transfer/Mint event.
// the 0x0 source address implies a mint
// It will also provide the circulating supply info.
emit TransferSingle(msg.sender, address(0x0), to, _id, quantity);
if (to.isContract()) {
require(IERC1155Receiver(to).onERC1155Received(msg.sender, msg.sender, _id, quantity, "") == ERC1155_RECEIVED);
}
}
}
}

View File

@@ -18,48 +18,58 @@
pragma solidity ^0.5.3;
import "./mixins/MNonFungibleToken.sol";
contract MixinNonFungibleToken {
// Use a split bit implementation.
// Store the type in the upper 128 bits..
contract MixinNonFungibleToken is
MNonFungibleToken
{
/// Use a split bit implementation.
/// Store the type in the upper 128 bits..
uint256 constant internal TYPE_MASK = uint256(uint128(~0)) << 128;
// ..and the non-fungible index in the lower 128
/// ..and the non-fungible index in the lower 128
uint256 constant internal NF_INDEX_MASK = uint128(~0);
// The top bit is a flag to tell if this is a NFI.
/// The top bit is a flag to tell if this is a NFI.
uint256 constant internal TYPE_NF_BIT = 1 << 255;
/// mapping of nft to owner
mapping (uint256 => address) internal nfOwners;
// Only to make code clearer. Should not be functions
/// @dev Returns true if token is non-fungible
function isNonFungible(uint256 _id) public pure returns(bool) {
return _id & TYPE_NF_BIT == TYPE_NF_BIT;
}
/// @dev Returns true if token is fungible
function isFungible(uint256 _id) public pure returns(bool) {
return _id & TYPE_NF_BIT == 0;
}
/// @dev Returns index of non-fungible token
function getNonFungibleIndex(uint256 _id) public pure returns(uint256) {
return _id & NF_INDEX_MASK;
}
/// @dev Returns base type of non-fungible token
function getNonFungibleBaseType(uint256 _id) public pure returns(uint256) {
return _id & TYPE_MASK;
}
/// @dev Returns true if input is base-type of a non-fungible token
function isNonFungibleBaseType(uint256 _id) public pure returns(bool) {
// A base type has the NF bit but does not have an index.
return (_id & TYPE_NF_BIT == TYPE_NF_BIT) && (_id & NF_INDEX_MASK == 0);
}
/// @dev Returns true if input is a non-fungible token
function isNonFungibleItem(uint256 _id) public pure returns(bool) {
// A base type has the NF bit but does has an index.
return (_id & TYPE_NF_BIT == TYPE_NF_BIT) && (_id & NF_INDEX_MASK != 0);
}
/// @dev returns owner of a non-fungible token
function ownerOf(uint256 _id) public view returns (address) {
return nfOwners[_id];
}

View File

@@ -0,0 +1,42 @@
pragma solidity ^0.5.3;
import "./IERC1155.sol";
/// @dev Mintable form of ERC1155
/// Shows how easy it is to mint new items
contract IERC1155Mintable is
IERC1155
{
/// @dev creates a new token
/// @param _uri URI of token
/// @param _isNF is non-fungible token
/// @return _type of token (a unique identifier)
function create(
string calldata _uri,
bool _isNF
)
external
returns (uint256 _type);
/// @dev mints fungible tokens
/// @param _id token type
/// @param _to beneficiaries of minted tokens
/// @param _quantities amounts of minted tokens
function mintFungible(
uint256 _id,
address[] calldata _to,
uint256[] calldata _quantities
)
external;
/// @dev mints a non-fungible token
/// @param _type token type
/// @param _to beneficiaries of minted tokens
function mintNonFungible(
uint256 _type,
address[] calldata _to
)
external;
}

View File

@@ -0,0 +1,44 @@
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.5.3;
contract MNonFungibleToken {
/// @dev Returns true if token is non-fungible
function isNonFungible(uint256 _id) public pure returns(bool);
/// @dev Returns true if token is fungible
function isFungible(uint256 _id) public pure returns(bool);
/// @dev Returns index of non-fungible token
function getNonFungibleIndex(uint256 _id) public pure returns(uint256);
/// @dev Returns base type of non-fungible token
function getNonFungibleBaseType(uint256 _id) public pure returns(uint256);
/// @dev Returns true if input is base-type of a non-fungible token
function isNonFungibleBaseType(uint256 _id) public pure returns(bool);
/// @dev Returns true if input is a non-fungible token
function isNonFungibleItem(uint256 _id) public pure returns(bool);
/// @dev returns owner of a non-fungible token
function ownerOf(uint256 _id) public view returns (address);
}