Even though require()
is used substantially more than assert()
, there are places where assert()
gets used.
One example would be in the widely adopted SafeMath
library by OpenZeppelin:
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
where a + b
is checked after summation to prevent overflow.
It seems like require(c >= a)
can be used in place of assert(c >= a)
for same functionality with an additional opportunity to refund unused gas. Are there any reason not to use require()
here? Is assert()
used just to force people not to perform operations that result in bad consequences like overflow/underflow?
Cheers