0

Assume balanceOf[msg.sender] is typed as uint256, and a user wants to withdraw _amount units from her balances (_amount is also typed uint256). In that case

balanceOf[msg.sender] -= _amount;

will revert if balanceOf[msg.sender] < _amount. Does this make the following requirement unnecessary?

require(balanceOf[msg.sender] >= _amount);

It now seems redundant.

2
  • I looked at OpenZeppelin's latest ERC20 token contract, and see the have uint256 fromBalance = _balances[from]; require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); unchecked { _balances[from] = fromBalance - amount; } I presume this is best practice, but I can't understand why. First, they maintain the seemingly redundant check require(fromBalance >= amount), but this become relevant because they then apply the decrement using 'unchecked,' which turns off the math overflow reversion built int to Solidity 8.0. Apr 21, 2022 at 14:33
  • Yes, that check is now redundant, though it doesn't hurt to perform it so you can throw a custom error. Check out my modern implementation of ERC-20. Apr 21, 2022 at 17:42

1 Answer 1

0

I discovered that if you use the OpenZeppelin method you save 300 to 2000 gas. For some reason the internal math overflow method is not very efficient, so if you do not mind wordiness, you add the 'unchecked' to a 'require' statement.

3
  • What you can "inefficient" is the default checks for arithmetic overflows which have been added in v0.8. You are still supposed to handle overflows if you use unchecked. See What is the purpose of "unchecked" in Solidity?. Apr 21, 2022 at 17:44
  • Also, a side note. Consider using custom errors instead of revert reason strings. Apr 21, 2022 at 17:45
  • By "inefficient" I mean the OpenZeppelin method does the same thing--checks balances, runs unchecked--but with less gas. I presume that's why they do it that way. Apr 22, 2022 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.