SafeERC20.sol 6.5 KB

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