2

According to the ERC20: Token Standard:

Callers MUST handle false from returns (bool success). Callers MUST NOT assume that false is never returned!

In OpenZeppelin's ERC20 contract, running the transfer function only seems to return true.

function transfer(address to, uint256 amount) public virtual override returns (bool) {
    address owner = _msgSender();
    _transfer(owner, to, amount);
    return true;
}

it also says that:

The function SHOULD throw if the message caller’s account balance does not have enough tokens to spend.

This is implemented in _transfer along with a few other require statements.

require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");

In every implementation I've seen of an ERC20 contract, there has been no instance of transfer containing code that would have it return false. It either returns true or reverts.

edit: Yes, I'm sure there are plenty of implementations of transfer returning false, I'm just saying from what I've seen.

  1. Does the token standard just say this in case someone decides to have transfer return false in some instance- since bool has the possibility to return false?

  2. Is it okay to assume transfer will never return false if the smart contract only deals with a specified token (that you know would never return false)?

  3. Is the point of SafeERC20 then to make sure an ERC20 would revert if it were to return false if your contract interacts with any number or ERC20s that potentially can return false?

Thanks

1 Answer 1

5

ERC20 Standard mentions handling false returns because some early token implementations return false instead of reverting.

OpenZeppelin's ERC20 contract returns true or reverts, which is a common practice. However, not every ERC20 token follows this approach.

SafeERC20 is a library that ensures consistent behavior when interacting with different ERC20 tokens. It makes sure that a token will revert if it doesn't return true, this is a safer way to interact with tokens that might have different implementations.

In summary, if you know the token you're working with will never return false, it's okay to assume transfer won't return false.

But if you're unsure or working with multiple tokens, using SafeERC20 is a good practice to ensure consistent and safe interactions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.