Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. pragma solidity ^0.4.23;
  2.  
  3. contract shop {
  4. //GC -------------------------------------------------start-----------------------------------------------------------------
  5. string public constant name="GC";
  6. string public constant symbol = "GC";
  7. uint256 public constant decimals = 18;
  8. uint256 private constant _totalSupply;
  9. uint256 public USD = 1;
  10.  
  11. mapping (address => uint256) balances;
  12.  
  13. constructor (uint256 __totalSupply) public{
  14. require(msg.sender == user);
  15. _totalSupply = __totalSupply;
  16. balances[user] = _totalSupply;
  17. }
  18.  
  19. function totalSupply() constant returns (uint256 totalSupply){
  20. return totalSupply;
  21. }
  22.  
  23. function balanceOf(address _owner) constant returns (uint256 balance){
  24. return balances[_owner];
  25. }
  26.  
  27. function transfer(address _to, uint256 _value) returns (bool success){
  28. require(balances[msg.sender] >= _value);
  29. balances[msg.sender] -= _value;
  30. balances[_to] += _value;
  31. emit transfer(msg.sender, _to ,_value);
  32. return true;
  33. }
  34.  
  35. function transferFrom(address _from, address _to, uint256 _value) returns (bool success){
  36. require(balances[msg.sender] >= _value);
  37. balances[msg.sender] -= _value;
  38. balances[_to] += _value;
  39. emit transfer(msg.sender, _to, _value);
  40. return true;
  41. }
  42.  
  43. function approve(address _spender, uint256 _value) returns (bool success){
  44. if(balances[msg.sender[]] >= _value){
  45. emit Approval(msg.sender, _spender, _value);
  46. return true;
  47. }
  48. }
  49.  
  50.  
  51. function allowance(address _owner, address _spender) constant returns (uint256 remain){
  52. return balances[_owner];
  53. }
  54.  
  55. //GC -------------------------------------------------finish-----------------------------------------------------------------
  56.  
  57. struct tovar {
  58. string name;
  59. uint256 cost;
  60. address seller;
  61. }
  62.  
  63. struct user {
  64. uint256 balance;
  65. address ip;
  66. tovar[] history_b;
  67. tovar[] history_s;
  68. }
  69.  
  70. user[] users;
  71. tovar[] tovars;
  72.  
  73. function strConcat(string _a, string _b, string _c, string _d, string _e) private returns (string){
  74. bytes memory _ba = bytes(_a);
  75. bytes memory _bb = bytes(_b);
  76. bytes memory _bc = bytes(_c);
  77. bytes memory _bd = bytes(_d);
  78. bytes memory _be = bytes(_e);
  79. string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
  80. bytes memory babcde = bytes(abcde);
  81. uint k = 0;
  82. for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
  83. for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
  84. for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
  85. for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
  86. for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
  87. return string(babcde);
  88. }
  89.  
  90. function strConcat(string _a, string _b, string _c, string _d) private returns (string) {
  91. return strConcat(_a, _b, _c, _d, "");
  92. }
  93.  
  94. function strConcat(string _a, string _b, string _c) private returns (string) {
  95. return strConcat(_a, _b, _c, "", "");
  96. }
  97.  
  98. function strConcat(string _a, string _b) private returns (string) {
  99. return strConcat(_a, _b, "", "", "");
  100. }
  101.  
  102. function check_costumer(address _ip) private returns(bool){
  103. bool flag = false;
  104. for (uint256 i = 0; i < users.length; i++) {
  105. if (users[i].ip == _ip) {
  106. flag = true;
  107. }
  108. }
  109. return flag;
  110. }
  111.  
  112. function view_balance() public view returns(uint256) {
  113. address views = msg.sender;
  114. bool flag = check_costumer(views);
  115. if (flag) {
  116. uint256 i = 0;
  117. while (users[i].ip != views) {
  118. i++;
  119. }
  120. return users[i].balance;
  121. } else {
  122. throw;
  123. }
  124. }
  125.  
  126. /*function view_hisrory_sell() public view returns(string) {
  127. address views = msg.sender;
  128. bool flag = check_costumer(views);
  129. if (flag) {
  130. uint256 i = 0;
  131. while (users[i].ip != views) {
  132. i++;
  133. }
  134. string ans;
  135. for (uint256 j = 0; j < users[i].history_s.length; j++) {
  136. ans = string(strConcat(ans, users[i].history_s[j].name, ","));
  137. }
  138. } else {
  139. throw;
  140. }
  141. }
  142.  
  143. function view_hisrory_buy() public view returns(string) {
  144. address views = msg.sender;
  145. bool flag = check_costumer(views);
  146. if (flag) {
  147. uint256 i = 0;
  148. while (users[i].ip != views) {
  149. i++;
  150. }
  151. string ans;
  152. for (uint256 j = 0; j < users[i].history_s.length; j++) {
  153. ans = string(strConcat(ans, users[i].history_b[j].name, ","));
  154. }
  155. } else {
  156. throw;
  157. }
  158. }*/
  159.  
  160. function init_user() public {
  161. bool flag = check_costumer(msg.sender);
  162. if (!flag) {
  163. users.length = users.length + 1;
  164. users[users.length - 1].ip = msg.sender;
  165. users[users.length - 1].balance = 10000;
  166. } else {
  167. throw;
  168. }
  169. }
  170.  
  171. function add_tovar(string _name, uint256 _cost) public {
  172. bool flag = check_costumer(msg.sender);
  173. if (flag) {
  174. tovars.length = tovars.length + 1;
  175. tovars[tovars.length - 1].name = _name;
  176. tovars[tovars.length - 1].cost = _cost;
  177. tovars[tovars.length - 1].seller = msg.sender;
  178. } else {
  179. throw;
  180. }
  181. }
  182.  
  183. function bye_tovar(uint256 _id) public {
  184. _id--;
  185. bool flag = check_costumer(msg.sender);
  186. if (flag) {
  187. address buy = msg.sender;
  188. uint256 m_id = 0;
  189. while (users[m_id].ip != buy) {
  190. m_id++;
  191. }
  192. if (users[m_id].balance < tovars[_id].cost) {
  193. //недостаточно денег - обработать
  194. } else {
  195. users[m_id].balance -= tovars[_id].cost;
  196. address sell = tovars[_id].seller;
  197. uint256 s_id = 0;
  198. while (users[s_id].ip != sell) {
  199. s_id++;
  200. }
  201. users[m_id].history_b.length++;
  202. users[m_id].history_b[users[m_id].history_b.length - 1] = tovars[_id];
  203. users[s_id].history_s.length++;
  204. users[s_id].history_s[users[s_id].history_s.length - 1] = tovars[_id];
  205. users[s_id].balance += tovars[_id].cost;
  206. for (uint256 i = _id + 1; i < tovars.length; i++) {
  207. tovars[i - 1] = tovars[i];
  208. }
  209. delete tovars[tovars.length - 1];
  210. tovars.length--;
  211. }
  212. } else {
  213. throw;
  214. }
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement