Created
January 12, 2021 15:54
-
-
Save chungquantin/7aa02a45766f0d7380763cde3bc77f3b to your computer and use it in GitHub Desktop.
ElectionSmartContract.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
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.7.4; | |
| contract Election{ | |
| // Model a candidate | |
| struct Candidate { | |
| uint id; | |
| string name; | |
| uint voteCount; | |
| } | |
| // Store accounts (voters) | |
| mapping(address => bool) public voters; | |
| // Store candidate | |
| mapping(uint => Candidate) public candidates; | |
| // Constructor | |
| uint public candidateCount; | |
| constructor() { | |
| addCandidate("Donald Trump"); | |
| addCandidate("Joe Biden"); | |
| } | |
| function addCandidate (string memory __name) private { | |
| // The number of candidates will be increased by 1 | |
| candidateCount++; | |
| // Add to the list of candidate with another named candidate | |
| candidates[candidateCount] = Candidate(candidateCount, __name, 0); | |
| } | |
| function vote(uint __candidateId) public { | |
| // Record that the voters have used their vote | |
| voters[msg.sender] = true; | |
| // Increase the vote count of the voted candidate by 1 | |
| candidates[__candidateId].voteCount++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment