SafeERC20.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <0.8.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. We use {Address.functionCall} to perform this call, which verifies that
  58. // the target address contains contract code and also asserts for success in the low-level call.
  59. bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
  60. if (returndata.length > 0) { // Return data is optional
  61. // solhint-disable-next-line max-line-length
  62. require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
  63. }
  64. }
  65. }