SafeERC20.sol 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../IERC20.sol";
  4. import "../../../utils/Address.sol";
  5. /**
  6. * @title SafeERC20
  7. * @dev Wrappers around ERC20 operations that throw on failure (when the token
  8. * contract returns false). Tokens that return no value (and instead revert or
  9. * throw on failure) are also supported, non-reverting calls are assumed to be
  10. * successful.
  11. * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
  12. * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
  13. */
  14. library SafeERC20 {
  15. using Address for address;
  16. function safeTransfer(IERC20 token, address to, uint256 value) internal {
  17. _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
  18. }
  19. function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
  20. _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
  21. }
  22. /**
  23. * @dev Deprecated. This function has issues similar to the ones found in
  24. * {IERC20-approve}, and its usage is discouraged.
  25. *
  26. * Whenever possible, use {safeIncreaseAllowance} and
  27. * {safeDecreaseAllowance} instead.
  28. */
  29. function safeApprove(IERC20 token, address spender, uint256 value) internal {
  30. // safeApprove should only be called when setting an initial allowance,
  31. // or when resetting it to zero. To increase and decrease it, use
  32. // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
  33. // solhint-disable-next-line max-line-length
  34. require((value == 0) || (token.allowance(address(this), spender) == 0),
  35. "SafeERC20: approve from non-zero to non-zero allowance"
  36. );
  37. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  38. }
  39. function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  40. uint256 newAllowance = token.allowance(address(this), spender) + value;
  41. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  42. }
  43. function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  44. unchecked {
  45. uint256 oldAllowance = token.allowance(address(this), spender);
  46. require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
  47. uint256 newAllowance = oldAllowance - value;
  48. _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  49. }
  50. }
  51. /**
  52. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  53. * on the return value: the return value is optional (but if data is returned, it must not be false).
  54. * @param token The token targeted by the call.
  55. * @param data The call data (encoded using abi.encode or one of its variants).
  56. */
  57. function _callOptionalReturn(IERC20 token, bytes memory data) private {
  58. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  59. // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
  60. // the target address contains contract code and also asserts for success in the low-level call.
  61. bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
  62. if (returndata.length > 0) { // Return data is optional
  63. // solhint-disable-next-line max-line-length
  64. require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
  65. }
  66. }
  67. }