Skip to content

Instantly share code, notes, and snippets.

@microchipgnu
Created June 26, 2018 18:31
Show Gist options
  • Save microchipgnu/fccf0a70f91bed44202091208e80411c to your computer and use it in GitHub Desktop.
Save microchipgnu/fccf0a70f91bed44202091208e80411c to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=false&gist=
pragma solidity ^0.4.24;
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
event OwnershipRenounced(address indexed previousOwner);
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
constructor() public {
owner = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* @dev Allows the current owner to relinquish control of the contract.
* @notice Renouncing to ownership will leave the contract without an owner.
* It will not be possible to call the functions with the `onlyOwner`
* modifier anymore.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipRenounced(owner);
owner = address(0);
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param _newOwner The address to transfer ownership to.
*/
function transferOwnership(address _newOwner) public onlyOwner {
_transferOwnership(_newOwner);
}
/**
* @dev Transfers control of the contract to a newOwner.
* @param _newOwner The address to transfer ownership to.
*/
function _transferOwnership(address _newOwner) internal {
require(_newOwner != address(0));
emit OwnershipTransferred(owner, _newOwner);
owner = _newOwner;
}
}
/**
* @title Pausable
* @dev Base contract which allows children to implement an emergency stop mechanism.
*/
contract Pausable is Ownable {
event Pause();
event Unpause();
bool public paused = false;
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*/
modifier whenNotPaused() {
require(!paused);
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*/
modifier whenPaused() {
require(paused);
_;
}
/**
* @dev called by the owner to pause, triggers stopped state
*/
function pause() onlyOwner whenNotPaused public {
paused = true;
emit Pause();
}
/**
* @dev called by the owner to unpause, returns to normal state
*/
function unpause() onlyOwner whenPaused public {
paused = false;
emit Unpause();
}
}
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return a / b;
}
/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
}
/** @title Administrable. */
contract Administrable is Ownable, Pausable{
using SafeMath for uint256;
/** @dev Number of added admin addresses to the admins mapping. */
uint256 public totalAdmins;
/** @dev Number of added admin address to the super admins mapping. */
uint256 public totalSuperAdmins;
mapping(uint256 => address) public idToAdminAddress;
mapping(uint256 => address) public idToSuperAdminAddress;
mapping(address => bool) public admins;
mapping(address => bool) public superAdmins;
mapping(address => bool) public processedAdmin;
mapping(address => bool) public processedSuperAdmin;
event AddAdmin(address indexed admin);
event RemoveAdmin(address indexed admin);
event AddSuperAdmin(address indexed admin);
event RemoveSuperAdmin(address indexed admin);
modifier onlyAdmins {
if (msg.sender != owner && !superAdmins[msg.sender] && !admins[msg.sender]) revert();
_;
}
modifier onlySuperAdmins {
if (msg.sender != owner && !superAdmins[msg.sender]) revert();
_;
}
/** @dev Gives an address super admin privileges.
@param admin The address to give admin privileges.
*/
function addSuperAdmin(address admin) public onlyOwner {
superAdmins[admin] = true;
if (!processedSuperAdmin[admin]) {
processedSuperAdmin[admin] = true;
idToSuperAdminAddress[totalSuperAdmins] = admin;
totalSuperAdmins = totalSuperAdmins.add(1);
}
emit AddSuperAdmin(admin);
}
/** @dev Removes super admin privileges from an address.
@param admin The address to remove super admin privileges from.
*/
function removeSuperAdmin(address admin) public onlyOwner {
superAdmins[admin] = false;
emit RemoveSuperAdmin(admin);
}
function addAdmin(address admin) public onlySuperAdmins {
admins[admin] = true;
if (!processedAdmin[admin]) {
processedAdmin[admin] = true;
idToAdminAddress[totalAdmins] = admin;
totalAdmins = totalAdmins.add(1);
}
emit AddAdmin(admin);
}
function removeAdmin(address admin) public onlySuperAdmins {
admins[admin] = false;
emit RemoveAdmin(admin);
}
}
contract MarketplaceManager {
}
contract Marketplace is Administrable{
string public name;
MarketplaceManager public managerAddress;
struct Store {
uint256 id;
string name;
address storeOwner;
uint256 balance;
}
struct Product {
uint256 id;
uint256 storeId;
string hash;
}
Store[] public stores;
Product[] public products
mapping(address => uint256[]) ownerAddressToStoreIds;
mapping(uint256 => address) storeIdToOwnerAddress;
mapping(uint256 => string[]) storeIdToHashes;
mapping(string => uint256) hashToProductId;
mapping(uint256 => address[]) productIdToUsers;
event NewProduct(address owner, uint256 storeId);
event NewStore();
event NewAuction();
event ChangeStoreName();
event ProductBought();
event ProductStopSale();
event ProductStartSale();
event ProductChangePrice();
modifier onlyStoreOwner(uint256 storeId){
if(msg.sender != storeIdToOwnerAddress[storeId]) revert();
_;
}
constructor(string _name, address _managerAddress) public {
require(_managerAddress != 0);
name = _name;
MarketplaceManager candidateContract = MarketplaceManager(_managerAddress);
managerAddress = candidateContract;
}
function setMarketplaceName(string _name) public onlyAdmins {
name = _name;
}
function getMarketplaceName() public returns(string){
return name;
}
function setManagerAddress(address _managerAddress) public onlyAdmins {
require(_managerAddress != 0);
MarketplaceManager candidateContract = MarketplaceManager(_managerAddress);
managerAddress = candidateContract;
}
function setStoreName(uint256 storeId, string name) public onlyStoreOwner(storeId) {
}
function getStoreName(uint256 storeId) public returns(string) {
return stores[storeId].name;
}
function createStore(address newOwner, string name) public onlyAdmins {
uint256 index = stores.push(Store(name, newOwner, 0)) - 1;
ownerAddressToStoreIds[newOwner].push(index);
storeIdToOwnerAddress[index] = newOwner;
}
function createProduct(uint256 storeId ) public onlyStoreOwner(storeId){
}
function startSale(uint256 storeId, uint256 productId) public onlyStoreOwner(storeId){
}
function stopSale(uint256 storeId, uint256 productId) public onlyStoreOwner(storeId){
}
function changeStoreOwnership(uint256 storeId, address newOwner) public onlyStoreOwner(storeId){
}
/*OUTSIDE CHAIN READERS*/
/*DO NOT CALL THESE FUNCTIONS WITHIN THE CONTRACT*/
function getListOfStoresIdByOwner(address storeOwner) external view returns(uint256[]){
uint256[] memory toReturnStoreIds = new uint256[](ownerAddressToStoreIds[storeOwner].length);
for(uint256 i = 0; i < ownerAddressToStoreIds[storeOwner].length; i++){
toReturnStoreIds[i] = i;
}
return toReturnStoreIds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment