ERC721BasicToken.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. pragma solidity ^0.4.24;
  2. import "./ERC721Basic.sol";
  3. import "./ERC721Receiver.sol";
  4. import "../../math/SafeMath.sol";
  5. import "../../AddressUtils.sol";
  6. import "../../introspection/SupportsInterfaceWithLookup.sol";
  7. /**
  8. * @title ERC721 Non-Fungible Token Standard basic implementation
  9. * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
  10. */
  11. contract ERC721BasicToken is SupportsInterfaceWithLookup, ERC721Basic {
  12. using SafeMath for uint256;
  13. using AddressUtils for address;
  14. // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
  15. // which can be also obtained as `ERC721Receiver(0).onERC721Received.selector`
  16. bytes4 private constant ERC721_RECEIVED = 0x150b7a02;
  17. // Mapping from token ID to owner
  18. mapping (uint256 => address) internal tokenOwner;
  19. // Mapping from token ID to approved address
  20. mapping (uint256 => address) internal tokenApprovals;
  21. // Mapping from owner to number of owned token
  22. mapping (address => uint256) internal ownedTokensCount;
  23. // Mapping from owner to operator approvals
  24. mapping (address => mapping (address => bool)) internal operatorApprovals;
  25. /**
  26. * @dev Guarantees msg.sender is owner of the given token
  27. * @param _tokenId uint256 ID of the token to validate its ownership belongs to msg.sender
  28. */
  29. modifier onlyOwnerOf(uint256 _tokenId) {
  30. require(ownerOf(_tokenId) == msg.sender);
  31. _;
  32. }
  33. /**
  34. * @dev Checks msg.sender can transfer a token, by being owner, approved, or operator
  35. * @param _tokenId uint256 ID of the token to validate
  36. */
  37. modifier canTransfer(uint256 _tokenId) {
  38. require(isApprovedOrOwner(msg.sender, _tokenId));
  39. _;
  40. }
  41. constructor()
  42. public
  43. {
  44. // register the supported interfaces to conform to ERC721 via ERC165
  45. _registerInterface(InterfaceId_ERC721);
  46. _registerInterface(InterfaceId_ERC721Exists);
  47. }
  48. /**
  49. * @dev Gets the balance of the specified address
  50. * @param _owner address to query the balance of
  51. * @return uint256 representing the amount owned by the passed address
  52. */
  53. function balanceOf(address _owner) public view returns (uint256) {
  54. require(_owner != address(0));
  55. return ownedTokensCount[_owner];
  56. }
  57. /**
  58. * @dev Gets the owner of the specified token ID
  59. * @param _tokenId uint256 ID of the token to query the owner of
  60. * @return owner address currently marked as the owner of the given token ID
  61. */
  62. function ownerOf(uint256 _tokenId) public view returns (address) {
  63. address owner = tokenOwner[_tokenId];
  64. require(owner != address(0));
  65. return owner;
  66. }
  67. /**
  68. * @dev Returns whether the specified token exists
  69. * @param _tokenId uint256 ID of the token to query the existence of
  70. * @return whether the token exists
  71. */
  72. function exists(uint256 _tokenId) public view returns (bool) {
  73. address owner = tokenOwner[_tokenId];
  74. return owner != address(0);
  75. }
  76. /**
  77. * @dev Approves another address to transfer the given token ID
  78. * The zero address indicates there is no approved address.
  79. * There can only be one approved address per token at a given time.
  80. * Can only be called by the token owner or an approved operator.
  81. * @param _to address to be approved for the given token ID
  82. * @param _tokenId uint256 ID of the token to be approved
  83. */
  84. function approve(address _to, uint256 _tokenId) public {
  85. address owner = ownerOf(_tokenId);
  86. require(_to != owner);
  87. require(msg.sender == owner || isApprovedForAll(owner, msg.sender));
  88. tokenApprovals[_tokenId] = _to;
  89. emit Approval(owner, _to, _tokenId);
  90. }
  91. /**
  92. * @dev Gets the approved address for a token ID, or zero if no address set
  93. * @param _tokenId uint256 ID of the token to query the approval of
  94. * @return address currently approved for the given token ID
  95. */
  96. function getApproved(uint256 _tokenId) public view returns (address) {
  97. return tokenApprovals[_tokenId];
  98. }
  99. /**
  100. * @dev Sets or unsets the approval of a given operator
  101. * An operator is allowed to transfer all tokens of the sender on their behalf
  102. * @param _to operator address to set the approval
  103. * @param _approved representing the status of the approval to be set
  104. */
  105. function setApprovalForAll(address _to, bool _approved) public {
  106. require(_to != msg.sender);
  107. operatorApprovals[msg.sender][_to] = _approved;
  108. emit ApprovalForAll(msg.sender, _to, _approved);
  109. }
  110. /**
  111. * @dev Tells whether an operator is approved by a given owner
  112. * @param _owner owner address which you want to query the approval of
  113. * @param _operator operator address which you want to query the approval of
  114. * @return bool whether the given operator is approved by the given owner
  115. */
  116. function isApprovedForAll(
  117. address _owner,
  118. address _operator
  119. )
  120. public
  121. view
  122. returns (bool)
  123. {
  124. return operatorApprovals[_owner][_operator];
  125. }
  126. /**
  127. * @dev Transfers the ownership of a given token ID to another address
  128. * Usage of this method is discouraged, use `safeTransferFrom` whenever possible
  129. * Requires the msg sender to be the owner, approved, or operator
  130. * @param _from current owner of the token
  131. * @param _to address to receive the ownership of the given token ID
  132. * @param _tokenId uint256 ID of the token to be transferred
  133. */
  134. function transferFrom(
  135. address _from,
  136. address _to,
  137. uint256 _tokenId
  138. )
  139. public
  140. canTransfer(_tokenId)
  141. {
  142. require(_from != address(0));
  143. require(_to != address(0));
  144. clearApproval(_from, _tokenId);
  145. removeTokenFrom(_from, _tokenId);
  146. addTokenTo(_to, _tokenId);
  147. emit Transfer(_from, _to, _tokenId);
  148. }
  149. /**
  150. * @dev Safely transfers the ownership of a given token ID to another address
  151. * If the target address is a contract, it must implement `onERC721Received`,
  152. * which is called upon a safe transfer, and return the magic value
  153. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  154. * the transfer is reverted.
  155. *
  156. * Requires the msg sender to be the owner, approved, or operator
  157. * @param _from current owner of the token
  158. * @param _to address to receive the ownership of the given token ID
  159. * @param _tokenId uint256 ID of the token to be transferred
  160. */
  161. function safeTransferFrom(
  162. address _from,
  163. address _to,
  164. uint256 _tokenId
  165. )
  166. public
  167. canTransfer(_tokenId)
  168. {
  169. // solium-disable-next-line arg-overflow
  170. safeTransferFrom(_from, _to, _tokenId, "");
  171. }
  172. /**
  173. * @dev Safely transfers the ownership of a given token ID to another address
  174. * If the target address is a contract, it must implement `onERC721Received`,
  175. * which is called upon a safe transfer, and return the magic value
  176. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  177. * the transfer is reverted.
  178. * Requires the msg sender to be the owner, approved, or operator
  179. * @param _from current owner of the token
  180. * @param _to address to receive the ownership of the given token ID
  181. * @param _tokenId uint256 ID of the token to be transferred
  182. * @param _data bytes data to send along with a safe transfer check
  183. */
  184. function safeTransferFrom(
  185. address _from,
  186. address _to,
  187. uint256 _tokenId,
  188. bytes _data
  189. )
  190. public
  191. canTransfer(_tokenId)
  192. {
  193. transferFrom(_from, _to, _tokenId);
  194. // solium-disable-next-line arg-overflow
  195. require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data));
  196. }
  197. /**
  198. * @dev Returns whether the given spender can transfer a given token ID
  199. * @param _spender address of the spender to query
  200. * @param _tokenId uint256 ID of the token to be transferred
  201. * @return bool whether the msg.sender is approved for the given token ID,
  202. * is an operator of the owner, or is the owner of the token
  203. */
  204. function isApprovedOrOwner(
  205. address _spender,
  206. uint256 _tokenId
  207. )
  208. internal
  209. view
  210. returns (bool)
  211. {
  212. address owner = ownerOf(_tokenId);
  213. // Disable solium check because of
  214. // https://github.com/duaraghav8/Solium/issues/175
  215. // solium-disable-next-line operator-whitespace
  216. return (
  217. _spender == owner ||
  218. getApproved(_tokenId) == _spender ||
  219. isApprovedForAll(owner, _spender)
  220. );
  221. }
  222. /**
  223. * @dev Internal function to mint a new token
  224. * Reverts if the given token ID already exists
  225. * @param _to The address that will own the minted token
  226. * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  227. */
  228. function _mint(address _to, uint256 _tokenId) internal {
  229. require(_to != address(0));
  230. addTokenTo(_to, _tokenId);
  231. emit Transfer(address(0), _to, _tokenId);
  232. }
  233. /**
  234. * @dev Internal function to burn a specific token
  235. * Reverts if the token does not exist
  236. * @param _tokenId uint256 ID of the token being burned by the msg.sender
  237. */
  238. function _burn(address _owner, uint256 _tokenId) internal {
  239. clearApproval(_owner, _tokenId);
  240. removeTokenFrom(_owner, _tokenId);
  241. emit Transfer(_owner, address(0), _tokenId);
  242. }
  243. /**
  244. * @dev Internal function to clear current approval of a given token ID
  245. * Reverts if the given address is not indeed the owner of the token
  246. * @param _owner owner of the token
  247. * @param _tokenId uint256 ID of the token to be transferred
  248. */
  249. function clearApproval(address _owner, uint256 _tokenId) internal {
  250. require(ownerOf(_tokenId) == _owner);
  251. if (tokenApprovals[_tokenId] != address(0)) {
  252. tokenApprovals[_tokenId] = address(0);
  253. }
  254. }
  255. /**
  256. * @dev Internal function to add a token ID to the list of a given address
  257. * @param _to address representing the new owner of the given token ID
  258. * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
  259. */
  260. function addTokenTo(address _to, uint256 _tokenId) internal {
  261. require(tokenOwner[_tokenId] == address(0));
  262. tokenOwner[_tokenId] = _to;
  263. ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
  264. }
  265. /**
  266. * @dev Internal function to remove a token ID from the list of a given address
  267. * @param _from address representing the previous owner of the given token ID
  268. * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
  269. */
  270. function removeTokenFrom(address _from, uint256 _tokenId) internal {
  271. require(ownerOf(_tokenId) == _from);
  272. ownedTokensCount[_from] = ownedTokensCount[_from].sub(1);
  273. tokenOwner[_tokenId] = address(0);
  274. }
  275. /**
  276. * @dev Internal function to invoke `onERC721Received` on a target address
  277. * The call is not executed if the target address is not a contract
  278. * @param _from address representing the previous owner of the given token ID
  279. * @param _to target address that will receive the tokens
  280. * @param _tokenId uint256 ID of the token to be transferred
  281. * @param _data bytes optional data to send along with the call
  282. * @return whether the call correctly returned the expected magic value
  283. */
  284. function checkAndCallSafeTransfer(
  285. address _from,
  286. address _to,
  287. uint256 _tokenId,
  288. bytes _data
  289. )
  290. internal
  291. returns (bool)
  292. {
  293. if (!_to.isContract()) {
  294. return true;
  295. }
  296. bytes4 retval = ERC721Receiver(_to).onERC721Received(
  297. msg.sender, _from, _tokenId, _data);
  298. return (retval == ERC721_RECEIVED);
  299. }
  300. }