2020-06-02 13:49:24 +00:00
|
|
|
pragma solidity ^0.6.8;
|
|
|
|
|
|
|
|
import "@openzeppelin/contracts/math/SafeMath.sol";
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
|
|
|
|
|
2020-06-03 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @title UniversalERC20 library
|
|
|
|
* @author Aave inspired by @k06a (Anton Bukov)
|
2020-06-03 11:08:22 +00:00
|
|
|
* original version: https://github.com/CryptoManiacsZone/1inchProtocol/blob/master/contracts/UniversalERC20.sol
|
2020-06-03 11:02:18 +00:00
|
|
|
* @dev Provides unified interface for ERC20 and native ETH operations
|
|
|
|
**/
|
2020-06-02 13:49:24 +00:00
|
|
|
library UniversalERC20 {
|
|
|
|
using SafeMath for uint256;
|
|
|
|
using SafeERC20 for IERC20;
|
|
|
|
|
|
|
|
IERC20 private constant ZERO_ADDRESS = IERC20(
|
|
|
|
0x0000000000000000000000000000000000000000
|
|
|
|
);
|
2020-06-03 11:02:18 +00:00
|
|
|
// @notice mock address of ETH
|
2020-06-02 13:49:24 +00:00
|
|
|
IERC20 private constant ETH_ADDRESS = IERC20(
|
|
|
|
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
|
|
|
|
);
|
|
|
|
|
2020-06-03 12:48:59 +00:00
|
|
|
uint256 private constant DEFAULT_TRANSFER_GAS = 50000;
|
|
|
|
|
2020-06-03 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @dev Moves amount of asset from caller to recipient
|
|
|
|
* @param token underlying asset address
|
|
|
|
* @param to asset recipient
|
|
|
|
* @param amount to move
|
|
|
|
**/
|
2020-06-02 13:49:24 +00:00
|
|
|
function universalTransfer(
|
|
|
|
IERC20 token,
|
|
|
|
address to,
|
|
|
|
uint256 amount
|
2020-06-03 11:02:18 +00:00
|
|
|
) internal {
|
2020-06-02 13:49:24 +00:00
|
|
|
if (amount == 0) {
|
2020-06-03 11:02:18 +00:00
|
|
|
return;
|
2020-06-02 13:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isETH(token)) {
|
2020-06-03 12:48:59 +00:00
|
|
|
(bool result, ) = payable(to).call{
|
|
|
|
value: amount,
|
|
|
|
gas: DEFAULT_TRANSFER_GAS
|
|
|
|
}("");
|
2020-06-02 15:16:27 +00:00
|
|
|
require(result, "ETH_TRANSFER_FAILED");
|
2020-06-02 13:49:24 +00:00
|
|
|
} else {
|
|
|
|
token.safeTransfer(to, amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @dev Moves amount of asset from sender to recipient
|
2020-06-03 12:48:59 +00:00
|
|
|
* in terms of ETH it redirects amount in transaction to recipient
|
2020-06-03 11:02:18 +00:00
|
|
|
* @param token underlying asset address
|
|
|
|
* @param from asset sender
|
|
|
|
* @param to asset recipient
|
|
|
|
* @param amount to move
|
|
|
|
* @param returnExcess if true returns exceeded amount to sender
|
|
|
|
**/
|
2020-06-02 13:49:24 +00:00
|
|
|
function universalTransferFrom(
|
|
|
|
IERC20 token,
|
|
|
|
address from,
|
|
|
|
address to,
|
|
|
|
uint256 amount,
|
|
|
|
bool returnExcess
|
|
|
|
) internal {
|
|
|
|
if (amount == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isETH(token)) {
|
|
|
|
require(
|
|
|
|
msg.value >= amount,
|
|
|
|
"Wrong usage of ETH.universalTransferFrom()"
|
|
|
|
); // TODO: think one more time from == msg.sender
|
|
|
|
if (to != address(this)) {
|
2020-06-03 12:48:59 +00:00
|
|
|
(bool result, ) = payable(to).call{
|
|
|
|
value: amount,
|
|
|
|
gas: DEFAULT_TRANSFER_GAS
|
|
|
|
}("");
|
2020-06-02 15:16:27 +00:00
|
|
|
require(result, "ETH_TRANSFER_FAILED");
|
2020-06-02 13:49:24 +00:00
|
|
|
}
|
|
|
|
if (returnExcess && msg.value > amount) {
|
2020-06-03 12:48:59 +00:00
|
|
|
(bool result, ) = payable(from).call{
|
2020-06-02 13:49:24 +00:00
|
|
|
value: msg.value.sub(amount),
|
2020-06-03 12:48:59 +00:00
|
|
|
gas: DEFAULT_TRANSFER_GAS
|
2020-06-02 13:49:24 +00:00
|
|
|
}("");
|
2020-06-02 15:16:27 +00:00
|
|
|
require(result, "ETH_TRANSFER_FAILED");
|
2020-06-02 13:49:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
token.safeTransferFrom(from, to, amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @dev Moves amount of asset from caller to this contract
|
|
|
|
* @param token underlying asset address
|
|
|
|
* @param amount to move
|
|
|
|
**/
|
2020-06-04 07:57:08 +00:00
|
|
|
function universalTransferFromSenderToThis(IERC20 token, uint256 amount, bool returnExcess)
|
2020-06-02 13:49:24 +00:00
|
|
|
internal
|
|
|
|
{
|
|
|
|
if (amount == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isETH(token)) {
|
2020-06-04 07:57:08 +00:00
|
|
|
if (msg.value > amount && returnExcess) {
|
2020-06-02 13:49:24 +00:00
|
|
|
// Return remainder if exist
|
|
|
|
(bool result, ) = msg.sender.call{
|
|
|
|
value: msg.value.sub(amount),
|
|
|
|
gas: 50000
|
|
|
|
}("");
|
2020-06-02 15:16:27 +00:00
|
|
|
require(result, "ETH_TRANSFER_FAILED");
|
2020-06-02 13:49:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
token.safeTransferFrom(msg.sender, address(this), amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @dev Sets the allowance over the caller's tokens to recipient address.
|
|
|
|
* @param token underlying asset address
|
|
|
|
* @param to allowance recipient
|
|
|
|
* @param amount of the allowance
|
|
|
|
**/
|
2020-06-02 13:49:24 +00:00
|
|
|
function universalApprove(
|
|
|
|
IERC20 token,
|
|
|
|
address to,
|
|
|
|
uint256 amount
|
|
|
|
) internal {
|
|
|
|
if (!isETH(token)) {
|
|
|
|
if (amount > 0 && token.allowance(address(this), to) > 0) {
|
|
|
|
token.safeApprove(to, 0);
|
|
|
|
}
|
|
|
|
token.safeApprove(to, amount);
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 11:08:22 +00:00
|
|
|
|
2020-06-03 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @dev Returns the amount of underlying asset owned by address
|
|
|
|
* @param token underlying asset address
|
|
|
|
* @param who address to check
|
2020-06-03 11:08:22 +00:00
|
|
|
* @return balance of the who address
|
2020-06-03 11:02:18 +00:00
|
|
|
**/
|
2020-06-02 13:49:24 +00:00
|
|
|
function universalBalanceOf(IERC20 token, address who)
|
|
|
|
internal
|
|
|
|
view
|
|
|
|
returns (uint256)
|
|
|
|
{
|
|
|
|
if (isETH(token)) {
|
|
|
|
return who.balance;
|
|
|
|
} else {
|
|
|
|
return token.balanceOf(who);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @dev Returns decimals of underlying asset
|
|
|
|
* @param token underlying asset address
|
2020-06-03 11:08:22 +00:00
|
|
|
* @return decimals
|
2020-06-03 11:02:18 +00:00
|
|
|
**/
|
2020-06-02 13:49:24 +00:00
|
|
|
function universalDecimals(IERC20 token) internal view returns (uint256) {
|
|
|
|
if (isETH(token)) {
|
|
|
|
return 18;
|
|
|
|
}
|
|
|
|
|
|
|
|
(bool success, bytes memory data) = address(token).staticcall{
|
|
|
|
gas: 10000
|
|
|
|
}(abi.encodeWithSignature("decimals()"));
|
|
|
|
if (!success || data.length == 0) {
|
|
|
|
(success, data) = address(token).staticcall{gas: 10000}(
|
|
|
|
abi.encodeWithSignature("DECIMALS()")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (success && data.length > 0) ? abi.decode(data, (uint256)) : 18;
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @dev Checks is underlying asset ETH or not
|
|
|
|
* @param token underlying asset address
|
2020-06-03 11:08:22 +00:00
|
|
|
* @return boolean
|
2020-06-03 11:02:18 +00:00
|
|
|
**/
|
2020-06-02 13:49:24 +00:00
|
|
|
function isETH(IERC20 token) internal pure returns (bool) {
|
|
|
|
return (address(token) == address(ZERO_ADDRESS) ||
|
|
|
|
address(token) == address(ETH_ADDRESS));
|
|
|
|
}
|
|
|
|
}
|