我正在研究Uniswap V2代码,发现它使用_safeTransfer函数在对合同中传输ERC20令牌。
function _safeTransfer(address token, address to, uint value) private {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'UniswapV2: TRANSFER_FAILED');
}
...
function burn(address to) external lock returns (uint amount0, uint amount1) {
...
_safeTransfer(_token0, to, amount0);
...
}为什么它不简单地调用IERC20(_token).transfer(to, amount0)?
发布于 2022-10-21 00:29:33
因为ERC20标准有点糟糕,并且不清楚您应该如何处理不成功的令牌传输,所以技术上允许恢复和返回false,所以这两种情况都考虑到了。
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
// Case where there is no return data case where there is return data
require(success && (data.length == 0 || abi.decode(data, (bool))), 'UniswapV2: TRANSFER_FAILED');success为false,则恢复对令牌契约的调用,然后恢复整个事务,success为真且没有返回数据,则令牌合同在传输时不返回任何内容,并且传输成功,success为真,但有返回数据,则检查它是否为bool (如果不是我们所称的不符合ERC20标准的合同,则恢复 )https://ethereum.stackexchange.com/questions/137882
复制相似问题