mustafanin amcigi

mail@pastecode.io avatar
unknown
plain_text
24 days ago
1.7 kB
0
Indexable
Never
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Votea {

    address public owner;
    mapping(address => mapping(bytes32 => bool)) public hasVoted;
    string public voteOption1 = 'asdasd';
    string public voteOption2 = 'qweqwe'; 
    address[] public voters;
    uint256 public voteFee = 0.005 ether;

    modifier onlyOwner() {
        require(msg.sender == owner, "Sender is not owner");
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    function updateVote(string memory votestring) external payable {
        require(msg.value >= voteFee, "Low sent BNB");
        require(!hasVoted[msg.sender][keccak256(bytes(voteOption1))], "User already voted!");
        require(
            keccak256(bytes(votestring)) == keccak256(bytes(voteOption1)) || 
            keccak256(bytes(votestring)) == keccak256(bytes(voteOption2)),
            "Vote does not match!"
        );

        if (voters.length > 0 && (hasVoted[msg.sender][keccak256(bytes(voteOption1))] || hasVoted[msg.sender][keccak256(bytes(voteOption2))])) {
            revert("User can't vote more than once");
        }

        hasVoted[msg.sender][keccak256(bytes(votestring))] = true;
        voters.push(msg.sender);
    } 

    function updateVoteOptions(string memory _voteOption1, string memory _voteOption2) external onlyOwner {
        voteOption1 = _voteOption1;
        voteOption2 = _voteOption2;
    }

    function updateVoteFee(uint256 _voteFee) external onlyOwner {
        voteFee = _voteFee;
    }

    function getVotersCount() external view returns (uint256) {
        return voters.length;
    }
}
Leave a Comment