Skip to content
Snippets Groups Projects
Select Git revision
  • main default protected
1 result

voting-contract.sol

Blame
  • voting-contract.sol 6.55 KiB
    import "@openzeppelin/contracts/access/Ownable.sol";
    
    pragma solidity ^0.8.0;
    
    contract Voting is Ownable{
    
        enum WorkflowStatus {
            RegisteringVoters,
            ProposalsRegistrationStarted,
            ProposalsRegistrationEnded,
            VotingSessionStarted,
            VotingSessionEnded,
            VotesTallied
        }
    
        WorkflowStatus public status; 
        uint public winningProposalId; 
    
        struct Voter {
            bool isRegistered;
            bool hasVoted;
            uint votedProposalId;
        }
    
        struct Proposal {
            string description;
            uint voteCount;
        }
    
        struct VoterProposal {
            string description;
            bool hasVoted;
        }
    
        mapping(address => VoterProposal) public voterProposals; 
        mapping(address => Voter) public voters; 
        mapping(address => address) public delegatedVotes; 
        
        Proposal[] public proposals; 
    
        event VoterRegistered(address voterAddress);
        
        event WorkflowStatusChange(WorkflowStatus previousStatus, WorkflowStatus newStatus);
        
        event ProposalRegistered(uint proposalId);
        
        event Voted(address voter, uint proposalId);
    
        constructor() Ownable(msg.sender) {
            status = WorkflowStatus.RegisteringVoters;
        }
    
        function registerVoter(address _voter) public onlyOwner {
            require(status == WorkflowStatus.RegisteringVoters, "Le vote n'est pas en cours d'inscription des votants.");
            require(!voters[_voter].isRegistered, "Le votant est deja inscrit.");
            voters[_voter].isRegistered = true;
            emit VoterRegistered(_voter);
        }
    
        function startProposalsRegistration() public onlyOwner {
            require(status == WorkflowStatus.RegisteringVoters, "Le vote n'est pas en cours d'inscription des votants.");
            
            status = WorkflowStatus.ProposalsRegistrationStarted;
            emit WorkflowStatusChange(WorkflowStatus.RegisteringVoters, WorkflowStatus.ProposalsRegistrationStarted);
        }
    
        function endProposalsRegistration() public onlyOwner {
            require(status == WorkflowStatus.ProposalsRegistrationStarted, "Le vote n'est pas en cours d'enregistrement des propositions.");
            
            status = WorkflowStatus.ProposalsRegistrationEnded;