AnozieChibuike / VotingDApp

0 stars 1 forks source link
hacktoberfest

Voting System Smart Contract

A simple, gas-efficient voting system written in Solidity that allows for secure, decentralized elections on the Ethereum blockchain. This contract includes features like registered voters, a time-based voting window, and candidate registration. It ensures that only registered voters can vote, each voter can vote once, and the voting process occurs within a specified time frame.

Features

Smart Contract Breakdown

Contract Variables

Structs

  1. Candidate: Holds details about each candidate:

    • id: Unique ID of the candidate.
    • name: Name of the candidate.
    • imageUrl: URL to the candidate's image.
    • voteCount: Number of votes the candidate has received.
  2. Voter: Keeps track of voter details:

    • voted: Whether the voter has cast a vote or not.
    • vote: ID of the candidate the voter voted for.

Mappings

Functions

  1. addCandidate(string calldata _name, string calldata _imageUrl): Allows the admin to add a new candidate. Emits a CandidateAdded event.

  2. registerVoter(address _voter): Admin function to register a voter by their Ethereum address. Emits a VoterRegistered event.

  3. vote(uint _candidateId): Allows a registered voter to cast their vote for a candidate, provided voting is active and they have not already voted. Emits a Voted event.

  4. endVoting(): Allows the admin to end the voting session before the voting period expires. Emits a VotingEnded event.

  5. getCandidate(uint \_candidateId): Returns a candidate's name and vote count by their ID.

  6. getAllCandidates(): Returns all registered candidates.

  7. totalCandidates(): Returns the total number of candidates.

Setup Instructions

Prerequisites

  1. Node.js & NPM: Ensure you have Node.js installed on your machine.

  2. Truffle or Hardhat: You can use either Truffle or Hardhat for deployment and testing.

Clone the repository:

git clone https://github.com/AnozieChibuikke/BlockchainVotingSystem.git
cd BlockchainVotingSystem

Install Dependencies:

If you're using Truffle, run:

npm install

Compile the Smart Contract:

For Truffle, run:

truffle compile

For Hardhat, run:

npx hardhat compile

Deploy the Smart Contract:

For Truffle, run:

truffle migrate --network <network_name>

For Hardhat, run:

npx hardhat run scripts/deploy.js --network <network_name>

Interact with the Contract:

You can use the Truffle console or Hardhat to interact with the deployed contract.

For Truffle:

truffle console --network <network_name>

For Hardhat:

npx hardhat console --network <network_name>

Testing

To run tests:

For Truffle:

truffle test

For Hardhat:

npx hardhat test

Example Usage

1. Admin adds candidates:

await votingInstance.addCandidate("Alice", "https://image.url/alice.jpg");

await votingInstance.addCandidate("Bob", "https://image.url/bob.jpg");

2. Admin registers voters:

await votingInstance.registerVoter("0xVoterAddress1");
await votingInstance.registerVoter("0xVoterAddress2");

3. Voters cast their votes:

await votingInstance.vote(0, { from: "0xVoterAddress1" }); // Votes for Alice
await votingInstance.vote(1, { from: "0xVoterAddress2" }); // Votes for Bob

4. End voting:

await votingInstance.endVoting({ from: admin });

5. View candidate results:

let candidate = await votingInstance.getCandidate(0); // Get Alice's data

Events

Security Considerations

License

This project is licensed under the MIT License - see the LICENSE file for details.