Last active
May 24, 2019 16:24
-
-
Save Isaac12x/01ecb2c6467ef9fecaa02bef491940d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File: openzeppelin-solidity/contracts/introspection/IERC165.sol | |
/** | |
* @title IERC165 | |
* @dev https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md | |
*/ | |
interface IERC165 { | |
/** | |
* @notice Query if a contract implements an interface | |
* @param interfaceId The interface identifier, as specified in ERC-165 | |
* @dev Interface identification is specified in ERC-165. This function | |
* uses less than 30,000 gas. | |
*/ | |
function supportsInterface(bytes4 interfaceId) external view returns (bool); | |
} | |
// File: openzeppelin-solidity/contracts/token/erc721/IERC721.sol | |
/** | |
* @title ERC721 Non-Fungible Token Standard basic interface | |
* @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract IERC721 is IERC165 { | |
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); | |
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); | |
event ApprovalForAll(address indexed owner, address indexed operator, bool approved); | |
function balanceOf(address owner) public view returns (uint256 balance); | |
function ownerOf(uint256 tokenId) public view returns (address owner); | |
function approve(address to, uint256 tokenId) public; | |
function getApproved(uint256 tokenId) public view returns (address operator); | |
function setApprovalForAll(address operator, bool _approved) public; | |
function isApprovedForAll(address owner, address operator) public view returns (bool); | |
function transferFrom(address from, address to, uint256 tokenId) public; | |
function safeTransferFrom(address from, address to, uint256 tokenId) public; | |
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public; | |
} | |
// File: openzeppelin-solidity/contracts/token/erc721/IERC721Receiver.sol | |
/** | |
* @title ERC721 token receiver interface | |
* @dev Interface for any contract that wants to support safeTransfers | |
* from ERC721 asset contracts. | |
*/ | |
contract IERC721Receiver { | |
/** | |
* @notice Handle the receipt of an NFT | |
* @dev The ERC721 smart contract calls this function on the recipient | |
* after a `safeTransfer`. This function MUST return the function selector, | |
* otherwise the caller will revert the transaction. The selector to be | |
* returned can be obtained as `this.onERC721Received.selector`. This | |
* function MAY throw to revert and reject the transfer. | |
* Note: the ERC721 contract address is always the message sender. | |
* @param operator The address which called `safeTransferFrom` function | |
* @param from The address which previously owned the token | |
* @param tokenId The NFT identifier which is being transferred | |
* @param data Additional data with no specified format | |
* @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` | |
*/ | |
function onERC721Received(address operator, address from, uint256 tokenId, bytes memory data) | |
public returns (bytes4); | |
} | |
// File: openzeppelin-solidity/contracts/math/SafeMath.sol | |
/** | |
* @title SafeMath | |
* @dev Unsigned math operations with safety checks that revert on error | |
*/ | |
library SafeMath { | |
/** | |
* @dev Multiplies two unsigned integers, reverts on overflow. | |
*/ | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
require(c / a == b); | |
return c; | |
} | |
/** | |
* @dev Integer division of two unsigned integers truncating the quotient, reverts on division by zero. | |
*/ | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
// Solidity only automatically asserts when dividing by 0 | |
require(b > 0); | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
/** | |
* @dev Subtracts two unsigned integers, reverts on overflow (i.e. if subtrahend is greater than minuend). | |
*/ | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b <= a); | |
uint256 c = a - b; | |
return c; | |
} | |
/** | |
* @dev Adds two unsigned integers, reverts on overflow. | |
*/ | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a); | |
return c; | |
} | |
/** | |
* @dev Divides two unsigned integers and returns the remainder (unsigned integer modulo), | |
* reverts when dividing by zero. | |
*/ | |
function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b != 0); | |
return a % b; | |
} | |
} | |
// File: openzeppelin-solidity/contracts/utils/Address.sol | |
/** | |
* Utility library of inline functions on addresses | |
*/ | |
library Address { | |
/** | |
* Returns whether the target address is a contract | |
* @dev This function will return false if invoked during the constructor of a contract, | |
* as the code is not actually created until after the constructor finishes. | |
* @param account address of the account to check | |
* @return whether the target address is a contract | |
*/ | |
function isContract(address account) internal view returns (bool) { | |
uint256 size; | |
// XXX Currently there is no better way to check if there is a contract in an address | |
// than to check the size of the code at that address. | |
// See https://ethereum.stackexchange.com/a/14016/36603 | |
// for more details about how this works. | |
// TODO Check this again before the Serenity release, because all addresses will be | |
// contracts then. | |
// solhint-disable-next-line no-inline-assembly | |
assembly { size := extcodesize(account) } | |
return size > 0; | |
} | |
} | |
// File: openzeppelin-solidity/contracts/introspection/ERC165.sol | |
/** | |
* @title ERC165 | |
* @author Matt Condon (@shrugs) | |
* @dev Implements ERC165 using a lookup table. | |
*/ | |
contract ERC165 is IERC165 { | |
bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7; | |
/** | |
* 0x01ffc9a7 === | |
* bytes4(keccak256('supportsInterface(bytes4)')) | |
*/ | |
/** | |
* @dev a mapping of interface id to whether or not it's supported | |
*/ | |
mapping(bytes4 => bool) private _supportedInterfaces; | |
/** | |
* @dev A contract implementing SupportsInterfaceWithLookup | |
* implement ERC165 itself | |
*/ | |
constructor () internal { | |
_registerInterface(_INTERFACE_ID_ERC165); | |
} | |
/** | |
* @dev implement supportsInterface(bytes4) using a lookup table | |
*/ | |
function supportsInterface(bytes4 interfaceId) external view returns (bool) { | |
return _supportedInterfaces[interfaceId]; | |
} | |
/** | |
* @dev internal method for registering an interface | |
*/ | |
function _registerInterface(bytes4 interfaceId) internal { | |
require(interfaceId != 0xffffffff); | |
_supportedInterfaces[interfaceId] = true; | |
} | |
} | |
// File: openzeppelin-solidity/contracts/token/erc721/ERC721.sol | |
/** | |
* @title ERC721 Non-Fungible Token Standard basic implementation | |
* @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721 is ERC165, IERC721 { | |
using SafeMath for uint256; | |
using Address for address; | |
// Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` | |
// which can be also obtained as `IERC721Receiver(0).onERC721Received.selector` | |
bytes4 private constant _ERC721_RECEIVED = 0x150b7a02; | |
// Mapping from token ID to owner | |
mapping (uint256 => address) private _tokenOwner; | |
// Mapping from token ID to approved address | |
mapping (uint256 => address) private _tokenApprovals; | |
// Mapping from owner to number of owned token | |
mapping (address => uint256) private _ownedTokensCount; | |
// Mapping from owner to operator approvals | |
mapping (address => mapping (address => bool)) private _operatorApprovals; | |
bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd; | |
/* | |
* 0x80ac58cd === | |
* bytes4(keccak256('balanceOf(address)')) ^ | |
* bytes4(keccak256('ownerOf(uint256)')) ^ | |
* bytes4(keccak256('approve(address,uint256)')) ^ | |
* bytes4(keccak256('getApproved(uint256)')) ^ | |
* bytes4(keccak256('setApprovalForAll(address,bool)')) ^ | |
* bytes4(keccak256('isApprovedForAll(address,address)')) ^ | |
* bytes4(keccak256('transferFrom(address,address,uint256)')) ^ | |
* bytes4(keccak256('safeTransferFrom(address,address,uint256)')) ^ | |
* bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) | |
*/ | |
constructor () public { | |
// register the supported interfaces to conform to ERC721 via ERC165 | |
_registerInterface(_INTERFACE_ID_ERC721); | |
} | |
/** | |
* @dev Gets the balance of the specified address | |
* @param owner address to query the balance of | |
* @return uint256 representing the amount owned by the passed address | |
*/ | |
function balanceOf(address owner) public view returns (uint256) { | |
require(owner != address(0)); | |
return _ownedTokensCount[owner]; | |
} | |
/** | |
* @dev Gets the owner of the specified token ID | |
* @param tokenId uint256 ID of the token to query the owner of | |
* @return owner address currently marked as the owner of the given token ID | |
*/ | |
function ownerOf(uint256 tokenId) public view returns (address) { | |
address owner = _tokenOwner[tokenId]; | |
require(owner != address(0)); | |
return owner; | |
} | |
/** | |
* @dev Approves another address to transfer the given token ID | |
* The zero address indicates there is no approved address. | |
* There can only be one approved address per token at a given time. | |
* Can only be called by the token owner or an approved operator. | |
* @param to address to be approved for the given token ID | |
* @param tokenId uint256 ID of the token to be approved | |
*/ | |
function approve(address to, uint256 tokenId) public { | |
address owner = ownerOf(tokenId); | |
require(to != owner); | |
require(msg.sender == owner || isApprovedForAll(owner, msg.sender)); | |
_tokenApprovals[tokenId] = to; | |
emit Approval(owner, to, tokenId); | |
} | |
/** | |
* @dev Gets the approved address for a token ID, or zero if no address set | |
* Reverts if the token ID does not exist. | |
* @param tokenId uint256 ID of the token to query the approval of | |
* @return address currently approved for the given token ID | |
*/ | |
function getApproved(uint256 tokenId) public view returns (address) { | |
require(_exists(tokenId)); | |
return _tokenApprovals[tokenId]; | |
} | |
/** | |
* @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 to operator address to set the approval | |
* @param approved representing the status of the approval to be set | |
*/ | |
function setApprovalForAll(address to, bool approved) public { | |
require(to != msg.sender); | |
_operatorApprovals[msg.sender][to] = approved; | |
emit ApprovalForAll(msg.sender, to, approved); | |
} | |
/** | |
* @dev Tells whether an operator is approved by a given owner | |
* @param owner owner address which you want to query the approval of | |
* @param operator operator address which you want to query the approval of | |
* @return bool whether the given operator is approved by the given owner | |
*/ | |
function isApprovedForAll(address owner, address operator) public view returns (bool) { | |
return _operatorApprovals[owner][operator]; | |
} | |
/** | |
* @dev Transfers the ownership of a given token ID to another address | |
* Usage of this method is discouraged, use `safeTransferFrom` whenever possible | |
* Requires the msg sender to be the owner, approved, or operator | |
* @param from current owner of the token | |
* @param to address to receive the ownership of the given token ID | |
* @param tokenId uint256 ID of the token to be transferred | |
*/ | |
function transferFrom(address from, address to, uint256 tokenId) public { | |
require(_isApprovedOrOwner(msg.sender, tokenId)); | |
_transferFrom(from, to, tokenId); | |
} | |
/** | |
* @dev Safely transfers the ownership of a given token ID to another address | |
* If the target address is a contract, it must implement `onERC721Received`, | |
* which is called upon a safe transfer, and return the magic value | |
* `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, | |
* the transfer is reverted. | |
* | |
* Requires the msg sender to be the owner, approved, or operator | |
* @param from current owner of the token | |
* @param to address to receive the ownership of the given token ID | |
* @param tokenId uint256 ID of the token to be transferred | |
*/ | |
function safeTransferFrom(address from, address to, uint256 tokenId) public { | |
safeTransferFrom(from, to, tokenId, ""); | |
} | |
/** | |
* @dev Safely transfers the ownership of a given token ID to another address | |
* If the target address is a contract, it must implement `onERC721Received`, | |
* which is called upon a safe transfer, and return the magic value | |
* `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, | |
* the transfer is reverted. | |
* Requires the msg sender to be the owner, approved, or operator | |
* @param from current owner of the token | |
* @param to address to receive the ownership of the given token ID | |
* @param tokenId uint256 ID of the token to be transferred | |
* @param _data bytes data to send along with a safe transfer check | |
*/ | |
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public { | |
transferFrom(from, to, tokenId); | |
require(_checkOnERC721Received(from, to, tokenId, _data)); | |
} | |
/** | |
* @dev Returns whether the specified token exists | |
* @param tokenId uint256 ID of the token to query the existence of | |
* @return whether the token exists | |
*/ | |
function _exists(uint256 tokenId) internal view returns (bool) { | |
address owner = _tokenOwner[tokenId]; | |
return owner != address(0); | |
} | |
/** | |
* @dev Returns whether the given spender can transfer a given token ID | |
* @param spender address of the spender to query | |
* @param tokenId uint256 ID of the token to be transferred | |
* @return bool whether the msg.sender is approved for the given token ID, | |
* is an operator of the owner, or is the owner of the token | |
*/ | |
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) { | |
address owner = ownerOf(tokenId); | |
return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender)); | |
} | |
/** | |
* @dev Internal function to mint a new token | |
* Reverts if the given token ID already exists | |
* @param to The address that will own the minted token | |
* @param tokenId uint256 ID of the token to be minted | |
*/ | |
function _mint(address to, uint256 tokenId) internal { | |
require(to != address(0)); | |
require(!_exists(tokenId)); | |
_tokenOwner[tokenId] = to; | |
_ownedTokensCount[to] = _ownedTokensCount[to].add(1); | |
emit Transfer(address(0), to, tokenId); | |
} | |
/** | |
* @dev Internal function to burn a specific token | |
* Reverts if the token does not exist | |
* Deprecated, use _burn(uint256) instead. | |
* @param owner owner of the token to burn | |
* @param tokenId uint256 ID of the token being burned | |
*/ | |
function _burn(address owner, uint256 tokenId) internal { | |
require(ownerOf(tokenId) == owner); | |
_clearApproval(tokenId); | |
_ownedTokensCount[owner] = _ownedTokensCount[owner].sub(1); | |
_tokenOwner[tokenId] = address(0); | |
emit Transfer(owner, address(0), tokenId); | |
} | |
/** | |
* @dev Internal function to burn a specific token | |
* Reverts if the token does not exist | |
* @param tokenId uint256 ID of the token being burned | |
*/ | |
function _burn(uint256 tokenId) internal { | |
_burn(ownerOf(tokenId), tokenId); | |
} | |
/** | |
* @dev Internal function to transfer ownership of a given token ID to another address. | |
* As opposed to transferFrom, this imposes no restrictions on msg.sender. | |
* @param from current owner of the token | |
* @param to address to receive the ownership of the given token ID | |
* @param tokenId uint256 ID of the token to be transferred | |
*/ | |
function _transferFrom(address from, address to, uint256 tokenId) internal { | |
require(ownerOf(tokenId) == from); | |
require(to != address(0)); | |
_clearApproval(tokenId); | |
_ownedTokensCount[from] = _ownedTokensCount[from].sub(1); | |
_ownedTokensCount[to] = _ownedTokensCount[to].add(1); | |
_tokenOwner[tokenId] = to; | |
emit Transfer(from, to, tokenId); | |
} | |
/** | |
* @dev Internal function to invoke `onERC721Received` on a target address | |
* The call is not executed if the target address is not a contract | |
* @param from address representing the previous owner of the given token ID | |
* @param to target address that will receive the tokens | |
* @param tokenId uint256 ID of the token to be transferred | |
* @param _data bytes optional data to send along with the call | |
* @return whether the call correctly returned the expected magic value | |
*/ | |
function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data) | |
internal returns (bool) | |
{ | |
if (!to.isContract()) { | |
return true; | |
} | |
bytes4 retval = IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data); | |
return (retval == _ERC721_RECEIVED); | |
} | |
/** | |
* @dev Private function to clear current approval of a given token ID | |
* @param tokenId uint256 ID of the token to be transferred | |
*/ | |
function _clearApproval(uint256 tokenId) private { | |
if (_tokenApprovals[tokenId] != address(0)) { | |
_tokenApprovals[tokenId] = address(0); | |
} | |
} | |
} | |
// File: openzeppelin-solidity/contracts/token/erc721/IERC721Enumerable.sol | |
/** | |
* @title ERC-721 Non-Fungible Token Standard, optional enumeration extension | |
* @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract IERC721Enumerable is IERC721 { | |
function totalSupply() public view returns (uint256); | |
function tokenOfOwnerByIndex(address owner, uint256 index) public view returns (uint256 tokenId); | |
function tokenByIndex(uint256 index) public view returns (uint256); | |
} | |
// File: openzeppelin-solidity/contracts/token/erc721/ERC721Enumerable.sol | |
/** | |
* @title ERC-721 Non-Fungible Token with optional enumeration extension logic | |
* @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable { | |
// Mapping from owner to list of owned token IDs | |
mapping(address => uint256[]) private _ownedTokens; | |
// Mapping from token ID to index of the owner tokens list | |
mapping(uint256 => uint256) private _ownedTokensIndex; | |
// Array with all token ids, used for enumeration | |
uint256[] private _allTokens; | |
// Mapping from token id to position in the allTokens array | |
mapping(uint256 => uint256) private _allTokensIndex; | |
bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63; | |
/** | |
* 0x780e9d63 === | |
* bytes4(keccak256('totalSupply()')) ^ | |
* bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^ | |
* bytes4(keccak256('tokenByIndex(uint256)')) | |
*/ | |
/** | |
* @dev Constructor function | |
*/ | |
constructor () public { | |
// register the supported interface to conform to ERC721 via ERC165 | |
_registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE); | |
} | |
/** | |
* @dev Gets the token ID at a given index of the tokens list of the requested owner | |
* @param owner address owning the tokens list to be accessed | |
* @param index uint256 representing the index to be accessed of the requested tokens list | |
* @return uint256 token ID at the given index of the tokens list owned by the requested address | |
*/ | |
function tokenOfOwnerByIndex(address owner, uint256 index) public view returns (uint256) { | |
require(index < balanceOf(owner)); | |
return _ownedTokens[owner][index]; | |
} | |
/** | |
* @dev Gets the total amount of tokens stored by the contract | |
* @return uint256 representing the total amount of tokens | |
*/ | |
function totalSupply() public view returns (uint256) { | |
return _allTokens.length; | |
} | |
/** | |
* @dev Gets the token ID at a given index of all the tokens in this contract | |
* Reverts if the index is greater or equal to the total number of tokens | |
* @param index uint256 representing the index to be accessed of the tokens list | |
* @return uint256 token ID at the given index of the tokens list | |
*/ | |
function tokenByIndex(uint256 index) public view returns (uint256) { | |
require(index < totalSupply()); | |
return _allTokens[index]; | |
} | |
/** | |
* @dev Internal function to transfer ownership of a given token ID to another address. | |
* As opposed to transferFrom, this imposes no restrictions on msg.sender. | |
* @param from current owner of the token | |
* @param to address to receive the ownership of the given token ID | |
* @param tokenId uint256 ID of the token to be transferred | |
*/ | |
function _transferFrom(address from, address to, uint256 tokenId) internal { | |
super._transferFrom(from, to, tokenId); | |
_removeTokenFromOwnerEnumeration(from, tokenId); | |
_addTokenToOwnerEnumeration(to, tokenId); | |
} | |
/** | |
* @dev Internal function to mint a new token | |
* Reverts if the given token ID already exists | |
* @param to address the beneficiary that will own the minted token | |
* @param tokenId uint256 ID of the token to be minted | |
*/ | |
function _mint(address to, uint256 tokenId) internal { | |
super._mint(to, tokenId); | |
_addTokenToOwnerEnumeration(to, tokenId); | |
_addTokenToAllTokensEnumeration(tokenId); | |
} | |
/** | |
* @dev Internal function to burn a specific token | |
* Reverts if the token does not exist | |
* Deprecated, use _burn(uint256) instead | |
* @param owner owner of the token to burn | |
* @param tokenId uint256 ID of the token being burned | |
*/ | |
function _burn(address owner, uint256 tokenId) internal { | |
super._burn(owner, tokenId); | |
_removeTokenFromOwnerEnumeration(owner, tokenId); | |
// Since tokenId will be deleted, we can clear its slot in _ownedTokensIndex to trigger a gas refund | |
_ownedTokensIndex[tokenId] = 0; | |
_removeTokenFromAllTokensEnumeration(tokenId); | |
} | |
/** | |
* @dev Gets the list of token IDs of the requested owner | |
* @param owner address owning the tokens | |
* @return uint256[] List of token IDs owned by the requested address | |
*/ | |
function _tokensOfOwner(address owner) internal view returns (uint256[] storage) { | |
return _ownedTokens[owner]; | |
} | |
/** | |
* @dev Private function to add a token to this extension's ownership-tracking data structures. | |
* @param to address representing the new owner of the given token ID | |
* @param tokenId uint256 ID of the token to be added to the tokens list of the given address | |
*/ | |
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private { | |
_ownedTokensIndex[tokenId] = _ownedTokens[to].length; | |
_ownedTokens[to].push(tokenId); | |
} | |
/** | |
* @dev Private function to add a token to this extension's token tracking data structures. | |
* @param tokenId uint256 ID of the token to be added to the tokens list | |
*/ | |
function _addTokenToAllTokensEnumeration(uint256 tokenId) private { | |
_allTokensIndex[tokenId] = _allTokens.length; | |
_allTokens.push(tokenId); | |
} | |
/** | |
* @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that | |
* while the token is not assigned a new owner, the _ownedTokensIndex mapping is _not_ updated: this allows for | |
* gas optimizations e.g. when performing a transfer operation (avoiding double writes). | |
* This has O(1) time complexity, but alters the order of the _ownedTokens array. | |
* @param from address representing the previous owner of the given token ID | |
* @param tokenId uint256 ID of the token to be removed from the tokens list of the given address | |
*/ | |
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private { | |
// To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and | |
// then delete the last slot (swap and pop). | |
uint256 lastTokenIndex = _ownedTokens[from].length.sub(1); | |
uint256 tokenIndex = _ownedTokensIndex[tokenId]; | |
// When the token to delete is the last token, the swap operation is unnecessary | |
if (tokenIndex != lastTokenIndex) { | |
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex]; | |
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token | |
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index | |
} | |
// This also deletes the contents at the last position of the array | |
_ownedTokens[from].length--; | |
// Note that _ownedTokensIndex[tokenId] hasn't been cleared: it still points to the old slot (now occcupied by | |
// lasTokenId, or just over the end of the array if the token was the last one). | |
} | |
/** | |
* @dev Private function to remove a token from this extension's token tracking data structures. | |
* This has O(1) time complexity, but alters the order of the _allTokens array. | |
* @param tokenId uint256 ID of the token to be removed from the tokens list | |
*/ | |
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private { | |
// To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and | |
// then delete the last slot (swap and pop). | |
uint256 lastTokenIndex = _allTokens.length.sub(1); | |
uint256 tokenIndex = _allTokensIndex[tokenId]; | |
// When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so | |
// rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding | |
// an 'if' statement (like in _removeTokenFromOwnerEnumeration) | |
uint256 lastTokenId = _allTokens[lastTokenIndex]; | |
_allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token | |
_allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index | |
// This also deletes the contents at the last position of the array | |
_allTokens.length--; | |
_allTokensIndex[tokenId] = 0; | |
} | |
} | |
// File: openzeppelin-solidity/contracts/token/erc721/IERC721Metadata.sol | |
/** | |
* @title ERC-721 Non-Fungible Token Standard, optional metadata extension | |
* @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract IERC721Metadata is IERC721 { | |
function name() external view returns (string memory); | |
function symbol() external view returns (string memory); | |
function tokenURI(uint256 tokenId) external view returns (string memory); | |
} | |
// File: openzeppelin-solidity/contracts/token/erc721/ERC721Metadata.sol | |
contract ERC721Metadata is ERC165, ERC721, IERC721Metadata { | |
// Token name | |
string private _name; | |
// Token symbol | |
string private _symbol; | |
// Optional mapping for token URIs | |
mapping(uint256 => string) private _tokenURIs; | |
bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f; | |
/** | |
* 0x5b5e139f === | |
* bytes4(keccak256('name()')) ^ | |
* bytes4(keccak256('symbol()')) ^ | |
* bytes4(keccak256('tokenURI(uint256)')) | |
*/ | |
/** | |
* @dev Constructor function | |
*/ | |
constructor (string memory name, string memory symbol) public { | |
_name = name; | |
_symbol = symbol; | |
// register the supported interfaces to conform to ERC721 via ERC165 | |
_registerInterface(_INTERFACE_ID_ERC721_METADATA); | |
} | |
/** | |
* @dev Gets the token name | |
* @return string representing the token name | |
*/ | |
function name() external view returns (string memory) { | |
return _name; | |
} | |
/** | |
* @dev Gets the token symbol | |
* @return string representing the token symbol | |
*/ | |
function symbol() external view returns (string memory) { | |
return _symbol; | |
} | |
/** | |
* @dev Returns an URI for a given token ID | |
* Throws if the token ID does not exist. May return an empty string. | |
* @param tokenId uint256 ID of the token to query | |
*/ | |
function tokenURI(uint256 tokenId) external view returns (string memory) { | |
require(_exists(tokenId)); | |
return _tokenURIs[tokenId]; | |
} | |
/** | |
* @dev Internal function to set the token URI for a given token | |
* Reverts if the token ID does not exist | |
* @param tokenId uint256 ID of the token to set its URI | |
* @param uri string URI to assign | |
*/ | |
function _setTokenURI(uint256 tokenId, string memory uri) internal { | |
require(_exists(tokenId)); | |
_tokenURIs[tokenId] = uri; | |
} | |
/** | |
* @dev Internal function to burn a specific token | |
* Reverts if the token does not exist | |
* Deprecated, use _burn(uint256) instead | |
* @param owner owner of the token to burn | |
* @param tokenId uint256 ID of the token being burned by the msg.sender | |
*/ | |
function _burn(address owner, uint256 tokenId) internal { | |
super._burn(owner, tokenId); | |
// Clear metadata (if any) | |
if (bytes(_tokenURIs[tokenId]).length != 0) { | |
delete _tokenURIs[tokenId]; | |
} | |
} | |
} | |
// File: openzeppelin-solidity/contracts/token/erc721/ERC721full.sol | |
/** | |
* @title Full ERC721 Token | |
* This implementation includes all the required and some optional functionality of the ERC721 standard | |
* Moreover, it includes approve all functionality using operator terminology | |
* @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721Full is ERC721, ERC721Enumerable, ERC721Metadata { | |
constructor (string memory name, string memory symbol) public ERC721Metadata(name, symbol) { | |
// solhint-disable-previous-line no-empty-blocks | |
} | |
} | |
// File: contracts/Library/SafeMath8.sol | |
/// @title SafeMath8 | |
/// @notice Adaptation of SafeMath libary for uint8 | |
/// @author isaac@mammbo.com | |
library SafeMath8 { | |
/** | |
* @dev Multiplies two numbers, throws on overflow. | |
*/ | |
function mul(uint8 _a, uint8 _b) internal pure returns (uint8 c) { | |
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 | |
if (_a == 0) { | |
return 0; | |
} | |
c = _a * _b; | |
assert(c / _a == _b); | |
return c; | |
} | |
/** | |
* @dev Integer division of two numbers, truncating the quotient. | |
*/ | |
function div(uint8 _a, uint8 _b) internal pure returns (uint8) { | |
// assert(_b > 0); // Solidity automatically throws when dividing by 0 | |
// uint8 c = _a / _b; | |
// assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold | |
return _a / _b; | |
} | |
/** | |
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). | |
*/ | |
function sub(uint8 _a, uint8 _b) internal pure returns (uint8) { | |
assert(_b <= _a); | |
return _a - _b; | |
} | |
/** | |
* @dev Adds two numbers, throws on overflow. | |
*/ | |
function add(uint8 _a, uint8 _b) internal pure returns (uint8 c) { | |
c = _a + _b; | |
assert(c >= _a); | |
return c; | |
} | |
} | |
// File: openzeppelin-solidity/contracts/ownership/Ownable.sol | |
/** | |
* @title Ownable | |
* @dev The Ownable contract has an owner address, and provides basic authorization control | |
* functions, this simplifies the implementation of "user permissions". | |
*/ | |
contract Ownable { | |
address private _owner; | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
/** | |
* @dev The Ownable constructor sets the original `owner` of the contract to the sender | |
* account. | |
*/ | |
constructor () internal { | |
_owner = msg.sender; | |
emit OwnershipTransferred(address(0), _owner); | |
} | |
/** | |
* @return the address of the owner. | |
*/ | |
function owner() public view returns (address) { | |
return _owner; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(isOwner()); | |
_; | |
} | |
/** | |
* @return true if `msg.sender` is the owner of the contract. | |
*/ | |
function isOwner() public view returns (bool) { | |
return msg.sender == _owner; | |
} | |
/** | |
* @dev Allows the current owner to relinquish control of the contract. | |
* @notice Renouncing to ownership will leave the contract without an owner. | |
* It will not be possible to call the functions with the `onlyOwner` | |
* modifier anymore. | |
*/ | |
function renounceOwnership() public onlyOwner { | |
emit OwnershipTransferred(_owner, address(0)); | |
_owner = address(0); | |
} | |
/** | |
* @dev Allows the current owner to transfer control of the contract to a newOwner. | |
* @param newOwner The address to transfer ownership to. | |
*/ | |
function transferOwnership(address newOwner) public onlyOwner { | |
_transferOwnership(newOwner); | |
} | |
/** | |
* @dev Transfers control of the contract to a newOwner. | |
* @param newOwner The address to transfer ownership to. | |
*/ | |
function _transferOwnership(address newOwner) internal { | |
require(newOwner != address(0)); | |
emit OwnershipTransferred(_owner, newOwner); | |
_owner = newOwner; | |
} | |
} | |
// File: contracts/Library/AccessControl.sol | |
/// @dev Base of the contract is openzeppelin-solidity Superuser contract since we | |
/// don't need to make transfers. | |
/// @title Access control | |
/// @notice The Superuser contract defines a single superuser who can transfer the ownership | |
/// of a contract to a new address, even if he is not the owner. | |
/// @author isaac@mammbo.com | |
/// @dev the contract has been extended with addresses and removed RBAC as per EIP-170(deployment size limit) | |
contract AccessControl is Ownable { | |
/// @dev The addresses of the accounts (or contracts) that can execute actions within each roles. | |
address payable public ceoAddress; | |
address payable public cfoAddress; | |
address payable public cooAddress; | |
address payable public cmoAddress; | |
address payable public BAEFeeAddress; | |
/// @dev Access modifier for CEO-only functionality | |
modifier onlyCEO() { | |
require( | |
msg.sender == ceoAddress, | |
"Only our CEO address can execute this function"); | |
_; | |
} | |
/// @dev Access modifier for CFO-only functionality | |
modifier onlyCFO() { | |
require( | |
msg.sender == cfoAddress, | |
"Only our CFO can can ll this function"); | |
_; | |
} | |
/// @dev Access modifier for COO-only functionality | |
modifier onlyCOO() { | |
require( | |
msg.sender == cooAddress, | |
"Only our COO can can ll this function"); | |
_; | |
} | |
/// @dev Access modifier for Clevel functions | |
modifier onlyCLevelOrOwner() { | |
require( | |
msg.sender == cooAddress || | |
msg.sender == ceoAddress || | |
msg.sender == cfoAddress || | |
msg.sender == cmoAddress || | |
msg.sender == owner(), | |
"You need to be the owner or a Clevel @BAE to call this function" | |
); | |
_; | |
} | |
modifier onlyNotCLevelOrOwner() { | |
require( | |
msg.sender != cooAddress && | |
msg.sender != ceoAddress && | |
msg.sender != cfoAddress && | |
msg.sender != cmoAddress && | |
msg.sender != owner(), | |
"You need to be the owner or a Clevel @BAE to call this function" | |
); | |
_; | |
} | |
modifier onlyValidDestination(address _to) { | |
require( | |
_to != address(0x0) && | |
_to != address(this) && | |
_to != address(0), | |
"Address needs to be valid" | |
); | |
_; | |
} | |
/// @dev Assigns a new address to act as the CEO. Only available to the current CEO. | |
/// @param _newCEO The address of the new CEO | |
function setCEO(address payable _newCEO) external onlyCLevelOrOwner onlyValidDestination(_newCEO) { | |
ceoAddress = _newCEO; | |
} | |
/// @dev Assigns a new address to act as the CFO. Only available to the current CEO. | |
/// @param _newCFO The address of the new CFO | |
function setCFO(address payable _newCFO) external onlyCLevelOrOwner onlyValidDestination(_newCFO) { | |
cfoAddress = _newCFO; | |
} | |
/// @dev Assigns a new address to act as the COO. Only available to the current CEO. | |
/// @param _newCOO The address of the new COO | |
function setCOO(address payable _newCOO) external onlyCLevelOrOwner onlyValidDestination(_newCOO) { | |
cooAddress = _newCOO; | |
} | |
/// @dev Assigns a new address to act as the CMO. | |
/// @param _newCMO The address of the new CMO | |
function setCMO(address payable _newCMO) external onlyCLevelOrOwner onlyValidDestination(_newCMO) { | |
cmoAddress = _newCMO; | |
} | |
function getBAEFeeAddress() external view onlyCLevelOrOwner returns (address) { | |
return BAEFeeAddress; | |
} | |
function setBAEFeeAddress(address payable _newAddress) public onlyCLevelOrOwner { | |
BAEFeeAddress = _newAddress; | |
} | |
} | |
// File: contracts/ArtShop.sol | |
/// @title Artshop contract | |
/// @notice This contract holds the artpiece and artpiece metadata data and structure and it's where | |
/// artpieces are being created. | |
/// @author isaac@mammbo.com | |
contract ArtShop is AccessControl { | |
using SafeMath for uint256; | |
/// @dev this fires everytime an artpiece is created | |
event NewArtpiece(uint pieceId, string name, string artist); | |
/// @dev this is set up to track how many changes the url has been changed | |
event UrlChange(uint pieceId); | |
/// @dev - both baeFeeLevel and royaltyFeeLevel are percentages and the combined should be | |
/// kept below 95% on first sale or 99% on secondary sale :) | |
uint8 internal baeFeeLevel; | |
uint8 internal royaltyFeeLevel; | |
uint8 internal potFeeLevel = 5; | |
/// @dev all metadata relating to an artpiece | |
/// @dev this is done to prevent the error: Stacktrace too long as per | |
/// @dev https://ethereum.stackexchange.com/questions/7325/stack-too-deep-try-removing-local-variables | |
struct ArtpieceMetaData { | |
uint8 remainingPrintings; | |
uint64 basePrice; ///@dev listing price | |
uint64 dateCreatedByTheArtist; //@dev this will be from the present to the past | |
bool isFirstSale; | |
bool physical; | |
} | |
/// @dev all properties of an artpiece | |
struct Artpiece { | |
string name; | |
string artist; | |
string thumbnailUrl; | |
string mainUrl; | |
string grade; | |
uint64 price; /// @dev current price | |
uint8 feeLevel; /// @dev this is the royalty fee | |
uint8 baeFeeLevel; | |
uint8 paid; | |
ArtpieceMetaData metadata; | |
} | |
Artpiece[] artpieces; | |
mapping(uint256 => address) public numArtInAddress; | |
mapping(address => uint256) public artCollection; | |
mapping(uint256 => address) internal artpieceApproved; | |
/// @dev contract-specific modifiers on fees | |
modifier onlyWithGloballySetFees() { | |
require(baeFeeLevel > 0, "Requires a fee level to be set up"); | |
require( | |
royaltyFeeLevel > 0, | |
"Requires a an artist fee level to be set up" | |
); | |
_; | |
} | |
modifier onlyWithBasicAddressesSet() { | |
require( | |
address(ceoAddress) != address(0) && | |
address(cfoAddress) != address(0) && | |
address(BAEFeeAddress) != address(0) | |
); | |
_; | |
} | |
/// @dev this is the gloabal fee setter | |
/// @dev setBAEFeeLevel should be 35 initially on first sale | |
function setBAEFeeLevel(uint8 _newFee) public onlyCLevelOrOwner { | |
baeFeeLevel = _newFee; | |
} | |
function setRoyaltyFeeLevel(uint8 _newFee) public onlyCLevelOrOwner { | |
royaltyFeeLevel = _newFee; | |
} | |
function _createArtpiece( | |
string memory _name, | |
string memory _artist, | |
string memory _thumbnailUrl, | |
string memory _mainUrl, | |
string memory _grade, | |
uint64 _price, | |
uint64 _basePrice, | |
uint64 _dateCreatedByTheArtist, | |
uint8 _remainingPrintings, | |
bool _physical | |
) internal onlyWithGloballySetFees onlyWithBasicAddressesSet { | |
ArtpieceMetaData memory metd = ArtpieceMetaData( | |
_remainingPrintings, | |
_basePrice, | |
_dateCreatedByTheArtist, | |
true, | |
_physical | |
); | |
Artpiece memory newArtpiece = Artpiece( | |
_name, | |
_artist, | |
_thumbnailUrl, | |
_mainUrl, | |
_grade, | |
_price, | |
royaltyFeeLevel, | |
baeFeeLevel, | |
0, | |
metd | |
); | |
uint id = artpieces.push(newArtpiece) - 1; | |
numArtInAddress[id] = msg.sender; | |
artCollection[msg.sender] = artCollection[msg.sender].add(1); | |
emit NewArtpiece(id, _name, _artist); | |
} | |
} | |
// File: contracts/Helpers.sol | |
/// @title Helpers | |
/// @notice Helper functions for artshop. | |
/// @author isaac@mammbo.com | |
/// @dev getters are conglomerated so also because of EIP-170 (deployment size limit). | |
contract Helpers is ArtShop { | |
event Printed(uint indexed _id, uint256 indexed _time); | |
/// @dev modifiers for the ERC721-compliant functions | |
modifier onlyOwnerOf(uint _artpieceId) { | |
require(msg.sender == numArtInAddress[_artpieceId]); | |
_; | |
} | |
/// @dev we use this so we can't delete artpieces once they are on auction | |
/// so people have the feeling they really own the artpiece. | |
modifier onlyBeforeFirstSale(uint _tokenId) { | |
(, , , bool isFirstSale, ) = getArtpieceMeta(_tokenId); | |
require(isFirstSale == true); | |
_; | |
} | |
function getArtpieceData(uint _id) | |
public | |
view | |
returns ( | |
string memory name, | |
string memory artist, | |
string memory thumbnailUrl, | |
string memory grade, | |
uint64 price, | |
uint8 _paid | |
) | |
{ | |
return (artpieces[_id].name, artpieces[_id].artist, artpieces[_id].thumbnailUrl, artpieces[_id].grade, artpieces[_id].price, artpieces[_id].paid); | |
} | |
function getArtpieceFeeLevels(uint _id) public view returns (uint8, uint8) { | |
return (artpieces[_id].feeLevel, artpieces[_id].baeFeeLevel); | |
} | |
function getArtpieceMeta(uint _id) | |
public | |
view | |
returns (uint8, uint64, uint64, bool, bool) | |
{ | |
return (artpieces[_id].metadata.remainingPrintings, artpieces[_id].metadata.basePrice, artpieces[_id].metadata.dateCreatedByTheArtist, artpieces[_id].metadata.isFirstSale, artpieces[_id].metadata.physical); | |
} | |
function getMainUrl(uint _id) | |
public | |
view | |
onlyOwnerOf(_id) | |
returns (string memory) | |
{ | |
return artpieces[_id].mainUrl; | |
} | |
function setArtpieceName(uint _id, string memory _name) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].name = _name; | |
} | |
function setArtist(uint _id, string memory _artist) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].artist = _artist; | |
} | |
function setThumbnailUrl(uint _id, string memory _newThumbnailUrl) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].thumbnailUrl = _newThumbnailUrl; | |
} | |
// this used to be internal | |
function setMainUrl(uint _id, string memory _newUrl) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].mainUrl = _newUrl; | |
emit UrlChange(_id); | |
} | |
function setGrade(uint _id, string memory _grade) | |
public | |
onlyCLevelOrOwner | |
returns (bool success) | |
{ | |
artpieces[_id].grade = _grade; | |
return true; | |
} | |
function setPrice(uint _id, uint64 _price) public onlyCLevelOrOwner { | |
artpieces[_id].price = _price; | |
} | |
function setPaid(uint _id, uint8 _paid) public onlyCLevelOrOwner { | |
artpieces[_id].paid = _paid; | |
} | |
function setArtpieceBAEFee(uint _id, uint8 _newFee) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].baeFeeLevel = _newFee; | |
} | |
function setArtpieceRoyaltyFeeLevel(uint _id, uint8 _newFee) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].feeLevel = _newFee; | |
} | |
function setRemainingPrintings(uint _id, uint8 _remainingPrintings) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].metadata.remainingPrintings = _remainingPrintings; | |
} | |
function setBasePrice(uint _id, uint64 _basePrice) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].metadata.basePrice = _basePrice; | |
} | |
function setDateCreateByArtist(uint _id, uint8 _dateCreatedByTheArtist) | |
public | |
onlyCLevelOrOwner | |
{ | |
artpieces[_id].metadata.dateCreatedByTheArtist = _dateCreatedByTheArtist; | |
} | |
function setIsPhysical(uint _id, bool _physical) public onlyCLevelOrOwner { | |
artpieces[_id].metadata.physical = _physical; | |
} | |
function getArtpiecesByOwner(address _owner) | |
external | |
view | |
returns (uint[] memory) | |
{ | |
uint[] memory result = new uint[](artCollection[_owner]); | |
uint counter = 0; | |
for (uint i = 0; i < artpieces.length; i++) { | |
if (numArtInAddress[i] == _owner) { | |
result[counter] = i; | |
counter = counter.add(1); | |
} | |
} | |
return result; | |
} | |
} | |
// File: contracts/Interfaces/IBAEPayments.sol | |
/// @title BAE Auction Interface contract | |
/// @notice | |
/// @author isaac@mammbo.com | |
/// @dev this contract interfaces with the BAEAuction contract which holds the auction functionality | |
contract IBAEPayments { | |
function setBAECoreAddress(address payable _address) external; | |
function addToBAEHolders(address _to) external; | |
function subToBAEHolders(address _from, address _to, uint _amount) external; | |
function setFinalPriceInPounds(uint _finalPrice) external; | |
function getContractBalance() external returns (uint256); | |
function withdraw() external; | |
} | |
// File: contracts/BAE.sol | |
/// @title BAE Token | |
/// @notice | |
/// @author isaac@mammbo.com | |
/// @dev This is the main contract which creates an ERC721 token | |
contract BAE is ERC721Full, Helpers { | |
using SafeMath for uint256; | |
using SafeMath8 for uint8; | |
/// @dev - extra events on the ERC721 contract | |
event Sold( | |
uint indexed _tokenId, | |
address _from, | |
address _to, | |
uint indexed _price | |
); | |
event Deleted(uint indexed _tokenId, address _from); | |
event PaymentsContractChange(address _prevAddress, address _futureAddress); | |
event AuctionContractChange(address _prevAddress, address _futureAddress); | |
uint public costToTokenise = 7.5 finney; | |
IBAEPayments public TokenInterface; | |
mapping(uint => address) artTransApprovals; | |
constructor() public ERC721Full("BlockchainArtExchange", "BAE") {} | |
/// @dev functions affecting ERC20 tokens | |
function setPaymentAddress(address payable _newAddress) public onlyCEO { | |
IBAEPayments tokenInterfaceCandidate = IBAEPayments(_newAddress); | |
TokenInterface = tokenInterfaceCandidate; | |
} | |
/// @dev set function for auction address | |
function setAuctionInstance(address _auction) public onlyCLevelOrOwner { | |
IBAEAuction candidate = IBAEAuction(_auction); | |
AuctionInstance = candidate; | |
} | |
/// @dev set post-purchase data | |
function _postPurchase(address _from, address _to, uint256 _tokenId) | |
internal | |
{ | |
artCollection[_to] = artCollection[_to].add(1); | |
artCollection[_from] = artCollection[_from].sub(1); | |
numArtInAddress[_tokenId] = _to; | |
// @dev change percentages after first sale. | |
if (artpieces[_tokenId].metadata.isFirstSale) { | |
artpieces[_tokenId].feeLevel = uint8(96); | |
artpieces[_tokenId].baeFeeLevel = uint8(3); | |
} | |
/// @dev and change firstSale to be false | |
artpieces[_tokenId].metadata.isFirstSale = false; | |
/// @dev emit the sold event to wrap it up | |
emit Sold(_tokenId, _from, _to, artpieces[_tokenId].price); | |
} | |
function _calculateFees(uint256 _tokenId, uint256 _finalPrice) | |
internal | |
view | |
returns (uint256 baeFee, uint256 royaltyFee, uint256 potFee) | |
{ | |
/// @dev calculate the fees. | |
uint256 baeFeeAmount = uint256( | |
(uint256(artpieces[_tokenId].baeFeeLevel).mul(_finalPrice)).div(100) | |
); | |
uint256 sellerFeeAmount = uint256( | |
(uint256(artpieces[_tokenId].feeLevel).mul(_finalPrice)).div(100) | |
); | |
/// @dev any extra money will be added to the pot | |
uint256 potFeeAmount = _finalPrice.sub( | |
baeFeeAmount.add(sellerFeeAmount) | |
); | |
return (baeFeeAmount, sellerFeeAmount, potFeeAmount); | |
} | |
function setCostOfTokenise(uint256 _newCost) public onlyCLevelOrOwner { | |
costToTokenise = _newCost; | |
} | |
/// @dev this function interfaces with the underlying _createArtpiece | |
function createArtpiece( | |
string memory _name, | |
string memory _artist, | |
string memory _thumbnailUrl, | |
string memory _mainUrl, | |
string memory _grade, | |
string memory _uri, | |
uint64 _price, | |
uint64 _basePrice, | |
uint64 _dateCreatedByTheArtist, | |
uint8 _remainingPrintings, | |
bool _physical | |
) public payable { | |
require( | |
msg.value >= costToTokenise, | |
"You need to pay the fee to create an artpiece" | |
); | |
super._createArtpiece( | |
_name, | |
_artist, | |
_thumbnailUrl, | |
_mainUrl, | |
_grade, | |
_price, | |
_basePrice, | |
_dateCreatedByTheArtist, | |
_remainingPrintings, | |
_physical | |
); | |
// @dev create the token | |
_mint(msg.sender, artpieces.length - 1); | |
// @dev set the tokenURI | |
_setTokenURI(artpieces.length - 1, _uri); | |
// @dev once it's all done we collect the money | |
cfoAddress.transfer(msg.value); | |
} | |
function payFees( | |
uint256 _baeFee, | |
uint256 _royaltyFee, | |
uint256 _potFee, | |
address payable _seller | |
) public payable { | |
uint256 totalToPay = (_baeFee.add(_royaltyFee)).add(_potFee); | |
require( | |
msg.value >= totalToPay, | |
"Value must be equal or greater than the cost of the fees" | |
); | |
BAEFeeAddress.transfer(msg.value.sub(_baeFee)); | |
_seller.transfer(msg.value.sub(_royaltyFee)); | |
// @dev send the value left to the POT contract | |
if (address(TokenInterface) != address(0)) { | |
// address(TokenInterface).transfer(msg.value); | |
} | |
} | |
/// @dev this method is not part of erc-721 - not yet tested | |
function deleteArtpiece(uint256 _tokenId) | |
public | |
onlyCLevelOrOwner | |
onlyBeforeFirstSale(_tokenId) | |
returns (bool deleted) | |
{ | |
address _from = numArtInAddress[_tokenId]; | |
delete numArtInAddress[_tokenId]; | |
artCollection[_from] = artCollection[_from].sub(1); | |
_burn(_from, _tokenId); | |
delete artpieces[_tokenId]; | |
emit Deleted(_tokenId, _from); | |
return true; | |
} | |
} | |
// File: contracts/Interfaces/IBAEAuction.sol | |
/// @title BAE Auction Interface contract | |
/// @notice | |
/// @author isaac@mammbo.com | |
/// @dev this contract interfaces with the BAEAuction contract which holds the auction functionality | |
contract IBAEAuction { | |
function setOwnerCut(uint256 _newAmount) external; | |
function setNFTAddress(address payable _candidateAddress) external; | |
function createAuction(uint256 _tokenId, uint128 _startingPrice, uint64 _duration, address payable _seller) external payable returns (uint256); | |
function checkItemIsOnAuction(uint256 _tokenId) external returns (bool); | |
function bid(uint256 _tokenId) external payable returns (bool); | |
function finalizeAuction(uint256 _tokenId, address payable _BAEFeeAddress, uint256 _baeFee, address payable _seller, uint256 _artistFee, address payable _token, uint256 _potFee) external returns (bool); | |
function getAuctionDetails(uint _tokenId) public view returns (address payable, uint128, uint128, uint64, uint64); | |
function highestBidder(uint _tokenId) external view returns (address); | |
function nftTransferred(uint256 _tokenId) external; | |
function withdraw() external returns (bool); | |
} | |
// File: contracts/BAECore.sol | |
/// @title BAECore | |
/// @notice | |
/// @author isaac@mammbo.com | |
/// @dev we inherit from destructible because if we inherit from BAE.sol we need to reimplement | |
/// here all the ERC721 functions. | |
contract BAECore is BAE { | |
using SafeMath for uint256; | |
using SafeMath8 for uint8; | |
/// @dev this will be private so no one can see where it is living and will be deployed by another address | |
IBAEAuction private AuctionInstance; | |
event NFTRetrievedForAuction(uint256 tokenId); | |
constructor(address payable _auctionAddress, address payable _paymentsAddress, address payable _BAEFeeAddress, address payable _cfoAddress, uint8 _baeFeeLevel, uint8 _royaltyFeeLevel) public { | |
ceoAddress = msg.sender; | |
cfoAddress = _cfoAddress; | |
initialise(_auctionAddress, _paymentsAddress, _BAEFeeAddress, _baeFeeLevel, _royaltyFeeLevel); | |
} | |
modifier withSetTokenAddress() { | |
require(address(TokenInterface) != address(0)); | |
_; | |
} | |
function initialise(address payable _auctionAddress, address payable _paymentsAddress, address payable _BAEFeeAddress, uint8 _baeFeeLevel, uint8 _royaltyFeeLevel) internal { | |
setAuctionInstance(_auctionAddress); | |
setPaymentAddress(_paymentsAddress); | |
setBAEFeeAddress(_BAEFeeAddress); | |
setRoyaltyFeeLevel(_royaltyFeeLevel); | |
setBAEFeeLevel(_baeFeeLevel); | |
} | |
/// @dev we can also charge straight away by charging an amount and making this function payable | |
function createAuction( | |
uint _tokenId, | |
uint128 _startingPrice, | |
uint64 _duration | |
) external payable { | |
require( | |
ownerOf(_tokenId) == msg.sender, | |
"You can't transfer an artpiece which is not yours" | |
); | |
require(_startingPrice >= artpieces[_tokenId].metadata.basePrice); | |
AuctionInstance.createAuction.value(msg.value)( | |
_tokenId, | |
_startingPrice, | |
_duration, | |
msg.sender | |
); | |
(, , , , , uint8 paid) = getArtpieceData(_tokenId); | |
setPaid(_tokenId, paid.add(1)); | |
} | |
function bid(uint _tokenId) external payable returns (bool) { | |
require(msg.sender != ownerOf(_tokenId)); | |
bool success = AuctionInstance.bid.value(msg.value)(_tokenId); | |
return success; | |
} | |
/// @dev pay for the artpiece | |
function payForArtpiece(uint _tokenId) public withSetTokenAddress { | |
(address payable seller, , uint128 finalPrice, , ) = AuctionInstance.getAuctionDetails( | |
_tokenId | |
); | |
address winner = getHighestBidder(_tokenId); | |
(uint256 baeFeeAmount, uint256 artistFeeAmount, uint256 potFeeAmount) = _calculateFees( | |
_tokenId, | |
uint256(finalPrice) | |
); | |
// @dev and send the remaining to the POT | |
address payable token = address(uint160(address(TokenInterface))); | |
bool result = AuctionInstance.finalizeAuction( | |
_tokenId, | |
BAEFeeAddress, | |
baeFeeAmount, | |
seller, | |
artistFeeAmount, | |
token, | |
potFeeAmount | |
); | |
// @dev once paid we update the reference | |
require(result == true); | |
(, , , , , uint8 paid) = getArtpieceData(_tokenId); | |
setPaid(_tokenId, paid.add(1)); | |
/// @dev approve the winner to | |
approve(winner, _tokenId); | |
// @dev add seller to the holders | |
TokenInterface.addToBAEHolders(seller); | |
} | |
function transferFrom(address _from, address _to, uint256 _tokenId) public { | |
require( | |
_to == getApproved(_tokenId) || _from == ownerOf(_tokenId) | |
); | |
(, , , , , uint paid) = getArtpieceData(_tokenId); | |
require((paid % 2) == 0); | |
super.transferFrom(_from, _to, _tokenId); | |
_postPurchase(_from, _to, _tokenId); | |
AuctionInstance.nftTransferred(_tokenId); | |
emit NFTRetrievedForAuction(_tokenId); | |
} | |
/// @dev will change this to be _tokenId so there is no need to really keep track of auctionIds | |
function getAuctionDetails(uint _tokenId) | |
public | |
view | |
returns (address, uint128, uint128, uint64, uint64) | |
{ | |
return AuctionInstance.getAuctionDetails(_tokenId); | |
} | |
function getHighestBidder(uint _tokenId) public view returns (address) { | |
return AuctionInstance.highestBidder(_tokenId); | |
} | |
function withdrawFunds() public { | |
AuctionInstance.withdraw(); | |
} | |
function() external payable {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment