59 lines
1.6 KiB
Solidity
59 lines
1.6 KiB
Solidity
/*
|
|
|
|
Copyright 2020 DODO ZOO.
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
pragma solidity 0.6.9;
|
|
|
|
import {SafeMath} from "./SafeMath.sol";
|
|
import {IERC20} from "../intf/IERC20.sol";
|
|
import {SafeERC20} from "./SafeERC20.sol";
|
|
|
|
library UniversalERC20 {
|
|
using SafeMath for uint256;
|
|
using SafeERC20 for IERC20;
|
|
|
|
IERC20 private constant ZERO_ADDRESS = IERC20(0x0000000000000000000000000000000000000000);
|
|
IERC20 private constant ETH_ADDRESS = IERC20(0x000000000000000000000000000000000000000E);
|
|
|
|
function isETH(IERC20 token) internal pure returns (bool) {
|
|
return (token == ZERO_ADDRESS || token == ETH_ADDRESS);
|
|
}
|
|
|
|
|
|
function universalTransfer(IERC20 token, address payable to, uint256 amount) internal {
|
|
if (amount > 0) {
|
|
if (isETH(token)) {
|
|
to.transfer(amount);
|
|
} else {
|
|
token.safeTransfer(to, amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
function universalApprove(IERC20 token, address to, uint256 amount) internal {
|
|
require(!isETH(token), "ETH Don't need approve");
|
|
if (amount == 0) {
|
|
token.safeApprove(to, 0);
|
|
} else {
|
|
uint256 allowance = token.allowance(address(this), to);
|
|
if (allowance < amount) {
|
|
if (allowance > 0) {
|
|
token.safeApprove(to, 0);
|
|
}
|
|
token.safeApprove(to, amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
function universalBalanceOf(IERC20 token, address who) internal view returns (uint256) {
|
|
if (isETH(token)) {
|
|
return who.balance;
|
|
} else {
|
|
return token.balanceOf(who);
|
|
}
|
|
}
|
|
}
|