SafeERC20.sol 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0-rc.2) (token/ERC20/utils/SafeERC20.sol)
  3. pragma solidity ^0.8.0;
  4. import "../IERC20.sol";
  5. import "../extensions/draft-IERC20Permit.sol";
  6. import "../../../utils/Address.sol";
  7. /**
  8. * @title SafeERC20
  9. * @dev Wrappers around ERC20 operations that throw on failure (when the token
  10. * contract returns false). Tokens that return no value (and instead revert or
  11. * throw on failure) are also supported, non-reverting calls are assumed to be
  12. * successful.
  13. * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
  14. * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
  15. */
  16. library SafeERC20 {
  17. using Address for address;
  18. function safeTransfer(
  19. IERC20 token,
  20. address to,
  21. uint256 value
  22. ) internal {
  23. _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
  24. }
  25. function safeTransferFrom(
  26. IERC20 token,
  27. address from,
  28. address to,
  29. uint256 value
  30. ) internal {
  31. _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
  32. }
  33. /**
  34. * @dev Deprecated. This function has issues similar to the ones found in
  35. * {IERC20-approve}, and its usage is discouraged.
  36. *
  37. * Whenever possible, use {safeIncreaseAllowance} and
  38. * {safeDecreaseAllowance} instead.
  39. */
  40. function safeApprove(
  41. IERC20 token,
  42. address spender,
  43. uint256 value
  44. ) internal {
  45. // safeApprove should only be called when setting an initial allowance,
  46. // or when resetting it to zero. To increase and decrease it, use
  47. // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
  48. require(
  49. (value == 0) || (token.allowance(address(this), spender) == 0),
  50. "SafeERC20: approve from non-zero to non-zero allowance"
  51. );
  52. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  53. }
  54. function safeIncreaseAllowance(
  55. IERC20 token,
  56. address spender,
  57. uint256 value
  58. ) internal {
  59. uint256 newAllowance = token.allowance(address(this), spender) + value;
  60. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  61. }
  62. function safeDecreaseAllowance(
  63. IERC20 token,
  64. address spender,
  65. uint256 value
  66. ) internal {
  67. unchecked {
  68. uint256 oldAllowance = token.allowance(address(this), spender);
  69. require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
  70. uint256 newAllowance = oldAllowance - value;
  71. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  72. }
  73. }
  74. function safePermit(
  75. IERC20Permit token,
  76. address owner,
  77. address spender,
  78. uint256 value,
  79. uint256 deadline,
  80. uint8 v,
  81. bytes32 r,
  82. bytes32 s
  83. ) internal {
  84. uint256 nonceBefore = token.nonces(owner);
  85. token.permit(owner, spender, value, deadline, v, r, s);
  86. uint256 nonceAfter = token.nonces(owner);
  87. require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
  88. }
  89. /**
  90. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  91. * on the return value: the return value is optional (but if data is returned, it must not be false).
  92. * @param token The token targeted by the call.
  93. * @param data The call data (encoded using abi.encode or one of its variants).
  94. */
  95. function _callOptionalReturn(IERC20 token, bytes memory data) private {
  96. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  97. // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
  98. // the target address contains contract code and also asserts for success in the low-level call.
  99. bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
  100. if (returndata.length > 0) {
  101. // Return data is optional
  102. require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
  103. }
  104. }
  105. }