Hi everyone, I've noticed that OZ ERC20 contracts have a "transfer" function that returns a bool. When I call this function from another contract should I check the result of that transfer? Like in this example:
require(token.transfer(_to, _amount), "Transfer failed");
Not directly, since some early ERC20 tokens have failed to fully comply with the ERC20 standard, namely, not returning a bool
result in some or all of the functions defined by the ERC20 standard.
If you rely on an ERC20 interface which declares a function returning a bool
result, but the actual function implemented in the ERC20 contract does not follow that, then your transaction may revert.
Instead of checking it directly as shown in your question, you can use SafeERC20:
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
1 Like
I would go beyond and say you should use SafeERC20
always. Otherwise your contract may not work with USDT.