SafeERC20.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.0-rc.1 (token/ERC20/utils/SafeERC20.sol)
  3. pragma solidity ^0.8.0;
  4. import "../IERC20.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 Address for address;
  17. function safeTransfer(
  18. IERC20 token,
  19. address to,
  20. uint256 value
  21. ) internal {
  22. _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
  23. }
  24. function safeTransferFrom(
  25. IERC20 token,
  26. address from,
  27. address to,
  28. uint256 value
  29. ) internal {
  30. _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
  31. }
  32. /**
  33. * @dev Deprecated. This function has issues similar to the ones found in
  34. * {IERC20-approve}, and its usage is discouraged.
  35. *
  36. * Whenever possible, use {safeIncreaseAllowance} and
  37. * {safeDecreaseAllowance} instead.
  38. */
  39. function safeApprove(
  40. IERC20 token,
  41. address spender,
  42. uint256 value
  43. ) internal {
  44. // safeApprove should only be called when setting an initial allowance,
  45. // or when resetting it to zero. To increase and decrease it, use
  46. // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
  47. require(
  48. (value == 0) || (token.allowance(address(this), spender) == 0),
  49. "SafeERC20: approve from non-zero to non-zero allowance"
  50. );
  51. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  52. }
  53. function safeIncreaseAllowance(
  54. IERC20 token,
  55. address spender,
  56. uint256 value
  57. ) internal {
  58. uint256 newAllowance = token.allowance(address(this), spender) + value;
  59. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  60. }
  61. function safeDecreaseAllowance(
  62. IERC20 token,
  63. address spender,
  64. uint256 value
  65. ) internal {
  66. unchecked {
  67. uint256 oldAllowance = token.allowance(address(this), spender);
  68. require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
  69. uint256 newAllowance = oldAllowance - value;
  70. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  71. }
  72. }
  73. /**
  74. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  75. * on the return value: the return value is optional (but if data is returned, it must not be false).
  76. * @param token The token targeted by the call.
  77. * @param data The call data (encoded using abi.encode or one of its variants).
  78. */
  79. function _callOptionalReturn(IERC20 token, bytes memory data) private {
  80. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  81. // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
  82. // the target address contains contract code and also asserts for success in the low-level call.
  83. bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
  84. if (returndata.length > 0) {
  85. // Return data is optional
  86. require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
  87. }
  88. }
  89. }