SafeERC20.sol 6.2 KB

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