-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathvoting
145 lines (97 loc) · 2.79 KB
/
voting
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
140
141
142
143
144
145
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract Voting{
mapping(address=>bool) public isVoted;
address [] public peopleVoted;
string []private votingOptions;
uint256 public startDelay; //after how much time voting to be started
uint256 public duration; //for how many days voting period should remain
uint256 [] private resultArr; //stores result
address public admin;
uint256 public creatingTime; //time at which voting created
mapping (string=>uint256) public findind; //find index in result arr from name of option
enum Stages{wait,active,closed}
Stages stage; //various stages in voting
constructor() public {
admin=msg.sender;
}
event Voted(address indexed _from,string indexed _to);
event NewVoting(uint256 indexed _options,uint256 _delay,uint256 _duration);
function updateTime() internal
{
if(creatingTime+startDelay*1 minutes<=block.timestamp && block.timestamp<=creatingTime+startDelay*1 minutes+duration*1 minutes)
{
stage=Stages.active;
}
else if(block.timestamp>=creatingTime+startDelay*1 minutes+duration*1 minutes)
{
stage=Stages.closed;
}
}
//update stages
modifier onlyByAdmin(address user)
{
require(user==admin,'invalid person');
_;
}
function destroyVoting ()internal {
for(uint256 i=0;i<peopleVoted.length;i++)
{
delete(isVoted[peopleVoted[i]]);
}
delete(peopleVoted);
for(uint256 i=0;i<votingOptions.length;i++)
delete(findind[votingOptions[i]]);
delete(resultArr);
delete(votingOptions);
creatingTime=0;
}
//deletes previous voting options and record
function newVoting(string [] memory _options,uint256 _startDelay,uint256 _duration) public onlyByAdmin(msg.sender){
destroyVoting();
for(uint256 i=0;i<_options.length;i++)
{
votingOptions.push(_options[i]);
findind[_options[i]]=i+1;
resultArr.push(0);
}
startDelay=_startDelay;
duration=_duration;
stage=Stages.wait;
creatingTime=block.timestamp;
emit NewVoting(_options.length,_startDelay,_duration);
}
//admin creates new voting
modifier onlyNonVoted(address user)
{
require(isVoted[user]==false,'already voted');
_;
}
modifier onlyVotingTime()
{
updateTime();
require(stage==Stages.active,'not voting time');
_;
}
modifier correctOptions(string memory _option )
{
require(findind[_option]>0,'not a valid option');
_;
}
function giveVote(string memory _option) public correctOptions(_option) onlyNonVoted(msg.sender) onlyVotingTime{
isVoted[msg.sender]=true;
peopleVoted.push(msg.sender);
resultArr[findind[_option]-1]+=1;
emit Voted(msg.sender,_option);
}
//people can give votes...only one time ovious.
function showResult() external view returns(uint256 [] memory,string [] memory){
return (resultArr,votingOptions);
}
//shows result
function showOptions() external view returns(string [] memory)
{
return votingOptions;
}
//shows voting options/candidates
}