123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- pragma solidity ^0.4.24;
- import "./ERC20.sol";
- import "./IERC20.sol";
- /**
- * @title SafeERC20
- * @dev Wrappers around ERC20 operations that throw on failure.
- * To use this library you can add a `using SafeERC20 for ERC20;` statement to your contract,
- * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
- */
- library SafeERC20 {
- using SafeMath for uint256;
- function safeTransfer(
- IERC20 token,
- address to,
- uint256 value
- )
- internal
- {
- require(token.transfer(to, value));
- }
- function safeTransferFrom(
- IERC20 token,
- address from,
- address to,
- uint256 value
- )
- internal
- {
- require(token.transferFrom(from, to, value));
- }
- function safeApprove(
- IERC20 token,
- address spender,
- uint256 value
- )
- internal
- {
- // safeApprove should only be called when setting an initial allowance,
- // or when resetting it to zero. To increase and decrease it, use
- // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
- require((value == 0) || (token.allowance(msg.sender, spender) == 0));
- require(token.approve(spender, value));
- }
- function safeIncreaseAllowance(
- IERC20 token,
- address spender,
- uint256 value
- )
- internal
- {
- uint256 newAllowance = token.allowance(address(this), spender).add(value);
- require(token.approve(spender, newAllowance));
- }
- function safeDecreaseAllowance(
- IERC20 token,
- address spender,
- uint256 value
- )
- internal
- {
- uint256 newAllowance = token.allowance(address(this), spender).sub(value);
- require(token.approve(spender, newAllowance));
- }
- }
|