SafeERC20.sol 3.7 KB

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