DEV Community

Cover image for The Require Statement in Solidity
Shlok Kumar
Shlok Kumar

Posted on

The Require Statement in Solidity

In Solidity, the require statement is used to check a condition and throw an exception if the condition is not met

The 'require' statements specify the conditions that must be met before the function can be executed, i.e., they specify the constraints that must be met before the code can be executed. This function only receives a single input and evaluates it to return a Boolean value; it also provides an option for a custom string message. If this is the case, an exception is thrown and the program is terminated. This results in the caller receiving any unused gas, as well as the state being reversed back to its previous condition.

This statement allows developers to create contracts that are secure and reliable, ensuring that all transactions within a contract will be executed as expected. Without this feature, it would be difficult for developers to guarantee their code's safety and accuracy.

The Require Statement works by verifying conditions before any transaction can take place within a contract. If these conditions are not met, then an error message will appear indicating which condition has been violated and preventing any further execution until the issue has been resolved. This ensures that no malicious or unintentional actions can occur during a transaction, making sure all users remain protected from potential harm or financial loss due to incorrect coding practices.
It returns two boolean values that are either true or false. If the specified condition returns a true value, it allows the code to continue executing. If it returns a false value, it throws an exception and reverts all changes made to the state of the contract.

Here's an example of using require in Solidity:

pragma solidity ^0.8.0; 
contract Example { 
    uint public x; 
    function setX(uint _x) public { 
        require(_x > 0, "Value of x must be greater than zero"); 
        x = _x;
    } 
} 
Enter fullscreen mode Exit fullscreen mode

In this example, we define a contract called Example with a public variable x. We also define a function called setX that takes an unsigned integer _x as input and sets the value of x to _x. However, before setting the value of x, we use require to check if _x is greater than zero. If _x is not greater than zero, then an exception is thrown with the message "Value of x must be greater than zero" and all changes made to the state of the contract are reverted. Note that using require instead of an if statement can help prevent errors by ensuring that certain conditions are met before continuing execution.

The Require Statement in Solidity is essential for providing safe transactions between users on Ethereum-based networks; without it there would be no way to ensure code security or accuracy when dealing with digital assets like cryptocurrency tokens or smart contracts themselves.

The following are some examples of situations in which the required sort of exception is triggered:

  • When the function require() is invoked with the specified arguments, the outcome is false.

  • When a function that was called by a message does not correctly terminate.

  • It occurs when a contract is made using the new keyword and the procedure is not complete successfully.

  • Any time a codeless contract is directed at an external function

  • Ethers are sent to the contract via the public getter method when this method is called.

  • When the .transfer() method does not work.

  • In the case of an assert that is called with a condition that results in a false result

  • It occurs when a function that has a zero-initialized variable is called.

  • It occurs when a process through which a big or a negative value is turned to an enumeration.

// Solidity program to demonstrate require statement
pragma solidity ^0.8.0;
 
// Creating a contract
contract requireStatement {
    
    // Defining function to check input
    function checkInput(uint input) public view returns(string memory){
        require(input >= 0, "invalid uint8");
        require(_input <= 255, "invalid uint8");
         
        return "Input is Uint8";
    }
    
    // Defining function to use require statement
    function Odd(uint input) public view returns(bool){
        require(input % 2 != 0);
        return true;
    }
}
Enter fullscreen mode Exit fullscreen mode

For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)