1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 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;
- mapping (address=>uint) public ownerAccount;
- mapping (address=>bool) accountIsInit;
- uint productsCount;
- constructor () public {
- productsCount = 0;
- }
- function initAccount() external {
- if (accountIsInit[msg.sender] == false) {
- ownerAccount[msg.sender] = 1000; // Init money of user
- accountIsInit[msg.sender] = true;
- }
- }
- function sell(uint id, uint price) external onlyOwnerOf(id) {
- require(price != 0); // 0 means not for sale
- if (showToPrice[id] == 0)
- productsCount = productsCount.add(1);
- _sell(id, price);
- }
- function buy(uint id) external {
- require(showToPrice[id] != 0);
- require(ownerAccount[msg.sender] >= showToPrice[id]);
- ownerAccount[msg.sender] = ownerAccount[msg.sender].sub(showToPrice[id]);
- address seller = showToOwner[id];
- ownerAccount[seller] = ownerAccount[seller].add(showToPrice[id]);
- _transfer(seller, msg.sender, id);
- showToPrice[id] = 0;
- 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);
- }
- }
|