Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. pragma solidity ^0.5.0;
  2.  
  3. contract Marketplace {
  4.  
  5. /* Define contract owner */
  6. address public owner;
  7.  
  8. /* Marketplace name */
  9. string public marketplaceName;
  10.  
  11. /* Map marketplace administrators */
  12. mapping (address => Administrator) public administratorsMapping;
  13. /* Save administrator addresses in an array */
  14. address[] public administrators;
  15.  
  16. /* Map registered store owners */
  17. mapping (address => StoreOwner) public storeOwnersMapping;
  18. /* Save store owners addresses in an array */
  19. address[] public storeOwners;
  20.  
  21. /* storeId will be used to identify stores */
  22. uint public storeId;
  23. /* Map registered stores */
  24. mapping (uint => Store) public storesMapping;
  25. /* Save stores' storeId id in an array */
  26. uint[] public stores;
  27.  
  28. /* productId will be used to identify products */
  29. uint public productId;
  30. /* Map registered products */
  31. mapping (uint => Product) public productsMapping;
  32. /* Save products' productId in an array */
  33. uint[] public products;
  34.  
  35. /* Enums */
  36. enum StoreState { Opened, Closed }
  37.  
  38.  
  39. /* Structs */
  40. struct Administrator {
  41. address addr;
  42. string name;
  43. bool isEnabled; // Enable admin permissions
  44. }
  45.  
  46. struct StoreOwner {
  47. address addr;
  48. string name;
  49. uint balance;
  50. bool isEnabled; // Enable store owner permissions
  51. }
  52.  
  53. struct Store {
  54. uint id;
  55. string name;
  56. string description;
  57. address storeOwner;
  58. StoreState state;
  59. }
  60.  
  61. struct Product {
  62. uint id;
  63. string name;
  64. string description;
  65. uint price;
  66. uint stock;
  67. uint storefrontId;
  68. }
  69.  
  70.  
  71. /* Events */
  72. event specialUserWasRegistered (address specialUserAddress, string name);
  73. event storeWasRegistered (uint id, string name);
  74. event productWasRegistered (uint id, string name, uint price, uint stock, uint store);
  75. event productWasBought (uint id, string name, uint quantity, uint amountToPay, string store);
  76.  
  77.  
  78. /* Modifiers */
  79. modifier isOwner() {
  80. require (msg.sender == owner, "You're not the contract owner.");
  81. _;
  82. }
  83.  
  84. modifier isAdmin() {
  85. require (administratorsMapping[msg.sender].isEnabled == true, "You're not a marketplace administrator.");
  86. _;
  87. }
  88.  
  89. modifier isStoreOwnerEnabled() {
  90. require (storeOwnersMapping[msg.sender].isEnabled == true, "You are not able to create stores.");
  91. _;
  92. }
  93.  
  94. modifier checkStoreExistence(uint _id) {
  95. require (storesMapping[_id].id == _id, "The provided storefront ID doesn't exist.");
  96. _;
  97. }
  98.  
  99. modifier checkStoreOwner(uint _id) {
  100. require (storesMapping[_id].storeOwner == msg.sender, "You can only register products in a store you own.");
  101. _;
  102. }
  103.  
  104. modifier checkInsertedProductInfo(uint _price, uint _stock) {
  105. require (_price > 0, "Product price has to be greater than zero.");
  106. require (_stock >= 0, "Product stock has to be equal or greater than zero.");
  107. _;
  108. }
  109.  
  110. modifier checkProductExistenceAndQuantity(uint _id, uint _quantity) {
  111. require (productsMapping[_id].id == _id, "The provided product ID doesn't exist.");
  112. require (productsMapping[_id].stock >= _quantity, "There's not enough stock available to fullfil your order.");
  113. _;
  114. }
  115.  
  116.  
  117. constructor() public {
  118. /* Set the owner as the person who instantiated the contract */
  119. owner = msg.sender;
  120.  
  121. /* Set the Marketplace name */
  122. marketplaceName = "Ethereum Marketplace";
  123.  
  124. /* Set the inital value for storeId */
  125. storeId = 1;
  126.  
  127. /* Set the inital value for produtcId */
  128. productId = 1;
  129. }
  130.  
  131.  
  132. /* Register a marketplace administrator */
  133. function registerAdmin(address _address, string memory _name) public isOwner() {
  134. administratorsMapping[_address] = Administrator({
  135. addr: _address,
  136. name: _name,
  137. isEnabled: true
  138. });
  139.  
  140. administrators.push(_address); // Add registered admin address to admins array
  141.  
  142. emit specialUserWasRegistered(_address, _name);
  143. }
  144.  
  145. /* Retrieve marketplace administrators */
  146. function getAdmins() public view returns (address[] memory) {
  147. return administrators;
  148. }
  149.  
  150. /* Register a store owner */
  151. function registerStoreOwner(address _address, string memory _name) public isAdmin() {
  152.  
  153. storeOwnersMapping[_address] = StoreOwner({
  154. addr: _address,
  155. name: _name,
  156. balance:0, // Store owners balance always start with zero
  157. isEnabled: true
  158. });
  159.  
  160. storeOwners.push(_address); // Add registered store owner address to store owners array
  161.  
  162. emit specialUserWasRegistered(_address, _name);
  163. }
  164.  
  165. /* Retrieve store owners*/
  166. function getStoreOwners() public view returns (address[] memory) {
  167. return storeOwners;
  168. }
  169.  
  170. /* Retrieve stores' storeId */
  171. function getStoreOwnerBalance(address _addr) public view returns (uint) {
  172. return storeOwnersMapping[_addr].balance;
  173. }
  174.  
  175. /* Register a store */
  176. function registerStore(string memory _name, string memory _description) public isStoreOwnerEnabled() {
  177.  
  178. storesMapping[storeId] = Store({
  179. id: storeId,
  180. name: _name,
  181. description: _description,
  182. storeOwner: msg.sender,
  183. state: StoreState.Opened
  184. });
  185.  
  186. stores.push(storeId); // Add registered store ID to stores array
  187.  
  188. emit storeWasRegistered(storeId, _name);
  189.  
  190. storeId += 1;
  191. }
  192.  
  193. /* Retrieve stores' storeId */
  194. function getStores() public view returns (uint[] memory) {
  195. return stores;
  196. }
  197.  
  198. /* Register a product */
  199. function registerProduct(string memory _name, string memory _description, uint _price, uint _stock, uint _storefrontId) public
  200. isStoreOwnerEnabled() checkStoreExistence(_storefrontId) checkStoreOwner(_storefrontId) checkInsertedProductInfo(_price, _stock) {
  201.  
  202. productsMapping[productId] = Product({
  203. id: productId,
  204. name: _name,
  205. description: _description,
  206. price: _price,
  207. stock: _stock,
  208. storefrontId: _storefrontId
  209. });
  210.  
  211. products.push(productId); // Add registered product ID to products array
  212.  
  213. emit productWasRegistered(productId, _name, _price, _stock, _storefrontId);
  214.  
  215. productId += 1;
  216. }
  217.  
  218. /* Retrieve products' productId */
  219. function getProducts() public view returns (uint[] memory) {
  220. return products;
  221. }
  222.  
  223. /* Buy a product */
  224. function buyProduct(uint _id, uint _quantity) public payable checkProductExistenceAndQuantity(_id, _quantity) {
  225. uint amountToPay = productsMapping[_id].price * _quantity;
  226.  
  227. // Only accepts the right amount, refunds cost gas ;)
  228. require(msg.value == amountToPay, "Please send the exact amount to finish your order.");
  229.  
  230. // Decrease product stock
  231. productsMapping[_id].stock -= _quantity;
  232.  
  233. // Transfer the amount to pay to the store owner
  234. storesMapping[productsMapping[_id].storefrontId].storeOwner.Transfer(msg.value);
  235.  
  236. emit productWasBought(_id, productsMapping[_id].name, _quantity, amountToPay, storesMapping[productsMapping[_id].storefrontId].name);
  237. }
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement