Math

In this section, you will find examples of a CosmWasm contract that demonstrates basic mathematical operations, including addition, subtraction, multiplication, division, modulo, and exponentiation.

The "execute" function showcased here accepts an enum of "ExecuteMsg," which encapsulates all the contract functions and matches them with the function the user is invoking.

pub fn execute(
    deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    msg: ExecuteMsg,
) -> Result<Response, ContractError> {
    match msg {
    ExecuteMsg::Operations { a, b } => execute_operations(deps,a,b),
}
}

The "execute" function receives an enum of "ExecuteMsg," which serves as a container for all the contract functions and matches them with the user's requested function. Subsequently, it invokes the "execute_operations" function:

pub fn execute_operations(deps: DepsMut, a: u128, b: u128) -> Result<Response, ContractError> {
        // Checking if numbers are not zero
        if a == 0 && b == 0 {
            return Err(ContractError::CanNotBeZero());
        }
        
        // Addition
        let addition_result = a + b;
        
        // Subtraction
        let subtraction_result = a - b;
        
        // Multiplication
        let multiplication_result = a * b;
        
        // Division
        let division_result = a / b;
        
        // Modulo
        let modulo_result = a % b;
        
        // Exponentiation
        let exponent: u32 = 3;
        let exponentiation_result: u128 = a.pow(exponent);
        
        // Create the response
        let response = OperationsResponse {
            addition_result,
            subtraction_result,
            multiplication_result,
            division_result,
            modulo_result,
            exponentiation_result,
        };
        
        // Fetching the state
        RESULT.load(deps.storage).unwrap();
        
        // Update the state
        RESULT.save(deps.storage, &response).unwrap();
        
        let res = Response::new().add_attributes(vec![
            ("action", "operations"),
            ("a", &a.to_string()),
            ("b", &b.to_string()),
            ("addition_res", &addition_result.to_string()),
            ("substraction_res", &subtraction_result.to_string()),
            ("multiplicationn_res", &multiplication_result.to_string()),
            ("division_res", &division_result.to_string()),
            ("modulo_res", &modulo_result.to_string()),
            ("exponential_res", &exponentiation_result.to_string()),
        ]);
        
        Ok(res)
    }

The execute_operations function accepts two parameters, 'a' and 'b,' for mathematical operations and stores the resulting value in the RESULT global state variable, which is located within the state.rs file:

You can utilize the following query endpoint to retrieve the results of mathematical operations:

The query function above accepts an enum of QueryMsg, which encapsulates all the contract query functions and aligns them with the user's requested function, specifically 'GetResponse' in our case. Subsequently, it invokes the get_response function:

The get_response function mentioned above returns the result of the mathematical operation.

Example

To employ mathematical operations within CosmWasm, you should generate the following files: lib.rs, contract.rs, msg.rs, error.rs, and state.rs.

lib.rs

contract.rs

msg.rs

error.rs

state.rs

Attribution: The code and examples are sourced from "CosmWasm by Example." You can review the code on GitHub.

Last updated

Was this helpful?