Require in Solidity

What is Require in Solidity?

As the word suggests, require basically means to demand something before availing the service to the users. For example, websites require you to create an account or login into an existing account before giving you access to the account which belongs to you. This is the basic flow of how 'require' works in Solidity also. Suppose there are is an account used by Suyash and he wants to send five Ethereum to an account used by Aditya. Now in order for Suyash to send Ethereum to Aditya, Suyash should have at least five Ethereum in his account to send the money to Aditya. So in the Solidity code, we can "require" the sender's balance to be greater than or equal to the balance he wants to send to the receiver otherwise the transaction should obviously fail. This is just an example of the numerous use cases that use "require" and enable us to perform multiple checks before approving a transaction or anything else.

The above was just a real-life example, let's take a look at how it works in technical terms. 'require' returns two boolean values that are either true or false, if the specified condition returns a true value it allows the code to flow and function accordingly. If the value returned is false, it throws an error and stops the code right there. "require" takes two parameters, first is the condition that you want to check and the second is an optional error message you want to show to the user when and if the value returned is false.

Let's take a look at how to use "require" in Solidity

pragma solidity ^0.8.4; 

contract Bank { 
mapping(address => uint) balance;
address owner; 
constructor() { 
owner = msg.sender; 
// address that deploys contract will be the owner 
} 

function addBalance(uint _toAdd) public returns(uint) {
 require(msg.sender == owner);
 balance[msg.sender] += _toAdd; 
 return balance[msg.sender]; 
} 

function getBalance() public view returns(uint) {
 return balance[msg.sender]; 
} 

function transfer(address recipient, uint amount) public { 
require(balance[msg.sender]>=amount, "Insufficient Balance"); 
require(msg.sender != recipient, "You can't send money to yourself!");
 _transfer(msg.sender, recipient, amount); 
} 

function _transfer(address from, address to, uint amount) private { 
balance[from] -= amount; balance[to] += amount; 
}

}

In the above code sample, we will take a look at multiple uses of 'require'. In the constructor, we are setting the owner of the contract to be the address that deploys the contract. In the addBalance() function, we are using require to ensure that only the owner of the smart contract can add balance to the contract and no one else can. In the transfer() function we are using require twice. The first time is to check the sender's balance and ensure that it is above the required balance. The second time we are making sure that the user cannot send money to himself. In both of these "require" calls, we are using the second parameter to show the error to the user in case the 'require' returns a false value.

If you have any more questions you can find me on my Twitter and LinkedIn