pragma solidity ^0.6.4; contract LBAChain { enum LandType{noLand,wet,solid} enum UserType{noUser,user,officer1,officer2,officer3} struct LandStruct{ LandType landType; int area; } struct UserStruct{ string name; uint addharId; uint phoneNo; uint wardNo; UserType userType; } struct OfficerStruct{ string name; uint addharId; uint phoneNo; uint villageNo; uint[] wardList; UserType userType; } struct LandRegisterStruct{ uint wardNo; uint survayNo; uint ownerId; uint officerId; } struct ChangeLandPropertyStruct{ uint wardNo; uint survayNo; uint ownerId; LandType currentLandType; LandType changedLandType; uint[] approverdUsers; uint approverdOfficer; uint approvedTime; bool valid; } // userId => UserStruct mapping (uint => UserStruct) public user; // userId => OfficerStruct mapping (uint => OfficerStruct) public officer; // userId = survayNoList mapping (uint => uint[] ) landIdList; // userAddress => userId mapping (address=>uint) public userId; // orderId => LandRegisterStruct mapping (uint=>LandRegisterStruct) public landAllotment; // survayNo => LandStruct mapping (uint=>LandStruct) public land; // survayNo => userId mapping (uint=>uint) public landOwner; // changeLandPropertyId => ChangeLandPropertyStruct mapping (uint=>ChangeLandPropertyStruct) public changeLand; // nested mapping for approver to check whether he is voted already mapping (address => mapping (uint => bool)) public isApproved; //userId uint public u_ID; //survayId uint public s_ID; //orderId uint public o_ID; //changeLandPropertyId uint public clp_ID; address admin; modifier onlyAdmin() { require(msg.sender == admin, "Only Admin Can Do that"); _; } modifier onlyOfficer() { require(uint(officer[userId[msg.sender]].userType) > 1, "Only Officer Can Do that"); _; } modifier mustNewUser() { require(userId[msg.sender] == 0, "Only New User Can Do that"); _; } modifier mustWardControl(address _userAddress) { uint[] memory a = officer[userId[msg.sender]].wardList; require( indexOf(a,user[userId[_userAddress]].wardNo) != uint(-1), "User and Officer must be in Same ward"); _; } modifier OnlyOwnerTransferLand(uint _s_ID,uint _area) { uint[] memory a = landIdList[userId[msg.sender]]; require( landOwner[_s_ID] == userId[msg.sender], "User must have the land "); require( land[_s_ID].area >= _area, " Given area is low "); _; } modifier OnlyOwnerChangeLandProperty(uint _s_ID) { require( landOwner[_s_ID] == userId[msg.sender], "User must have the land "); _; } modifier VotingEligibility(uint _clp_ID) { require(userId[msg.sender] != 0, "User Must be Registerd"); UserStruct memory tuser = user[userId[msg.sender]]; require(changeLand[_clp_ID].ownerId != userId[msg.sender], "Owner can't Voted"); require(isApproved[msg.sender][_clp_ID] == false, "User Already Voted"); require(tuser.wardNo == changeLand[_clp_ID].wardNo, "User Must be in Same Ward"); require(2 >= changeLand[_clp_ID].approverdUsers.length,"Only 2 votes currently allowed"); _; } modifier ApproveEligibility(uint _clp_ID) { require(userId[msg.sender] != 0, "Officer Must be Registerd"); OfficerStruct memory tofficer = officer[userId[msg.sender]]; require(changeLand[_clp_ID].ownerId != userId[msg.sender], "Owner can't Voted"); require(isApproved[msg.sender][_clp_ID] == false, "Officer Already Voted"); uint[] memory a = officer[userId[msg.sender]].wardList; require( indexOf(a,changeLand[_clp_ID].wardNo) != uint(-1), "Officer Must be in Same Ward"); require(changeLand[_clp_ID].approverdUsers.length == 2,"Need 2 User votes From Same Ward"); _; } constructor () public { admin = msg.sender; } function UserRegistraion( string calldata _name, uint _addharId, uint _wardNo, uint _phoneNo ) external mustNewUser { u_ID++; userId[msg.sender] = u_ID; user[u_ID] = UserStruct(_name,_addharId,_phoneNo,_wardNo ,UserType(1)); } function OfficerRegistraion( string calldata _name, uint _addharId, uint _phoneNo, uint _villageNo, uint[] calldata _wardlist, UserType _userType, address _userAddress ) external onlyAdmin { u_ID++; userId[_userAddress] = u_ID; officer[u_ID] = OfficerStruct(_name,_addharId,_phoneNo,_villageNo,_wardlist,_userType); } function NewLandRegistraion( address _userAddress, LandType _landType, uint _area) external onlyOfficer mustWardControl(_userAddress) { s_ID++; land[s_ID] = LandStruct(_landType,_area); o_ID++; UserStruct memory tUser = user[userId[_userAddress]]; landAllotment[o_ID] = LandRegisterStruct(tUser.wardNo,s_ID,userId[_userAddress],userId[msg.sender]); landIdList[userId[_userAddress]].push(s_ID); landOwner[s_ID] = userId[_userAddress]; } function TransferLand( uint _s_ID, uint _area, address _buyer) external OnlyOwnerTransferLand(_s_ID,_area) { land[_s_ID].area-=_area; if (land[_s_ID].area == 0) { uint index = indexOf(landIdList[userId[msg.sender]],_s_ID); delete landIdList[userId[msg.sender]][index]; delete landOwner[_s_ID]; } s_ID++; land[s_ID] = LandStruct(land[_s_ID].landType,_area); landIdList[userId[_buyer]].push(s_ID); landOwner[s_ID] = userId[_buyer]; } function ChangeLandProperty( uint _s_ID, LandType _newlandType) OnlyOwnerChangeLandProperty(_s_ID) external { uint[] memory a = new uint[](0); UserStruct memory tuser = user[userId[msg.sender]]; clp_ID++; changeLand[clp_ID] = ChangeLandPropertyStruct( tuser.wardNo, _s_ID, userId[msg.sender], land[_s_ID].landType, _newlandType,a,0,0,false ); } function Voting(uint _clp_ID) external VotingEligibility(_clp_ID) { changeLand[_clp_ID].approverdUsers.push(userId[msg.sender]); isApproved[msg.sender][_clp_ID] = true; } function Approve(uint _clp_ID) external ApproveEligibility(_clp_ID) { changeLand[_clp_ID].approverdOfficer = userId[msg.sender]; changeLand[_clp_ID].approvedTime = now; isApproved[msg.sender][_clp_ID] = true; land[changeLand[_clp_ID].survayNo] = LandStruct(changeLand[_clp_ID].changedLandType,land[changeLand[_clp_ID].survayNo].area); } function indexOf(uint[] memory self, uint value) internal pure returns (uint) { for (uint i = 0; i < self.length; i++) if (self[i] == value) return i; return uint(-1); } // Read Functions function LandList(uint _userId) external view returns(uint[] memory){ return(landIdList[_userId]); } function ApproverdUsers(uint _clp_ID) external view returns(uint [] memory){ return(changeLand[_clp_ID].approverdUsers); } function OfficerWardList(uint _officerId) external view returns(uint [] memory){ return(officer[_officerId].wardList); } }
0.6.4