Select Page

Assert vs. require in Solidity

by | Dec 28, 2022

In Solidity, assert and require are two functions that are used to enforce conditions or constraints within a contract. Both functions are used to check if a given condition is true, and will cause the contract to terminate if the condition is not met. In this post we break down when to use assert vs. require in Solidity.

Both assert() and require() are part of the Solidity control structures. However, there are some key differences between the two functions in terms of their behavior and intended use.

  • assert: The assert function is used to perform internal checks within a contract and is typically used to enforce invariants (conditions that must always be true). If the condition being checked by assert is not met, the contract will terminate and all state changes made within the transaction will be rolled back. assert is intended to be used for debugging and testing purposes and should not be used to enforce business logic.
  • require: The require function is similar to assert, but is intended to be used to enforce business logic and ensure that a contract’s external dependencies are satisfied. If the condition being checked by require is not met, the contract will also terminate, but state changes made within the transaction will not be rolled back. require is intended to be used to enforce conditions that must be met in order to execute a contract’s functionality.

Using ‘assert’ in Solidity

Here is an example of using the assert function in Solidity:

pragma solidity ^0.6.12;

contract AssertExample {
    function exampleFunction(uint256 x) public {
        // Assert that x is greater than 0
        assert(x > 0);

        // Do some other processing here
        // ...
    }
}




In this example, the assert function is used to check that the input value x is greater than 0. If x is not greater than 0, the assert function will throw an exception and the contract will terminate.

It’s important to note that the assert function should only be used for internal error checking and should not be used to enforce business logic. In production contracts, it is generally recommended to use more robust error handling mechanisms such as require or revert.

Using ‘require’ in Solidity

Here is an example of using the require function in Solidity:

pragma solidity ^0.6.12;

contract RequireExample {
    function exampleFunction(uint256 x) public {
        // Require that x is greater than 0
        require(x > 0, "x must be greater than 0");

        // Do some other processing here
        // ...
    }
}

In this example, the require function is used to check that the input value x is greater than 0. If x is not greater than 0, the require function will throw an exception and revert the state of the contract to its previous state. This can be useful for enforcing business logic and ensuring that the contract’s state remains valid.

It’s important to note that the require function should be used to enforce external invariants and should not be used for internal error checking. For internal error checking, it is generally recommended to use the assert function.

Assert vs. require: which one should you use?

In general, assert should be used for internal checks and debugging, while require should be used to enforce business logic and ensure that a contract’s dependencies are satisfied. It is important to use these functions appropriately to ensure that a contract’s behavior is correct and that errors are handled correctly.

Looking to learn more about Solidity? Get started here.

Looking for more Solidity content?

Best Smart Contract Security Audit Teams

Best Smart Contract Security Audit Teams

Smart contract technology is revolutionizing the way we conduct business and transfer value online. One of the most important aspects of using smart contracts is ensuring their security and reliability. There are many smart contract security audit teams, but which one...

The Best ERC-20 Wallet For Developers

The Best ERC-20 Wallet For Developers

Ethereum is a decentralized platform that enables the creation of smart contracts and decentralized applications (dApps). The Ethereum platform uses its own cryptocurrency, Ether (ETH), to facilitate transactions and execute smart contracts. One of the most popular...

How To Perform A Web3 Security Risk Assessment

How To Perform A Web3 Security Risk Assessment

Performing a web3 security risk assessment is an essential step when running a Web3 product. Many companies fail to perform the proper security steps. Often, they get hacked and lose customer data and funds. In order to prevent your project from getting hacked, we...

Our Recommended Solidity Security Audit Process

Our Recommended Solidity Security Audit Process

A Solidity security audit is a vital step in deploying smart contracts. If you choose not to have your contracts audited, you are putting your community and reputation at risk. However, many audit firms do not perform the proper due diligence on your smart contracts....

What Is The Ethereum Virtual Machine?

What Is The Ethereum Virtual Machine?

The Ethereum Virtual Machine (EVM) is the runtime environment for smart contracts on the Ethereum blockchain. It is a software-based virtual machine that can execute code in the form of smart contracts on the Ethereum network. The EVM is designed to be...

Complete Guide To Learn Solidity

Complete Guide To Learn Solidity

Ready to learn Solidity? Whether you are a smart contract developer or simply want to add better understanding of the programming language to your toolbelt, this guide provides everything that you need to get started. Ready to learn Solidity? What is Solidity?...

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *