SafeERC20.sol 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. pragma solidity ^0.5.2;
  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. require((value == 0) || (token.allowance(address(this), spender) == 0));
  28. callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
  29. }
  30. function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  31. uint256 newAllowance = token.allowance(address(this), spender).add(value);
  32. callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  33. }
  34. function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
  35. uint256 newAllowance = token.allowance(address(this), spender).sub(value);
  36. callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
  37. }
  38. /**
  39. * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
  40. * on the return value: the return value is optional (but if data is returned, it must not be false).
  41. * @param token The token targeted by the call.
  42. * @param data The call data (encoded using abi.encode or one of its variants).
  43. */
  44. function callOptionalReturn(IERC20 token, bytes memory data) private {
  45. // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
  46. // we're implementing it ourselves.
  47. // A Solidity high level call has three parts:
  48. // 1. The target address is checked to verify it contains contract code
  49. // 2. The call itself is made, and success asserted
  50. // 3. The return value is decoded, which in turn checks the size of the returned data.
  51. require(address(token).isContract());
  52. // solhint-disable-next-line avoid-low-level-calls
  53. (bool success, bytes memory returndata) = address(token).call(data);
  54. require(success);
  55. if (returndata.length > 0) { // Return data is optional
  56. require(abi.decode(returndata, (bool)));
  57. }
  58. }
  59. }