pragma solidity ^0.5.0; contract DecisionMaking { struct opinion{ address decisionMakerAddress; uint choice; } struct decisionMaker{ string name; bool voted; } uint private opinionResult = 0; uint public finalResult = 0; uint public totalDMs = 0; uint public totalVotes = 0; address public proposerOfficialAddress; string public proposerOfficialName; string public proposal; mapping(uint => opinion) private opinions; mapping(address => decisionMaker) public decisionMakerRegister; enum State { Created, Voting, Ended } State public state; constructor( string memory _proposerOfficialName, string memory _proposal) public { proposerOfficialAddress = msg.sender; proposerOfficialName = _proposerOfficialName; proposal = _proposal; state = State.Created; } modifier condition(bool _condition) { require(_condition); _; } modifier onlyOfficial() { require(msg.sender ==proposerOfficialAddress); _; } modifier inState(State _state) { require(state == _state); _; } event decisionMakerAdded(address decisionMaker); event voteStarted(); event voteEnded(uint finalResult); event voteDone(address decisionMaker); //add decision maker function addDecisionMaker(address _dmAddress, string memory _dmName) public inState(State.Created) onlyOfficial { decisionMaker memory v; v.name = _dmName; v.voted = false; decisionMakerRegister[_dmAddress] = v; totalDMs++; emit decisionMakerAdded(_dmAddress); } //declare decision making starts now function startVote() public inState(State.Created) onlyOfficial { state = State.Voting; emit voteStarted(); } //DMs vote by indicating their choice (on a scale of 0-99) function doVote(uint _choice) public inState(State.Voting) returns (bool voted) { bool found = false; if (bytes(decisionMakerRegister[msg.sender].name).length != 0 && !decisionMakerRegister[msg.sender].voted){ decisionMakerRegister[msg.sender].voted = true; opinion memory v; v.decisionMakerAddress = msg.sender; v.choice = _choice; opinionResult+=_choice; opinions[totalVotes] = v; totalVotes++; found = true; } emit voteDone(msg.sender); return found; } //end decision making function endVote() public inState(State.Voting) onlyOfficial { state = State.Ended; finalResult = opinionResult/totalVotes; emit voteEnded(finalResult); } }
0.4.18