eatthecode.com

Solidity require function

Solidity

Introduction

Solidity, blockchain, and a variety of other related concepts and technologies can become relatively new for novices, it may become difficult to grasp the process behind them. The Solidity programming language is also young. Various programming languages exist that we have not seen or heard about. This is the result of repeated discoveries and the development of new languages. Solidity is one of the best programming languages available for writing smart contracts time being.

Overview of Solidity programming language

  Solidity

Solidity

Solidity is an object-oriented programming language for implementing smart contracts on various blockchain platforms, most notably, Ethereum. It was developed by Christian Reitwiessner, Alex Beregszaszi, and several former Ethereum core contributors.

Require function

The require function takes a condition and message parameters. If the condition is true, the code will flow and logic continues. If the condition of require function is evaluated as false then the message will appear with executing the revert.

In the following cases, Solidity triggers a require-type of exception:

Smar contract example with require function

// an example of a smart contract

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

contract CustomerContract {
    // The Global public variables (make getters with "public")
    address public owner = msg.sender;
    uint256 public publicNumber;

    // Private variables
    uint256 private currentNumber;
    string private message;

    // A function to change the current number by any uint256 value. The stored number and the new number
    // should not be the same, If it is, it will fail with message "Identical numbers"
    // If the person who ask to change the number is not the contract owner, require function will fails
    // With returning error message "Only the owner who can update"
    function changeNumber(uint256 number) public {
        require(currentNumber != number, "Identical numbers");
        require(msg.sender != owner, "Only the owner who can update");

        currentNumber = number;
    }

    // This is how to return the current number.
    function getCurrentNumber() public view returns (uint256) {
        return currentNumber;
    }
}

 

Smar contract example with restricted and require function

// an example of a smart contract

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

contract NumberContract {
    // The Global public variables (make getters with "public")
    address public owner = msg.sender;
    uint256 public publicNumber;

    // Private variables
    uint256 private currentNumber;
    string private message;

    modifier restricted() {
        require(
            msg.sender == owner,
            "This function is restricted to the contract's owner"
        );
        _;
    }

    // A function to change the current number by any uint256 value. The stored number and the new number
    // should not be the same, If it is, it will fail with message "Identical numbers"
    // If the person who ask to change the number is not the contract owner, require function via restricted will fails
    // With returning error message "Only the owner who can update"
    function changeNumber(uint256 number) public restricted {
        require(currentNumber != number, "Identical numbers");

        currentNumber = number;
    }

    // This is how to return the current number.
    function getCurrentNumber() public view returns (uint256) {
        return currentNumber;
    }
}

Conclusion

The require function may be a confusing concept to some developers when writing smart contracts. In this post, I illustrated the require function when writing solidity smart contracts. Solidity is a programming language that is used in building smart contracts.

You can find the project source code at github Enjoy…!!!

I can help you to build such as software tools/snippets, you contact me from here  

 
Exit mobile version