Rise In Logo

Build on Solana

In this homework, you will modify the functionality of increment and decrement functions. Right now, they are incrementing/decrementing the state by 1. Let’s make them get input from the user and increment/decrement the state accordingly.

Step 1

Add a u32 argument to both increment and decrement in, enum CounterInstructions. They should get a value as a parameter, just like the Update function.

Step 2

In the unpack function, you should unpack the u32 value for both increment and decrement, just like what we are doing in the Update function.

Step 3

In lib.rs, you need to modify the following lines, so that instead of incrementing/decrementing by 1, it should increment/decrement by the u32 parameter value.

    CounterInstructions::Increment => {
      counter_account.counter += 1;
    }
    CounterInstructions::Decrement => {
      counter_account.counter -= 1;
    }

Note: In CounterInstructions::Decrement, you need to be careful about the value being negative. Since it is defined as u32, it should not be less than 0. In case parameter is greater than actual state, it should set the state to 0

Project

Rise In Logo

Rise together in web3

Website

BootcampsWhat is the
Web3 Bootcamp?
CommunityPartner with UsBlogEvents

Bootcamps

Company

About UsJoin UsFAQTerms ConditionsPrivacy PolicyGDPR NoticeCookies