|
@ -14,9 +14,9 @@ contract Voting { |
|
|
uint topicID; |
|
|
uint topicID; |
|
|
uint numOptions; |
|
|
uint numOptions; |
|
|
string dataHash; |
|
|
string dataHash; |
|
|
mapping (address => uint) voters; |
|
|
mapping (address => uint) votes; |
|
|
uint[] voteCounts; // First element will hold total count |
|
|
uint[] voteCounts; // First element will hold total count |
|
|
uint timestamp; |
|
|
uint timestamp; // Timestamp of creation |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
mapping (uint => Poll) polls; |
|
|
mapping (uint => Poll) polls; |
|
@ -50,11 +50,16 @@ contract Voting { |
|
|
|
|
|
|
|
|
function hasVoted(uint topicID, address voter) public view returns (bool) { |
|
|
function hasVoted(uint topicID, address voter) public view returns (bool) { |
|
|
require(isPollExistent(topicID)); |
|
|
require(isPollExistent(topicID)); |
|
|
if (polls[topicID].voters[voter] != 0) |
|
|
if (polls[topicID].votes[voter] != 0) |
|
|
return true; |
|
|
return true; |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getVote(uint topicID, address voter) public view returns (uint) { |
|
|
|
|
|
require(hasVoted(topicID, voter)); |
|
|
|
|
|
return polls[topicID].votes[voter]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function getPollInfo(uint topicID) public view returns (uint, string memory, uint, uint) { |
|
|
function getPollInfo(uint topicID) public view returns (uint, string memory, uint, uint) { |
|
|
require(isPollExistent(topicID)); |
|
|
require(isPollExistent(topicID)); |
|
|
return ( |
|
|
return ( |
|
@ -71,12 +76,16 @@ contract Voting { |
|
|
return (polls[topicID].voteCounts[option]); |
|
|
return (polls[topicID].voteCounts[option]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getTotalVotes(uint topicID) public view returns (uint) { |
|
|
|
|
|
return getVoteCount(topicID, 0); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function vote(uint topicID, uint option) public { |
|
|
function vote(uint topicID, uint option) public { |
|
|
require(isPollExistent(topicID)); |
|
|
require(isPollExistent(topicID)); |
|
|
Poll storage poll = polls[topicID]; |
|
|
Poll storage poll = polls[topicID]; |
|
|
require(option > 0 && option <= poll.numOptions); // Verify that this option exists |
|
|
require(option > 0 && option <= poll.numOptions); // Verify that this option exists |
|
|
address voter = msg.sender; |
|
|
address voter = msg.sender; |
|
|
uint currentVote = poll.voters[voter]; |
|
|
uint currentVote = poll.votes[voter]; |
|
|
if(currentVote == option) |
|
|
if(currentVote == option) |
|
|
return; |
|
|
return; |
|
|
if(currentVote == 0) // Voter hadn't voted before |
|
|
if(currentVote == 0) // Voter hadn't voted before |
|
@ -84,7 +93,7 @@ contract Voting { |
|
|
else |
|
|
else |
|
|
poll.voteCounts[currentVote]--; |
|
|
poll.voteCounts[currentVote]--; |
|
|
poll.voteCounts[option]++; |
|
|
poll.voteCounts[option]++; |
|
|
poll.voters[voter] = option; |
|
|
poll.votes[voter] = option; |
|
|
emit UserVoted(voter); |
|
|
emit UserVoted(voter); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|