Solidity Require

whiteboard crypto logo
Published by:
Whiteboard Crypto

The require statement in Solidity is used to check that a conditional is true before continuing with future code. It takes 2 arguments, including the conditional to check, and the error message given when the conditional is false. Here’s an example of when you should use a require statement in Solidity:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract requireExample{

    address public owner = 0xdead2e4d46B25Fe98626a7F386108b7C99251eaF;

    function example() public returns (bool){
        require(owner == msg.sender, "You are not the owner");
        require(1==1, "There's a glitch in the matrix")
        return true;

You can see here the first thing this contract does is create a new state variable address named “owner” and sets it to an address that I generated. In the function named “example”, the very first line is a require statement that checks if the sender of the message is the same address as the owner variable we just created.

If it is, the next line of code will be ran, however if you run this code, it is very unlikely that you own that address, so it should fail. If you replace that address with your address, then recompile and redeploy the contract, it should pass this first require statement!

After we pass the first require statement, you can see there is a second require statement that is very simple and checks that 1 is equal to 1. This should always be true, and if for some reason it isn’t, it throws the error “There’s a glitch in the matrix” so the developer can find why the transaction failed. Since 1 should always equal 1, if you replaced the address earlier, we should finally reach the return statement successfully.

“Require” is one of many ways to check for things and throw errors in Solidity. Revert, Assert, and Throw are 3 other error handling functions you can utilize. Revert is a simple function that can be called when a portion of code is reached to force the transaction to fail.

Assert is very similar to require in that it also takes a conditional and returns an error, however the most popular use of it is to validate inputs and ensure that some important state variables have remain unchanged.

One way to think about the difference between Assert and Require is to think about them as confrontational and non-confrontational lines of code. Require is non-confrontational, while Assert is confrontational and should never be false. Before the Byzantium update, Assert and Require operated the same, however their opcodes were different.

Revert, on the other hand, is much nicer than both of these since it refunds gas paid and throws an error message.

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.