-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVoting.sol
122 lines (105 loc) · 3.73 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
pragma solidity ^0.4.11;
// specifies what version of compiler this code will be compiled with
contract Voting {
/* the mapping field below is equivalent to an associative array or hash.
*/
mapping (int => int) public votesReceived;
mapping (int => int) public userId;
mapping (int => bytes32) public user;
mapping (int => int) public addressedUserId;
mapping (int => bytes32) public addressedUser;
/* Solidity doesn't let you pass in an array of strings in the constructor (yet).
We will use an array of bytes32 instead to store the list of candidates
*/
bytes32[] public candidateList;
/* This is the constructor which will be called once and only once - when you
deploy the contract to the blockchain. When we deploy the contract,
we will pass an array of candidates who will be contesting in the election
*/
function Voting(bytes32[] candidateNames) public {
candidateList = candidateNames;
userId[0] = 0;
userId[1] = 3;
userId[2] = 1;
userId[3] = 4;
userId[4] = 2;
addressedUserId[0] = 0;
addressedUserId[1] = 1;
addressedUserId[2] = 0;
addressedUserId[3] = 2;
addressedUserId[4] = 4;
addressedUser[0] = bytes32("tony");
addressedUser[1] = bytes32("hritvi");
addressedUser[2] = bytes32("tony");
addressedUser[3] = bytes32("bhartiya");
addressedUser[4] = bytes32("palak");
user[0] = bytes32("tony");
user[1] = bytes32("supra");
user[2] = bytes32("hritvi");
user[3] = bytes32("palak");
user[4] = bytes32("bhartiya");
}
// This function returns the total votes a candidate has received so far
function totalVotesFor(int id) returns (int) {
assert(int256(candidateList.length) > id);
return votesReceived[id];
}
// This function increments the vote count for the specified candidate. This
// is equivalent to casting a vote
function voteForCandidate(int id) public {
assert(int256(candidateList.length) > id);
votesReceived[id] += 1;
}
function validCandidate(bytes32 candidate) returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
function getIssueCount() returns (uint256) {
return candidateList.length;
}
function addIssue(bytes32 candidate, int userIdin, int userAddressedId) public {
candidateList.push(candidate);
userId[int(candidateList.length) - 1] = int(userIdin);
addressedUserId[int(candidateList.length) - 1] = userAddressedId;
}
function addUsers(bytes32 username, bytes32 addressedUsername) public {
addressedUser[int(candidateList.length) - 1] = addressedUsername;
user[int(candidateList.length) - 1] = username;
}
function bytes32ToString(bytes32 x) constant returns (string) {
bytes memory bytesString = new bytes(32);
uint charCount = 0;
for (uint j = 0; j < 32; j++) {
byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
if (char != 0) {
bytesString[charCount] = char;
charCount++;
}
}
bytes memory bytesStringTrimmed = new bytes(charCount);
for (j = 0; j < charCount; j++) {
bytesStringTrimmed[j] = bytesString[j];
}
return string(bytesStringTrimmed);
}
function getIssue(uint id) returns (string) {
assert((candidateList.length) > id);
return bytes32ToString(candidateList[id]);
}
function getUserId(int id) returns (int) {
return userId[id];
}
function getAddressedUserId(int id) returns (int) {
return addressedUserId[id];
}
function getAddressedUser(int id) returns (string) {
return bytes32ToString(addressedUser[id]);
}
function getUser(int id) returns (string) {
return bytes32ToString(user[id]);
}
}