Balazs_Homework_2/voting.sol

48 lines
1.6 KiB
Solidity
Raw Permalink Normal View History

2022-03-02 09:56:12 +00:00
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;
contract voting {
struct Voter {
bool hasVoted;
bool exists;
}
struct Option {
uint voteCount;
bool exists;
}
mapping(string => Option) public options;
mapping(address => Voter) public voters;
constructor() public {
options["nft"] = Option(0, true);
options["token"] = Option(0, true);
options["dao"] = Option(0, true);
voters[0x5B38Da6a701c568545dCfcB03FcB875f56beddC4] = Voter(false, true); // repeat for all voters
}
function vote(string memory _option) public {
require(voters[msg.sender].exists = true);
require(voters[msg.sender].hasVoted = false);
require(options[_option].exists = true);
options[_option].voteCount = options[_option].voteCount + 1;
voters[msg.sender].hasVoted = true;
}
function getVoteCount(string memory _option) private view returns (uint) {
return options[_option].voteCount;
}
function determineWinner() public view returns (string memory){
if (getVoteCount("nft") > getVoteCount("token") && getVoteCount("nft") > getVoteCount("dao")) {
return "nft";
} else if (getVoteCount("token") > getVoteCount("nft") && getVoteCount("token") > getVoteCount("dao")) {
return "token";
} else if (getVoteCount("dao") > getVoteCount("nft") && getVoteCount("dao") > getVoteCount("token")) {
return "dao";
}
else return "manual tiebreaker required";
}
}