Created
March 24, 2024 16:51
-
-
Save Ayomisco/eb0f6d645fc8c0ba79d0644a91edebdd to your computer and use it in GitHub Desktop.
VotingSystem | BaseTask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
// The VotingSystem smart contract enables secure and transparent voting on Solidity. | |
// It facilitates voter registration, candidate addition, and vote casting. | |
// The contract owner manages the process, ensuring integrity and accountability. | |
// It is deployed on Base | |
contract VotingSystem { | |
// Define the structure of a voter | |
struct Voter { | |
bool isRegistered; | |
bool hasVoted; | |
} | |
// Define the structure of a candidate | |
struct Candidate { | |
string name; | |
uint256 voteCount; | |
} | |
// Define the owner of the contract | |
address public owner; | |
// Define the state variables | |
mapping(address => Voter) public voters; | |
Candidate[] public candidates; | |
// Define events | |
event VoteCast(address indexed voter, uint256 indexed candidateIndex); | |
// Constructor function | |
constructor() { | |
owner = msg.sender; | |
} | |
// Modifier to restrict access to the owner | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Only the owner can call this function"); | |
_; | |
} | |
// Function to register voters | |
function registerVoter(address _voter) external onlyOwner { | |
require(!voters[_voter].isRegistered, "Voter is already registered"); | |
voters[_voter].isRegistered = true; | |
} | |
// Function to add candidates | |
function addCandidate(string memory _name) external onlyOwner { | |
candidates.push(Candidate(_name, 0)); | |
} | |
// Function to cast votes | |
function vote(uint256 _candidateIndex) external { | |
require(voters[msg.sender].isRegistered, "Voter is not registered"); | |
require(!voters[msg.sender].hasVoted, "Voter has already voted"); | |
require(_candidateIndex < candidates.length, "Invalid candidate index"); | |
voters[msg.sender].hasVoted = true; | |
candidates[_candidateIndex].voteCount++; | |
emit VoteCast(msg.sender, _candidateIndex); | |
} | |
// Function to get the total number of candidates | |
function getTotalCandidates() external view returns (uint256) { | |
return candidates.length; | |
} | |
// Function to get the details of a candidate | |
function getCandidate(uint256 _candidateIndex) external view returns (string memory, uint256) { | |
require(_candidateIndex < candidates.length, "Invalid candidate index"); | |
return (candidates[_candidateIndex].name, candidates[_candidateIndex].voteCount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
0x574f04239404002c65e4d8fbe1e9def2b9aa340ec510fc6ecbaf3fc4e53bc77e
0x9f27ee62cc6209fde5dd46b73600d8196f758298