This is a simple Ethereum smart contract written in Solidity for conducting a voting process. The contract allows the deployment of candidates, voting by addresses, and declaring a winner based on the highest number of votes.
-
Adding Candidates: The owner of the contract can add candidates to the election.
-
Voting: Any address can vote for a candidate once.
-
Declare Winner: A function is provided to determine and declare the winner based on the highest number of votes.
The smart contract is implemented in Solidity version ^0.8.18.
-
addCandidate(string memory name)
- Adds a candidate to the election.
- Only the owner of the contract can add candidates.
-
getCandidate(uint id) external view returns (string memory name, uint noOfVotes)
- Retrieves information about a specific candidate.
-
getCandidates() external view returns (string[] memory, uint[] memory)
- Retrieves information about all candidates.
-
vote(uint id) external
- Allows an address to vote for a candidate.
- An address can vote only once.
-
declareWinner() external view returns (string memory winnerName, uint winnerVotes)
- Determines and returns the winner's name and vote count.
-
changeOwner(address newOwner) public
- Changes the owner of the contract.
-
Voted(uint indexed id)
- Emitted when a vote is cast.
-
CandidateCreated(uint indexed candidateNo, string indexed candidateName)
- Emitted when a new candidate is added.
-
OwnerChange(address indexed oldOwner, address indexed newOwner)
- Emitted when the owner of the contract is changed.
- Candidate information is stored in a mapping (
candidateLookUp
). - Voter status is stored in a mapping (
voterLookUp
).
The smart contract can be tested using foundry (forge test
)
Deploy the smart contract to the Ethereum blockchain using your preferred deployment method or tool.(forge in foundry)