-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voting.sol
62 lines (42 loc) · 1.67 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Voting {
event RegesteringCandidate(uint256 candidate_id, string name, uint256 totalVotes);
event Voted(uint256 id);
struct Candidate {
string name;
uint256 id;
uint256 voteCount;
string image;
}
mapping(address => bool) public voters;
mapping(uint => Candidate) public candidates;
uint256 public candidatesCount;
modifier alreadyVoted(){
require(voters[msg.sender] == false, 'The vote has already been casted');
_;
}
function addCandidate(string memory _name, string memory _image) public {
require(bytes(_name).length > 0, "Empty Name");
candidates[candidatesCount] = Candidate(_name, candidatesCount, 0, _image);
candidatesCount++;
emit RegesteringCandidate(candidatesCount, _name, candidates[candidatesCount - 1].voteCount );
}
function addVote(uint256 _id) public alreadyVoted(){
require(_id >= 0 && _id < candidatesCount, "Invalid option");
voters[msg.sender] = true;
candidates[_id].voteCount++;
emit Voted(_id);
}
function getLeader() public view returns (string memory _name, uint256 _totalVotes) {
for (uint i=0; i < candidatesCount; i++) {
if (candidates[i].voteCount > _totalVotes) {
_name = candidates[i].name;
_totalVotes += candidates[i].voteCount;
} else {
}
}
}
function fight(string memory _challenger, string memory _challenged) public {
}
}