From a1a4aaf00b9d7948082a36672dc51b3777f6e3a0 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Tue, 17 Nov 2020 20:42:51 +0200 Subject: [PATCH] Add functions --- .../concordia-contracts/contracts/Voting.sol | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/concordia-contracts/contracts/Voting.sol b/packages/concordia-contracts/contracts/Voting.sol index debf752..030a1c5 100644 --- a/packages/concordia-contracts/contracts/Voting.sol +++ b/packages/concordia-contracts/contracts/Voting.sol @@ -14,9 +14,9 @@ contract Voting { uint topicID; uint numOptions; string dataHash; - mapping (address => uint) voters; + mapping (address => uint) votes; uint[] voteCounts; // First element will hold total count - uint timestamp; + uint timestamp; // Timestamp of creation } mapping (uint => Poll) polls; @@ -50,11 +50,16 @@ contract Voting { function hasVoted(uint topicID, address voter) public view returns (bool) { require(isPollExistent(topicID)); - if (polls[topicID].voters[voter] != 0) + if (polls[topicID].votes[voter] != 0) return true; 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) { require(isPollExistent(topicID)); return ( @@ -71,12 +76,16 @@ contract Voting { return (polls[topicID].voteCounts[option]); } + function getTotalVotes(uint topicID) public view returns (uint) { + return getVoteCount(topicID, 0); + } + function vote(uint topicID, uint option) public { require(isPollExistent(topicID)); Poll storage poll = polls[topicID]; require(option > 0 && option <= poll.numOptions); // Verify that this option exists address voter = msg.sender; - uint currentVote = poll.voters[voter]; + uint currentVote = poll.votes[voter]; if(currentVote == option) return; if(currentVote == 0) // Voter hadn't voted before @@ -84,7 +93,7 @@ contract Voting { else poll.voteCounts[currentVote]--; poll.voteCounts[option]++; - poll.voters[voter] = option; + poll.votes[voter] = option; emit UserVoted(voter); } }