Rise In Logo





Solidity Fundamentals

Last lesson, we said that we had 2 problems in our logic.

  • The first problem is that a user can vote for a proposal even it is not active.
  • The second problem is that, a user can vote to the same proposal over and over again.

You will be solving this issues with the help of the modifiers.

The first modifier you are going to be creating will check whether the current proposal is active or not.

modifier active() {
    require(proposal_history[counter].is_active == true, "The proposal is not active");
    _;
}

The modifier checks whether the proposal is active or not. If the proposal is not active, it returns the message "The proposal is not active".

This modifier will check if the voter has voted before. But to check this you need a keep the addresses that has voted for our contract. We can achieve this with an array.

Let's create the following variable under the mapping.

address[]  private voted_addresses;

Next, you need to modify our constructor so that we can add the owner of the contract to the array since the owner of the contract will be the one who creates the proposals and we do not want him/her to vote on the contracts that himself/herself has created.

You will be adding the following line at the end of our contructor function:

voted_addresses.push(msg.sender);

Now, your constructor becomes:

constructor() {
    owner = msg.sender;
    voted_addresses.push(msg.sender);
}
Rise In Logo

Rise together in web3