Implement a smart contract for voting.
A high school class has to decide whether they should participate in a national competition. They decide they will vote on the issue.
The teacher creates a contract, submitting a description of the question, its hash value, and a deadline a week from now. Then the teacher proceeds to register all students of the class as participants.
In the following week, some or all of the students can vote "yes" or "no". In the end, 20 of 30 students have voted, 16 for "yes", 4 for "no".
After the deadline, anyone can read the result. As 16/30 > 50% of students voted "yes", the end result is also "yes".
constructor: function SingleUseVote(string link, string hash, uint256 deadline)
link
. A hash of the description (hash
) is also submitted so that the participants can make sure it has not been altered. deadline
is a timestamp representing the deadline after which no one can vote.msg.sender
) is the owner of the contract.register: function registerParticipant(address participant)
participant
as a participant of the vote, unless they are already registered.vote: function vote(bool voteYes)
voteYes
(true
means "yes").check result: function getResult() view returns (bool)
pragma solidity ^0.4.21;
contract SingleUseVote {
// <contract_variables>
// </contract_variables>
function SingleUseVote(string link, string hash, uint256 deadline) public
{
// TODO
}
function registerParticipant(address participant) public {
// TODO
}
function vote(bool voteYes) public {
// TODO
}
function getResult() public view returns (bool) {
// TODO
}
}