Created
June 15, 2021 09:15
-
-
Save korrio/557069d544dcef89e49f74605f2bb133 to your computer and use it in GitHub Desktop.
voted.sol
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
/** | |
*Submitted for verification at Etherscan.io on 2019-05-27 | |
*/ | |
pragma solidity ^0.5.0; | |
pragma experimental ABIEncoderV2; | |
// specifies what version of compiler this code will be compiled with | |
contract Voting { | |
/* the mapping field below is equivalent to an associative array or hash. | |
*/ | |
mapping (string => uint256) votesReceived; | |
/* Solidity doesn't let you pass in an array of strings in the constructor (yet). | |
We will use an array of bytes32 instead to store the list of candidates | |
*/ | |
string[] public candidateList; | |
event Voted(string candidate, uint256 name); | |
/* This is the constructor which will be called once and only once - when you | |
deploy the contract to the blockchain. When we deploy the contract, | |
we will pass an array of candidates who will be contesting in the election | |
*/ | |
constructor(string[] memory candidateNames) public { | |
candidateList = candidateNames; | |
} | |
// This function returns the total votes a candidate has received so far | |
function totalVotesFor(string memory candidate) public view returns (uint256) { | |
return votesReceived[candidate]; | |
} | |
// This function increments the vote count for the specified candidate. This | |
// is equivalent to casting a vote | |
function voteForCandidate(string memory candidate) public { | |
votesReceived[candidate] += 1; | |
emit Voted(candidate, votesReceived[candidate]); | |
} | |
function candidateCount() public view returns (uint256) { | |
return candidateList.length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment