|
| 1 | +pragma solidity ^0.5.0; |
| 2 | + |
| 3 | +import "mchplus-contracts/libraries/Uint256.sol"; |
| 4 | +import "mchplus-contracts/libraries/Address.sol"; |
| 5 | + |
| 6 | +import "mchplus-contracts/interfaces/IERC721.sol"; |
| 7 | +import "mchplus-contracts/interfaces/IERC721TokenReceiver.sol"; |
| 8 | +import "mchplus-contracts/erc/ERC165.sol"; |
| 9 | + |
| 10 | +contract ERC721 is IERC721, ERC165 { |
| 11 | + using Uint256 for uint256; |
| 12 | + using Address for address; |
| 13 | + |
| 14 | + bytes4 private constant _ERC721_RECEIVED = 0x150b7a02; |
| 15 | + bytes4 private constant _InterfaceId_ERC721 = 0x80ac58cd; |
| 16 | + |
| 17 | + mapping (uint256 => address) private _tokenOwner; |
| 18 | + mapping (address => uint256) private _balance; |
| 19 | + mapping (uint256 => address) private _tokenApproved; |
| 20 | + mapping (address => mapping (address => bool)) private _operatorApprovals; |
| 21 | + |
| 22 | + constructor () public { |
| 23 | + _registerInterface(_InterfaceId_ERC721); |
| 24 | + } |
| 25 | + |
| 26 | + function balanceOf(address _owner) public view returns (uint256) { |
| 27 | + return _balance[_owner]; |
| 28 | + } |
| 29 | + |
| 30 | + function ownerOf(uint256 _tokenId) public view returns (address) { |
| 31 | + require(_exist(_tokenId), |
| 32 | + "`_tokenId` is not a valid NFT."); |
| 33 | + return _tokenOwner[_tokenId]; |
| 34 | + } |
| 35 | + |
| 36 | + function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes memory _data) public payable { |
| 37 | + require(_data.length == 0, "data is not implemented"); |
| 38 | + safeTransferFrom(_from, _to, _tokenId); |
| 39 | + } |
| 40 | + |
| 41 | + function safeTransferFrom(address _from, address _to, uint256 _tokenId) public payable { |
| 42 | + require(_checkOnERC721Received(_from, _to, _tokenId, ""), |
| 43 | + "`_to` is a smart contract and onERC721Received is invalid"); |
| 44 | + transferFrom(_from, _to, _tokenId); |
| 45 | + } |
| 46 | + |
| 47 | + function transferFrom(address _from, address _to, uint256 _tokenId) public payable { |
| 48 | + require(_transferable(_from, _tokenId), |
| 49 | + "Unless `msg.sender` is the current owner, an authorized operator, or the approved address for this NFT."); |
| 50 | + require(ownerOf(_tokenId) == _from, |
| 51 | + "`_from` is not the current owner."); |
| 52 | + require(_to != address(0), |
| 53 | + "`_to` is the zero address."); |
| 54 | + require(_exist(_tokenId), |
| 55 | + "`_tokenId` is not a valid NFT."); |
| 56 | + _transfer(_from, _to, _tokenId); |
| 57 | + } |
| 58 | + |
| 59 | + function approve(address _approved, uint256 _tokenId) public payable { |
| 60 | + address owner = ownerOf(_tokenId); |
| 61 | + require(msg.sender == owner || isApprovedForAll(owner, msg.sender), |
| 62 | + "Unless `msg.sender` is the current NFT owner, or an authorized operator of the current owner."); |
| 63 | + |
| 64 | + _tokenApproved[_tokenId] = _approved; |
| 65 | + emit Approval(msg.sender, _approved, _tokenId); |
| 66 | + } |
| 67 | + |
| 68 | + function setApprovalForAll(address _operator, bool _approved) public { |
| 69 | + _setApprovalForAll(msg.sender, _operator, _approved); |
| 70 | + } |
| 71 | + |
| 72 | + function _setApprovalForAll(address _owner, address _operator, bool _approved) internal { |
| 73 | + _operatorApprovals[_owner][_operator] = _approved; |
| 74 | + emit ApprovalForAll(_owner, _operator, _approved); |
| 75 | + } |
| 76 | + |
| 77 | + function getApproved(uint256 _tokenId) public view returns (address) { |
| 78 | + require(_exist(_tokenId), |
| 79 | + "`_tokenId` is not a valid NFT."); |
| 80 | + return _tokenApproved[_tokenId]; |
| 81 | + } |
| 82 | + |
| 83 | + function isApprovedForAll(address _owner, address _operator) public view returns (bool) { |
| 84 | + return _operatorApprovals[_owner][_operator]; |
| 85 | + } |
| 86 | + |
| 87 | + function _transferable(address _spender, uint256 _tokenId) internal view returns (bool){ |
| 88 | + address owner = ownerOf(_tokenId); |
| 89 | + return (_spender == owner || getApproved(_tokenId) == _spender || isApprovedForAll(owner, _spender)); |
| 90 | + } |
| 91 | + |
| 92 | + function _transfer(address _from, address _to, uint256 _tokenId) internal { |
| 93 | + _clearApproval(_tokenId); |
| 94 | + _tokenOwner[_tokenId] = _to; |
| 95 | + _balance[_from] = _balance[_from].sub(1); |
| 96 | + _balance[_to] = _balance[_to].add(1); |
| 97 | + emit Transfer(_from, _to, _tokenId); |
| 98 | + } |
| 99 | + |
| 100 | + function _mint(address _to, uint256 _tokenId) internal { |
| 101 | + require(!_exist(_tokenId), "mint token already exists"); |
| 102 | + _tokenOwner[_tokenId] = _to; |
| 103 | + _balance[_to] = _balance[_to].add(1); |
| 104 | + emit Transfer(address(0), _to, _tokenId); |
| 105 | + } |
| 106 | + |
| 107 | + function _burn(uint256 _tokenId) internal { |
| 108 | + require(_exist(_tokenId), "burn token does not already exists"); |
| 109 | + address owner = ownerOf(_tokenId); |
| 110 | + _clearApproval(_tokenId); |
| 111 | + _tokenOwner[_tokenId] = address(0); |
| 112 | + _balance[owner] = _balance[owner].sub(1); |
| 113 | + emit Transfer(owner, address(0), _tokenId); |
| 114 | + } |
| 115 | + |
| 116 | + function _exist(uint256 _tokenId) internal view returns (bool) { |
| 117 | + address owner = _tokenOwner[_tokenId]; |
| 118 | + return owner != address(0); |
| 119 | + } |
| 120 | + |
| 121 | + function _checkOnERC721Received(address _from, address _to, uint256 _tokenId, bytes memory _data) internal returns (bool) { |
| 122 | + if (!_to.isContract()) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + bytes4 retval = IERC721TokenReceiver(_to).onERC721Received(msg.sender, _from, _tokenId, _data); |
| 126 | + return (retval == _ERC721_RECEIVED); |
| 127 | + } |
| 128 | + |
| 129 | + function _clearApproval(uint256 tokenId) internal { |
| 130 | + if (_tokenApproved[tokenId] != address(0)) { |
| 131 | + _tokenApproved[tokenId] = address(0); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments