Order of _transfer and _approve in transferFrom

Hi guys,
I have question, more for my understanding than anything.

Here’s the transferFrom function in the ERC20 standard:

function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, “ERC20: transfer amount exceeds allowance”));
return true;
}

Shouldn’t it be:

function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
uint256 decreasedAllowance = allowance(account, _msgSender()).sub(amount, “ERC20: burn amount exceeds allowance”);
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), decreasedAllowance );
return true;
}

(or even calling _approve before the _transfer)
So that the 1st line triggers the condition (SafeMath) that the msg.sender has enough allowance.

??

Note:
I also looked at the burnFrom function and it’s implemented with the decreasedAllowance 1st, then approve, then _burn.
I wonder why it’s not the case for transferFrom. (gas limits?)

Thanks for your explanations!

1 Like

Hi @Steven_Defiat,

Welcome to the community :wave:

Thanks for the question. The order of _transfer and _approve was discussed in the following issue:

Specifically:

In the case of ERC20 , the order is irrelevant because _transfer and _approve are independent and never call any other contract: they are executed atomically.

Let me know if you need more information.

Thanks a lot!
It makes perfect sense.

1 Like

Hi @Steven_Defiat,

Feel free to ask all the questions that you need.

If you have a moment, it would be great if you could introduce yourself.