In this part, you will be implementing an execute function to vote on a proposal.
Let's start by defining our function:
function vote(uint8 choice) external {
// Function logic
}
We will be using uint8 to represent the three different options we can have:
Let's move on with the function logic.
// First part
Proposal storage proposal = proposal_history[counter];
uint256 total_vote = proposal.approve + proposal.reject + proposal.pass;
// Second part
if (choice == 1) {
proposal.approve += 1;
proposal.current_state = calculateCurrentState();
} else if (choice == 2) {
proposal.reject += 1;
proposal.current_state = calculateCurrentState();
} else if (choice == 0) {
proposal.pass += 1;
proposal.current_state = calculateCurrentState();
}
// Third part
if ((proposal.total_vote_to_end - total_vote == 1) && (choice == 1 || choice == 2 || choice == 0)) {
proposal.is_active = false;
}
Let's break down the three parts of our function logic.
proposal_history mapping. Any change we make on the storage variable proposal will be reflect to the current proposal. Then you are calculating the total_vote so that at the end you can check if the total vote to end this tutorial has finished with this final vote or not.approve, we say proposal.approve += 1 which increases the number of approve votes by one. After incrementing the field we calculate the state of the proposal with the line proposal.current_state = calculateState();. The method calculateState() may seem new but do not worry, you have not implemented this method yet, you will implement it after finishing the vote function.active field to false.At this point, even though your syntax is correct, you have 2 big problems with your logic. Can you see them?
Swap insights and ask questions about “Learn everything about Solidity”.
Ask a question or share your thoughts about this lesson.