Skip to content

Instantly share code, notes, and snippets.

@idimitrov07
Last active April 4, 2018 14:20
Show Gist options
  • Select an option

  • Save idimitrov07/2ee0464c731eb5b7a7f3904ee7210202 to your computer and use it in GitHub Desktop.

Select an option

Save idimitrov07/2ee0464c731eb5b7a7f3904ee7210202 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.19;
contract Owned {
address owner;
function Owned() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}
contract Marketplace is Owned {
bytes32 productID = bytes32(0);
address marketAddress;
event ItemPurchase(
string name,
uint quantity,
uint totalPrice
);
event OverpayTip(
uint amount
);
function Marketplace() public {
marketAddress = owner;
}
function() payable public {
}
struct Product {
bytes32 ID;
string name;
uint quantity;
uint price;
}
mapping(bytes32 => Product) products;
bytes32[] public productsIDs;
// implement index incrementing like in a DB
function incrementProductId() private returns(bytes32) {
uint id = uint(productID);
id++;
productID = bytes32(id);
return productID;
}
//creates a new product and returns its ID
function newProduct(string name, uint price, uint quantity) onlyOwner public returns(bytes32) {
Product storage product = products[incrementProductId()];
product.name = name;
product.price = price;
product.quantity = quantity;
product.ID = productID;
productsIDs.push(product.ID);
return product.ID;
}
//
function getProducts() public view returns(bytes32[]) {
return productsIDs;
}
// check if there is enough products and funds are enough or more
function buy(bytes32 ID, uint quantity) public payable{
if(checkoutValid(ID, quantity)) {
uint checkoutSum = getPrice(ID, quantity);
products[ID].quantity -= quantity;
require(msg.value >= checkoutSum);
if(msg.value - checkoutSum > 0) {
OverpayTip(msg.value - checkoutSum);
}
ItemPurchase(products[ID].name, quantity, checkoutSum);
} else {
revert();
}
}
function checkoutValid(bytes32 ID, uint quantity) private view returns(bool) {
if (msg.sender.balance >= getPrice(ID, quantity) &&
products[ID].quantity >= quantity ) {
return true;
}
return false;
}
function update(bytes32 ID, uint newQuantity) onlyOwner public {
products[ID].quantity = newQuantity;
}
function getProduct(bytes32 ID) public view returns(string name, uint price, uint quantity) {
Product memory product = products[ID];
return (product.name, product.price, product.quantity);
}
function getPrice(bytes32 ID, uint quantity) public view returns (uint) {
return products[ID].price * quantity;
}
function getMarketBalance() onlyOwner public view returns(uint) {
return this.balance;
}
//function for the optional requirement for a withdraw function. Implement if you want.
function withdraw() onlyOwner public {
// withdraw all market balance to owner's address
owner.transfer(getMarketBalance());
//selfdestruct(owner);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment