Sol.txt 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-3.0
  2. pragma solidity >=0.7.0 <0.9.0;
  3. pragma experimental ABIEncoderV2;
  4. contract UserDemo
  5. {
  6. struct User
  7. {
  8. bytes32 Password;
  9. uint Balance;
  10. bool isExsist;
  11. uint Role;
  12. }
  13. struct Zapros
  14. {
  15. address Login;
  16. uint NowRole;
  17. uint NextRole;
  18. bool Status;
  19. }
  20. Zapros[] zapros;
  21. struct Product
  22. {
  23. string Title;
  24. uint Price;
  25. }
  26. Product[] shop;
  27. struct ZaprosShop
  28. {
  29. address Login;
  30. uint IndexProduct;
  31. uint Count;
  32. uint Type; //0 - Покупка 1 - Возврат 2 - Брак
  33. bool Status;
  34. }
  35. ZaprosShop[] zaproshop;
  36. constructor()
  37. {
  38. string memory _password = "111";
  39. user[msg.sender].Password = keccak256(abi.encodePacked(_password));
  40. user[msg.sender].isExsist = true;
  41. user[msg.sender].Role = 0;
  42. user[msg.sender].Balance = 100000;
  43. user[0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2].Password = keccak256(abi.encodePacked(_password));
  44. user[0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2].isExsist = true;
  45. user[0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2].Role = 0;
  46. user[0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2].Balance = 100000;
  47. user[0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db].Password = keccak256(abi.encodePacked("12345"));
  48. user[0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db].isExsist = true;
  49. user[0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db].Role = 2;
  50. user[0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db].Balance = 100000;
  51. user[0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB].Password = keccak256(abi.encodePacked("12345"));
  52. user[0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB].isExsist = true;
  53. user[0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB].Role = 2;
  54. user[0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB].Balance = 100000;
  55. shop.push(Product("Tomat",100));
  56. shop.push(Product("Perec",200));
  57. shop.push(Product("Vodka",300));
  58. }
  59. string[] roles = ["Admin","Seller","Buyer"];
  60. mapping(address=> User) user;
  61. address[] userAddress;
  62. //Администратор системы
  63. //Повышает обычного покупателя до роли продавец
  64. function UpBuyerToSeller(address _Adr) public
  65. {
  66. require(user[msg.sender].isExsist == true, "User not registr");
  67. require(user[msg.sender].Role == 0, "You not admin");
  68. require(user[_Adr].Role == 2, "This not buyer");
  69. user[_Adr].Role = 1;
  70. }
  71. //Понижает продавца до роли покупатель
  72. function DownSellerToBuyer(address _Adr) public
  73. {
  74. require(user[msg.sender].isExsist == true, "User not registr");
  75. require(user[msg.sender].Role == 0, "You not admin");
  76. require(user[_Adr].Role == 1, "This not seller");
  77. user[_Adr].Role = 2;
  78. }
  79. //Может переключиться к роли покупатель
  80. function toAdmintoBuyer() public
  81. {
  82. require(user[msg.sender].isExsist == true, "User not registr");
  83. require(user[msg.sender].Role == 0, "You not admin");
  84. user[msg.sender].Role = 2;
  85. }
  86. //Может ввести в систему новых администраторов
  87. function registrNewAdmin(address _adr,string memory _password,uint _balance) public
  88. {
  89. require(user[msg.sender].isExsist == true, "User not registr");
  90. require(user[msg.sender].Role == 0, "You not admin");
  91. require(user[_adr].isExsist == false, "Users already exist");
  92. //require(_role > roles.length, "Role not exsist");
  93. user[_adr].Password = keccak256(abi.encodePacked(_password));
  94. user[_adr].isExsist = true;
  95. user[_adr].Role = 0;
  96. user[_adr].Balance = _balance;
  97. userAddress.push(msg.sender);
  98. }
  99. //Меняет роль по запросу
  100. function updateZapros(uint _index) public returns (string memory _login)
  101. {
  102. require(user[msg.sender].isExsist == true, "User not registr");
  103. require(user[msg.sender].Role == 0, "You not admin");
  104. require(zapros[_index].Status == false, "Zapros done!");
  105. zapros[_index].Status = true;
  106. user[zapros[_index].Login].Role = zapros[_index].NextRole;
  107. string memory newrole = roles[user[zapros[_index].Login].Role];
  108. return newrole;
  109. }
  110. //Продавец
  111. //Может переключиться к роли покупатель
  112. function sellerToBuyer() public
  113. {
  114. require(user[msg.sender].isExsist == true, "User not registr");
  115. require(user[msg.sender].Role == 1, "You not seller");
  116. user[msg.sender].Role = 2;
  117. }
  118. //Может отправить запрос на понижение до роли покупатель
  119. function zaprosToBuyer() public
  120. {
  121. require(user[msg.sender].isExsist == true, "User not registr");
  122. require(user[msg.sender].Role == 1, "You not seller");
  123. zapros.push(Zapros(msg.sender,user[msg.sender].Role,2,false));
  124. }
  125. //Подтверждение покупки товара
  126. function updateZaprosShopBuy(uint _index) public
  127. {
  128. require(user[msg.sender].isExsist == true, "User not registr");
  129. require(user[msg.sender].Role == 1, "You not seller");
  130. require(zaproshop[_index].Type == 0, "This is not a buy!");
  131. require(zaproshop[_index].Status == false, "Zapros done!");
  132. zaproshop[_index].Status = true;
  133. uint summ = zaproshop[_index].Count * shop[zaproshop[_index].IndexProduct].Price;
  134. user[msg.sender].Balance += summ;
  135. user[zaproshop[_index].Login].Balance -= summ;
  136. }
  137. //Подтверждение возврата товара
  138. function updateZaproshShopBack(uint _index) public
  139. {
  140. require(user[msg.sender].isExsist == true, "User not registr");
  141. require(user[msg.sender].Role == 1, "You not seller");
  142. require(zaproshop[_index].Type == 1, "This is not a payment!");
  143. require(zaproshop[_index].Status == false, "Zapros done!");
  144. zaproshop[_index].Status = true;
  145. uint summ = zaproshop[_index].Count * shop[zaproshop[_index].IndexProduct].Price;
  146. user[msg.sender].Balance -= summ;
  147. user[zaproshop[_index].Login].Balance += summ;
  148. }
  149. //Покупатель
  150. //Может подать запрос на повышение до роли продавец.
  151. function zaprosToSeller() public
  152. {
  153. require(user[msg.sender].isExsist == true, "User not registr");
  154. require(user[msg.sender].Role == 2, "You not buyer");
  155. zapros.push(Zapros(msg.sender,user[msg.sender].Role,1,false));
  156. }
  157. //Может купить товар в магазине
  158. function zaprosToBuyProduct(uint _index,uint count) public
  159. {
  160. require(user[msg.sender].isExsist == true, "User not registr");
  161. require(user[msg.sender].Role == 2, "You not buyer");
  162. require(shop.length > _index, "Does not exist product");
  163. zaproshop.push(ZaprosShop(msg.sender,_index,count,0,false));
  164. }
  165. //Может вернуть товар в магазине
  166. function zaprosToBackProduct(uint _index) public
  167. {
  168. require(user[msg.sender].isExsist == true, "User not registr");
  169. require(user[msg.sender].Role == 2, "You not buyer");
  170. require(zaproshop[_index].Status == true, "Unpaid");
  171. require(zaproshop[_index].Type == 0, "this is not a payment");
  172. require(zaproshop.length > _index, "Does not exist product");
  173. zaproshop[_index].Status = false;
  174. zaproshop[_index].Type = 1;
  175. }
  176. //Прочие функции
  177. //Регистрация пользователей - Роль покупатель
  178. function RegistrNewUser(string memory _Password) public
  179. {
  180. require(user[msg.sender].isExsist == false, "Users already exist");
  181. user[msg.sender].Password = keccak256(abi.encodePacked(_Password));
  182. user[msg.sender].isExsist = true;
  183. user[msg.sender].Role = 2;
  184. user[msg.sender].Balance = 50000;
  185. }
  186. //Авторизация пользователя
  187. function AuthorizationUser(string memory _Password) public view returns(bool)
  188. {
  189. require(user[msg.sender].isExsist == true, "User not registr");
  190. require(user[msg.sender].Password == keccak256(abi.encodePacked(_Password)), "Wrong password");
  191. return user[msg.sender].isExsist;
  192. }
  193. //Просмотр всех пользователей
  194. function retrunAllUser() external view returns(address[] memory)
  195. {
  196. return userAddress;
  197. }
  198. //Просмотр всех пользователей
  199. function retrunAllZapros() external view returns(Zapros[] memory)
  200. {
  201. return zapros;
  202. }
  203. //Просмотр своего баланса
  204. function returnBalance() external view returns(uint)
  205. {
  206. return user[msg.sender].Balance;
  207. }
  208. //Просмотри личной информации
  209. function returnMyInfo() external view returns(bytes32,string memory,uint)
  210. {
  211. require(user[msg.sender].isExsist == true, "User not registr");
  212. return (user[msg.sender].Password,roles[user[msg.sender].Role],user[msg.sender].Balance);
  213. }
  214. //Просмотр товара в магазине
  215. function returnProduct() public view returns(Product[] memory)
  216. {
  217. return shop;
  218. }
  219. }