-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBallot V1.sol
47 lines (39 loc) · 1.35 KB
/
Ballot V1.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
pragma solidity ^0.4.0;
contract ballot{
struct voter{
uint weight;
bool voted;
uint256 vote;
}
struct proposal{
uint voteCount;
}
address chairperson;
mapping(address => voter) voters;
proposal[] proposals;
function ballot(uint _numProposals) public{
chairperson = msg.sender;
voters[chairperson].weight = 2;
proposals.length = _numProposals;
}
function register(address toVoter) public{
if(msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
voters[toVoter].voted = false;
}
function vote(uint toProposal) public{
voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}
function winningProposal() public constant returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}