question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ERC20._approve remove check

See original GitHub issue

🧐 Motivation Save gas for a condition requirement that could never be false

📝 Details in ERC20.sol you have requirement

require(owner != address(0), "ERC20: approve from the zero address");

here

    function _approve(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

it’s used here (but owner=_msgSender)

    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, amount);
        return true;
    }

and here (but owner=_msgSender)

    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, allowance(owner, spender) + addedValue);
        return true;
    }

and here (but owner=_msgSender)

    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        address owner = _msgSender();
        uint256 currentAllowance = allowance(owner, spender);
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(owner, spender, currentAllowance - subtractedValue);
        }

        return true;
    }

and here (but if you try to do transferFrom(address(0), msg.sender, 100) it will raise not enough allowance error)

    function _spendAllowance(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        uint256 currentAllowance = allowance(owner, spender);
        if (currentAllowance != type(uint256).max) {
            require(currentAllowance >= amount, "ERC20: insufficient allowance");
            unchecked {
                _approve(owner, spender, currentAllowance - amount);
            }
        }
    }

how you can see it’s not possible for this condition to be false, so you can safely remove it, or at least give motivation why this condition is in ERC20.sol

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Amxxcommented, Aug 11, 2022

Having both a public and an internal is confusing to some. Having an internal and a private is to much IMO.

A if (x == address(0)) check doesn’t include any sload. Its really cheap.

1reaction
Amxxcommented, Aug 8, 2022

Rather than just discussing if that is a legitimate thing to do, what we should all discuss “what would the consequence be if someone does that”. People do mistakes, and we need to thing if/how we can limit the impact of these mistakes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

why ERC20 standard doesn't have any method to remove the ...
But I am just thinking if the approve() is the method to add the allowance why the ERC20 standard doesn't have any method...
Read more >
ERC 20 - OpenZeppelin Docs
Emits an Approval event indicating the updated allowance. This is not required by the EIP. See the note at the beginning of ERC20...
Read more >
Bad Actors Abusing ERC20 Approval to Steal Your Tokens!
Exploiting token approvals is a clever approach because users generally think: “If they don't have my key then they can't sign a transaction,...
Read more >
what's the purpose of the approve function in erc 20
You can change the approved amount or revoke it altogether (only the unspent amount). But you cannot take back an already sent transfer....
Read more >
How can I revoke token approvals and permissions on ...
Press Connect to Web3 to connect your wallet. · Navigate through the ERC-20, ERC-721, or ERC-1155 tabs until you see the token approval...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found