DEV Community

tawseef nabi
tawseef nabi

Posted on

Require vs Assert in Solidity

in Solidity version 0.4.10, the assert(), require()and revert() functions were introduced.
assert and require are convenience functions that check for conditions. In cases when conditions are not met, they throw exceptions.

  • require is used to validate inputs and conditions before execution.

  • assert is used to check for code that should never be false. Failing assertion probably means that there is a bug.

  • revert() is used abort execution and revert state changes

Syntax

assert(bool condition) causes a Panic error and thus state change reversion if the condition is not met - to be used for internal errors.

require(bool condition) reverts if the condition is not met - to be used for errors in inputs or external components.

require(bool condition, string memory message) reverts if the condition is not met - to be used for errors in inputs or external components. Also provides an error message.

revert() abort execution and revert state changes

revert(string memory reason) abort execution and revert state changes, providing an explanatory string

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Error {
    function testRequire(uint _i) public pure {
        // Require should be used to validate conditions such as:
        // - inputs
        // - conditions before execution
        // - return values from calls to other functions
        require(_i > 10, "Input must be greater than 10");
    }

    function testRevert(uint _i) public pure {
        // Revert is useful when the condition to check is complex.
        // This code does the exact same thing as the example above
        if (_i <= 10) {
            revert("Input must be greater than 10");
        }
    }

    uint public num;

    function testAssert() public view {
        // Assert should only be used to test for internal errors,
        // and to check invariants.

        // Here we assert that num is always equal to 0
        // since it is impossible to update the value of num
        assert(num == 0);
    }

    // custom error
    error InsufficientBalance(uint balance, uint withdrawAmount);

    function testCustomError(uint _withdrawAmount) public view {
        uint bal = address(this).balance;
        if (bal < _withdrawAmount) {
            revert InsufficientBalance({balance: bal, withdrawAmount: _withdrawAmount});
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

*Main Differences Between Assert vs Require In Solidity
*

  • ** Behavior of assert and require functions**

The assert() **and require() **functions are a part of the error handling aspect in Solidity. Solidity makes use of state-reverting error handling exceptions.

This means all changes made to the contract on that call or any sub-calls are undone if an error is thrown. It also flags an error.
They are quite similar as both check for conditions and if they are not met, would throw an error.

  • Gas Utility

The big difference between the two is that the **assert() **function when false, uses up all the remaining gas and reverts all the changes made.

Meanwhile, a** require() **function when false, also reverts back all the changes made to the contract but does refund all the remaining gas fees we offered to pay.
This is the most common Solidity function used by developers for debugging and error handling.

Conclusion

To handle errors, Solidity undoes changes that might have caused issues.

  • assert checks for internal errors. require analyzes conditions.
  • Exceptions that Solidity revert generates can contain error strings

Top comments (3)

Collapse
 
gldnwzrd profile image
WZRD

Concise! Great read.

Collapse
 
tawseef profile image
tawseef nabi

thanks.. trying to make it like that

Collapse
 
pfedprog profile image
Pavel Fedotov