1
0

ShowTrader.sol 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. pragma solidity ^0.4.23;
  2. import "./ShowOwnership.sol";
  3. contract ShowTrader is ShowOwnership {
  4. event Sell(uint id, uint price);
  5. event Buy(uint id);
  6. mapping (uint=>uint) public showToPrice;
  7. mapping (address=>uint) public ownerAccount;
  8. mapping (address=>bool) accountIsInit;
  9. uint productsCount;
  10. constructor () public {
  11. productsCount = 0;
  12. }
  13. function initAccount() external {
  14. if (accountIsInit[msg.sender] == false) {
  15. ownerAccount[msg.sender] = 1000; // Init money of user
  16. accountIsInit[msg.sender] = true;
  17. }
  18. }
  19. function sell(uint id, uint price) external onlyOwnerOf(id) {
  20. require(price != 0); // 0 means not for sale
  21. if (showToPrice[id] == 0)
  22. productsCount = productsCount.add(1);
  23. _sell(id, price);
  24. }
  25. function buy(uint id) external {
  26. require(showToPrice[id] != 0);
  27. require(ownerAccount[msg.sender] >= showToPrice[id]);
  28. ownerAccount[msg.sender] = ownerAccount[msg.sender].sub(showToPrice[id]);
  29. address seller = showToOwner[id];
  30. ownerAccount[seller] = ownerAccount[seller].add(showToPrice[id]);
  31. _transfer(seller, msg.sender, id);
  32. showToPrice[id] = 0;
  33. productsCount = productsCount.sub(1);
  34. }
  35. function getShowsForSale() external view returns (uint[]) {
  36. uint[] memory result = new uint[](productsCount);
  37. uint counter = 0;
  38. for (uint id = 0; id < shows.length; id++) {
  39. if (showToPrice[id] != 0) {
  40. result[counter] = id;
  41. counter++;
  42. }
  43. }
  44. return result;
  45. }
  46. function _sell(uint id, uint price) private {
  47. showToPrice[id] = price;
  48. emit Sell(id, price);
  49. }
  50. }