-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathophirvotingdapp.sol
More file actions
119 lines (76 loc) · 2.61 KB
/
ophirvotingdapp.sol
File metadata and controls
119 lines (76 loc) · 2.61 KB
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
//SPDX-License-Identifier:MIT
pragma solidity >=0.7.0<0.9.0;
/*
Intrinsic function of voting dapp
1. It will accept proposal Name, and number for tracking
2. Allow for members to vote and exercise voting ability.
(keep track of voting, check if voters are authenticated to vote.)
3. There will be an "authenticator" to approve wallets to vote
*/
contract OphirVotingDapp {
struct Voter {
uint vote;
bool anyvotes;
uint value;
}
struct Proposal{
bytes32 name;
uint voteCount;
}
Proposal [] public proposals;
mapping(address => Voter) public voters;
address public authenticator;
constructor (bytes32 [] memory proposalNames) {
authenticator = msg.sender;
voters[authenticator].value = 1;
for (uint i=0; i <
proposalNames.length; i++) {
proposals.push(Proposal({
name:proposalNames[i],
voteCount: 0
}));
}
}
//Function to authenticate votes
function giveRightToVote(address voter) public {
require(msg.sender == authenticator,
'Only the authenticator gives access to vote');
//require that voter hasn't voted yet
require(!voters[voter].anyvotes,
'The voter has already voted');
require(voters[voter].value == 0);
voters[voter].value = 1;
}
//Function for voting
function vote (uint proposal) public{
Voter storage sender =
voters[msg.sender];
require(sender.value !=0, 'Has no right to vote');
require(!sender.anyvotes, 'Already voted');
sender.anyvotes = true;
sender.vote = proposal;
proposals[proposal].voteCount = proposals[proposal].voteCount + sender.value;
}
//functions for showing results
// 1. function that shows the winning proposal by integer
function winningProposal() public
view returns (uint
winningProposal_) {
uint winningVoteCount = 0;
for(uint i=0; i <proposals.length;
i++)
{
if(proposals[i].voteCount >
winningVoteCount) {
winningVoteCount =
proposals[i].voteCount;
winningProposal_=i;
}
}
}
//2. function that shows the winner by name
function winningName() public view returns (bytes32 winningName_) {
winningName_ =
proposals[winningProposal()].name;
}
}