1
0

ShowTrader.sol 1.8 KB

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