ShowOwnership.sol 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. pragma solidity ^0.4.23;
  2. import "./ERC721.sol";
  3. import "./SafeMath.sol";
  4. import "./ShowHelper.sol";
  5. contract ShowOwnership is ERC721, ShowHelper {
  6. event WantToExchange(uint mine, uint yours);
  7. event RefuseToExchange(uint indexed yours);
  8. using SafeMath for uint256;
  9. mapping (uint=>address) showApprovals;
  10. mapping (uint=>uint) public showExchange; // 0: not for exchange, n: exchange for the n_th show
  11. function balanceOf(address _owner) public view returns (uint256 _balance) {
  12. return ownerShowCount[_owner];
  13. }
  14. function ownerOf(uint256 _tokenId) public view returns (address _owner) {
  15. return showToOwner[_tokenId];
  16. }
  17. function transfer(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
  18. _transfer(msg.sender, _to, _tokenId);
  19. }
  20. function approve(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
  21. showApprovals[_tokenId] = _to;
  22. emit Approval(msg.sender, _to, _tokenId);
  23. }
  24. function takeOwnership(uint256 _tokenId) public {
  25. require(msg.sender == showApprovals[_tokenId] && showExchange[_tokenId] == 0);
  26. address owner = ownerOf(_tokenId);
  27. _transfer(owner, msg.sender, _tokenId);
  28. }
  29. function wantToExchange(uint mine, uint yours) public onlyOwnerOf(mine) {
  30. if (msg.sender == showApprovals[yours] && showExchange[yours] == mine.add(1)) {
  31. showExchange[yours] = 0;
  32. transfer(ownerOf(yours), mine);
  33. takeOwnership(yours);
  34. } else {
  35. showExchange[mine] = yours.add(1);
  36. showApprovals[mine] = ownerOf(yours);
  37. emit WantToExchange(mine, yours);
  38. }
  39. }
  40. function refuseToExchange(uint yours) public {
  41. require(msg.sender == showApprovals[yours]);
  42. require(showExchange[yours] != 0);
  43. require(msg.sender == showToOwner[showExchange[yours].sub(1)]);
  44. showApprovals[yours] = address(0);
  45. showExchange[yours] = 0;
  46. }
  47. function _transfer(address _from, address _to, uint _tokenId) internal {
  48. ownerShowCount[_from] = ownerShowCount[_from].sub(1);
  49. ownerShowCount[_to] = ownerShowCount[_to].add(1);
  50. showToOwner[_tokenId] = _to;
  51. emit Transfer(_from, _to, _tokenId);
  52. }
  53. }