Created
October 21, 2021 16:59
-
-
Save SAYONG/920a57296d933a28ee5c73de16b2f01a to your computer and use it in GitHub Desktop.
Voting Contract
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
[ | |
{ | |
"inputs": [ | |
{ | |
"internalType": "address", | |
"name": "chairperson_", | |
"type": "address" | |
} | |
], | |
"stateMutability": "nonpayable", | |
"type": "constructor" | |
}, | |
{ | |
"inputs": [], | |
"name": "candidateRegis", | |
"outputs": [], | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"inputs": [ | |
{ | |
"internalType": "address", | |
"name": "", | |
"type": "address" | |
} | |
], | |
"name": "candidates", | |
"outputs": [ | |
{ | |
"internalType": "bool", | |
"name": "isRegistered", | |
"type": "bool" | |
}, | |
{ | |
"internalType": "uint256", | |
"name": "voteCount", | |
"type": "uint256" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "chairperson", | |
"outputs": [ | |
{ | |
"internalType": "address", | |
"name": "", | |
"type": "address" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "endVoting", | |
"outputs": [], | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "getWinner", | |
"outputs": [ | |
{ | |
"internalType": "address", | |
"name": "", | |
"type": "address" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "startVoting", | |
"outputs": [], | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "state", | |
"outputs": [ | |
{ | |
"internalType": "uint256", | |
"name": "", | |
"type": "uint256" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [ | |
{ | |
"internalType": "address", | |
"name": "candidate", | |
"type": "address" | |
} | |
], | |
"name": "vote", | |
"outputs": [], | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"inputs": [ | |
{ | |
"internalType": "address", | |
"name": "", | |
"type": "address" | |
} | |
], | |
"name": "voters", | |
"outputs": [ | |
{ | |
"internalType": "bool", | |
"name": "voted", | |
"type": "bool" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
} | |
] |
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: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
// Modified from Ballot contract https://docs.soliditylang.org/en/v0.8.9/solidity-by-example.html | |
contract VotingContract { | |
struct Voter { | |
bool voted; // if true, that person already voted | |
} | |
struct Candidate { | |
bool isRegistered; // if true, that person is a candidate | |
uint voteCount; | |
} | |
address public chairperson; | |
mapping(address => Voter) public voters; | |
mapping(address => Candidate) public candidates; | |
uint public state; // 0: setup, 1: voting, 2: voting end | |
address private topCandidate; | |
constructor(address chairperson_) { | |
chairperson = chairperson_; | |
} | |
function candidateRegis() public { | |
require(state == 0, "Registration is now closed."); | |
require(msg.sender != chairperson, "Chairperson cannot be a candidate."); | |
require( | |
!voters[msg.sender].voted, | |
"Voter cannot be a candidate." | |
); | |
candidates[msg.sender].isRegistered = true; | |
} | |
function startVoting() public { | |
require( | |
msg.sender == chairperson, | |
"Only chairperson can start voting." | |
); | |
require(state == 0, "Unable to start voting."); | |
state = 1; | |
} | |
function endVoting() public { | |
require( | |
msg.sender == chairperson, | |
"Only chairperson can end voting." | |
); | |
require(state == 1, "Voting is not open or already closed."); | |
state = 2; | |
} | |
function vote(address candidate) public { | |
Voter storage sender = voters[msg.sender]; | |
require(state == 1, "Voting is not open or already closed."); | |
require(msg.sender != chairperson, "Chairperson cannot give a vote."); | |
require( | |
!candidates[msg.sender].isRegistered, | |
"Candidate cannot vote." | |
); | |
require(!sender.voted, "Already voted."); | |
sender.voted = true; | |
candidates[candidate].voteCount++; | |
if (candidates[candidate].voteCount > candidates[topCandidate].voteCount) { | |
topCandidate = candidate; | |
} | |
} | |
function getWinner() public view | |
returns (address) | |
{ | |
require(state == 2, "Winner is not announced yet."); | |
return topCandidate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment