Skip to content

Instantly share code, notes, and snippets.

@chungquantin
Created January 12, 2021 15:54
Show Gist options
  • Select an option

  • Save chungquantin/7aa02a45766f0d7380763cde3bc77f3b to your computer and use it in GitHub Desktop.

Select an option

Save chungquantin/7aa02a45766f0d7380763cde3bc77f3b to your computer and use it in GitHub Desktop.
ElectionSmartContract.sol
// 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