- Tests for new features are pending - ERC721 is abstract, since it requires metadata implementation - Move some methods into DeprecatedERC721 contract - Reorganise base vs full implementation - Pending tokenByIndexpull/803/head
parent
ca163a849b
commit
71cbc511ec
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
library AddressUtils { |
||||
|
||||
function isContract(address addr) internal view returns (bool) { |
||||
uint size; |
||||
assembly { size := extcodesize(addr) } |
||||
return size > 0; |
||||
} |
||||
|
||||
} |
@ -1,19 +0,0 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "../token/ERC721/BaseERC721Token.sol"; |
||||
|
||||
/** |
||||
* @title BaseERC721TokenMock |
||||
* This mock just provides a public mint and burn functions for testing purposes. |
||||
*/ |
||||
contract BaseERC721TokenMock is BaseERC721Token { |
||||
function BaseERC721TokenMock() BaseERC721Token() public { } |
||||
|
||||
function mint(address _to, uint256 _tokenId) public { |
||||
super._mint(_to, _tokenId); |
||||
} |
||||
|
||||
function burn(uint256 _tokenId) public { |
||||
super._burn(_tokenId); |
||||
} |
||||
} |
@ -0,0 +1,17 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "../token/ERC721/ERC721BasicToken.sol"; |
||||
|
||||
/** |
||||
* @title ERC721BasicTokenMock |
||||
* This mock just provides a public mint and burn functions for testing purposes |
||||
*/ |
||||
contract ERC721BasicTokenMock is ERC721BasicToken { |
||||
function mint(address _to, uint256 _tokenId) public { |
||||
super.doMint(_to, _tokenId); |
||||
} |
||||
|
||||
function burn(uint256 _tokenId) public { |
||||
super.doBurn(_tokenId); |
||||
} |
||||
} |
@ -1,16 +1,35 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "./BaseERC721TokenMock.sol"; |
||||
import "./ERC721BasicTokenMock.sol"; |
||||
import "../token/ERC721/ERC721Token.sol"; |
||||
|
||||
/** |
||||
* @title ERC721TokenMock |
||||
* This mock just provides a public mint and burn functions for testing purposes. |
||||
* This mock just provides a public mint and burn functions for testing purposes, |
||||
* and a mock metadata URI implementation |
||||
*/ |
||||
contract ERC721TokenMock is ERC721Token, BaseERC721TokenMock { |
||||
contract ERC721TokenMock is ERC721Token, ERC721BasicTokenMock { |
||||
function ERC721TokenMock(string name, string symbol) |
||||
BaseERC721TokenMock() |
||||
ERC721BasicTokenMock() |
||||
ERC721Token(name, symbol) |
||||
public |
||||
{ } |
||||
|
||||
// Mock implementation for testing. |
||||
// Do not use this code in production! |
||||
function tokenURI(uint256 _tokenId) public view returns (string) { |
||||
require(exists(_tokenId)); |
||||
|
||||
bytes memory uri = new bytes(78); |
||||
|
||||
uint256 i; |
||||
uint256 value = _tokenId; |
||||
|
||||
for (i = 0; i < 78; i++) { |
||||
uri[7 + 78 - i] = byte(value % 10 + 48); |
||||
value = value / 10; |
||||
} |
||||
|
||||
return string(uri); |
||||
} |
||||
} |
||||
|
@ -1,16 +0,0 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
/** |
||||
* @title Base ERC721 interface |
||||
* @dev see https://github.com/ethereum/eips/issues/721 and https://github.com/ethereum/EIPs/pull/841 |
||||
*/ |
||||
contract BaseERC721 { |
||||
event Transfer(address indexed _from, address indexed _to, uint256 _tokenId); |
||||
event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId); |
||||
|
||||
function balanceOf(address _owner) public view returns (uint256 _balance); |
||||
function ownerOf(uint256 _tokenId) public view returns (address _owner); |
||||
function transfer(address _to, uint256 _tokenId) public; |
||||
function approve(address _to, uint256 _tokenId) public; |
||||
function takeOwnership(uint256 _tokenId) public; |
||||
} |
@ -1,214 +0,0 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "./BaseERC721.sol"; |
||||
import "../../math/SafeMath.sol"; |
||||
|
||||
/** |
||||
* @title BaseERC721Token |
||||
* Generic implementation for the required functionality of the ERC721 standard |
||||
*/ |
||||
contract BaseERC721Token is BaseERC721 { |
||||
using SafeMath for uint256; |
||||
|
||||
// Total amount of tokens |
||||
uint256 private totalTokens; |
||||
|
||||
// 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 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; |
||||
|
||||
/** |
||||
* @dev Guarantees msg.sender is owner of the given token |
||||
* @param _tokenId uint256 ID of the token to validate its ownership belongs to msg.sender |
||||
*/ |
||||
modifier onlyOwnerOf(uint256 _tokenId) { |
||||
require(ownerOf(_tokenId) == msg.sender); |
||||
_; |
||||
} |
||||
|
||||
/** |
||||
* @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 totalTokens; |
||||
} |
||||
|
||||
/** |
||||
* @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) { |
||||
return ownedTokens[_owner].length; |
||||
} |
||||
|
||||
/** |
||||
* @dev Gets the list of tokens owned by a given address |
||||
* @param _owner address to query the tokens of |
||||
* @return uint256[] representing the list of tokens owned by the passed address |
||||
*/ |
||||
function tokensOf(address _owner) public view returns (uint256[]) { |
||||
return ownedTokens[_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 Gets the approved address to take ownership of a given token ID |
||||
* @param _tokenId uint256 ID of the token to query the approval of |
||||
* @return address currently approved to take ownership of the given token ID |
||||
*/ |
||||
function approvedFor(uint256 _tokenId) public view returns (address) { |
||||
return tokenApprovals[_tokenId]; |
||||
} |
||||
|
||||
/** |
||||
* @dev Transfers the ownership of a given token ID to another address |
||||
* @param _to address to receive the ownership of the given token ID |
||||
* @param _tokenId uint256 ID of the token to be transferred |
||||
*/ |
||||
function transfer(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) { |
||||
clearApprovalAndTransfer(msg.sender, _to, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Approves another address to claim for the ownership of the given token ID |
||||
* @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 onlyOwnerOf(_tokenId) { |
||||
address owner = ownerOf(_tokenId); |
||||
require(_to != owner); |
||||
if (approvedFor(_tokenId) != 0 || _to != 0) { |
||||
tokenApprovals[_tokenId] = _to; |
||||
Approval(owner, _to, _tokenId); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @dev Claims the ownership of a given token ID |
||||
* @param _tokenId uint256 ID of the token being claimed by the msg.sender |
||||
*/ |
||||
function takeOwnership(uint256 _tokenId) public { |
||||
require(isApprovedFor(msg.sender, _tokenId)); |
||||
clearApprovalAndTransfer(ownerOf(_tokenId), msg.sender, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Mint token function |
||||
* @param _to The address that will own the minted token |
||||
* @param _tokenId uint256 ID of the token to be minted by the msg.sender |
||||
*/ |
||||
function _mint(address _to, uint256 _tokenId) internal { |
||||
require(_to != address(0)); |
||||
addToken(_to, _tokenId); |
||||
Transfer(0x0, _to, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Burns a specific token |
||||
* @param _tokenId uint256 ID of the token being burned by the msg.sender |
||||
*/ |
||||
function _burn(uint256 _tokenId) onlyOwnerOf(_tokenId) internal { |
||||
if (approvedFor(_tokenId) != 0) { |
||||
clearApproval(msg.sender, _tokenId); |
||||
} |
||||
removeToken(msg.sender, _tokenId); |
||||
Transfer(msg.sender, 0x0, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Tells whether the given spender is approved for the given token ID or not |
||||
* This function is not private so it can be extended in further implementations like the operatable ERC721 |
||||
* @param _spender address of the spender to query the approval of |
||||
* @param _tokenId uint256 ID of the token to query the approval of |
||||
* @return bool whether the msg.sender is approved for the given token ID or not |
||||
*/ |
||||
function isApprovedFor(address _spender, uint256 _tokenId) internal view returns (bool) { |
||||
return approvedFor(_tokenId) == _spender; |
||||
} |
||||
|
||||
/** |
||||
* @dev Internal function to clear current approval and transfer the ownership of a given token ID |
||||
* @param _from address which you want to send tokens from |
||||
* @param _to address which you want to transfer the token to |
||||
* @param _tokenId uint256 ID of the token to be transferred |
||||
*/ |
||||
function clearApprovalAndTransfer(address _from, address _to, uint256 _tokenId) internal { |
||||
require(_to != address(0)); |
||||
require(_to != ownerOf(_tokenId)); |
||||
require(ownerOf(_tokenId) == _from); |
||||
|
||||
clearApproval(_from, _tokenId); |
||||
removeToken(_from, _tokenId); |
||||
addToken(_to, _tokenId); |
||||
Transfer(_from, _to, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Internal function to clear current approval of a given token ID |
||||
* @param _tokenId uint256 ID of the token to be transferred |
||||
*/ |
||||
function clearApproval(address _owner, uint256 _tokenId) private { |
||||
require(ownerOf(_tokenId) == _owner); |
||||
tokenApprovals[_tokenId] = 0; |
||||
Approval(_owner, 0, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Internal function to add a token ID to the list of a given address |
||||
* @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 addToken(address _to, uint256 _tokenId) private { |
||||
require(tokenOwner[_tokenId] == address(0)); |
||||
tokenOwner[_tokenId] = _to; |
||||
uint256 length = balanceOf(_to); |
||||
ownedTokens[_to].push(_tokenId); |
||||
ownedTokensIndex[_tokenId] = length; |
||||
totalTokens = totalTokens.add(1); |
||||
} |
||||
|
||||
/** |
||||
* @dev Internal function to remove a token ID from the list of a given address |
||||
* @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 removeToken(address _from, uint256 _tokenId) private { |
||||
require(ownerOf(_tokenId) == _from); |
||||
|
||||
uint256 tokenIndex = ownedTokensIndex[_tokenId]; |
||||
uint256 lastTokenIndex = balanceOf(_from).sub(1); |
||||
uint256 lastToken = ownedTokens[_from][lastTokenIndex]; |
||||
|
||||
tokenOwner[_tokenId] = 0; |
||||
ownedTokens[_from][tokenIndex] = lastToken; |
||||
ownedTokens[_from][lastTokenIndex] = 0; |
||||
// Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to |
||||
// be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping |
||||
// the lastToken to the first position, and then dropping the element placed in the last position of the list |
||||
|
||||
ownedTokens[_from].length--; |
||||
ownedTokensIndex[_tokenId] = 0; |
||||
ownedTokensIndex[lastToken] = tokenIndex; |
||||
totalTokens = totalTokens.sub(1); |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "./ERC721.sol"; |
||||
|
||||
contract DeprecatedERC721 is ERC721 { |
||||
function takeOwnership(uint256 _tokenId) public; |
||||
function transfer(address _to, uint256 _tokenId) public; |
||||
function tokensOf(address _owner) public view returns (uint256[]); |
||||
} |
||||
|
@ -0,0 +1,35 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "./DeprecatedERC721.sol"; |
||||
import "./ERC721Token.sol"; |
||||
|
||||
contract DeprecatedERC721Token is DeprecatedERC721, ERC721Token { |
||||
/** |
||||
* @dev Claims the ownership of a given token ID |
||||
* @param _tokenId uint256 ID of the token being claimed by the msg.sender |
||||
*/ |
||||
function takeOwnership(uint256 _tokenId) canTransfer(_tokenId) public { |
||||
require(msg.sender != ownerOf(_tokenId)); |
||||
clearApprovalAndTransfer(ownerOf(_tokenId), msg.sender, _tokenId, "", false); |
||||
} |
||||
|
||||
/** |
||||
* @dev Transfers the ownership of a given token ID to another address |
||||
* @param _to address to receive the ownership of the given token ID |
||||
* @param _tokenId uint256 ID of the token to be transferred |
||||
*/ |
||||
function transfer(address _to, uint256 _tokenId) public { |
||||
address owner = ownerOf(_tokenId); |
||||
require(owner == msg.sender); |
||||
clearApprovalAndTransfer(owner, _to, _tokenId, "", false); |
||||
} |
||||
|
||||
/** |
||||
* @dev Gets the list of tokens owned by a given address |
||||
* @param _owner address to query the tokens of |
||||
* @return uint256[] representing the list of tokens owned by the passed address |
||||
*/ |
||||
function tokensOf(address _owner) public view returns (uint256[]) { |
||||
return ownedTokens[_owner]; |
||||
} |
||||
} |
@ -1,19 +1,18 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "./BaseERC721.sol"; |
||||
import "./ERC721Basic.sol"; |
||||
|
||||
contract ERC721Enumerable is ERC721Basic { |
||||
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); |
||||
} |
||||
|
||||
/** |
||||
* @title Full ERC721 interface |
||||
* @dev see https://github.com/ethereum/eips/issues/721 and https://github.com/ethereum/EIPs/pull/841 |
||||
*/ |
||||
contract ERC721 is BaseERC721 { |
||||
event OperatorApproval(address indexed _owner, address indexed _operator, bool _approved); |
||||
|
||||
contract ERC721Metadata is ERC721Basic { |
||||
function name() public view returns (string _name); |
||||
function symbol() public view returns (string _symbol); |
||||
function takeOwnershipFor(address _to, uint256 _tokenId) public; |
||||
function setOperatorApproval(address _to, bool _approved) public; |
||||
function isOperatorApprovedFor(address _owner, address _operator) public view returns (bool); |
||||
function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256 _tokenId); |
||||
function tokenURI(uint256 _tokenId) public view returns (string); |
||||
} |
||||
|
||||
contract ERC721 is ERC721Basic, ERC721Enumerable, ERC721Metadata { |
||||
} |
||||
|
@ -0,0 +1,25 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
/** |
||||
* @title Base ERC721 interface |
||||
* @dev see https://github.com/ethereum/eips/issues/721 and https://github.com/ethereum/EIPs/pull/841 |
||||
*/ |
||||
contract ERC721Basic { |
||||
event Transfer(address indexed _from, address indexed _to, uint256 _tokenId); |
||||
event Approval(address indexed _owner, address indexed _approved, uint256 _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 exists(uint256 _tokenId) public view returns (bool _exists); |
||||
|
||||
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 _data) public; |
||||
} |
@ -0,0 +1,247 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "./ERC721Basic.sol"; |
||||
import "./ERC721Receiver.sol"; |
||||
import "../../math/SafeMath.sol"; |
||||
import "../../AddressUtils.sol"; |
||||
|
||||
/** |
||||
* @title ERC721BasicToken |
||||
* Generic implementation for the required functionality of the ERC721 standard |
||||
*/ |
||||
contract ERC721BasicToken is ERC721Basic { |
||||
using SafeMath for uint256; |
||||
using AddressUtils for address; |
||||
|
||||
uint256 SAFE_TRANSFER_GAS_STIPEND = 50000; |
||||
|
||||
// Equals to bytes4(keccak256("onERC721Received(address,uint256,bytes)")) |
||||
bytes4 ERC721_RECEIVED = 0xf0b9e5ba; |
||||
|
||||
// Total amount of tokens |
||||
uint256 internal totalTokens; |
||||
|
||||
// Mapping from token ID to owner |
||||
mapping (uint256 => address) internal tokenOwner; |
||||
|
||||
// Mapping from token ID to approved address |
||||
mapping (uint256 => address) internal tokenApprovals; |
||||
|
||||
// Mapping from owner to number of owned token |
||||
mapping (address => uint256) internal ownedTokensCount; |
||||
|
||||
// Mapping from owner to operator approvals |
||||
mapping (address => mapping (address => bool)) internal operatorApprovals; |
||||
|
||||
/** |
||||
* @dev Guarantees msg.sender is owner of the given token |
||||
* @param _tokenId uint256 ID of the token to validate its ownership belongs to msg.sender |
||||
*/ |
||||
modifier onlyOwnerOf(uint256 _tokenId) { |
||||
require(ownerOf(_tokenId) == msg.sender); |
||||
_; |
||||
} |
||||
|
||||
modifier canTransfer(uint256 _tokenId) { |
||||
require(isApprovedOrOwner(msg.sender, _tokenId)); |
||||
_; |
||||
} |
||||
|
||||
/** |
||||
* @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) { |
||||
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; |
||||
} |
||||
|
||||
function exists(uint256 _tokenId) public view returns (bool) { |
||||
address owner = tokenOwner[_tokenId]; |
||||
return owner != 0; |
||||
} |
||||
|
||||
/** |
||||
* @dev Approves another address to claim for the ownership of the given token ID |
||||
* @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 onlyOwnerOf(_tokenId) { |
||||
address owner = ownerOf(_tokenId); |
||||
require(_to != owner); |
||||
if (getApproved(_tokenId) != 0 || _to != 0) { |
||||
tokenApprovals[_tokenId] = _to; |
||||
Approval(owner, _to, _tokenId); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @dev Gets the approved address to take ownership of a given token ID |
||||
* @param _tokenId uint256 ID of the token to query the approval of |
||||
* @return address currently approved to take ownership of the given token ID |
||||
*/ |
||||
function getApproved(uint256 _tokenId) public view returns (address) { |
||||
return tokenApprovals[_tokenId]; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @dev Sets the approval of a given operator |
||||
* @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; |
||||
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 |
||||
* @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 canTransfer(_tokenId) { |
||||
clearApprovalAndTransfer(_from, _to, _tokenId, "", false); |
||||
} |
||||
|
||||
/** |
||||
* @dev Transfers the ownership of a given token ID to another address |
||||
* @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 canTransfer(_tokenId) { |
||||
clearApprovalAndTransfer(_from, _to, _tokenId, "", true); |
||||
} |
||||
|
||||
/** |
||||
* @dev Transfers the ownership of a given token ID to another address |
||||
* @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 _data) public canTransfer(_tokenId) { |
||||
clearApprovalAndTransfer(_from, _to, _tokenId, _data, true); |
||||
} |
||||
|
||||
/** |
||||
* @dev Tells 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 Mint token function |
||||
* @param _to The address that will own the minted token |
||||
* @param _tokenId uint256 ID of the token to be minted by the msg.sender |
||||
*/ |
||||
function doMint(address _to, uint256 _tokenId) internal { |
||||
require(_to != address(0)); |
||||
addToken(_to, _tokenId); |
||||
totalTokens = totalTokens.add(1); |
||||
Transfer(0x0, _to, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Burns a specific token |
||||
* @param _tokenId uint256 ID of the token being burned by the msg.sender |
||||
*/ |
||||
function doBurn(uint256 _tokenId) onlyOwnerOf(_tokenId) internal { |
||||
if (getApproved(_tokenId) != 0) { |
||||
clearApproval(msg.sender, _tokenId); |
||||
} |
||||
removeToken(msg.sender, _tokenId); |
||||
totalTokens = totalTokens.sub(1); |
||||
Transfer(msg.sender, 0x0, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Internal function to clear current approval and transfer the ownership of a given token ID |
||||
* @param _from address which you want to send tokens from |
||||
* @param _to address which you want to transfer the token to |
||||
* @param _tokenId uint256 ID of the token to be transferred |
||||
* @param _data bytes data to send along with a safe transfer check |
||||
* @param _safe bool whether to perform a safe transfer |
||||
*/ |
||||
function clearApprovalAndTransfer(address _from, address _to, uint256 _tokenId, bytes _data, bool _safe) internal { |
||||
require(_from != address(0)); |
||||
require(_to != address(0)); |
||||
require(_to != ownerOf(_tokenId)); |
||||
require(ownerOf(_tokenId) == _from); |
||||
|
||||
clearApproval(_from, _tokenId); |
||||
removeToken(_from, _tokenId); |
||||
addToken(_to, _tokenId); |
||||
|
||||
require(!_safe || checkSafeTransfer(_from, _to, _tokenId, _data)); |
||||
|
||||
Transfer(_from, _to, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Internal function to clear current approval of a given token ID |
||||
* @param _tokenId uint256 ID of the token to be transferred |
||||
*/ |
||||
function clearApproval(address _owner, uint256 _tokenId) internal { |
||||
require(ownerOf(_tokenId) == _owner); |
||||
tokenApprovals[_tokenId] = 0; |
||||
Approval(_owner, 0, _tokenId); |
||||
} |
||||
|
||||
/** |
||||
* @dev Internal function to add a token ID to the list of a given address |
||||
* @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 addToken(address _to, uint256 _tokenId) internal { |
||||
require(tokenOwner[_tokenId] == address(0)); |
||||
tokenOwner[_tokenId] = _to; |
||||
ownedTokensCount[_to] = ownedTokensCount[_to].add(1); |
||||
} |
||||
|
||||
/** |
||||
* @dev Internal function to remove a token ID from the list of a given address |
||||
* @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 removeToken(address _from, uint256 _tokenId) internal { |
||||
require(ownerOf(_tokenId) == _from); |
||||
ownedTokensCount[_from] = ownedTokensCount[_from].sub(1); |
||||
tokenOwner[_tokenId] = 0; |
||||
} |
||||
|
||||
function checkSafeTransfer(address _from, address _to, uint256 _tokenId, bytes _data) internal returns (bool) { |
||||
return !_to.isContract() || |
||||
(ERC721Receiver(_to).onERC721Received.gas(SAFE_TRANSFER_GAS_STIPEND)(_from, _tokenId, _data) == ERC721_RECEIVED); |
||||
} |
||||
} |
@ -0,0 +1,9 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
import "./ERC721Receiver.sol"; |
||||
|
||||
contract ERC721Holder is ERC721Receiver { |
||||
function onERC721Received(address, uint256, bytes) public returns(bytes4) { |
||||
return ERC721_RECEIVED; |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
pragma solidity ^0.4.18; |
||||
|
||||
contract ERC721Receiver { |
||||
/// @dev Magic value to be returned upon successful reception of an NFT |
||||
/// Equals to bytes4(keccak256("onERC721Received(address,uint256,bytes)")) |
||||
bytes4 ERC721_RECEIVED = 0xf0b9e5ba; |
||||
|
||||
/// @notice Handle the receipt of an NFT |
||||
/// @dev The ERC721 smart contract calls this function on the recipient |
||||
/// after a `safetransfer`. This function MAY throw to revert and reject the |
||||
/// transfer. This function MUST use 50,000 gas or less. Return of other |
||||
/// than the magic value MUST result in the transaction being reverted. |
||||
/// Note: the contract address is always the message sender. |
||||
/// @param _from The sending address |
||||
/// @param _tokenId The NFT identifier which is being transfered |
||||
/// @param _data Additional data with no specified format |
||||
/// @return `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))` |
||||
function onERC721Received(address _from, uint256 _tokenId, bytes _data) public returns(bytes4); |
||||
} |
@ -1,534 +0,0 @@ |
||||
import assertRevert from '../../helpers/assertRevert'; |
||||
const BigNumber = web3.BigNumber; |
||||
const BaseERC721Token = artifacts.require('BaseERC721TokenMock.sol'); |
||||
|
||||
require('chai') |
||||
.use(require('chai-as-promised')) |
||||
.use(require('chai-bignumber')(BigNumber)) |
||||
.should(); |
||||
|
||||
contract('BaseERC721Token', accounts => { |
||||
let token = null; |
||||
const _firstTokenId = 1; |
||||
const _secondTokenId = 2; |
||||
const _unknownTokenId = 3; |
||||
const _creator = accounts[0]; |
||||
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; |
||||
|
||||
beforeEach(async function () { |
||||
token = await BaseERC721Token.new({ from: _creator }); |
||||
await token.mint(_creator, _firstTokenId, { from: _creator }); |
||||
await token.mint(_creator, _secondTokenId, { from: _creator }); |
||||
}); |
||||
|
||||
describe('totalSupply', function () { |
||||
it('has a total supply equivalent to the inital supply', async function () { |
||||
const totalSupply = await token.totalSupply(); |
||||
totalSupply.should.be.bignumber.equal(2); |
||||
}); |
||||
}); |
||||
|
||||
describe('balanceOf', function () { |
||||
describe('when the given address owns some tokens', function () { |
||||
it('returns the amount of tokens owned by the given address', async function () { |
||||
const balance = await token.balanceOf(_creator); |
||||
balance.should.be.bignumber.equal(2); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given address does not own any tokens', function () { |
||||
it('returns 0', async function () { |
||||
const balance = await token.balanceOf(accounts[1]); |
||||
balance.should.be.bignumber.equal(0); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('ownerOf', function () { |
||||
describe('when the given token ID was tracked by this token', function () { |
||||
const tokenId = _firstTokenId; |
||||
|
||||
it('returns the owner of the given token ID', async function () { |
||||
const owner = await token.ownerOf(tokenId); |
||||
owner.should.be.equal(_creator); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was not tracked by this token', function () { |
||||
const tokenId = _unknownTokenId; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.ownerOf(tokenId)); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('mint', function () { |
||||
describe('when the given token ID was not tracked by this contract', function () { |
||||
const tokenId = _unknownTokenId; |
||||
|
||||
describe('when the given address is not the zero address', function () { |
||||
const to = accounts[1]; |
||||
|
||||
it('mints the given token ID to the given address', async function () { |
||||
const previousBalance = await token.balanceOf(to); |
||||
|
||||
await token.mint(to, tokenId); |
||||
|
||||
const owner = await token.ownerOf(tokenId); |
||||
owner.should.be.equal(to); |
||||
|
||||
const balance = await token.balanceOf(to); |
||||
balance.should.be.bignumber.equal(previousBalance + 1); |
||||
}); |
||||
|
||||
it('adds that token to the token list of the owner', async function () { |
||||
await token.mint(to, tokenId); |
||||
|
||||
const tokens = await token.tokensOf(to); |
||||
tokens.length.should.be.equal(1); |
||||
tokens[0].should.be.bignumber.equal(tokenId); |
||||
}); |
||||
|
||||
it('emits a transfer event', async function () { |
||||
const { logs } = await token.mint(to, tokenId); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Transfer'); |
||||
logs[0].args._from.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._to.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given address is the zero address', function () { |
||||
const to = ZERO_ADDRESS; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.mint(to, tokenId)); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was already tracked by this contract', function () { |
||||
const tokenId = _firstTokenId; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.mint(accounts[1], tokenId)); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('burn', function () { |
||||
describe('when the given token ID was tracked by this contract', function () { |
||||
const tokenId = _firstTokenId; |
||||
|
||||
describe('when the msg.sender owns given token', function () { |
||||
const sender = _creator; |
||||
|
||||
it('burns the given token ID and adjusts the balance of the owner', async function () { |
||||
const previousBalance = await token.balanceOf(sender); |
||||
|
||||
await token.burn(tokenId, { from: sender }); |
||||
|
||||
await assertRevert(token.ownerOf(tokenId)); |
||||
const balance = await token.balanceOf(sender); |
||||
balance.should.be.bignumber.equal(previousBalance - 1); |
||||
}); |
||||
|
||||
it('removes that token from the token list of the owner', async function () { |
||||
await token.burn(tokenId, { from: sender }); |
||||
|
||||
const tokens = await token.tokensOf(sender); |
||||
tokens.length.should.be.equal(1); |
||||
tokens[0].should.be.bignumber.equal(_secondTokenId); |
||||
}); |
||||
|
||||
it('emits a burn event', async function () { |
||||
const { logs } = await token.burn(tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Transfer'); |
||||
logs[0].args._from.should.be.equal(sender); |
||||
logs[0].args._to.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
|
||||
describe('when there is an approval for the given token ID', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(accounts[1], tokenId, { from: sender }); |
||||
}); |
||||
|
||||
it('clears the approval', async function () { |
||||
await token.burn(tokenId, { from: sender }); |
||||
const approvedAccount = await token.approvedFor(tokenId); |
||||
approvedAccount.should.be.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
const { logs } = await token.burn(tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(2); |
||||
|
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the msg.sender does not own given token', function () { |
||||
const sender = accounts[1]; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.burn(tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was not tracked by this contract', function () { |
||||
const tokenID = _unknownTokenId; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.burn(tokenID, { from: _creator })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('transfer', function () { |
||||
describe('when the address to transfer the token to is not the zero address', function () { |
||||
const to = accounts[1]; |
||||
|
||||
describe('when the given token ID was tracked by this token', function () { |
||||
const tokenId = _firstTokenId; |
||||
|
||||
describe('when the msg.sender is the owner of the given token ID', function () { |
||||
const sender = _creator; |
||||
|
||||
it('transfers the ownership of the given token ID to the given address', async function () { |
||||
await token.transfer(to, tokenId, { from: sender }); |
||||
|
||||
const newOwner = await token.ownerOf(tokenId); |
||||
newOwner.should.be.equal(to); |
||||
}); |
||||
|
||||
it('clears the approval for the token ID', async function () { |
||||
await token.approve(accounts[2], tokenId, { from: sender }); |
||||
|
||||
await token.transfer(to, tokenId, { from: sender }); |
||||
|
||||
const approvedAccount = await token.approvedFor(tokenId); |
||||
approvedAccount.should.be.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('emits an approval and transfer events', async function () { |
||||
const { logs } = await token.transfer(to, tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(2); |
||||
|
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
|
||||
logs[1].event.should.be.eq('Transfer'); |
||||
logs[1].args._from.should.be.equal(sender); |
||||
logs[1].args._to.should.be.equal(to); |
||||
logs[1].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
|
||||
it('adjusts owners balances', async function () { |
||||
const previousBalance = await token.balanceOf(sender); |
||||
await token.transfer(to, tokenId, { from: sender }); |
||||
|
||||
const newOwnerBalance = await token.balanceOf(to); |
||||
newOwnerBalance.should.be.bignumber.equal(1); |
||||
|
||||
const previousOwnerBalance = await token.balanceOf(_creator); |
||||
previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1); |
||||
}); |
||||
|
||||
it('adds the token to the tokens list of the new owner', async function () { |
||||
await token.transfer(to, tokenId, { from: sender }); |
||||
|
||||
const tokenIDs = await token.tokensOf(to); |
||||
tokenIDs.length.should.be.equal(1); |
||||
tokenIDs[0].should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the msg.sender is not the owner of the given token ID', function () { |
||||
const sender = accounts[2]; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.transfer(to, tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was not tracked by this token', function () { |
||||
let tokenId = _unknownTokenId; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.transfer(to, tokenId, { from: _creator })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the address to transfer the token to is the zero address', function () { |
||||
const to = ZERO_ADDRESS; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.transfer(to, 0, { from: _creator })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('approve', function () { |
||||
describe('when the given token ID was already tracked by this contract', function () { |
||||
const tokenId = _firstTokenId; |
||||
|
||||
describe('when the sender owns the given token ID', function () { |
||||
const sender = _creator; |
||||
|
||||
describe('when the address that receives the approval is the 0 address', function () { |
||||
const to = ZERO_ADDRESS; |
||||
|
||||
describe('when there was no approval for the given token ID before', function () { |
||||
it('clears the approval for that token', async function () { |
||||
await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
const approvedAccount = await token.approvedFor(tokenId); |
||||
approvedAccount.should.be.equal(to); |
||||
}); |
||||
|
||||
it('does not emit an approval event', async function () { |
||||
const { logs } = await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(0); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was approved for another account', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(accounts[2], tokenId, { from: sender }); |
||||
}); |
||||
|
||||
it('clears the approval for the token ID', async function () { |
||||
await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
const approvedAccount = await token.approvedFor(tokenId); |
||||
approvedAccount.should.be.equal(to); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
const { logs } = await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the address that receives the approval is not the 0 address', function () { |
||||
describe('when the address that receives the approval is different than the owner', function () { |
||||
const to = accounts[1]; |
||||
|
||||
describe('when there was no approval for the given token ID before', function () { |
||||
it('approves the token ID to the given address', async function () { |
||||
await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
const approvedAccount = await token.approvedFor(tokenId); |
||||
approvedAccount.should.be.equal(to); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
const { logs } = await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was approved for the same account', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(to, tokenId, { from: sender }); |
||||
}); |
||||
|
||||
it('keeps the approval to the given address', async function () { |
||||
await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
const approvedAccount = await token.approvedFor(tokenId); |
||||
approvedAccount.should.be.equal(to); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
const { logs } = await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was approved for another account', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(accounts[2], tokenId, { from: sender }); |
||||
}); |
||||
|
||||
it('changes the approval to the given address', async function () { |
||||
await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
const approvedAccount = await token.approvedFor(tokenId); |
||||
approvedAccount.should.be.equal(to); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
const { logs } = await token.approve(to, tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the address that receives the approval is the owner', function () { |
||||
const to = _creator; |
||||
|
||||
describe('when there was no approval for the given token ID before', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.approve(to, tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was approved for another account', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(accounts[2], tokenId, { from: sender }); |
||||
}); |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.approve(to, tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the sender does not own the given token ID', function () { |
||||
const sender = accounts[1]; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.approve(accounts[2], tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was not tracked by the contract before', function () { |
||||
const tokenId = _unknownTokenId; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.approve(accounts[1], tokenId, { from: _creator })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('takeOwnership', function () { |
||||
describe('when the given token ID was already tracked by this contract', function () { |
||||
const tokenId = _firstTokenId; |
||||
|
||||
describe('when the sender has the approval for the token ID', function () { |
||||
const sender = accounts[1]; |
||||
|
||||
beforeEach(async function () { |
||||
await token.approve(sender, tokenId, { from: _creator }); |
||||
}); |
||||
|
||||
it('transfers the ownership of the given token ID to the given address', async function () { |
||||
await token.takeOwnership(tokenId, { from: sender }); |
||||
|
||||
const newOwner = await token.ownerOf(tokenId); |
||||
newOwner.should.be.equal(sender); |
||||
}); |
||||
|
||||
it('clears the approval for the token ID', async function () { |
||||
await token.takeOwnership(tokenId, { from: sender }); |
||||
|
||||
const approvedAccount = await token.approvedFor(tokenId); |
||||
approvedAccount.should.be.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('emits an approval and transfer events', async function () { |
||||
const { logs } = await token.takeOwnership(tokenId, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(2); |
||||
|
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(_creator); |
||||
logs[0].args._approved.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
|
||||
logs[1].event.should.be.eq('Transfer'); |
||||
logs[1].args._from.should.be.equal(_creator); |
||||
logs[1].args._to.should.be.equal(sender); |
||||
logs[1].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
|
||||
it('adjusts owners balances', async function () { |
||||
const previousBalance = await token.balanceOf(_creator); |
||||
|
||||
await token.takeOwnership(tokenId, { from: sender }); |
||||
|
||||
const newOwnerBalance = await token.balanceOf(sender); |
||||
newOwnerBalance.should.be.bignumber.equal(1); |
||||
|
||||
const previousOwnerBalance = await token.balanceOf(_creator); |
||||
previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1); |
||||
}); |
||||
|
||||
it('adds the token to the tokens list of the new owner', async function () { |
||||
await token.takeOwnership(tokenId, { from: sender }); |
||||
|
||||
const tokenIDs = await token.tokensOf(sender); |
||||
tokenIDs.length.should.be.equal(1); |
||||
tokenIDs[0].should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the sender does not have an approval for the token ID', function () { |
||||
const sender = accounts[1]; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.takeOwnership(tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the sender is already the owner of the token', function () { |
||||
const sender = _creator; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.takeOwnership(tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was not tracked by the contract before', function () { |
||||
const tokenId = _unknownTokenId; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.takeOwnership(tokenId, { from: _creator })); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,498 @@ |
||||
import assertRevert from '../../helpers/assertRevert'; |
||||
const BigNumber = web3.BigNumber; |
||||
const ERC721BasicToken = artifacts.require('ERC721BasicTokenMock.sol'); |
||||
|
||||
require('chai') |
||||
.use(require('chai-as-promised')) |
||||
.use(require('chai-bignumber')(BigNumber)) |
||||
.should(); |
||||
|
||||
contract('ERC721BasicToken', accounts => { |
||||
let token = null; |
||||
const _firstTokenId = 1; |
||||
const _secondTokenId = 2; |
||||
const _unknownTokenId = 3; |
||||
const _creator = accounts[0]; |
||||
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; |
||||
|
||||
beforeEach(async function () { |
||||
token = await ERC721BasicToken.new({ from: _creator }); |
||||
await token.mint(_creator, _firstTokenId, { from: _creator }); |
||||
await token.mint(_creator, _secondTokenId, { from: _creator }); |
||||
}); |
||||
|
||||
describe('balanceOf', function () { |
||||
describe('when the given address owns some tokens', function () { |
||||
it('returns the amount of tokens owned by the given address', async function () { |
||||
const balance = await token.balanceOf(_creator); |
||||
balance.should.be.bignumber.equal(2); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given address does not own any tokens', function () { |
||||
it('returns 0', async function () { |
||||
const balance = await token.balanceOf(accounts[1]); |
||||
balance.should.be.bignumber.equal(0); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('exists', function () { |
||||
describe('when the token exists', function () { |
||||
const tokenId = _firstTokenId; |
||||
|
||||
it('should return true', async function () { |
||||
const result = await token.exists(tokenId); |
||||
result.should.be.true; |
||||
}); |
||||
}); |
||||
|
||||
describe('when the token does not exist', function () { |
||||
const tokenId = _unknownTokenId; |
||||
|
||||
it('should return false', async function () { |
||||
const result = await token.exists(tokenId); |
||||
result.should.be.false; |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('ownerOf', function () { |
||||
describe('when the given token ID was tracked by this token', function () { |
||||
const tokenId = _firstTokenId; |
||||
|
||||
it('returns the owner of the given token ID', async function () { |
||||
const owner = await token.ownerOf(tokenId); |
||||
owner.should.be.equal(_creator); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was not tracked by this token', function () { |
||||
const tokenId = _unknownTokenId; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.ownerOf(tokenId)); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('mint', function () { |
||||
const to = accounts[1]; |
||||
const tokenId = _unknownTokenId; |
||||
let logs = null; |
||||
|
||||
describe('when successful', function () { |
||||
beforeEach(async function () { |
||||
const result = await token.mint(to, tokenId); |
||||
logs = result.logs; |
||||
}); |
||||
|
||||
it('assigns the token to the new owner', async function () { |
||||
const owner = await token.ownerOf(tokenId); |
||||
owner.should.be.equal(to); |
||||
}); |
||||
|
||||
it('increases the balance of its owner', async function () { |
||||
const balance = await token.balanceOf(to); |
||||
balance.should.be.bignumber.equal(1); |
||||
}); |
||||
|
||||
it.skip('adds that token to the token list of the owner', async function () { |
||||
await token.mint(to, tokenId); |
||||
|
||||
const tokens = await token.tokensOf(to); |
||||
tokens.length.should.be.equal(1); |
||||
tokens[0].should.be.bignumber.equal(tokenId); |
||||
}); |
||||
|
||||
it('emits a transfer event', async function () { |
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Transfer'); |
||||
logs[0].args._from.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._to.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given owner address is the zero address', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.mint(ZERO_ADDRESS, tokenId)); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was already tracked by this contract', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.mint(accounts[1], _firstTokenId)); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('burn', function () { |
||||
const tokenId = _firstTokenId; |
||||
const sender = _creator; |
||||
let logs = null; |
||||
|
||||
describe('when successful', function () { |
||||
beforeEach(async function () { |
||||
const result = await token.burn(tokenId, { from: sender }); |
||||
logs = result.logs; |
||||
}); |
||||
|
||||
it('burns the given token ID and adjusts the balance of the owner', async function () { |
||||
await assertRevert(token.ownerOf(tokenId)); |
||||
const balance = await token.balanceOf(sender); |
||||
balance.should.be.bignumber.equal(1); |
||||
}); |
||||
|
||||
it.skip('removes that token from the token list of the owner', async function () { |
||||
const tokens = await token.tokensOf(sender); |
||||
tokens.length.should.be.equal(1); |
||||
tokens[0].should.be.bignumber.equal(_secondTokenId); |
||||
}); |
||||
|
||||
it('emits a burn event', async function () { |
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Transfer'); |
||||
logs[0].args._from.should.be.equal(sender); |
||||
logs[0].args._to.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when there is a previous approval', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(accounts[1], tokenId, { from: sender }); |
||||
const result = await token.burn(tokenId, { from: sender }); |
||||
logs = result.logs; |
||||
}); |
||||
|
||||
it('clears the approval', async function () { |
||||
const approvedAccount = await token.getApproved(tokenId); |
||||
approvedAccount.should.be.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
logs.length.should.be.equal(2); |
||||
|
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
|
||||
logs[1].event.should.be.eq('Transfer'); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the msg.sender does not own given token', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.burn(tokenId, { from: accounts[1] })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID was not tracked by this contract', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.burn(_unknownTokenId, { from: _creator })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('transferFrom', function () { |
||||
const sender = _creator; |
||||
const from = _creator; |
||||
const to = accounts[1]; |
||||
const tokenId = _firstTokenId; |
||||
const approved = accounts[2]; |
||||
let logs = null; |
||||
|
||||
describe('when successful', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(approved, tokenId, { from: sender }); |
||||
const result = await token.transferFrom(from, to, tokenId, { from: sender }); |
||||
logs = result.logs; |
||||
}); |
||||
|
||||
it('transfers the ownership of the given token ID to the given address', async function () { |
||||
const newOwner = await token.ownerOf(tokenId); |
||||
newOwner.should.be.equal(to); |
||||
}); |
||||
|
||||
it('clears the approval for the token ID', async function () { |
||||
const approvedAccount = await token.getApproved(tokenId); |
||||
approvedAccount.should.be.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('emits an approval and transfer events', async function () { |
||||
logs.length.should.be.equal(2); |
||||
|
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
|
||||
logs[1].event.should.be.eq('Transfer'); |
||||
logs[1].args._from.should.be.equal(sender); |
||||
logs[1].args._to.should.be.equal(to); |
||||
logs[1].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
|
||||
it('adjusts owners balances', async function () { |
||||
const newOwnerBalance = await token.balanceOf(to); |
||||
newOwnerBalance.should.be.bignumber.equal(1); |
||||
|
||||
const previousOwnerBalance = await token.balanceOf(_creator); |
||||
previousOwnerBalance.should.be.bignumber.equal(1); |
||||
}); |
||||
|
||||
it.skip('adds the token to the tokens list of the new owner', async function () { |
||||
await token.transfer(to, tokenId, { from: sender }); |
||||
|
||||
const tokenIDs = await token.tokensOf(to); |
||||
tokenIDs.length.should.be.equal(1); |
||||
tokenIDs[0].should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the address of the previous owner is incorrect', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.transferFrom(accounts[3], to, tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the msg.sender is not the owner of the given token ID', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.transferFrom(from, to, tokenId, { from: accounts[2] })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID does not exist', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.transferFrom(from, to, _unknownTokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the address to transfer the token to is the zero address', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.transferFrom(from, to, _unknownTokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('approve', function () { |
||||
const tokenId = _firstTokenId; |
||||
const sender = _creator; |
||||
const to = accounts[1]; |
||||
|
||||
let logs = null; |
||||
|
||||
describe('when clearing approval', function () { |
||||
describe('when there was no prior approval', function () { |
||||
beforeEach(async function () { |
||||
({ logs } = await token.approve(ZERO_ADDRESS, tokenId, { from: sender })); |
||||
}); |
||||
|
||||
it('clears the approval for that token', async function () { |
||||
const approvedAccount = await token.getApproved(tokenId); |
||||
approvedAccount.should.be.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('does not emit an approval event', async function () { |
||||
logs.length.should.be.equal(0); |
||||
}); |
||||
}); |
||||
|
||||
describe('when there was a prior approval', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(to, tokenId, { from: sender }); |
||||
({ logs } = await token.approve(ZERO_ADDRESS, tokenId, { from: sender })); |
||||
}); |
||||
|
||||
it('clears the approval for that token', async function () { |
||||
const approvedAccount = await token.getApproved(tokenId); |
||||
approvedAccount.should.be.equal(ZERO_ADDRESS); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(ZERO_ADDRESS); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when approving a non-zero address', function () { |
||||
describe('when there was no prior approval', function () { |
||||
beforeEach(async function () { |
||||
({ logs } = await token.approve(to, tokenId, { from: sender })); |
||||
}); |
||||
|
||||
it('sets the approval for that token', async function () { |
||||
const approvedAccount = await token.getApproved(tokenId); |
||||
approvedAccount.should.be.equal(to); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when there was a prior approval to the same address', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(to, tokenId, { from: sender }); |
||||
({ logs } = await token.approve(to, tokenId, { from: sender })); |
||||
}); |
||||
|
||||
it('keeps the approval for that token', async function () { |
||||
const approvedAccount = await token.getApproved(tokenId); |
||||
approvedAccount.should.be.equal(to); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
|
||||
describe('when there was a prior approval to a different address', function () { |
||||
beforeEach(async function () { |
||||
await token.approve(accounts[2], tokenId, { from: sender }); |
||||
({ logs } = await token.approve(to, tokenId, { from: sender })); |
||||
}); |
||||
|
||||
it('sets the approval for that token', async function () { |
||||
const approvedAccount = await token.getApproved(tokenId); |
||||
approvedAccount.should.be.equal(to); |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('Approval'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._approved.should.be.equal(to); |
||||
logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the address that receives the approval is the owner', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.approve(sender, tokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the sender does not own the given token ID', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.approve(to, tokenId, { from: accounts[2] })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the sender is approved for the given token ID', function () { |
||||
it('reverts', async function () { |
||||
await token.approve(accounts[2], tokenId, { from: sender }); |
||||
await assertRevert(token.approve(to, tokenId, { from: accounts[2] })); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the given token ID does not exist', function () { |
||||
it('reverts', async function () { |
||||
await assertRevert(token.approve(to, _unknownTokenId, { from: sender })); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('setApprovalForAll', function () { |
||||
const sender = _creator; |
||||
|
||||
describe('when the operator willing to approve is not the owner', function () { |
||||
const operator = accounts[1]; |
||||
|
||||
describe('when there is no operator approval set by the sender', function () { |
||||
it('approves the operator', async function () { |
||||
await token.setApprovalForAll(operator, true, { from: sender }); |
||||
|
||||
const isApproved = await token.isApprovedForAll(sender, operator); |
||||
isApproved.should.be.true; |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
const { logs } = await token.setApprovalForAll(operator, true, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('ApprovalForAll'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._operator.should.be.equal(operator); |
||||
logs[0].args._approved.should.be.true; |
||||
}); |
||||
}); |
||||
|
||||
describe('when the operator was set as not approved', function () { |
||||
beforeEach(async function () { |
||||
await token.setApprovalForAll(operator, false, { from: sender }); |
||||
}); |
||||
|
||||
it('approves the operator', async function () { |
||||
await token.setApprovalForAll(operator, true, { from: sender }); |
||||
|
||||
const isApproved = await token.isApprovedForAll(sender, operator); |
||||
isApproved.should.be.true; |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
const { logs } = await token.setApprovalForAll(operator, true, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('ApprovalForAll'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._operator.should.be.equal(operator); |
||||
logs[0].args._approved.should.be.true; |
||||
}); |
||||
|
||||
it('can unset the operator approval', async function () { |
||||
await token.setApprovalForAll(operator, false, { from: sender }); |
||||
|
||||
const isApproved = await token.isApprovedForAll(sender, operator); |
||||
isApproved.should.be.false; |
||||
}); |
||||
}); |
||||
|
||||
describe('when the operator was already approved', function () { |
||||
beforeEach(async function () { |
||||
await token.setApprovalForAll(operator, true, { from: sender }); |
||||
}); |
||||
|
||||
it('keeps the approval to the given address and does not emit an approval event', async function () { |
||||
await token.setApprovalForAll(operator, true, { from: sender }); |
||||
|
||||
const isApproved = await token.isApprovedForAll(sender, operator); |
||||
isApproved.should.be.true; |
||||
}); |
||||
|
||||
it('emits an approval event', async function () { |
||||
const { logs } = await token.setApprovalForAll(operator, true, { from: sender }); |
||||
|
||||
logs.length.should.be.equal(1); |
||||
logs[0].event.should.be.eq('ApprovalForAll'); |
||||
logs[0].args._owner.should.be.equal(sender); |
||||
logs[0].args._operator.should.be.equal(operator); |
||||
logs[0].args._approved.should.be.true; |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when the operator is the owner', function () { |
||||
const operator = _creator; |
||||
|
||||
it('reverts', async function () { |
||||
await assertRevert(token.setApprovalForAll(operator, true, { from: sender })); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
Loading…
Reference in new issue