// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; pragma experimental ABIEncoderV2; contract UserDemo { struct User { bytes32 Password; uint Balance; bool isExsist; uint Role; } struct Zapros { address Login; uint NowRole; uint NextRole; bool Status; } Zapros[] zapros; struct Product { string Title; uint Price; } Product[] shop; struct ZaprosShop { address Login; uint IndexProduct; uint Count; uint Type; //0 - Покупка 1 - Возврат 2 - Брак bool Status; } ZaprosShop[] zaproshop; constructor() { string memory _password = "111"; user[msg.sender].Password = keccak256(abi.encodePacked(_password)); user[msg.sender].isExsist = true; user[msg.sender].Role = 0; user[msg.sender].Balance = 100000; user[0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2].Password = keccak256(abi.encodePacked(_password)); user[0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2].isExsist = true; user[0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2].Role = 0; user[0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2].Balance = 100000; user[0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db].Password = keccak256(abi.encodePacked("12345")); user[0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db].isExsist = true; user[0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db].Role = 2; user[0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db].Balance = 100000; user[0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB].Password = keccak256(abi.encodePacked("12345")); user[0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB].isExsist = true; user[0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB].Role = 2; user[0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB].Balance = 100000; shop.push(Product("Tomat",100)); shop.push(Product("Perec",200)); shop.push(Product("Vodka",300)); } string[] roles = ["Admin","Seller","Buyer"]; mapping(address=> User) user; address[] userAddress; //Администратор системы //Повышает обычного покупателя до роли продавец function UpBuyerToSeller(address _Adr) public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 0, "You not admin"); require(user[_Adr].Role == 2, "This not buyer"); user[_Adr].Role = 1; } //Понижает продавца до роли покупатель function DownSellerToBuyer(address _Adr) public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 0, "You not admin"); require(user[_Adr].Role == 1, "This not seller"); user[_Adr].Role = 2; } //Может переключиться к роли покупатель function toAdmintoBuyer() public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 0, "You not admin"); user[msg.sender].Role = 2; } //Может ввести в систему новых администраторов function registrNewAdmin(address _adr,string memory _password,uint _balance) public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 0, "You not admin"); require(user[_adr].isExsist == false, "Users already exist"); //require(_role > roles.length, "Role not exsist"); user[_adr].Password = keccak256(abi.encodePacked(_password)); user[_adr].isExsist = true; user[_adr].Role = 0; user[_adr].Balance = _balance; userAddress.push(msg.sender); } //Меняет роль по запросу function updateZapros(uint _index) public returns (string memory _login) { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 0, "You not admin"); require(zapros[_index].Status == false, "Zapros done!"); zapros[_index].Status = true; user[zapros[_index].Login].Role = zapros[_index].NextRole; string memory newrole = roles[user[zapros[_index].Login].Role]; return newrole; } //Продавец //Может переключиться к роли покупатель function sellerToBuyer() public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 1, "You not seller"); user[msg.sender].Role = 2; } //Может отправить запрос на понижение до роли покупатель function zaprosToBuyer() public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 1, "You not seller"); zapros.push(Zapros(msg.sender,user[msg.sender].Role,2,false)); } //Подтверждение покупки товара function updateZaprosShopBuy(uint _index) public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 1, "You not seller"); require(zaproshop[_index].Type == 0, "This is not a buy!"); require(zaproshop[_index].Status == false, "Zapros done!"); zaproshop[_index].Status = true; uint summ = zaproshop[_index].Count * shop[zaproshop[_index].IndexProduct].Price; user[msg.sender].Balance += summ; user[zaproshop[_index].Login].Balance -= summ; } //Подтверждение возврата товара function updateZaproshShopBack(uint _index) public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 1, "You not seller"); require(zaproshop[_index].Type == 1, "This is not a payment!"); require(zaproshop[_index].Status == false, "Zapros done!"); zaproshop[_index].Status = true; uint summ = zaproshop[_index].Count * shop[zaproshop[_index].IndexProduct].Price; user[msg.sender].Balance -= summ; user[zaproshop[_index].Login].Balance += summ; } //Покупатель //Может подать запрос на повышение до роли продавец. function zaprosToSeller() public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 2, "You not buyer"); zapros.push(Zapros(msg.sender,user[msg.sender].Role,1,false)); } //Может купить товар в магазине function zaprosToBuyProduct(uint _index,uint count) public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 2, "You not buyer"); require(shop.length > _index, "Does not exist product"); zaproshop.push(ZaprosShop(msg.sender,_index,count,0,false)); } //Может вернуть товар в магазине function zaprosToBackProduct(uint _index) public { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Role == 2, "You not buyer"); require(zaproshop[_index].Status == true, "Unpaid"); require(zaproshop[_index].Type == 0, "this is not a payment"); require(zaproshop.length > _index, "Does not exist product"); zaproshop[_index].Status = false; zaproshop[_index].Type = 1; } //Прочие функции //Регистрация пользователей - Роль покупатель function RegistrNewUser(string memory _Password) public { require(user[msg.sender].isExsist == false, "Users already exist"); user[msg.sender].Password = keccak256(abi.encodePacked(_Password)); user[msg.sender].isExsist = true; user[msg.sender].Role = 2; user[msg.sender].Balance = 50000; } //Авторизация пользователя function AuthorizationUser(string memory _Password) public view returns(bool) { require(user[msg.sender].isExsist == true, "User not registr"); require(user[msg.sender].Password == keccak256(abi.encodePacked(_Password)), "Wrong password"); return user[msg.sender].isExsist; } //Просмотр всех пользователей function retrunAllUser() external view returns(address[] memory) { return userAddress; } //Просмотр всех пользователей function retrunAllZapros() external view returns(Zapros[] memory) { return zapros; } //Просмотр своего баланса function returnBalance() external view returns(uint) { return user[msg.sender].Balance; } //Просмотри личной информации function returnMyInfo() external view returns(bytes32,string memory,uint) { require(user[msg.sender].isExsist == true, "User not registr"); return (user[msg.sender].Password,roles[user[msg.sender].Role],user[msg.sender].Balance); } //Просмотр товара в магазине function returnProduct() public view returns(Product[] memory) { return shop; } }