Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. pragma solidity ^0.4.23;
  2.  
  3. contract shop {
  4.  
  5. struct tovar {
  6. string name;
  7. uint256 cost;
  8. address seller;
  9. }
  10.  
  11. struct user {
  12. uint256 balance;
  13. address ip;
  14. tovar[] history_b;
  15. tovar[] history_s;
  16. }
  17.  
  18. user[] users;
  19. tovar[] tovars;
  20.  
  21. function strConcat(string _a, string _b, string _c, string _d, string _e) private returns (string){
  22. bytes memory _ba = bytes(_a);
  23. bytes memory _bb = bytes(_b);
  24. bytes memory _bc = bytes(_c);
  25. bytes memory _bd = bytes(_d);
  26. bytes memory _be = bytes(_e);
  27. string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
  28. bytes memory babcde = bytes(abcde);
  29. uint k = 0;
  30. for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
  31. for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
  32. for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
  33. for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
  34. for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
  35. return string(babcde);
  36. }
  37.  
  38. function strConcat(string _a, string _b, string _c, string _d) private returns (string) {
  39. return strConcat(_a, _b, _c, _d, "");
  40. }
  41.  
  42. function strConcat(string _a, string _b, string _c) private returns (string) {
  43. return strConcat(_a, _b, _c, "", "");
  44. }
  45.  
  46. function strConcat(string _a, string _b) private returns (string) {
  47. return strConcat(_a, _b, "", "", "");
  48. }
  49.  
  50. function check_costumer(address _ip) private returns(bool){
  51. bool flag = false;
  52. for (uint256 i = 0; i < users.length; i++) {
  53. if (users[i].ip == _ip) {
  54. flag = true;
  55. }
  56. }
  57. return flag;
  58. }
  59.  
  60. function view_balance() public view returns(uint256) {
  61. address views = msg.sender;
  62. bool flag = check_costumer(views);
  63. if (flag) {
  64. uint256 i = 0;
  65. while (users[i].ip != views) {
  66. i++;
  67. }
  68. return users[i].balance;
  69. } else {
  70. throw;
  71. }
  72. }
  73.  
  74. function view_hisrory_sell() public view returns(string) {
  75. address views = msg.sender;
  76. bool flag = check_costumer(views);
  77. if (flag) {
  78. uint256 i = 0;
  79. while (users[i].ip != views) {
  80. i++;
  81. }
  82. string ans;
  83. for (uint256 j = 0; j < users[i].history_s.length; j++) {
  84. ans = string(strConcat(ans, users[i].history_s[j].name, ","));
  85. }
  86. } else {
  87. throw;
  88. }
  89. }
  90.  
  91. function view_hisrory_buy() public view returns(string) {
  92. address views = msg.sender;
  93. bool flag = check_costumer(views);
  94. if (flag) {
  95. uint256 i = 0;
  96. while (users[i].ip != views) {
  97. i++;
  98. }
  99. string ans;
  100. for (uint256 j = 0; j < users[i].history_s.length; j++) {
  101. ans = string(strConcat(ans, users[i].history_b[j].name, ","));
  102. }
  103. } else {
  104. throw;
  105. }
  106. }
  107.  
  108. function init_user() public {
  109. bool flag = check_costumer(msg.sender);
  110. if (!flag) {
  111. users.length = users.length + 1;
  112. users[users.length - 1].ip = msg.sender;
  113. users[users.length - 1].balance = 10000;
  114. } else {
  115. throw;
  116. }
  117. }
  118.  
  119. function add_tovar(string _name, uint256 _cost) public {
  120. bool flag = check_costumer(msg.sender);
  121. if (flag) {
  122. tovars.length = tovars.length + 1;
  123. tovars[tovars.length - 1].name = _name;
  124. tovars[tovars.length - 1].cost = _cost;
  125. tovars[tovars.length - 1].seller = msg.sender;
  126. } else {
  127. throw;
  128. }
  129. }
  130.  
  131. function bye_tovar(uint256 _id) public {
  132. _id--;
  133. bool flag = check_costumer(msg.sender);
  134. if (flag) {
  135. address buy = msg.sender;
  136. uint256 m_id = 0;
  137. while (users[m_id].ip != buy) {
  138. m_id++;
  139. }
  140. if (users[m_id].balance < tovars[_id].cost) {
  141. //недостаточно денег - обработать
  142. } else {
  143. users[m_id].balance -= tovars[_id].cost;
  144. address sell = tovars[_id].seller;
  145. uint256 s_id = 0;
  146. while (users[s_id].ip != sell) {
  147. s_id++;
  148. }
  149. users[m_id].history_b.length++;
  150. users[m_id].history_b[users[m_id].history_b.length - 1] = tovars[_id];
  151. users[s_id].history_s.length++;
  152. users[s_id].history_s[users[s_id].history_s.length - 1] = tovars[_id];
  153. users[s_id].balance += tovars[_id].cost;
  154. for (uint256 i = _id + 1; i < tovars.length; i++) {
  155. tovars[i - 1] = tovars[i];
  156. }
  157. delete tovars[tovars.length - 1];
  158. tovars.length--;
  159. }
  160. } else {
  161. throw;
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement