-
Notifications
You must be signed in to change notification settings - Fork 0
/
RiseInProposalContract-Homework2.sol
175 lines (130 loc) · 4.79 KB
/
RiseInProposalContract-Homework2.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract ProposalContract {
uint voteLimit; //Reveals owners allowable max votes
address public owner;
address[] Voters; //Stores voters address
uint256 totalVotes;
struct Proposal {
string title;
string description; // Description of the proposal
uint256 approve; // Number of approve votes
uint256 reject; // Number of reject votes
uint256 total_vote_to_end; // When the total votes in the proposal reaches this limit, proposal ends
bool is_active; // This shows if others can vote to our contract
}
Proposal proposal;
//The rest of this code is the task.
constructor ( uint _voteLimit, string memory _title, string memory _description ) {
owner = msg.sender;
Voters.push(msg.sender); //Prevents owner to vote. (Unless they trasnfer ownership)
proposal.is_active = true;
proposal.title = _title;
proposal.description = _description;
voteLimit = _voteLimit;
}
//Makes sure voting limit reached before ending vote
modifier maxVote {
require(proposal.total_vote_to_end == voteLimit, "Maximum votes not yet reached.");
_;
}
modifier onlyOwner {
require(msg.sender == owner, "Only owner authorized to call this.");
_;
}
// Prevents double voting
modifier Voted {
uint i; // Declare `i`
for (i = 0; i < Voters.length; i++) {
if (msg.sender == Voters[i]) {
revert("Already voted pal.");
}
}
Voters.push(msg.sender); // Add voter to the list
_;
}
//Sets state of proposal to allow votes or not.
modifier votingAllowed() {
require(proposal.is_active, "Voting has ended.");
_;
}
//Vote to approve proposal
function approveVote() public Voted votingAllowed {
proposal.approve += 1;
proposal.total_vote_to_end ++;
if (proposal.total_vote_to_end == voteLimit) {
endVote(); // Automatically end vote if limit reached
}
}
//Vote to reject proposal
function rejectVote() public Voted votingAllowed{
proposal.reject += 1;
proposal.total_vote_to_end ++;
if (proposal.total_vote_to_end == voteLimit) {
endVote(); // Automatically end vote if limit reached
}
}
/*
Automatically ends vote when
proposal.total_vote_to_end == voteLimit
callable by approve and reject vote functions
*/
function endVote() private {
proposal.is_active = false;
}
//Reveals vote limit
function getVoteLimit () public view returns(uint) {
return voteLimit;
}
//Reveals number of approved votes
function getApprovedVoteCounts() public view returns(uint) {
return proposal.approve;
}
//reveals number of rejected votes
function getRejectedVoteCounts() public view returns(uint) {
return proposal.reject;
}
// Added this to get total number of voters / votes.
function getTotalVotes() external view returns (uint) {
uint256 total_vote = proposal.approve + proposal.reject;
return total_vote;
}
/* Will retrieve whether a given address has voted or not.
Owner will return true since they were pushed to Voters[].
Drwback: Owner must watch close so total don't
*/
function votedOrNot (address _address) public view returns (bool) {
for (uint i = 0; i < Voters.length; i++) {
if (Voters[i] == _address) {
return true;
}
}
return false;
}
/// true = approve votes > reject votes
function voteOutcome() external view returns(bool) {
if (proposal.approve > proposal.reject) {
return true;
} else {
return false;
}
}
/* Function to transfer ownership of contract
The Voted modifier is to check if the address has voted already.
If yes, transfer of ownership not possible.
If no, address is added to Voters[] to prevent theri voting.
*/
function transferOwnership(address _newOwner) external onlyOwner {
owner = _newOwner;
uint i; // Declare `i`
for (i = 0; i < Voters.length; i++) {
if (msg.sender == Voters[i]) {
delete Voters[i]; // Remove msg.sender directly;
}
if (owner == Voters[i]) {
revert("Voters cannot own this proposal.");
}
}
Voters.push(owner); // Add owner to the list
}
}