DEV Community

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

Posted on

The Assert Statement in Solidity

The assert statement checks for internal errors and should only be used to examine invariants and test for internal problems

The assert statement in Solidity is used for ensuring that the code runs as expected. It can be used to check conditions and throw an error if the condition fails, allowing developers to catch potential errors before they become problems. This makes it invaluable when writing secure smart contracts on Ethereum.

The beauty of the assert statement lies in its simplicity; just one line of code can do so much! By using simple Boolean expressions, developers can quickly check whether certain conditions are met and take action accordingly without having to write long lines of complicated logic or debug complex functions. This helps make smart contract development faster, easier, and more reliable than ever before.

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. If debugging is needed, the sources could be recompiled with the same settings to figure out which assertion terminated. It is important to note that using assert() can consume all remaining gas and revert all changes made if it fails while using require() will only consume gas up until that point and revert state changes if it fails. Therefore, developers should use assert() only for internal errors or for analyzing invariants.

The assert statement is an essential part of developing secure smart contracts on Ethereum with Solidity programming language; it provides a quick way for developers to ensure their code works as intended while reducing complexity at every step along the way.

Its syntax is very similar to that of the require statement. Following the execution of the condition, it returns a Boolean value to the user. It will depend on the return value whether the application will continue to run or whether it will throw an exception. Instead of returning the unused gas, the assert statement uses the entire gas supply, causing the state to be reverted to its initial condition. Before executing the contract, Assert is used to verify that the current state and function circumstances are as expected.

The following are some examples of assert-type exceptions:

  • When an assert is called with a condition that results in false.

  • When a zero-initialized variable of a function is called.

  • When a large or a negative value is converted to an enum.

  • When a value is divided or modulo by zero.

  • When accessing an array in an index which is too big or negative.

pragma solidity ^0.8.0;
 
// Creating a contract
contract assertStatement {
   
    // Defining a state variable 
    bool result;
 
    // Defining a function to check condition
    function checkOverflow(uint num1, uint num2) public {
        uint sum = num1 + num2;
        assert(sum<=255);
        result = true;
    }
    
    // Defining a function to print result of assert statement
    function getResult() public view returns(string memory){
         if(result == true){
             return "No Overflow";
         }
         else{
             return "Overflow exist";
         }
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)