30 lines
1.4 KiB
Solidity
30 lines
1.4 KiB
Solidity
|
// SPDX-License-Identifier: CC0-1.0
|
||
|
|
||
|
pragma solidity ^0.8.0;
|
||
|
|
||
|
// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false
|
||
|
library TransferHelper {
|
||
|
function safeApprove(address token, address to, uint value) internal {
|
||
|
// bytes4(keccak256(bytes("approve(address,uint256)")));
|
||
|
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
|
||
|
require(success && (data.length == 0 || abi.decode(data, (bool))), "AF");
|
||
|
}
|
||
|
|
||
|
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))), "TF");
|
||
|
}
|
||
|
|
||
|
function safeTransferFrom(address token, address from, address to, uint value) internal {
|
||
|
// bytes4(keccak256(bytes("transferFrom(address,address,uint256)")));
|
||
|
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
|
||
|
require(success && (data.length == 0 || abi.decode(data, (bool))), "TFF");
|
||
|
}
|
||
|
|
||
|
function safeTransferETH(address to, uint value) internal {
|
||
|
(bool success,) = to.call{value:value}(new bytes(0));
|
||
|
require(success, "ETF");
|
||
|
}
|
||
|
}
|