Skip to content

Instantly share code, notes, and snippets.

@kctam
Created May 19, 2019 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kctam/ed55870a0fd8482f9b00c118cd808d65 to your computer and use it in GitHub Desktop.
Save kctam/ed55870a0fd8482f9b00c118cd808d65 to your computer and use it in GitHub Desktop.
marketplace with no token
pragma solidity ^0.5.6;
contract Emarket {
struct Item {
string description;
address seller;
address buyer;
uint price;
bool sold;
}
mapping(uint256=>Item) public items;
uint256 public itemCount;
function addItem(string memory _description, uint _price) public returns(uint) {
itemCount++;
items[itemCount].description = _description;
items[itemCount].seller = msg.sender;
items[itemCount].price = _price;
return itemCount;
}
function getItem(uint _index) public view returns(string memory, uint){
Item storage i = items[_index];
require(i.seller != address(0), "no such item"); // not exists
return(i.description, i.price);
}
function checkItemExisting(uint _index) public view returns (bool) {
Item storage i = items[_index];
return (i.seller != address(0));
}
function checkItemSold(uint _index) public view returns (bool) {
Item storage i = items[_index];
require(i.seller != address(0), "no such item"); // not exists
return i.sold;
}
function removeItem(uint _index) public {
Item storage i = items[_index];
require(i.seller != address(0), "no such item"); // not exists
require(i.seller == msg.sender, "only seller can remove item");
require(!i.sold, "item sold already");
delete items[_index];
}
function buyItem(uint _index) public {
Item storage i = items[_index];
require(i.seller != address(0), "no such item"); // not exists
require(!i.sold, "item sold already");
i.buyer = msg.sender;
i.sold = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment