-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voting.sol
79 lines (65 loc) · 2.04 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
pragma solidity ^0.4.0;
contract Voting{
//individual Voter
struct Voter{
uint weight;
bool voted;
address delegate;
uint vote;
}
//indidual proposal
struct proposal{
bytes32 name;
uint voteCount;
}
address public chairman;
mapping (address => Voter) public voters;
proposal[] public proposals;
function Voting (bytes32[] proposalNames){
chairman = msg.sender;
voters[chairman].weight = 1;
for (uint i = 0; i < proposalNames.length; i++)
proposals.push(proposal({
name: proposalNames[i],
voteCount: 0
}));
}
function giveRightToVote(address voter){
if (msg.sender != chairman || voters[voter].voted)
throw;
voters[voter].weight = 1;
}
function delegate(address to){
Voter sender = voters[msg.sender];
if(sender.voted)
throw;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender)
throw;
sender.voted = true;
sender.delegate = to;
Voter delegate = voters[to];
if (delegate.voted)
proposals[delegate.vote].voteCount += sender.weight;
else
delegate.weight += sender.weight;
}
function vote(uint proposal){
Voter sender = voters[msg.sender];
if (sender.voted) throw;
sender.voted = true;
sender.vote = proposal;
proposals[proposal].voteCount += sender.weight;
}
function winningProposal() constant returns (uint winningProposal){
uint winningVoteCount = 0;
for(uint p = 0; p < proposals.length; p++){
if (proposals[p].voteCount > winningVoteCount)
{
winningVoteCount = proposals[p].voteCount;
winningProposal = p;
}
}
}
}