SafeERC20.sol 5.9 KB

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