Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.23;
- contract shop {
- //GC -------------------------------------------------start-----------------------------------------------------------------
- string public constant name="GC";
- string public constant symbol = "GC";
- uint256 public constant decimals = 18;
- uint256 private constant _totalSupply;
- uint256 public USD = 1;
- mapping (address => uint256) balances;
- constructor (uint256 __totalSupply) public{
- require(msg.sender == user);
- _totalSupply = __totalSupply;
- balances[user] = _totalSupply;
- }
- function totalSupply() constant returns (uint256 totalSupply){
- return totalSupply;
- }
- function balanceOf(address _owner) constant returns (uint256 balance){
- return balances[_owner];
- }
- function transfer(address _to, uint256 _value) returns (bool success){
- require(balances[msg.sender] >= _value);
- balances[msg.sender] -= _value;
- balances[_to] += _value;
- emit transfer(msg.sender, _to ,_value);
- return true;
- }
- function transferFrom(address _from, address _to, uint256 _value) returns (bool success){
- require(balances[msg.sender] >= _value);
- balances[msg.sender] -= _value;
- balances[_to] += _value;
- emit transfer(msg.sender, _to, _value);
- return true;
- }
- function approve(address _spender, uint256 _value) returns (bool success){
- if(balances[msg.sender[]] >= _value){
- emit Approval(msg.sender, _spender, _value);
- return true;
- }
- }
- function allowance(address _owner, address _spender) constant returns (uint256 remain){
- return balances[_owner];
- }
- //GC -------------------------------------------------finish-----------------------------------------------------------------
- struct tovar {
- string name;
- uint256 cost;
- address seller;
- }
- struct user {
- uint256 balance;
- address ip;
- tovar[] history_b;
- tovar[] history_s;
- }
- user[] users;
- tovar[] tovars;
- function strConcat(string _a, string _b, string _c, string _d, string _e) private returns (string){
- bytes memory _ba = bytes(_a);
- bytes memory _bb = bytes(_b);
- bytes memory _bc = bytes(_c);
- bytes memory _bd = bytes(_d);
- bytes memory _be = bytes(_e);
- string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
- bytes memory babcde = bytes(abcde);
- uint k = 0;
- for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
- for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
- for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
- for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
- for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
- return string(babcde);
- }
- function strConcat(string _a, string _b, string _c, string _d) private returns (string) {
- return strConcat(_a, _b, _c, _d, "");
- }
- function strConcat(string _a, string _b, string _c) private returns (string) {
- return strConcat(_a, _b, _c, "", "");
- }
- function strConcat(string _a, string _b) private returns (string) {
- return strConcat(_a, _b, "", "", "");
- }
- function check_costumer(address _ip) private returns(bool){
- bool flag = false;
- for (uint256 i = 0; i < users.length; i++) {
- if (users[i].ip == _ip) {
- flag = true;
- }
- }
- return flag;
- }
- function view_balance() public view returns(uint256) {
- address views = msg.sender;
- bool flag = check_costumer(views);
- if (flag) {
- uint256 i = 0;
- while (users[i].ip != views) {
- i++;
- }
- return users[i].balance;
- } else {
- throw;
- }
- }
- /*function view_hisrory_sell() public view returns(string) {
- address views = msg.sender;
- bool flag = check_costumer(views);
- if (flag) {
- uint256 i = 0;
- while (users[i].ip != views) {
- i++;
- }
- string ans;
- for (uint256 j = 0; j < users[i].history_s.length; j++) {
- ans = string(strConcat(ans, users[i].history_s[j].name, ","));
- }
- } else {
- throw;
- }
- }
- function view_hisrory_buy() public view returns(string) {
- address views = msg.sender;
- bool flag = check_costumer(views);
- if (flag) {
- uint256 i = 0;
- while (users[i].ip != views) {
- i++;
- }
- string ans;
- for (uint256 j = 0; j < users[i].history_s.length; j++) {
- ans = string(strConcat(ans, users[i].history_b[j].name, ","));
- }
- } else {
- throw;
- }
- }*/
- function init_user() public {
- bool flag = check_costumer(msg.sender);
- if (!flag) {
- users.length = users.length + 1;
- users[users.length - 1].ip = msg.sender;
- users[users.length - 1].balance = 10000;
- } else {
- throw;
- }
- }
- function add_tovar(string _name, uint256 _cost) public {
- bool flag = check_costumer(msg.sender);
- if (flag) {
- tovars.length = tovars.length + 1;
- tovars[tovars.length - 1].name = _name;
- tovars[tovars.length - 1].cost = _cost;
- tovars[tovars.length - 1].seller = msg.sender;
- } else {
- throw;
- }
- }
- function bye_tovar(uint256 _id) public {
- _id--;
- bool flag = check_costumer(msg.sender);
- if (flag) {
- address buy = msg.sender;
- uint256 m_id = 0;
- while (users[m_id].ip != buy) {
- m_id++;
- }
- if (users[m_id].balance < tovars[_id].cost) {
- //недостаточно денег - обработать
- } else {
- users[m_id].balance -= tovars[_id].cost;
- address sell = tovars[_id].seller;
- uint256 s_id = 0;
- while (users[s_id].ip != sell) {
- s_id++;
- }
- users[m_id].history_b.length++;
- users[m_id].history_b[users[m_id].history_b.length - 1] = tovars[_id];
- users[s_id].history_s.length++;
- users[s_id].history_s[users[s_id].history_s.length - 1] = tovars[_id];
- users[s_id].balance += tovars[_id].cost;
- for (uint256 i = _id + 1; i < tovars.length; i++) {
- tovars[i - 1] = tovars[i];
- }
- delete tovars[tovars.length - 1];
- tovars.length--;
- }
- } else {
- throw;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement