Voting DApp

Voting DApp is a decentralized application that allows users to cast their votes to candidates standing in an election.

This DApp will ensure that every vote is immutable and verifiable, promoting fair elections, minimizing fraudulent activities, and fostering trust among participants in the voting ecosystem.

Features of the Smart Contract:

  • Candidate Representation
  • Voter Tracking
  • Voting Window
  • Event Notification
  • Initial Candidate Setup
  • Voting Mechanism
  • Candidate Retrieval
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract VotingSystem {

    struct Candidate {
        uint8 id; // Reduced size for optimization
        string name; // Using string for name
        uint voteCount;
    }

    mapping(uint8 => Candidate) public candidates;
    mapping(address => bool) public voters;

    uint8 public candidatesCount = 0;

    uint public startTime;
    uint public endTime;

    event VotedEvent(uint8 indexed _candidateId);

    constructor(uint _durationInMinutes) {
        startTime = block.timestamp;
        endTime = startTime + (_durationInMinutes * 1 minutes);
        
        addCandidate("Candidate 1");
        addCandidate("Candidate 2");
    }

    function addCandidate(string memory _name) private {
        candidatesCount++;
        candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
    }

    function vote(uint8 _candidateId) public {
        // Short-circuiting by placing the cheaper operations first
        require(block.timestamp >= startTime && block.timestamp <= endTime, "Voting not allowed now.");
        require(_candidateId > 0 && _candidateId <= candidatesCount, "Invalid candidate ID.");
        require(!voters[msg.sender], "You have voted already.");

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

        emit VotedEvent(_candidateId);
    }

    function getAllCandidates() public view returns (Candidate[] memory) {
        Candidate[] memory candidateArray = new Candidate[](candidatesCount);
        for (uint8 i = 1; i <= candidatesCount; i++) {
            candidateArray[i-1] = candidates[i];
        }
        return candidateArray;
    }

    function getCurrentLeader() public view returns (string memory) {
        uint maxVotes = 0;
        uint8 leadingCandidateId = 0;

        for (uint8 i = 1; i <= candidatesCount; i++) {
            if (candidates[i].voteCount > maxVotes) {
                maxVotes = candidates[i].voteCount;
                leadingCandidateId = i;
            }
        }

        return candidates[leadingCandidateId].name;
    }
}

Testing with Remix

Open the https://remix.ethereum.org/. Create ‘Voting_DApp.sol’ file and paste the code inside.

  • Deploy the Smart Contract on Remix

Set the ‘DURATIONINMINUTES:’ to 5 for testing purposes. This system will enable us to vote for candidates within a five-minute window, after which the voting will conclude automatically.

Click the ‘transact’ button to deploy the contract. This initiates the contract with the two candidates, ‘Bob’ and ‘Alice’.

  • Voting for the Candidates

Open the contract and Input ‘1’ for Bob or ‘2’ for Alice.

Click the ‘vote’ button.

Each account can vote for only one candidate; attempting to vote for an additional candidate will result in an error.

  • Retrieving Data from Contract

Voting will be open for 5 minutes only.

The current voting status can be obtained from the contract.