mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
50 lines
2.0 KiB
Solidity
50 lines
2.0 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity 0.6.8;
|
|
|
|
import {IERC20} from "../interfaces/IERC20.sol";
|
|
import {SafeMath} from "../libraries/math/SafeMath.sol";
|
|
import {Address} from "./Address.sol";
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
library SafeERC20 {
|
|
using SafeMath for uint256;
|
|
using Address for address;
|
|
|
|
function safeTransfer(IERC20 token, address to, uint256 value) internal {
|
|
callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
|
|
}
|
|
|
|
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
|
|
callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
|
|
}
|
|
|
|
function safeApprove(IERC20 token, address spender, uint256 value) internal {
|
|
require((value == 0) || (token.allowance(address(this), spender) == 0),
|
|
"SafeERC20: approve from non-zero to non-zero allowance"
|
|
);
|
|
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
|
|
}
|
|
|
|
function callOptionalReturn(IERC20 token, bytes memory data) private {
|
|
require(address(token).isContract(), "SafeERC20: call to non-contract");
|
|
|
|
// solhint-disable-next-line avoid-low-level-calls
|
|
(bool success, bytes memory returndata) = address(token).call(data);
|
|
require(success, "SafeERC20: low-level call failed");
|
|
|
|
if (returndata.length > 0) { // Return data is optional
|
|
// solhint-disable-next-line max-line-length
|
|
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
|
|
}
|
|
}
|
|
}
|