forked from kshitijkota/Blockheads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoting.sol
More file actions
54 lines (42 loc) · 1.79 KB
/
voting.sol
File metadata and controls
54 lines (42 loc) · 1.79 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Voting {
// Struct to represent a voter
struct Voter {
bool isRegistered; // Flag indicating whether the voter is registered
bool hasVoted; // Flag indicating whether the voter has already cast a vote
}
// Mapping to store voters' details
mapping(address => Voter) public voters;
// Array to store candidate names
string[] public candidateList;
// Mapping to store votes for each candidate
mapping(string => uint256) public votesReceived;
// Function to register a voter
function registerVoter() public {
require(!voters[msg.sender].isRegistered, "Voter is already registered");
voters[msg.sender].isRegistered = true;
}
// Function to cast a vote
function castVote(string memory candidateName) public {
require(voters[msg.sender].isRegistered, "Voter is not registered");
require(!voters[msg.sender].hasVoted, "Voter has already voted");
// Increment vote count for the candidate
votesReceived[candidateName]++;
// Mark the voter as having voted
voters[msg.sender].hasVoted = true;
}
// Function to add a candidate to the list
function addCandidate(string memory candidateName) public {
// You can add additional logic here, such as checking if the candidate already exists
candidateList.push(candidateName);
}
// Function to retrieve the total votes for a candidate
function getVotesForCandidate(string memory candidateName) public view returns (uint256) {
return votesReceived[candidateName];
}
// Function to retrieve the list of candidates
function getCandidateList() public view returns (string[] memory) {
return candidateList;
}
}