SafeERC20.sol 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. pragma solidity ^0.5.2;
  2. import "./IERC20.sol";
  3. import "../../math/SafeMath.sol";
  4. /**
  5. * @title SafeERC20
  6. * @dev Wrappers around ERC20 operations that throw on failure (when the token
  7. * contract returns false). Tokens that return no value (and instead revert or
  8. * throw on failure) are also supported, non-reverting calls are assumed to be
  9. * successful.
  10. * To use this library you can add a `using SafeERC20 for ERC20;` statement to your contract,
  11. * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
  12. */
  13. library SafeERC20 {
  14. using SafeMath for uint256;
  15. function safeTransfer(IERC20 token, address to, uint256 value) internal {
  16. callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
  17. }
  18. function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
  19. callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
  20. }
  21. function safeApprove(IERC20 token, address spender, uint256 value) internal {
  22. // safeApprove should only be called when setting an initial allowance,
  23. // or when resetting it to zero. To increase and decrease it, use
  24. // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
  25. require((value == 0) || (token.allowance(address(this), spender) == 0));
  26. callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  27. }
  28. function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  29. uint256 newAllowance = token.allowance(address(this), spender).add(value);
  30. callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  31. }
  32. function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  33. uint256 newAllowance = token.allowance(address(this), spender).sub(value);
  34. callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  35. }
  36. /**
  37. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  38. * on the return value: the return value is optional (but if data is returned, it must equal true).
  39. * @param token The token targeted by the call.
  40. * @param data The call data (encoded using abi.encode or one of its variants).
  41. */
  42. function callOptionalReturn(IERC20 token, bytes memory data) private {
  43. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  44. // we're implementing it ourselves.
  45. // solhint-disable-next-line avoid-low-level-calls
  46. (bool success, bytes memory returndata) = address(token).call(data);
  47. require(success);
  48. if (returndata.length > 0) {
  49. require(abi.decode(returndata, (bool)));
  50. }
  51. }
  52. }