i have a question, the openzeppelin implementation of ERC20 (upgradeable) in their 5.0.2 version doesnt have safeTransfer
function. since some dapps using safeTransfer for safety in order to transfer tokens, i wanna know what was the logic behind this and also is it a good practice to add these functionalities (safeTransfer
and safeTransferFrom
) in the token contract?
1 Answer
From what I can tell, 5.0.2v of Openzeppelin safeTransfer (in SafeERC20 library) is made to increase compatibility with other token contracts while explicitly checking for successful execution of transfer.
It allows working with tokens that don't have return values (ie. return true) in their transfer functions, while still forcing return value to not be false if it exists.
It also checks if the address you are using is a token contract with transfer function in correct format.
It can be useful to implement, based on what you are trying to accomplish, but in a lot of cases its not needed, and removing it can save gas.
In ERC20 standard, transfer can only fail if sender doesn't have enough balance to send (depending on implementation it also fails if you are sending to prohibited address like address zero), so if balance is high enough its guaranteed to succeed without additional checks.
If it doesn't succeed it will revert by itself.
-
you can check their github, they dont implement the SafeTransfer anymore and there is no SafeERC20 implementation. openzeppelin Git my concern is that some smart contracts, use safeTransfer when they want to transfer token, this would not be backward compatible not to implement the safeTransfer. what was the logic of not implementing it in the new ERC20 implementation?– MostafaCommented Oct 23 at 8:50