Created
September 4, 2022 01:35
-
-
Save casweeney/49ce47699a3e7e95fe78a67022655a29 to your computer and use it in GitHub Desktop.
Simple Voting Contract
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.4; | |
| /// @author Casweeney Ojukwu | |
| contract PollingSystem { | |
| /// @dev Created events for each transaction | |
| event pollCreated(bool indexed status); | |
| event voted(uint indexed candidateIndex); | |
| event votingStarted(bool indexed _votingOpened); | |
| event votingStopped(bool indexed _votingOpened); | |
| /// @dev setting an owner state variable | |
| address owner; | |
| /// @dev Struct of poll candidates, holding each candidate's name and voteCount | |
| struct Candidate { | |
| string name; | |
| uint voteCount; | |
| } | |
| /// @dev Array of Candidate struct that contains all the candidate in the created poll | |
| Candidate[] public candidates; | |
| /// @dev struct of voter that holds if a user has voted and who they voted for | |
| struct Voter { | |
| bool voted; | |
| uint votedFor; | |
| } | |
| mapping(address => Voter) voters; | |
| bool votingOpened; | |
| modifier onlyOwner() { | |
| require(msg.sender == owner, "not owner"); | |
| _; | |
| } | |
| constructor() { | |
| owner = msg.sender; | |
| } | |
| function createPoll(string[] memory _candidates) external onlyOwner { | |
| require(_candidates.length > 0, "cant't create poll without a candidate"); | |
| for(uint i = 0; i < _candidates.length; i++) { | |
| Candidate memory cnd; | |
| cnd.name = _candidates[i]; | |
| cnd.voteCount = 0; | |
| candidates.push(cnd); | |
| } | |
| emit pollCreated(true); | |
| } | |
| function showCandidates() external view returns (Candidate[] memory) { | |
| return candidates; | |
| } | |
| function vote(uint _candidateIndex) external { | |
| require(votingOpened, "voting not opened yet, kindly wait..."); | |
| Voter storage vt = voters[msg.sender]; | |
| require(!vt.voted, "already voted, can't vote twice"); | |
| vt.voted = true; | |
| vt.votedFor = _candidateIndex; | |
| candidates[_candidateIndex].voteCount += 1; | |
| emit voted(_candidateIndex); | |
| } | |
| function getWinnerIndex() public view returns (uint winnerIndex) { | |
| require(!votingOpened, "can't get winner until voting ends"); | |
| uint winningVote = 0; | |
| for(uint n = 0; n < candidates.length; n++) { | |
| if(candidates[n].voteCount > winningVote) { | |
| winningVote = candidates[n].voteCount; | |
| winnerIndex = n; | |
| } | |
| } | |
| } | |
| function winnerName() external view returns (string memory _winnerName) { | |
| require(!votingOpened, "can't get winner until voting ends"); | |
| uint voteWinnerIndex = getWinnerIndex(); | |
| _winnerName = candidates[voteWinnerIndex].name; | |
| } | |
| function openVote() external onlyOwner { | |
| require(!votingOpened, "Vote already in progress"); | |
| votingOpened = true; | |
| emit votingStarted(true); | |
| } | |
| function closeVote() external onlyOwner { | |
| require(votingOpened, "Vote is already closed"); | |
| votingOpened = false; | |
| emit votingStopped(false); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment