0

I have a simple copied smart contract for elections, where a voter votes only one time for one candidate, and then is denied to vote more times.

But would it be possible to allow the voter to vote up to 3 times and sending a vote to a different candidate each time? Meaning when you send more than one vote to one candidate, you would be denied. How would it be done?

The code

contract Election {

struct Candidate {
    string name;
    uint voteCount;
}
struct Voter {
    uint voteIndex;
    bool voted;
    uint weight;
}

address public owner;
string public name;
mapping(address => Voter) public voters;
Candidate[] public candidates;
uint public auctionEnd;

event ElectionResult(string name, uint voteCount);

function Election(string _name, uint durationMinutes, string candidate1, string candidate2, string candidate3, string candidate4, string candidate5) public{
    owner = msg.sender;
    name = _name; 
    auctionEnd = now + (durationMinutes * 1 minutes);

    candidates.push(Candidate(candidate1, 0));
    candidates.push(Candidate(candidate2, 0));
    candidates.push(Candidate(candidate3, 0));
    candidates.push(Candidate(candidate4, 0));
    candidates.push(Candidate(candidate5, 0));
}

function authorize(address voter) public {
    require(msg.sender == owner);
    require(!voters[voter].voted);

    voters[voter].weight = 1;

}

function vote(uint voteIndex) public {
    require(now < auctionEnd);
    require(!voters[msg.sender].voted);

    voters[msg.sender].voted = true;
    voters[msg.sender].voteIndex = voteIndex;

    candidates[voteIndex].voteCount += voters[msg.sender].weight;
}

function end() public {
    require(msg.sender == owner);
    require(now >= auctionEnd);

    for(uint i=0; i < candidates.length; i++) {
        ElectionResult(candidates[i].name, candidates[i].voteCount);
    }
}
}
  • this is not about Ethereum, it is about general programming. – Nulik Apr 14 '18 at 21:11
1

I agree with @Nulik that this is more generally about programming. You want to change the rules. Basically, instead of checking if the voter ever volted, you want to change how many times they voted.

This

struct Voter {
    uint voteIndex;
    bool voted;  // <= ever voted
    uint weight;
}

To this:

struct Voter {
    uint voteIndex;
    uint voted; // <== count of votes cast
    uint weight;
}

This

require(!voters[voter].voted);

To this

require(voters[voter].voted < 3);

This

voters[msg.sender].voted = true;

To this

voters[msg.sender].voted += 1;

This is by no means thoroughly tested and there is no warranty. It may be a start for educational purposes only.

Hope it helps.

  • Thank you, it definitely helped! I didn't mean to break the rules, it is still a smart contract development, and I couldn't think of a better place to ask this. – Mark OD Apr 15 '18 at 8:08
  • It actually works quite nicely, but I can't for the life of me figure out how to state that each time you have to vote for a different candidate and not be able to give one candidate all three votes – Mark OD Apr 15 '18 at 8:46
  • You would need another storage arrangement and another requirement in the check. You could consider a mapping using hashed voter+vote for keys to ensure the same vote is not cast twice. – Rob Hitchens - B9lab Apr 15 '18 at 18:09

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.