current position:Home>See that many contracts use safetransfer instead of transfer to transfer tokens. Is it necessary?

See that many contracts use safetransfer instead of transfer to transfer tokens. Is it necessary?

2022-02-03 23:47:38 Q & A of Denglian community

The general problem of transfer is that the balance is not enough , When the balance is not enough safeMath Just report an error , Why use safeTransfer Well




Take the answer 1:

Regular transfers are like this :

bool success = token.transfer(addr, amount);

But there are some token Of transfer no return value ( for example USDT ), The above statement will not execute .

Instead, use the following safeTransfer, The above problems can be avoided .

    function safeTransfer(address token, address to, uint value) internal {        // bytes4(keccak256(bytes('transfer(address,uint256)')));        (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));        require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');    }



copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202032347363742.html

Random recommended