fix test
This commit is contained in:
59
contracts/SmartRoute/lib/UniversalERC20.sol
Normal file
59
contracts/SmartRoute/lib/UniversalERC20.sol
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {SafeERC20} from "../../lib/SafeERC20.sol";
|
||||
|
||||
library UniversalERC20 {
|
||||
using SafeMath for uint256;
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
IERC20 private constant ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
|
||||
|
||||
function isETH(IERC20 token) internal pure returns (bool) {
|
||||
return 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 universalApproveMax(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
uint256 allowance = token.allowance(address(this), to);
|
||||
if (allowance < amount) {
|
||||
if (allowance > 0) {
|
||||
token.safeApprove(to, 0);
|
||||
}
|
||||
token.safeApprove(to, uint256(-1));
|
||||
}
|
||||
}
|
||||
|
||||
function universalBalanceOf(IERC20 token, address who) internal view returns (uint256) {
|
||||
if (isETH(token)) {
|
||||
return who.balance;
|
||||
} else {
|
||||
return token.balanceOf(who);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user