ShowTrader.sol 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. uint productsCount;
  8. constructor () public {
  9. productsCount = 0;
  10. }
  11. function sell(uint id, uint price) external onlyOwnerOf(id) {
  12. require(price != 0); // 0 means not for sale
  13. productsCount = productsCount.add(1);
  14. _sell(id, price);
  15. }
  16. function buy(uint id) external payable {
  17. require(msg.value >= showToPrice[id]);
  18. msg.sender.transfer(msg.value.sub(showToPrice[id])); // Take charge
  19. ownerOf(id).transfer(showToPrice[id]);
  20. transfer(msg.sender, id);
  21. productsCount = productsCount.sub(1);
  22. }
  23. function getShowsForSale() external view returns (uint[]) {
  24. uint[] memory result = new uint[](productsCount);
  25. uint counter = 0;
  26. for (uint id = 0; id < shows.length; id++) {
  27. if (showToPrice[id] != 0) {
  28. result[counter] = id;
  29. counter++;
  30. }
  31. }
  32. return result;
  33. }
  34. function _sell(uint id, uint price) private {
  35. showToPrice[id] = price;
  36. emit Sell(id, price);
  37. }
  38. }