SafeERC20.sol 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "./IERC20.sol";
  4. import "../../math/SafeMath.sol";
  5. import "../../utils/Address.sol";
  6. /**
  7. * @title SafeERC20
  8. * @dev Wrappers around ERC20 operations that throw on failure (when the token
  9. * contract returns false). Tokens that return no value (and instead revert or
  10. * throw on failure) are also supported, non-reverting calls are assumed to be
  11. * successful.
  12. * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
  13. * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
  14. */
  15. library SafeERC20 {
  16. using SafeMath for uint256;
  17. using Address for address;
  18. function safeTransfer(IERC20 token, address to, uint256 value) internal {
  19. _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
  20. }
  21. function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
  22. _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
  23. }
  24. /**
  25. * @dev Deprecated. This function has issues similar to the ones found in
  26. * {IERC20-approve}, and its usage is discouraged.
  27. *
  28. * Whenever possible, use {safeIncreaseAllowance} and
  29. * {safeDecreaseAllowance} instead.
  30. */
  31. function safeApprove(IERC20 token, address spender, uint256 value) internal {
  32. // safeApprove should only be called when setting an initial allowance,
  33. // or when resetting it to zero. To increase and decrease it, use
  34. // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
  35. // solhint-disable-next-line max-line-length
  36. require((value == 0) || (token.allowance(address(this), spender) == 0),
  37. "SafeERC20: approve from non-zero to non-zero allowance"
  38. );
  39. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  40. }
  41. function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  42. uint256 newAllowance = token.allowance(address(this), spender).add(value);
  43. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  44. }
  45. function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  46. uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
  47. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  48. }
  49. /**
  50. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  51. * on the return value: the return value is optional (but if data is returned, it must not be false).
  52. * @param token The token targeted by the call.
  53. * @param data The call data (encoded using abi.encode or one of its variants).
  54. */
  55. function _callOptionalReturn(IERC20 token, bytes memory data) private {
  56. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  57. // we're implementing it ourselves.
  58. // A Solidity high level call has three parts:
  59. // 1. The target address is checked to verify it contains contract code
  60. // 2. The call itself is made, and success asserted
  61. // 3. The return value is decoded, which in turn checks the size of the returned data.
  62. // solhint-disable-next-line max-line-length
  63. require(address(token).isContract(), "SafeERC20: call to non-contract");
  64. // solhint-disable-next-line avoid-low-level-calls
  65. (bool success, bytes memory returndata) = address(token).call(data);
  66. require(success, "SafeERC20: low-level call failed");
  67. if (returndata.length > 0) { // Return data is optional
  68. // solhint-disable-next-line max-line-length
  69. require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
  70. }
  71. }
  72. }