update universalERC20 code
This commit is contained in:
@@ -18,48 +18,26 @@ library UniversalERC20 {
|
||||
IERC20 private constant ZERO_ADDRESS = IERC20(0x0000000000000000000000000000000000000000);
|
||||
IERC20 private constant ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
|
||||
|
||||
function universalTransfer(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
universalTransfer(token, to, amount, false);
|
||||
function isETH(IERC20 token) internal pure returns (bool) {
|
||||
return (token == ZERO_ADDRESS || token == ETH_ADDRESS);
|
||||
}
|
||||
|
||||
function universalTransfer(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount,
|
||||
bool mayFail
|
||||
) internal returns (bool) {
|
||||
if (amount == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (token == ZERO_ADDRESS || token == ETH_ADDRESS) {
|
||||
if (mayFail) {
|
||||
return address(uint160(to)).send(amount);
|
||||
function universalTransfer(IERC20 token, address payable to, uint256 amount) internal {
|
||||
if (amount > 0) {
|
||||
if (isETH(token)) {
|
||||
to.transfer(amount);
|
||||
} else {
|
||||
address(uint160(to)).transfer(amount);
|
||||
return true;
|
||||
token.safeTransfer(to, amount);
|
||||
}
|
||||
} else {
|
||||
token.safeTransfer(to, amount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function universalApprove(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
if (token != ZERO_ADDRESS && token != ETH_ADDRESS) {
|
||||
if (amount == 0) {
|
||||
token.safeApprove(to, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -70,31 +48,8 @@ library UniversalERC20 {
|
||||
}
|
||||
}
|
||||
|
||||
function universalTransferFrom(
|
||||
IERC20 token,
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
if (amount == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (token == ZERO_ADDRESS || token == ETH_ADDRESS) {
|
||||
require(from == msg.sender && msg.value >= amount, "msg.value is zero");
|
||||
if (to != address(this)) {
|
||||
address(uint160(to)).transfer(amount);
|
||||
}
|
||||
if (msg.value > amount) {
|
||||
msg.sender.transfer(msg.value.sub(amount));
|
||||
}
|
||||
} else {
|
||||
token.safeTransferFrom(from, to, amount);
|
||||
}
|
||||
}
|
||||
|
||||
function universalBalanceOf(IERC20 token, address who) internal view returns (uint256) {
|
||||
if (token == ZERO_ADDRESS || token == ETH_ADDRESS) {
|
||||
if (isETH(token)) {
|
||||
return who.balance;
|
||||
} else {
|
||||
return token.balanceOf(who);
|
||||
|
||||
Reference in New Issue
Block a user