pragma solidity ^0.6.4; library SafeMath { function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction overflow"); } function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } function mul(uint256 a, uint256 b) internal pure returns (uint256) { if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b > 0, errorMessage); uint256 c = a / b; return c; } function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } } contract Ecommerce { using SafeMath for uint256; enum UserType{noUser,buyer,seller} address[] admins; struct UserStruct{ string name; string location; string email; uint256 mobile; UserType userType; } struct DisputeStruct{ uint256 productId; uint256 bVote; uint256 sVote; uint256 count; mapping (uint256 => mapping (bool=>address payable )) votedAdmin; bool isDisputeCleared; } struct ProductStruct{ string name; string description; uint256 price; uint256 amount; uint256 disputePrice; address payable buyer; address payable seller; bool isBuyerOrderd; bool isShiped; bool confirmDelivery; } mapping (address => UserStruct) public user; mapping (uint256 => ProductStruct) public product; mapping (uint256 => DisputeStruct) public dispute; mapping (address => uint256[]) productList; mapping (address => mapping (uint256 => bool)) isVoted; uint256 public P_ID; uint256 public D_ID; constructor () public { admins = [msg.sender, 0xa6455803915297DBB51655af95D7A71aE277E169, 0x834Da5856df94843Fa78Ef9eda67bc7E846CFe40]; } function UserRegistraion( string calldata _name, string calldata _location, string calldata _email, uint256 _mobile,UserType _userType ) external { user[msg.sender]= UserStruct(_name,_location,_email,_mobile,_userType); } function SellProduct(string calldata _name,string calldata _description,uint256 _price) external payable { require(uint(user[msg.sender].userType) == 2,'Invalid User'); require(msg.value == _price.div(100),'insuffent Dispute value'); P_ID++; product[P_ID].name= _name; product[P_ID].description= _description; product[P_ID].disputePrice= msg.value; product[P_ID].price= _price; product[P_ID].seller= msg.sender; productList[msg.sender].push(P_ID); } function BuyProduct(uint256 _P_ID) external payable { require(uint(user[msg.sender].userType) == 1,'Invalid User'); require(_P_ID > 0,'Invalid Product Id'); require(product[_P_ID].isBuyerOrderd == false,'Product Already Placed'); require(msg.value == product[_P_ID].disputePrice.add(product[_P_ID].price),'insuffient Ethereum'); product[_P_ID].buyer = msg.sender; product[_P_ID].amount = msg.value.sub(product[_P_ID].disputePrice); product[_P_ID].disputePrice += msg.value.sub(product[_P_ID].amount); product[_P_ID].isBuyerOrderd = true; } function ProductShiping(uint256 _P_ID) external { require(uint(user[msg.sender].userType) == 2,'Invalid User'); require(product[_P_ID].seller == msg.sender,'Must be a Seller'); require(product[_P_ID].isBuyerOrderd == true,'Nobody is Orderd'); product[_P_ID].isShiped = true; } function ConfirmDelivery(uint256 _P_ID) external { require(uint(user[msg.sender].userType) == 1,'Invalid User'); require(product[_P_ID].buyer == msg.sender,'Must be a Seller'); require(product[_P_ID].isShiped == true,'Product Not Shipped'); product[_P_ID].seller.transfer(product[_P_ID].amount); product[_P_ID].buyer.transfer(product[_P_ID].disputePrice.div(2)); product[_P_ID].seller.transfer(product[_P_ID].disputePrice.div(2)); product[_P_ID].amount =0; product[_P_ID].disputePrice = 0; product[_P_ID].confirmDelivery = true; } function DisputeCreation(uint256 _P_ID) external { require(uint(user[msg.sender].userType) == 2,'Invalid User'); require(product[_P_ID].seller == msg.sender,'Must be a Seller'); require(product[_P_ID].isShiped == true,'Product not Shiped'); D_ID++; dispute[D_ID].productId = _P_ID; } function DisputeVoting(uint256 _D_ID,bool _vote) external { require(indexOf(admins,msg.sender) != uint(-1),'Not An Admin'); require(dispute[_D_ID].isDisputeCleared == false,'Dispute Already Cleared'); require(dispute[_D_ID].count <= 3,'Maximum Vote Reached'); if (_vote == true) { dispute[_D_ID].bVote++; } else { dispute[_D_ID].sVote++; } dispute[D_ID].count++; dispute[D_ID].votedAdmin[dispute[D_ID].count][_vote] = msg.sender; if (dispute[D_ID].count == 3) { if (dispute[_D_ID].bVote > dispute[_D_ID].sVote) { // buyer wins product[dispute[_D_ID].productId].buyer.transfer(product[dispute[_D_ID].productId].amount); product[dispute[_D_ID].productId].buyer.transfer(product[dispute[_D_ID].productId].disputePrice.div(2)); product[dispute[_D_ID].productId].isBuyerOrderd = false; product[dispute[_D_ID].productId].isShiped = false; } else { // seller wins product[dispute[_D_ID].productId].seller.transfer(product[dispute[_D_ID].productId].amount); product[dispute[_D_ID].productId].seller.transfer(product[dispute[_D_ID].productId].disputePrice.div(2)); product[dispute[_D_ID].productId].confirmDelivery = true; } PayAdmin(_D_ID,_vote); product[dispute[_D_ID].productId].amount = 0; product[dispute[_D_ID].productId].disputePrice = 0; dispute[_D_ID].isDisputeCleared = true; } } function PayAdmin(uint256 _D_ID,bool _vote) internal { uint256 count; if (_vote == true) { count = dispute[_D_ID].bVote; } else { count = dispute[_D_ID].sVote; } for(uint256 i = 1;i<=3;i++){ if (dispute[_D_ID].votedAdmin[i][_vote] != address(0)) { dispute[_D_ID].votedAdmin[i][_vote].transfer(product[dispute[_D_ID].productId].disputePrice.div(count)); } } } function indexOf(address[] memory self, address value) internal pure returns (uint256) { for (uint256 i = 0; i < self.length; i++) if (self[i] == value) return i; return uint256(-1); } //Read function ProductList() external view returns(uint[] memory List){ return(productList[msg.sender]); } }
0.6.4