Rise In Logo



Learn everything about Solidity

In this final part of the tutorial, you will be finalizing your Proposal Contract.

Let's start with a function to terminate a proposal.

function teminateProposal() external onlyOwner active {
    proposal_history[counter].is_active = false;
}

This function terminates the current proposal. It can only be called by the owner of the contract and the current proposal should be active to run this function which is indicated by active modifier.

Now, you will implement 3 query functions to retrieve data from the blockchain. Remember, the person/contract that call a query function does not pay gas fees.

  • The following function, will retrieve the information whether a given address has voted or not.
  • To figure this out, you will iterate through voted_addresses array and look for any matches with the given address parameter. Here is the implementation:
function isVoted(address _address) public view returns (bool) {
    for (uint i = 0; i < voted_addresses.length; i++) {
        if (voted_addresses[i] == _address) {
            return true;
        }
    }
    return false;
}
  • Next, you will implement a basic getter function to retrieve the current proposal:
function getCurrentProposal() external view returns(Proposal memory) {
    return proposal_history[counter];
}

Memory refers to a temporary location where data can be stored. It's erased between (external) function calls and is cheaper to use than storage. You may think it as the RAM of the EVM.

  • Finally, let's implement our a function to get a specific proposal:
function getProposal(uint256 number) external view returns(Proposal memory) {
    return proposal_history[number];
}

Congrulations, now you can look through the window thinking how did you became a smart contract developer!

On the next page, you can find the full contract ✌️

Comments

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!

Disclaimer: The information /programs / events provided on https://patika.dev and https://risein.com are strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice and do not make any representations regarding the value, profitability, or future price of any blockchain or cryptocurrency. Users are encouraged to conduct their own research and consult with licensed financial professionals before engaging in any investment activities. https://patika.dev and https://risein.com disclaim any responsibility for financial decisions made by users based on information provided here.