Skip to content

Instantly share code, notes, and snippets.

@anthonymq
Created May 5, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonymq/c2844730e8fe3627f502372ee3d2078c to your computer and use it in GitHub Desktop.
Save anthonymq/c2844730e8fe3627f502372ee3d2078c to your computer and use it in GitHub Desktop.
pragma solidity ^0.6.4;
contract EsensVoteDappHero {
struct Scrutin {
bytes32 name;
address scrutinOwner;
bool isVisibleResult;
bool isOpenToProposal;
bool isStartVoted;
uint counter;
}
struct Vote {
address voter;
uint scrutinId;
uint proposalId;
}
struct Proposal {
uint scrutinId;
bytes32 description;
uint counter;
address scrutinOwner;
}
Scrutin[] public scrutins;
Proposal[] proposals;
Vote[] private votes;
mapping(address => mapping(uint => uint)) private proposalIdVotedForScrutinId;
mapping(address => mapping(uint => bool)) private isScrutinVoted;
event VoteSubmitted(uint _scrutinId, uint _proposalId, uint _counter);
event ScrutinCreated(uint _scrutinId, bytes32 _name, address _scrutinOwner, bool _isVisibleResult, bool _isOpenToProposal);
event ScrutinUpdated(uint _scrutinId, bytes32 _name, address _scrutinOwner, bool _isVisibleResult, bool _isOpenToProposal);
event ProposalCreated(uint _proposalId, uint _scrutinId, bytes32 _description);
event ProposalUpdated(uint _proposalId, uint _scrutinId, bytes32 _description);
constructor() public {
createScrutin('Meilleur Fast food', true, false);
createProposal(0, 'Macdo');
createProposal(0, 'KFC');
createProposal(0, 'Burger King');
createScrutin('Meilleur OS', true, false);
createProposal(1, 'Windows');
createProposal(1, 'OSX');
createProposal(1, 'Linux');
}
function getScrutinsCount() public view returns (uint256) {
return scrutins.length;
}
function getScrutinName(uint256 _scrutinId) public view returns (bytes32) {
return scrutins[_scrutinId].name;
}
function getScrutinVoteCount(uint256 _scrutinId) public view returns (uint256) {
return scrutins[_scrutinId].counter;
}
function getProposalName(uint256 _proposalId) public view returns (bytes32) {
return proposals[_proposalId].description;
}
function getVotesCount() public view returns (uint256) {
return votes.length;
}
function getProposalVoteCount(uint _proposalId) public view returns (uint256) {
return proposals[_proposalId].counter;
}
function createScrutin(bytes32 _name, bool _isVisibleResult, bool _isOpenToProposal) public returns (uint) {
scrutins.push(Scrutin(_name, msg.sender, _isVisibleResult, _isOpenToProposal, false, 6));
uint _scrutinId = scrutins.length - 1;
emit ScrutinCreated(_scrutinId, _name, msg.sender, _isVisibleResult, _isOpenToProposal);
}
function isAdmin(uint _scrutinId) public view returns (bool) {
return scrutins[_scrutinId].scrutinOwner == msg.sender;
}
function isAdminProposal(uint _proposalId) public view returns (bool) {
return proposals[_proposalId].scrutinOwner == msg.sender;
}
function updateScrutin(uint _scrutinId, bytes32 _name, bool _isVisibleResult, bool _isOpenToProposal) public {
require(scrutins[_scrutinId].scrutinOwner == msg.sender);
scrutins[_scrutinId].name = _name;
scrutins[_scrutinId].isVisibleResult = _isVisibleResult;
scrutins[_scrutinId].isOpenToProposal = _isOpenToProposal;
emit ScrutinUpdated(_scrutinId, _name, msg.sender, _isVisibleResult, _isOpenToProposal);
}
function createProposal(uint _scrutinId, bytes32 _description) public {
Scrutin storage scrutin = scrutins[_scrutinId];
if (scrutin.isOpenToProposal || scrutin.scrutinOwner == msg.sender) {
proposals.push(Proposal(_scrutinId, _description, 0, msg.sender));
uint _proposalId = proposals.length - 1;
emit ProposalCreated(_proposalId, _scrutinId, _description);
}
}
function updateProposition(uint _proposalId, bytes32 _description) public {
require(!scrutins[proposals[_proposalId].scrutinId].isStartVoted);
Proposal storage proposal = proposals[_proposalId];
require(proposal.scrutinOwner == msg.sender || scrutins[proposal.scrutinId].scrutinOwner == msg.sender);
proposal.description = _description;
emit ProposalUpdated(_proposalId, proposal.scrutinId, _description);
}
function submitVote(uint _proposalId) public {
Proposal storage proposal = proposals[_proposalId];
require(isScrutinVoted[msg.sender][proposal.scrutinId] == false);
proposal.counter++;
votes.push(Vote(msg.sender, proposal.scrutinId, _proposalId));
scrutins[proposal.scrutinId].isStartVoted = true;
isScrutinVoted[msg.sender][proposal.scrutinId] = true;
proposalIdVotedForScrutinId[msg.sender][proposal.scrutinId] = _proposalId;
scrutins[proposal.scrutinId].counter++;
emit VoteSubmitted(proposal.scrutinId, _proposalId, proposal.counter);
}
function getPropositionIdIfUserHasAlreadyVotedOnScrutinId(uint _scrutinId) public view returns (int) {
if (isScrutinVoted[msg.sender][_scrutinId] != false) {
return int(proposalIdVotedForScrutinId[msg.sender][_scrutinId]);
}
return - 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment