nautical / lpu_presentation

0 stars 0 forks source link

VotingApp.sol #7

Open mandalabhash opened 1 month ago

mandalabhash commented 1 month ago

the required modifier and constructor


    modifier onlyAdmin() {
        // TODO: Implement this modifier to restrict function calls to the admin
        require(msg.sender == admin, "Only the admin can perform this action");
        _;
    }

    modifier duringElection() {
        // TODO: Implement this modifier to allow voting only during the election period
        require(block.timestamp >= startTime && block.timestamp <= endTime, "Election is not currently active");
        _;
    }

    modifier afterElection() {
        // TODO: Implement this modifier to restrict certain actions to after the election ends
        require(block.timestamp > endTime, "Election is still ongoing");
        _;
    }

    constructor() {
         // TODO: Set the admin to the address deploying the contract
        admin = msg.sender; 
    }
mandalabhash commented 1 month ago

the code so far dtd: 01 oct 2024


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract VotingContract {
   struct Candidate {
      // name of potential CTOs
      string name;
      uint256 voteCount;
 }

    struct Voter {
        // All engineers
        bool isRegistered;
        bool hasVoted;
    }

    address public admin; // CEO
    uint256 public startTime; // time at which voting will start
    uint256 public endTime; // time at which voting will end
    bool public electionFinalized; // admin can close the election

    Candidate[] public candidates; // Array of candidate struct

    mapping(address => Voter) public voters; // mapping of voters

    event ElectionStarted(uint256 startTime, uint256 endTime);
    event VoterRegistered(address voter);
    event VoteCast(address voter, uint256 candidateIndex);
    event ElectionFinalized(string[] candidateNames, uint256[] voteCounts);

    modifier onlyAdmin() {
        require(msg.sender == admin, "Only the admin can perform this action");
        _;
    }

    modifier duringElection() {
        require(block.timestamp >= startTime && block.timestamp <= endTime, "Election is not currently active");
        _;
    }

    modifier afterElection() {
        require(block.timestamp > endTime, "Election is still ongoing");
        _;
    }

    constructor() {
        admin = msg.sender; 
    }

    // Admin starts the election
    function startElection(string[] memory candidateNames, uint256 _durationInMinutes) external onlyAdmin {

        // start time => time at which this function was called
        // end time => time at which function is called + ( duration in minutes * 1 minute )
        // candidates => push Candidate({name , voteCount})
        // emit election started

        require(!electionFinalized, "Election has already been finalized");
        require(candidates.length == 0, "Election has already started");

        startTime = block.timestamp; 
        endTime = block.timestamp + (_durationInMinutes * 1 minutes); 

        for (uint256 i = 0; i < candidateNames.length; i++) {
            candidates.push(Candidate({
                name: candidateNames[i],
                voteCount: 0
            }));
        }

        emit ElectionStarted(startTime, endTime); 

    }

    function registerVoter() external {
        require(!voters[msg.sender].isRegistered , "Voter already registered");
        voters[msg.sender] = Voter({
            isRegistered: true,
            hasVoted: false
        });
        emit VoterRegistered(msg.sender);
    }

    // Voter casts vote by selecting a candidate index
    function castVote(uint256 candidateIndex) external duringElection {
        // TODO: Implement logic to allow voters to cast their vote
        // - Check that the voter is registered and has not already voted
        // - Validate the candidate index
        // - Increase the vote count of the selected candidate
        // - Mark the voter as having voted
        // - Emit VoteCast event
    }

    function finalizeElection() external onlyAdmin afterElection {
        require(!electionFinalized, "Election already finalized");
        electionFinalized = true;
        string[] memory candidateNames = new string[](candidates.length);
        uint256[] memory voteCounts = new uint256[](candidates.length);
        for (uint256 i = 0; i < candidates.length; i++) {
            candidateNames[i] = candidates[i].name;
            voteCounts[i] = candidates[i].voteCount;
        }
        emit ElectionFinalized(candidateNames, voteCounts);
    }

    function getTimeLeft() external view returns (uint256) {
        if(block.timestamp > endTime) {
            return 0;
        }
        return endTime - block.timestamp;
    }

    function getCandidateCount() external view returns (uint256) {
        return candidates.length;
    }

    function getCandidate(uint256 index) external view returns (string memory, uint256) {
        require(index < candidates.length , "Invalid candidate selection");
        return (candidates[index].name, candidates[index].voteCount);
    }
} 
mandalabhash commented 1 month ago

function; castVote()


  function castVote(uint256 candidateIndex) external duringElection {
        // TODO: Implement logic to allow voters to cast their vote
        // - Check that the voter is registered and has not already voted
        // - Validate the candidate index
        // - Increase the vote count of the selected candidate
        // - Mark the voter as having voted
        // - Emit VoteCast event
        require(voters[msg.sender].isRegistered, "You're not registered!");
        require(!voters[msg.sender].hasVoted, "One vote per voter!");
        require(candidateIndex < candidates.length, "No candidate has that index no.");

        candidates[candidateIndex].voteCount++;
        voters[msg.sender].hasVoted = true;

        emit VoteCast(msg.sender, candidateIndex);
    }