SafeERC20.sol 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (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. function safeTransfer(IERC20 token, address to, uint256 value) internal {
  19. _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
  20. }
  21. function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
  22. _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
  23. }
  24. /**
  25. * @dev Deprecated. This function has issues similar to the ones found in
  26. * {IERC20-approve}, and its usage is discouraged.
  27. *
  28. * Whenever possible, use {safeIncreaseAllowance} and
  29. * {safeDecreaseAllowance} instead.
  30. */
  31. function safeApprove(IERC20 token, address spender, uint256 value) internal {
  32. // safeApprove should only be called when setting an initial allowance,
  33. // or when resetting it to zero. To increase and decrease it, use
  34. // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
  35. require(
  36. (value == 0) || (token.allowance(address(this), spender) == 0),
  37. "SafeERC20: approve from non-zero to non-zero allowance"
  38. );
  39. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  40. }
  41. function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  42. uint256 newAllowance = token.allowance(address(this), spender) + value;
  43. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  44. }
  45. function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  46. unchecked {
  47. uint256 oldAllowance = token.allowance(address(this), spender);
  48. require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
  49. uint256 newAllowance = oldAllowance - value;
  50. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  51. }
  52. }
  53. function safePermit(
  54. IERC20Permit token,
  55. address owner,
  56. address spender,
  57. uint256 value,
  58. uint256 deadline,
  59. uint8 v,
  60. bytes32 r,
  61. bytes32 s
  62. ) internal {
  63. uint256 nonceBefore = token.nonces(owner);
  64. token.permit(owner, spender, value, deadline, v, r, s);
  65. uint256 nonceAfter = token.nonces(owner);
  66. require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
  67. }
  68. /**
  69. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  70. * on the return value: the return value is optional (but if data is returned, it must not be false).
  71. * @param token The token targeted by the call.
  72. * @param data The call data (encoded using abi.encode or one of its variants).
  73. */
  74. function _callOptionalReturn(IERC20 token, bytes memory data) private {
  75. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  76. // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
  77. // the target address contains contract code and also asserts for success in the low-level call.
  78. bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
  79. if (returndata.length > 0) {
  80. // Return data is optional
  81. require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
  82. }
  83. }
  84. }