1

Is there a way to allow user to vote for multiple proposals. For example: User A can vote for proposals :P1,P2,P3,etc. Once A has voted for P1 he should not be allowed to vote for P1 but is eligible to vote for P2 and P3. I am able to write a contract that shows which proposals the candidate has voted for. Can any one help!!

  • Are you talking about Proof of Stake? – Briomkez Sep 19 '18 at 15:24
  • Anything is possible! How this can be achieved depends on your implementation, however. Please enlighten us. – Henk Sep 19 '18 at 15:27
  • I am unable to devise a logic for it. can anyone help me with that. I have struct for proposal: – aib92 Sep 19 '18 at 15:36
  • Oh, I just answered, but forgot to ask if you actually use Solidity. – Utgarda Sep 19 '18 at 15:46
  • Yes. I am using solidity – aib92 Sep 19 '18 at 15:53
1

You can keep users' eligibility to vote for a particular proposal in a structure like

mapping(uint => mapping(address => bool)) hasVoted;
//      ^proposal IDs   ^users     ^already voted or not
mapping(uint => uint) votesCount;

and then check if they are eligible to vote, here's an example in Solidity:

modifier canVote(address user, uint proposal){
    require(!hasVoted[proposal][user]);
    _;
}

function vote(uint proposal)
public 
canVote(msg.sender, proposal) //throw if user has already voted
{
    hasVoted[proposal][msg.sender] = true; //make note of the fact he's voting now
    votesCount[proposal]++;
    //other voting logic you need
}

Take care to use a suitable type for your proposal IDs, like uint8 or whatever is enough.

  • if i want to check/allow user to vote for candidate in a proposal then i would have to modify my hasVoted mapping right?? mapping(bytes32 => mapping(bytes32 => mapping(address => bool))) hasVoted;. however the user should be able to vote for candidate in other proposal say P2. – aib92 Sep 19 '18 at 16:20
  • Well, naturally, the code should depend on what you want it to do. This example shows just voting for proposals, i.e. how many votes a proposal collects. If voting structure is more complex than that, it probably means you just have to change votesCount structure and its use. hasVoted can stay the same. – Utgarda Sep 19 '18 at 16:24
  • I am sorry to have asked these questions. I am new to programming and solidity – aib92 Sep 19 '18 at 16:25
  • No probs, questions is what this sites is all about. Clarifying the above: I think you don't have to alter hasVoted, because a user can vote for a proposal only once, no matter which candidate he picked ( is that right? ) – Utgarda Sep 19 '18 at 16:26
  • 1
    Yep, for this use case hasVoted structure and the modifier in this example stay the same. – Utgarda Sep 19 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.