SafeERC20.sol 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)
  3. pragma solidity ^0.8.0;
  4. import "../IERC20.sol";
  5. import "../extensions/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. /**
  19. * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
  20. * non-reverting calls are assumed to be successful.
  21. */
  22. function safeTransfer(IERC20 token, address to, uint256 value) internal {
  23. _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
  24. }
  25. /**
  26. * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
  27. * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
  28. */
  29. function safeTransferFrom(IERC20 token, address from, address to, uint256 value) 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(IERC20 token, address spender, uint256 value) internal {
  40. // safeApprove should only be called when setting an initial allowance,
  41. // or when resetting it to zero. To increase and decrease it, use
  42. // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
  43. require(
  44. (value == 0) || (token.allowance(address(this), spender) == 0),
  45. "SafeERC20: approve from non-zero to non-zero allowance"
  46. );
  47. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  48. }
  49. /**
  50. * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
  51. * non-reverting calls are assumed to be successful.
  52. */
  53. function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  54. uint256 oldAllowance = token.allowance(address(this), spender);
  55. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
  56. }
  57. /**
  58. * @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
  59. * non-reverting calls are assumed to be successful.
  60. */
  61. function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  62. unchecked {
  63. uint256 oldAllowance = token.allowance(address(this), spender);
  64. require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
  65. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
  66. }
  67. }
  68. /**
  69. * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
  70. * non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
  71. * to be set to zero before setting it to a non-zero value, such as USDT.
  72. */
  73. function forceApprove(IERC20 token, address spender, uint256 value) internal {
  74. bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
  75. if (!_callOptionalReturnBool(token, approvalCall)) {
  76. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
  77. _callOptionalReturn(token, approvalCall);
  78. }
  79. }
  80. /**
  81. * @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
  82. * Revert on invalid signature.
  83. */
  84. function safePermit(
  85. IERC20Permit token,
  86. address owner,
  87. address spender,
  88. uint256 value,
  89. uint256 deadline,
  90. uint8 v,
  91. bytes32 r,
  92. bytes32 s
  93. ) internal {
  94. uint256 nonceBefore = token.nonces(owner);
  95. token.permit(owner, spender, value, deadline, v, r, s);
  96. uint256 nonceAfter = token.nonces(owner);
  97. require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
  98. }
  99. /**
  100. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  101. * on the return value: the return value is optional (but if data is returned, it must not be false).
  102. * @param token The token targeted by the call.
  103. * @param data The call data (encoded using abi.encode or one of its variants).
  104. */
  105. function _callOptionalReturn(IERC20 token, bytes memory data) private {
  106. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  107. // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
  108. // the target address contains contract code and also asserts for success in the low-level call.
  109. bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
  110. require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
  111. }
  112. /**
  113. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  114. * on the return value: the return value is optional (but if data is returned, it must not be false).
  115. * @param token The token targeted by the call.
  116. * @param data The call data (encoded using abi.encode or one of its variants).
  117. *
  118. * This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
  119. */
  120. function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
  121. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  122. // we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
  123. // and not revert is the subcall reverts.
  124. (bool success, bytes memory returndata) = address(token).call(data);
  125. return
  126. success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
  127. }
  128. }