ERC721Basic.sol 10 KB

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