Trying to understand function transferFrom in solidity

Hi!
I'm a newbie. I have a question. Why function transferFrom in lib not check allowance before exec _transfer and require can revert data when the condition inside is failed?

the function here, code I get from github

function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);

        uint256 currentAllowance = _allowances[sender][_msgSender()];
        require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
        unchecked {
            _approve(sender, _msgSender(), currentAllowance - amount);
        }

        return true;
    }

Ths for you support.

If you look at the transfer function, you'll see that the checks you're looking for are located there.

Not sure I understand the part that reads require can revert data when the condition inside is failed, but if you're asking if the state changes will be undone, the answer is yes. See here for an (admittedly older, but still valid) description of what happens under the hood.

TL;DR:

  • Revert all state changes
  • Return any unused gas
  • (Optionally) Return a value
1 Like