Files
dodo-contractV2/contracts/SmartRoute/lib/UniversalERC20.sol

64 lines
1.6 KiB
Solidity
Raw Normal View History

2020-11-09 14:26:38 +08:00
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
2020-11-28 21:57:09 +08:00
import {SafeMath} from "../../lib/SafeMath.sol";
import {IERC20} from "../../intf/IERC20.sol";
import {SafeERC20} from "../../lib/SafeERC20.sol";
2020-11-09 14:26:38 +08:00
library UniversalERC20 {
using SafeMath for uint256;
using SafeERC20 for IERC20;
2020-11-27 15:35:36 +08:00
IERC20 private constant ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
2020-11-09 14:26:38 +08:00
2020-11-28 17:44:39 +08:00
function universalTransfer(
IERC20 token,
address payable to,
uint256 amount
) internal {
2020-11-18 21:47:06 +08:00
if (amount > 0) {
if (isETH(token)) {
to.transfer(amount);
2020-11-09 14:26:38 +08:00
} else {
2020-11-18 21:47:06 +08:00
token.safeTransfer(to, amount);
2020-11-09 14:26:38 +08:00
}
}
}
2020-11-28 17:44:39 +08:00
function universalApproveMax(
IERC20 token,
address to,
uint256 amount
) internal {
2020-11-27 15:35:36 +08:00
uint256 allowance = token.allowance(address(this), to);
if (allowance < amount) {
if (allowance > 0) {
token.safeApprove(to, 0);
}
2020-11-28 17:44:39 +08:00
token.safeApprove(to, uint256(-1));
2020-11-27 15:35:36 +08:00
}
}
2020-11-09 14:26:38 +08:00
function universalBalanceOf(IERC20 token, address who) internal view returns (uint256) {
2020-11-18 21:47:06 +08:00
if (isETH(token)) {
2020-11-09 14:26:38 +08:00
return who.balance;
} else {
return token.balanceOf(who);
}
}
2020-11-30 16:08:11 +08:00
2020-12-01 01:47:22 +08:00
function tokenBalanceOf(IERC20 token, address who) internal view returns (uint256) {
return token.balanceOf(who);
}
2020-11-30 16:08:11 +08:00
function isETH(IERC20 token) internal pure returns (bool) {
return token == ETH_ADDRESS;
}
}