SafeERC20.sol 3.7 KB

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