12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- pragma solidity ^0.4.23;
- import "./ShowOwnership.sol";
- contract ShowTrader is ShowOwnership {
- event Sell(uint id, uint price);
- event Buy(uint id);
- mapping (uint=>uint) public showToPrice;
- uint productsCount;
- constructor () public {
- productsCount = 0;
- }
- function sell(uint id, uint price) external onlyOwnerOf(id) {
- require(price != 0); // 0 means not for sale
- productsCount = productsCount.add(1);
- _sell(id, price);
- }
- function buy(uint id) external payable {
- require(msg.value >= showToPrice[id]);
- msg.sender.transfer(msg.value.sub(showToPrice[id])); // Take charge
- ownerOf(id).transfer(showToPrice[id]);
- transfer(msg.sender, id);
- productsCount = productsCount.sub(1);
- }
- function getShowsForSale() external view returns (uint[]) {
- uint[] memory result = new uint[](productsCount);
- uint counter = 0;
- for (uint id = 0; id < shows.length; id++) {
- if (showToPrice[id] != 0) {
- result[counter] = id;
- counter++;
- }
- }
- return result;
- }
- function _sell(uint id, uint price) private {
- showToPrice[id] = price;
- emit Sell(id, price);
- }
- }
|