-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCampusVotingSystem.sol
More file actions
69 lines (54 loc) · 1.81 KB
/
CampusVotingSystem.sol
File metadata and controls
69 lines (54 loc) · 1.81 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract CampusVotingSystem {
// Admin
address public admin;
//Candidate structure
struct Candidate {
string name;
uint voteCount;
}
// List of candidates
Candidate[] public candidates;
// Track eligible voters
mapping(address => bool) public isEligibleVoter;
// Prevent double voting
mapping(address => bool) public hasVoted;
// Constructor runs once when deployed
constructor() {
admin = msg.sender;
// Initialize 3 candidates
candidates.push(Candidate("Candidate A", 0));
candidates.push(Candidate("Candidate B", 0));
candidates.push(Candidate("Candidate C", 0));
}
// Only admin can execute certain functions
modifier onlyAdmin() {
require(msg.sender == admin, "Only admin allowed");
_;
}
// Only registered voters can vote
modifier onlyEligibleVoter() {
require(isEligibleVoter[msg.sender], "Not an eligible voter");
_;
}
// Register voter
function registerVoter(address voter) public onlyAdmin {
isEligibleVoter[voter] = true;
}
// Voting function
function vote(uint candidateIndex) public onlyEligibleVoter {
require(!hasVoted[msg.sender], "You have already voted");
require(candidateIndex < candidates.length, "Invalid candidate");
hasVoted[msg.sender] = true;
candidates[candidateIndex].voteCount++;
}
// Get full election results
function getResults() public view returns (Candidate[] memory) {
return candidates;
}
// Get number of candidates
function getCandidateCount() public view returns (uint) {
return candidates.length;
}
}