If it is not possible to over/underflow in this transfer due to a check at the mint function, then why are these 2 lines (236/237) not within the unchecked field?
1 Answer
Those two lines do not perform an arithmetic operation, so moving them inside the unchecked
block doesn't provide any benefit. Perhaps it will make the code a little harder to read:
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
-
Yup you're right, I tried this in remix, the gas is exactly the same inside or outside.– NatCommented Oct 20, 2022 at 21:27
amount
is user supplied so it could be an invalid amount.