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.
Candidate Registration: Admin can add candidates with their name and image URL.
Voter Registration: Admin can register specific Ethereum addresses as eligible voters.
Vote Casting: Registered voters can cast their vote for any candidate during the active voting period.
Voting Timeframe: Voting is allowed only between a start and end time specified by the admin.
Vote Tracking: Each voter can only vote once, and the vote is permanently recorded on the blockchain.
Results Viewing: Anyone can view the candidates and their current vote counts.
admin
: The address of the contract creator who has exclusive control over admin functions like adding candidates and registering voters.
votingOpen
: A boolean flag to check whether voting is still active.
votingStartTime
: The timestamp when voting begins.
votingEndTime
: The timestamp when voting ends.
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.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.voters
: Stores voter information mapped by their Ethereum address.
registeredVoters
: Keeps track of whether a voter is registered to vote.
candidateExists
: Tracks whether a candidate with a given name already exists (to prevent duplicate entries).
addCandidate(string calldata _name, string calldata _imageUrl)
:
Allows the admin to add a new candidate. Emits a CandidateAdded
event.
registerVoter(address _voter)
:
Admin function to register a voter by their Ethereum address. Emits a VoterRegistered
event.
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.
endVoting()
:
Allows the admin to end the voting session before the voting period expires. Emits a VotingEnded
event.
getCandidate(uint \_candidateId)
:
Returns a candidate's name and vote count by their ID.
getAllCandidates()
:
Returns all registered candidates.
totalCandidates()
:
Returns the total number of candidates.
Node.js & NPM: Ensure you have Node.js installed on your machine.
Truffle or Hardhat: You can use either Truffle or Hardhat for deployment and testing.
git clone https://github.com/AnozieChibuikke/BlockchainVotingSystem.git
cd BlockchainVotingSystem
If you're using Truffle, run:
npm install
For Truffle, run:
truffle compile
For Hardhat, run:
npx hardhat compile
For Truffle, run:
truffle migrate --network <network_name>
For Hardhat, run:
npx hardhat run scripts/deploy.js --network <network_name>
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>
To run tests:
For Truffle:
truffle test
For Hardhat:
npx hardhat test
await votingInstance.addCandidate("Alice", "https://image.url/alice.jpg");
await votingInstance.addCandidate("Bob", "https://image.url/bob.jpg");
await votingInstance.registerVoter("0xVoterAddress1");
await votingInstance.registerVoter("0xVoterAddress2");
await votingInstance.vote(0, { from: "0xVoterAddress1" }); // Votes for Alice
await votingInstance.vote(1, { from: "0xVoterAddress2" }); // Votes for Bob
await votingInstance.endVoting({ from: admin });
let candidate = await votingInstance.getCandidate(0); // Get Alice's data
CandidateAdded(string name, uint id)
: Emitted when a new candidate is added.VoterRegistered(address voter)
: Emitted when a voter is registered.Voted(address indexed voter, uint indexed candidateId)
: Emitted when a voter casts a vote.VotingEnded()
: Emitted when the admin ends the voting.Only Admin Actions: Only the admin can register voters, add candidates, and end the voting.
Voting Once Per Voter: Each voter can only cast one vote.
Registered Voters Only: Only registered voters are allowed to vote.
Time-Restricted Voting: Voting can only happen during the specified voting period.
This project is licensed under the MIT License - see the LICENSE file for details.