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
: Theassert
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 byassert
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
: Therequire
function is similar toassert
, 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 byrequire
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.
0 Comments