Last active
February 14, 2022 08:44
-
-
Save jedsada-gh/fea0a39ff8fcc5fb2d2d549272f18f5b to your computer and use it in GitHub Desktop.
This file contains 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.7; | |
contract ElectionVote { | |
address public owner; | |
mapping(address => bool) adminLookup; | |
Candidate[] candidateLookup; | |
mapping(address => bytes32) vouter; | |
uint startDate = 1644883200; // February 15, 2022 12:00:00 AM | |
uint endDate = 1645315200; // February 20, 2022 12:00:00 AM | |
struct CandidateDetail { | |
string firstName; | |
string lastName; | |
uint8 age; | |
string gender; | |
string description; | |
string imageURL; | |
} | |
struct Candidate { | |
bytes32 id; | |
CandidateDetail detail; | |
uint voteCount; | |
} | |
constructor() { | |
owner = msg.sender; | |
adminLookup[owner] = true; | |
} | |
modifier onlyOwner() { | |
require(adminLookup[msg.sender] == true, "this function for only admin"); | |
_; | |
} | |
function addAdmin(address _admin) public onlyOwner { | |
require(adminLookup[_admin] == false, "this address is admin already exists"); | |
adminLookup[_admin] = true; | |
} | |
function removeAdmin(address _admin) public { | |
require(_admin != owner, "you can't remove deployer"); | |
require(adminLookup[_admin] == true, "this address is not admin yet"); | |
adminLookup[_admin] = false; | |
} | |
function addCandidate(CandidateDetail memory detail) public onlyOwner { | |
require(block.timestamp < startDate, "this functiaon can call before start date"); | |
bytes32 id = keccak256(abi.encodePacked(block.timestamp, candidateLookup.length + 1)); | |
candidateLookup.push(Candidate(id, detail, 0)); | |
} | |
function updateCandidate(bytes32 id, CandidateDetail memory detail) public onlyOwner { | |
require(block.timestamp < startDate, "this functiaon can call before start date"); | |
uint index = findIndexById(id); | |
candidateLookup[index].detail = detail; | |
} | |
function getCandidates() public view returns(Candidate[] memory) { | |
return candidateLookup; | |
} | |
function vote(bytes32 candidateId) public { | |
require(block.timestamp >= startDate && block.timestamp <= endDate, "not availble date to voting"); | |
require(msg.sender != address(0), "address invalid"); | |
require(vouter[msg.sender] == bytes32(0), "you're have voited already"); | |
uint index = findIndexById(candidateId); | |
candidateLookup[index].voteCount++; | |
vouter[msg.sender] = candidateId; | |
} | |
function getWinner() public view returns(Candidate memory) { | |
require(block.timestamp > endDate, "this function cam call after end date"); | |
return getMaxVote(); | |
} | |
function getMaxVote() public view returns (Candidate memory){ | |
uint index = 0; | |
uint max = candidateLookup[index].voteCount; | |
for(uint i = 0; i < candidateLookup.length; i++) { | |
if (candidateLookup[i].voteCount > max) { | |
max = candidateLookup[i].voteCount; | |
index = i; | |
} | |
} | |
return candidateLookup[index]; | |
} | |
function findIndexById(bytes32 id) private view returns(uint) { | |
for(uint i = 0; i < candidateLookup.length; i++) { | |
if (candidateLookup[i].id == id) { | |
return i; | |
} | |
} | |
revert("not found candidate"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment