Solidity ERC-20 – Why is from Balance and Require Check Not Within Unchecked Function in Latest OpenZeppelin ERC20 Transfer?

erc-20solidity

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?

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol#L236

Best 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;
    }
Related Topic