-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingContract.sol
More file actions
51 lines (41 loc) · 1.46 KB
/
VotingContract.sol
File metadata and controls
51 lines (41 loc) · 1.46 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract VotingProposals{
enum Opinion{
NoVote,
Abstain,
For,
Against
}
string public proposal;
address[] public voters;
mapping(address => Opinion) public opinions;
uint256 public voteCount;
function Propose(string memory _proposal) public payable{
require(msg.value > 1, "Can't propose for free");
proposal = _proposal;
}
function vote(Opinion _opinion) public payable {
require(opinions[msg.sender] == Opinion.NoVote, "Dude already voted");
if(msg.value < 3){
revert("You must have up to 3 to vote");
}
voters.push(msg.sender);
opinions[msg.sender] = _opinion;
voteCount++;
}
function tallyVotes() public view returns (uint256 forVotes, uint256 againstVotes, uint256 abstainVotes) {
for (uint256 i = 0; i < voters.length; i++) {
if (opinions[voters[i]] == Opinion.For) {
forVotes++;
} else if (opinions[voters[i]] == Opinion.Against) {
againstVotes++;
} else if(opinions[voters[i]] == Opinion.Abstain){
abstainVotes++;
}
}
}
function getResult() public view returns(uint256){
return voteCount;
}// This function does same thing as "uint256 public voteCount;" declared above and may be omitted
}