-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoting.sol
139 lines (103 loc) · 4.06 KB
/
Voting.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
pragma solidity ^0.4.21;
import "Main.sol";
import "CoalichainToken.sol";
import "Types.sol";
contract Voting {
using SafeMath for uint256;
enum Stages {
UNREGISTERED,
REGISTERED,
VOTED
}
struct Voter {
address voterAddress; // The address of the voter
Stages state;
address votedFor;
}
mapping (address => Voter) private voterInfo;
address private owner;
Main private mainContract;
address private mainAddress;
mapping (address => uint256) public votesReceived;
address[] public candidateList;
address private coaliWallet;
uint256 public _balloutDBId = 0;
event Voted(address voterAddress, address candidate, uint256 newVotesReceived, uint256 correlationID);
event Unvoted(address voterAddress, address candidate, uint256 newVotesReceived, uint256 correlationID);
event voterApproved(address voterAddress);
constructor(
address _owner,
address[] candidateNames,
address _coaliWallet,
uint256 balloutDBId)
public {
candidateList = candidateNames;
coaliWallet = _coaliWallet;
mainContract = Main(msg.sender); // TODO: check if the msg.sender is secure!
mainAddress = msg.sender;
owner = _owner;
_balloutDBId = balloutDBId;
voterInfo[_owner].state = Stages.VOTED;
}
function getCandidatesList() view public returns (address[], uint256[]){
uint256[] memory votes = new uint256[](candidateList.length);
for (uint index = 0; index < candidateList.length; index++) {
votes[index] = votesReceived[candidateList[index]];
}
return (candidateList, votes);
}
function getMyVoterState() public view returns (uint8) {
return (uint8(voterInfo[msg.sender].state) + 1);
}
function totalVotesFor(address candidate) view public returns (uint256) {
require(validCandidate(candidate));
return votesReceived[candidate];
}
function getBalloutDBID() view public returns (uint256){
return _balloutDBId;
}
function voteForCandidate(address candidate, uint256 correlationID) public returns (bool) {
require(validCandidate(candidate));
if(voterInfo[msg.sender].votedFor != candidate) {
voterInfo[msg.sender].votedFor = candidate;
votesReceived[candidate] = votesReceived[candidate].add(1);
emit Voted(msg.sender, candidate, votesReceived[candidate], correlationID);
mainContract.payForService(msg.sender, 1000000);
}
return true;
}
function changeVote(address newCandidate, uint256 correlationID) public returns (bool) {
require(msg.sender != owner);
address oldCandidate = voterInfo[msg.sender].votedFor;
require(validCandidate(newCandidate));
require(validCandidate(oldCandidate));
votesReceived[oldCandidate] = votesReceived[oldCandidate].sub(1);
votesReceived[newCandidate] = votesReceived[newCandidate].add(1);
voterInfo[msg.sender].votedFor = newCandidate;
emit Unvoted(msg.sender, oldCandidate, votesReceived[oldCandidate], correlationID);
emit Voted(msg.sender, newCandidate, votesReceived[newCandidate], correlationID);
if (mainContract.chargeZuz()) {
mainContract.payForService(msg.sender, uint256(Types.Service.CHANGE_VOTE));
}
return true;
}
function unvoteForCandidate(uint256 correlationID) public returns (bool) {
require(msg.sender != owner);
address candidate = voterInfo[msg.sender].votedFor;
votesReceived[candidate] = votesReceived[candidate].sub(1);
voterInfo[msg.sender].state = Stages(uint(voterInfo[msg.sender].state).sub(1));
emit Unvoted(msg.sender, candidate, votesReceived[candidate], correlationID);
if (mainContract.chargeZuz()) {
mainContract.payForService(msg.sender, uint256(Types.Service.UNVOTE));
}
return true;
}
function validCandidate(address candidate) view public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
}