2020-10-27 12:17:33 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
2020-11-25 14:07:33 +00:00
|
|
|
pragma solidity 0.6.12;
|
2020-10-27 12:17:33 +00:00
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
|
2020-11-25 14:07:33 +00:00
|
|
|
import {PercentageMath} from '../protocol/libraries/math/PercentageMath.sol';
|
2020-10-27 12:17:33 +00:00
|
|
|
import {SafeMath} from '../dependencies/openzeppelin/contracts/SafeMath.sol';
|
|
|
|
import {IERC20} from '../dependencies/openzeppelin/contracts/IERC20.sol';
|
2020-10-30 19:59:25 +00:00
|
|
|
import {IERC20Detailed} from '../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
|
2020-10-27 12:17:33 +00:00
|
|
|
import {SafeERC20} from '../dependencies/openzeppelin/contracts/SafeERC20.sol';
|
2020-12-03 11:13:06 +00:00
|
|
|
import {Ownable} from '../dependencies/openzeppelin/contracts/Ownable.sol';
|
2020-10-27 12:17:33 +00:00
|
|
|
import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol';
|
2020-11-25 14:07:33 +00:00
|
|
|
import {DataTypes} from '../protocol/libraries/types/DataTypes.sol';
|
2020-10-27 12:17:33 +00:00
|
|
|
import {IUniswapV2Router02} from '../interfaces/IUniswapV2Router02.sol';
|
|
|
|
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
|
2020-11-02 20:33:00 +00:00
|
|
|
import {IERC20WithPermit} from '../interfaces/IERC20WithPermit.sol';
|
2020-11-30 13:14:29 +00:00
|
|
|
import {FlashLoanReceiverBase} from '../flashloan/base/FlashLoanReceiverBase.sol';
|
|
|
|
import {IBaseUniswapAdapter} from './interfaces/IBaseUniswapAdapter.sol';
|
2020-10-27 12:17:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @title BaseUniswapAdapter
|
|
|
|
* @notice Implements the logic for performing assets swaps in Uniswap V2
|
|
|
|
* @author Aave
|
|
|
|
**/
|
2020-12-03 11:13:06 +00:00
|
|
|
abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapter, Ownable {
|
2020-10-27 12:17:33 +00:00
|
|
|
using SafeMath for uint256;
|
|
|
|
using PercentageMath for uint256;
|
|
|
|
using SafeERC20 for IERC20;
|
2020-10-30 19:59:25 +00:00
|
|
|
|
2020-11-03 15:22:15 +00:00
|
|
|
// Max slippage percent allowed
|
2020-11-30 13:14:29 +00:00
|
|
|
uint256 public constant override MAX_SLIPPAGE_PERCENT = 3000; // 30%
|
2020-11-04 19:51:21 +00:00
|
|
|
// FLash Loan fee set in lending pool
|
2020-11-30 13:14:29 +00:00
|
|
|
uint256 public constant override FLASHLOAN_PREMIUM_TOTAL = 9;
|
2020-11-04 19:51:21 +00:00
|
|
|
// USD oracle asset address
|
2020-11-30 13:14:29 +00:00
|
|
|
address public constant override USD_ADDRESS = 0x10F7Fc1F91Ba351f9C629c5947AD69bD03C05b96;
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-12-02 15:56:38 +00:00
|
|
|
address public immutable override WETH_ADDRESS;
|
2020-11-30 13:14:29 +00:00
|
|
|
IPriceOracleGetter public immutable override ORACLE;
|
|
|
|
IUniswapV2Router02 public immutable override UNISWAP_ROUTER;
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-12-02 15:56:38 +00:00
|
|
|
constructor(
|
|
|
|
ILendingPoolAddressesProvider addressesProvider,
|
|
|
|
IUniswapV2Router02 uniswapRouter,
|
|
|
|
address wethAddress
|
|
|
|
) public FlashLoanReceiverBase(addressesProvider) {
|
2020-10-30 19:59:25 +00:00
|
|
|
ORACLE = IPriceOracleGetter(addressesProvider.getPriceOracle());
|
|
|
|
UNISWAP_ROUTER = uniswapRouter;
|
2020-12-02 15:56:38 +00:00
|
|
|
WETH_ADDRESS = wethAddress;
|
2020-10-27 12:17:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 19:32:09 +00:00
|
|
|
/**
|
2020-11-30 13:14:29 +00:00
|
|
|
* @dev Given an input asset amount, returns the maximum output amount of the other asset and the prices
|
|
|
|
* @param amountIn Amount of reserveIn
|
|
|
|
* @param reserveIn Address of the asset to be swap from
|
|
|
|
* @param reserveOut Address of the asset to be swap to
|
|
|
|
* @return uint256 Amount out of the reserveOut
|
|
|
|
* @return uint256 The price of out amount denominated in the reserveIn currency (18 decimals)
|
|
|
|
* @return uint256 In amount of reserveIn value denominated in USD (8 decimals)
|
|
|
|
* @return uint256 Out amount of reserveOut value denominated in USD (8 decimals)
|
|
|
|
*/
|
|
|
|
function getAmountsOut(
|
|
|
|
uint256 amountIn,
|
|
|
|
address reserveIn,
|
2021-01-12 12:20:32 +00:00
|
|
|
address reserveOut
|
2020-11-30 13:14:29 +00:00
|
|
|
)
|
2020-11-04 19:51:21 +00:00
|
|
|
external
|
|
|
|
view
|
2020-11-30 13:14:29 +00:00
|
|
|
override
|
|
|
|
returns (
|
|
|
|
uint256,
|
|
|
|
uint256,
|
|
|
|
uint256,
|
2020-12-02 13:39:05 +00:00
|
|
|
uint256,
|
|
|
|
address[] memory
|
2020-11-30 13:14:29 +00:00
|
|
|
)
|
2020-10-27 19:32:09 +00:00
|
|
|
{
|
2021-01-12 12:20:32 +00:00
|
|
|
AmountCalc memory results = _getAmountsOutData(reserveIn, reserveOut, amountIn);
|
2020-11-04 19:51:21 +00:00
|
|
|
|
|
|
|
return (
|
2020-11-05 14:33:11 +00:00
|
|
|
results.calculatedAmount,
|
|
|
|
results.relativePrice,
|
|
|
|
results.amountInUsd,
|
2020-12-02 13:39:05 +00:00
|
|
|
results.amountOutUsd,
|
|
|
|
results.path
|
2020-11-04 19:51:21 +00:00
|
|
|
);
|
2020-10-27 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-05 14:33:11 +00:00
|
|
|
* @dev Returns the minimum input asset amount required to buy the given output asset amount and the prices
|
2020-10-27 19:32:09 +00:00
|
|
|
* @param amountOut Amount of reserveOut
|
|
|
|
* @param reserveIn Address of the asset to be swap from
|
|
|
|
* @param reserveOut Address of the asset to be swap to
|
2020-11-05 14:33:11 +00:00
|
|
|
* @return uint256 Amount in of the reserveIn
|
|
|
|
* @return uint256 The price of in amount denominated in the reserveOut currency (18 decimals)
|
|
|
|
* @return uint256 In amount of reserveIn value denominated in USD (8 decimals)
|
|
|
|
* @return uint256 Out amount of reserveOut value denominated in USD (8 decimals)
|
2020-10-27 19:32:09 +00:00
|
|
|
*/
|
2020-11-30 13:14:29 +00:00
|
|
|
function getAmountsIn(
|
|
|
|
uint256 amountOut,
|
|
|
|
address reserveIn,
|
2021-01-12 12:20:32 +00:00
|
|
|
address reserveOut
|
2020-11-30 13:14:29 +00:00
|
|
|
)
|
2020-11-04 19:51:21 +00:00
|
|
|
external
|
|
|
|
view
|
2020-11-30 13:14:29 +00:00
|
|
|
override
|
|
|
|
returns (
|
|
|
|
uint256,
|
|
|
|
uint256,
|
|
|
|
uint256,
|
2020-12-02 13:39:05 +00:00
|
|
|
uint256,
|
|
|
|
address[] memory
|
2020-11-30 13:14:29 +00:00
|
|
|
)
|
2020-10-27 19:32:09 +00:00
|
|
|
{
|
2021-01-12 12:20:32 +00:00
|
|
|
AmountCalc memory results = _getAmountsInData(reserveIn, reserveOut, amountOut);
|
2020-10-27 19:32:09 +00:00
|
|
|
|
2020-11-05 14:33:11 +00:00
|
|
|
return (
|
|
|
|
results.calculatedAmount,
|
|
|
|
results.relativePrice,
|
|
|
|
results.amountInUsd,
|
2020-12-02 13:39:05 +00:00
|
|
|
results.amountOutUsd,
|
|
|
|
results.path
|
2020-11-05 14:33:11 +00:00
|
|
|
);
|
2020-10-27 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:17:33 +00:00
|
|
|
/**
|
2020-11-03 15:22:15 +00:00
|
|
|
* @dev Swaps an exact `amountToSwap` of an asset to another
|
2020-10-27 12:17:33 +00:00
|
|
|
* @param assetToSwapFrom Origin asset
|
|
|
|
* @param assetToSwapTo Destination asset
|
|
|
|
* @param amountToSwap Exact amount of `assetToSwapFrom` to be swapped
|
2020-11-03 15:22:15 +00:00
|
|
|
* @param minAmountOut the min amount of `assetToSwapTo` to be received from the swap
|
2020-10-27 12:17:33 +00:00
|
|
|
* @return the amount received from the swap
|
|
|
|
*/
|
2020-11-05 19:49:55 +00:00
|
|
|
function _swapExactTokensForTokens(
|
2020-10-27 12:17:33 +00:00
|
|
|
address assetToSwapFrom,
|
|
|
|
address assetToSwapTo,
|
|
|
|
uint256 amountToSwap,
|
2020-12-02 14:30:57 +00:00
|
|
|
uint256 minAmountOut,
|
|
|
|
bool useEthPath
|
2020-11-30 13:14:29 +00:00
|
|
|
) internal returns (uint256) {
|
2020-10-30 19:59:25 +00:00
|
|
|
uint256 fromAssetDecimals = _getDecimals(assetToSwapFrom);
|
|
|
|
uint256 toAssetDecimals = _getDecimals(assetToSwapTo);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-10-30 19:59:25 +00:00
|
|
|
uint256 fromAssetPrice = _getPrice(assetToSwapFrom);
|
|
|
|
uint256 toAssetPrice = _getPrice(assetToSwapTo);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-11-30 13:14:29 +00:00
|
|
|
uint256 expectedMinAmountOut =
|
|
|
|
amountToSwap
|
|
|
|
.mul(fromAssetPrice.mul(10**toAssetDecimals))
|
|
|
|
.div(toAssetPrice.mul(10**fromAssetDecimals))
|
|
|
|
.percentMul(PercentageMath.PERCENTAGE_FACTOR.sub(MAX_SLIPPAGE_PERCENT));
|
2020-11-03 15:22:15 +00:00
|
|
|
|
|
|
|
require(expectedMinAmountOut < minAmountOut, 'minAmountOut exceed max slippage');
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2021-01-14 14:21:42 +00:00
|
|
|
// Approves the transfer for the swap. Approves for 0 first to comply with tokens that implement the anti frontrunning approval fix.
|
|
|
|
IERC20(assetToSwapFrom).safeApprove(address(UNISWAP_ROUTER), 0);
|
|
|
|
IERC20(assetToSwapFrom).safeApprove(address(UNISWAP_ROUTER), amountToSwap);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-12-02 14:30:57 +00:00
|
|
|
address[] memory path;
|
|
|
|
if (useEthPath) {
|
|
|
|
path = new address[](3);
|
|
|
|
path[0] = assetToSwapFrom;
|
|
|
|
path[1] = WETH_ADDRESS;
|
|
|
|
path[2] = assetToSwapTo;
|
|
|
|
} else {
|
|
|
|
path = new address[](2);
|
|
|
|
path[0] = assetToSwapFrom;
|
|
|
|
path[1] = assetToSwapTo;
|
|
|
|
}
|
2020-11-30 13:14:29 +00:00
|
|
|
uint256[] memory amounts =
|
|
|
|
UNISWAP_ROUTER.swapExactTokensForTokens(
|
|
|
|
amountToSwap,
|
|
|
|
minAmountOut,
|
|
|
|
path,
|
|
|
|
address(this),
|
|
|
|
block.timestamp
|
|
|
|
);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-12-02 16:59:38 +00:00
|
|
|
emit Swapped(assetToSwapFrom, assetToSwapTo, amounts[0], amounts[amounts.length - 1]);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-12-02 16:59:38 +00:00
|
|
|
return amounts[amounts.length - 1];
|
2020-10-27 12:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Receive an exact amount `amountToReceive` of `assetToSwapTo` tokens for as few `assetToSwapFrom` tokens as
|
|
|
|
* possible.
|
|
|
|
* @param assetToSwapFrom Origin asset
|
|
|
|
* @param assetToSwapTo Destination asset
|
|
|
|
* @param maxAmountToSwap Max amount of `assetToSwapFrom` allowed to be swapped
|
|
|
|
* @param amountToReceive Exact amount of `assetToSwapTo` to receive
|
2020-11-03 15:22:15 +00:00
|
|
|
* @return the amount swapped
|
2020-10-27 12:17:33 +00:00
|
|
|
*/
|
2020-11-05 19:49:55 +00:00
|
|
|
function _swapTokensForExactTokens(
|
2020-10-27 12:17:33 +00:00
|
|
|
address assetToSwapFrom,
|
|
|
|
address assetToSwapTo,
|
|
|
|
uint256 maxAmountToSwap,
|
2021-01-14 14:21:42 +00:00
|
|
|
uint256 amountToReceive,
|
|
|
|
bool useEthPath
|
2020-11-30 13:14:29 +00:00
|
|
|
) internal returns (uint256) {
|
2020-10-30 19:59:25 +00:00
|
|
|
uint256 fromAssetDecimals = _getDecimals(assetToSwapFrom);
|
|
|
|
uint256 toAssetDecimals = _getDecimals(assetToSwapTo);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-10-30 19:59:25 +00:00
|
|
|
uint256 fromAssetPrice = _getPrice(assetToSwapFrom);
|
|
|
|
uint256 toAssetPrice = _getPrice(assetToSwapTo);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-11-30 13:14:29 +00:00
|
|
|
uint256 expectedMaxAmountToSwap =
|
|
|
|
amountToReceive
|
|
|
|
.mul(toAssetPrice.mul(10**fromAssetDecimals))
|
|
|
|
.div(fromAssetPrice.mul(10**toAssetDecimals))
|
|
|
|
.percentMul(PercentageMath.PERCENTAGE_FACTOR.add(MAX_SLIPPAGE_PERCENT));
|
2020-10-27 12:17:33 +00:00
|
|
|
|
|
|
|
require(maxAmountToSwap < expectedMaxAmountToSwap, 'maxAmountToSwap exceed max slippage');
|
|
|
|
|
2021-01-14 14:21:42 +00:00
|
|
|
// Approves the transfer for the swap. Approves for 0 first to comply with tokens that implement the anti frontrunning approval fix.
|
|
|
|
IERC20(assetToSwapFrom).safeApprove(address(UNISWAP_ROUTER), 0);
|
|
|
|
IERC20(assetToSwapFrom).safeApprove(address(UNISWAP_ROUTER), maxAmountToSwap);
|
|
|
|
|
|
|
|
address[] memory path;
|
|
|
|
if (useEthPath) {
|
|
|
|
path = new address[](3);
|
|
|
|
path[0] = assetToSwapFrom;
|
|
|
|
path[1] = WETH_ADDRESS;
|
|
|
|
path[2] = assetToSwapTo;
|
|
|
|
} else {
|
|
|
|
path = new address[](2);
|
|
|
|
path[0] = assetToSwapFrom;
|
|
|
|
path[1] = assetToSwapTo;
|
|
|
|
}
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-11-30 13:14:29 +00:00
|
|
|
uint256[] memory amounts =
|
|
|
|
UNISWAP_ROUTER.swapTokensForExactTokens(
|
|
|
|
amountToReceive,
|
|
|
|
maxAmountToSwap,
|
|
|
|
path,
|
|
|
|
address(this),
|
|
|
|
block.timestamp
|
|
|
|
);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-12-02 16:59:38 +00:00
|
|
|
emit Swapped(assetToSwapFrom, assetToSwapTo, amounts[0], amounts[amounts.length - 1]);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
2020-11-03 15:22:15 +00:00
|
|
|
return amounts[0];
|
2020-10-27 12:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-30 19:59:25 +00:00
|
|
|
* @dev Get the price of the asset from the oracle denominated in eth
|
|
|
|
* @param asset address
|
|
|
|
* @return eth price for the asset
|
2020-10-27 12:17:33 +00:00
|
|
|
*/
|
2020-10-30 19:59:25 +00:00
|
|
|
function _getPrice(address asset) internal view returns (uint256) {
|
|
|
|
return ORACLE.getAssetPrice(asset);
|
2020-10-27 12:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Get the decimals of an asset
|
|
|
|
* @return number of decimals of the asset
|
|
|
|
*/
|
2020-10-30 19:59:25 +00:00
|
|
|
function _getDecimals(address asset) internal view returns (uint256) {
|
|
|
|
return IERC20Detailed(asset).decimals();
|
2020-10-27 12:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Get the aToken associated to the asset
|
|
|
|
* @return address of the aToken
|
|
|
|
*/
|
2020-11-25 14:07:33 +00:00
|
|
|
function _getReserveData(address asset) internal view returns (DataTypes.ReserveData memory) {
|
2020-11-30 13:14:29 +00:00
|
|
|
return LENDING_POOL.getReserveData(asset);
|
2020-10-27 12:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-27 19:32:09 +00:00
|
|
|
* @dev Pull the ATokens from the user
|
2020-10-27 12:17:33 +00:00
|
|
|
* @param reserve address of the asset
|
2020-11-05 19:49:55 +00:00
|
|
|
* @param reserveAToken address of the aToken of the reserve
|
2020-10-27 12:17:33 +00:00
|
|
|
* @param user address
|
2020-10-27 19:32:09 +00:00
|
|
|
* @param amount of tokens to be transferred to the contract
|
2020-11-02 20:33:00 +00:00
|
|
|
* @param permitSignature struct containing the permit signature
|
2020-10-27 12:17:33 +00:00
|
|
|
*/
|
2020-11-05 19:49:55 +00:00
|
|
|
function _pullAToken(
|
2020-10-27 12:17:33 +00:00
|
|
|
address reserve,
|
2020-11-05 19:49:55 +00:00
|
|
|
address reserveAToken,
|
2020-10-27 12:17:33 +00:00
|
|
|
address user,
|
2020-11-02 20:33:00 +00:00
|
|
|
uint256 amount,
|
|
|
|
PermitSignature memory permitSignature
|
2020-10-27 12:17:33 +00:00
|
|
|
) internal {
|
2020-11-02 20:33:00 +00:00
|
|
|
if (_usePermit(permitSignature)) {
|
|
|
|
IERC20WithPermit(reserveAToken).permit(
|
|
|
|
user,
|
|
|
|
address(this),
|
2020-11-06 15:12:08 +00:00
|
|
|
permitSignature.amount,
|
2020-11-02 20:33:00 +00:00
|
|
|
permitSignature.deadline,
|
|
|
|
permitSignature.v,
|
|
|
|
permitSignature.r,
|
|
|
|
permitSignature.s
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:17:33 +00:00
|
|
|
// transfer from user to adapter
|
2020-10-27 19:32:09 +00:00
|
|
|
IERC20(reserveAToken).safeTransferFrom(user, address(this), amount);
|
2020-10-27 12:17:33 +00:00
|
|
|
|
|
|
|
// withdraw reserve
|
2020-11-30 13:14:29 +00:00
|
|
|
LENDING_POOL.withdraw(reserve, amount, address(this));
|
2020-10-27 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 20:33:00 +00:00
|
|
|
/**
|
|
|
|
* @dev Tells if the permit method should be called by inspecting if there is a valid signature.
|
|
|
|
* If signature params are set to 0, then permit won't be called.
|
|
|
|
* @param signature struct containing the permit signature
|
|
|
|
* @return whether or not permit should be called
|
|
|
|
*/
|
|
|
|
function _usePermit(PermitSignature memory signature) internal pure returns (bool) {
|
2020-11-30 13:14:29 +00:00
|
|
|
return
|
|
|
|
!(uint256(signature.deadline) == uint256(signature.v) && uint256(signature.deadline) == 0);
|
2020-11-02 20:33:00 +00:00
|
|
|
}
|
2020-11-04 19:51:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Calculates the value denominated in USD
|
|
|
|
* @param reserve Address of the reserve
|
|
|
|
* @param amount Amount of the reserve
|
|
|
|
* @param decimals Decimals of the reserve
|
|
|
|
* @return whether or not permit should be called
|
|
|
|
*/
|
2020-11-30 13:14:29 +00:00
|
|
|
function _calcUsdValue(
|
|
|
|
address reserve,
|
|
|
|
uint256 amount,
|
|
|
|
uint256 decimals
|
|
|
|
) internal view returns (uint256) {
|
2020-11-04 19:51:21 +00:00
|
|
|
uint256 ethUsdPrice = _getPrice(USD_ADDRESS);
|
|
|
|
uint256 reservePrice = _getPrice(reserve);
|
|
|
|
|
2020-11-30 13:14:29 +00:00
|
|
|
return amount.mul(reservePrice).div(10**decimals).mul(ethUsdPrice).div(10**18);
|
2020-11-04 19:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Given an input asset amount, returns the maximum output amount of the other asset
|
|
|
|
* @param reserveIn Address of the asset to be swap from
|
|
|
|
* @param reserveOut Address of the asset to be swap to
|
|
|
|
* @param amountIn Amount of reserveIn
|
2020-11-05 14:33:11 +00:00
|
|
|
* @return Struct containing the following information:
|
|
|
|
* uint256 Amount out of the reserveOut
|
|
|
|
* uint256 The price of out amount denominated in the reserveIn currency (18 decimals)
|
|
|
|
* uint256 In amount of reserveIn value denominated in USD (8 decimals)
|
|
|
|
* uint256 Out amount of reserveOut value denominated in USD (8 decimals)
|
2020-11-04 19:51:21 +00:00
|
|
|
*/
|
2020-11-30 13:14:29 +00:00
|
|
|
function _getAmountsOutData(
|
|
|
|
address reserveIn,
|
|
|
|
address reserveOut,
|
2021-01-12 12:20:32 +00:00
|
|
|
uint256 amountIn
|
2020-11-30 13:14:29 +00:00
|
|
|
) internal view returns (AmountCalc memory) {
|
2020-11-05 14:33:11 +00:00
|
|
|
// Subtract flash loan fee
|
2021-01-12 12:20:32 +00:00
|
|
|
uint256 finalAmountIn = amountIn.sub(amountIn.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000));
|
2020-12-02 13:39:05 +00:00
|
|
|
|
2021-02-01 14:13:53 +00:00
|
|
|
if (reserveIn == reserveOut) {
|
|
|
|
uint256 reserveDecimals = _getDecimals(reserveIn);
|
2021-01-25 08:29:07 +00:00
|
|
|
address[] memory path = new address[](1);
|
|
|
|
path[0] = reserveIn;
|
|
|
|
|
2021-02-01 14:13:53 +00:00
|
|
|
return
|
|
|
|
AmountCalc(
|
|
|
|
finalAmountIn,
|
|
|
|
finalAmountIn.mul(10**18).div(amountIn),
|
|
|
|
_calcUsdValue(reserveIn, amountIn, reserveDecimals),
|
|
|
|
_calcUsdValue(reserveIn, finalAmountIn, reserveDecimals),
|
2021-01-25 08:29:07 +00:00
|
|
|
path
|
2021-02-01 14:13:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:20:32 +00:00
|
|
|
address[] memory simplePath = new address[](2);
|
|
|
|
simplePath[0] = reserveIn;
|
|
|
|
simplePath[1] = reserveOut;
|
2020-12-02 16:59:38 +00:00
|
|
|
|
2021-01-12 12:20:32 +00:00
|
|
|
uint256[] memory amountsWithoutWeth;
|
|
|
|
uint256[] memory amountsWithWeth;
|
|
|
|
|
|
|
|
address[] memory pathWithWeth = new address[](3);
|
2020-12-03 15:24:57 +00:00
|
|
|
if (reserveIn != WETH_ADDRESS && reserveOut != WETH_ADDRESS) {
|
2021-01-12 12:20:32 +00:00
|
|
|
pathWithWeth[0] = reserveIn;
|
|
|
|
pathWithWeth[1] = WETH_ADDRESS;
|
|
|
|
pathWithWeth[2] = reserveOut;
|
2020-12-03 15:24:57 +00:00
|
|
|
|
2021-01-12 12:20:32 +00:00
|
|
|
try UNISWAP_ROUTER.getAmountsOut(finalAmountIn, pathWithWeth) returns (
|
2020-12-03 15:24:57 +00:00
|
|
|
uint256[] memory resultsWithWeth
|
|
|
|
) {
|
2021-01-12 12:20:32 +00:00
|
|
|
amountsWithWeth = resultsWithWeth;
|
2020-12-03 15:24:57 +00:00
|
|
|
} catch {
|
2021-01-12 12:20:32 +00:00
|
|
|
amountsWithWeth = new uint256[](3);
|
2020-12-03 15:24:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-12 12:20:32 +00:00
|
|
|
amountsWithWeth = new uint256[](3);
|
2020-12-03 10:36:15 +00:00
|
|
|
}
|
2020-12-02 17:19:29 +00:00
|
|
|
|
|
|
|
uint256 bestAmountOut;
|
2021-01-12 12:20:32 +00:00
|
|
|
try UNISWAP_ROUTER.getAmountsOut(finalAmountIn, simplePath) returns (
|
2020-12-02 13:39:05 +00:00
|
|
|
uint256[] memory resultAmounts
|
|
|
|
) {
|
2021-01-12 12:20:32 +00:00
|
|
|
amountsWithoutWeth = resultAmounts;
|
2020-11-04 19:51:21 +00:00
|
|
|
|
2021-01-12 12:20:32 +00:00
|
|
|
bestAmountOut = (amountsWithWeth[2] > amountsWithoutWeth[1])
|
|
|
|
? amountsWithWeth[2]
|
|
|
|
: amountsWithoutWeth[1];
|
2020-12-02 17:19:29 +00:00
|
|
|
} catch {
|
2021-01-12 12:20:32 +00:00
|
|
|
amountsWithoutWeth = new uint256[](2);
|
|
|
|
bestAmountOut = amountsWithWeth[2];
|
2020-12-02 17:19:29 +00:00
|
|
|
}
|
2020-12-02 16:59:38 +00:00
|
|
|
|
2020-11-05 14:33:11 +00:00
|
|
|
uint256 reserveInDecimals = _getDecimals(reserveIn);
|
|
|
|
uint256 reserveOutDecimals = _getDecimals(reserveOut);
|
|
|
|
|
2020-11-30 13:14:29 +00:00
|
|
|
uint256 outPerInPrice =
|
2021-01-12 12:20:32 +00:00
|
|
|
finalAmountIn.mul(10**18).mul(10**reserveOutDecimals).div(
|
2020-12-02 16:59:38 +00:00
|
|
|
bestAmountOut.mul(10**reserveInDecimals)
|
2020-11-30 13:14:29 +00:00
|
|
|
);
|
2020-11-05 14:33:11 +00:00
|
|
|
|
2020-11-30 13:14:29 +00:00
|
|
|
return
|
|
|
|
AmountCalc(
|
2020-12-02 16:59:38 +00:00
|
|
|
bestAmountOut,
|
2020-11-30 13:14:29 +00:00
|
|
|
outPerInPrice,
|
|
|
|
_calcUsdValue(reserveIn, amountIn, reserveInDecimals),
|
2020-12-02 16:59:38 +00:00
|
|
|
_calcUsdValue(reserveOut, bestAmountOut, reserveOutDecimals),
|
2021-01-12 12:20:32 +00:00
|
|
|
(bestAmountOut == 0) ? new address[](2) : (bestAmountOut == amountsWithoutWeth[1])
|
|
|
|
? simplePath
|
|
|
|
: pathWithWeth
|
2020-11-30 13:14:29 +00:00
|
|
|
);
|
2020-11-05 14:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Returns the minimum input asset amount required to buy the given output asset amount
|
|
|
|
* @param reserveIn Address of the asset to be swap from
|
|
|
|
* @param reserveOut Address of the asset to be swap to
|
|
|
|
* @param amountOut Amount of reserveOut
|
|
|
|
* @return Struct containing the following information:
|
|
|
|
* uint256 Amount in of the reserveIn
|
|
|
|
* uint256 The price of in amount denominated in the reserveOut currency (18 decimals)
|
|
|
|
* uint256 In amount of reserveIn value denominated in USD (8 decimals)
|
|
|
|
* uint256 Out amount of reserveOut value denominated in USD (8 decimals)
|
|
|
|
*/
|
2020-11-30 13:14:29 +00:00
|
|
|
function _getAmountsInData(
|
|
|
|
address reserveIn,
|
|
|
|
address reserveOut,
|
2021-01-12 12:20:32 +00:00
|
|
|
uint256 amountOut
|
2020-11-30 13:14:29 +00:00
|
|
|
) internal view returns (AmountCalc memory) {
|
2021-02-01 14:13:53 +00:00
|
|
|
if (reserveIn == reserveOut) {
|
|
|
|
// Add flash loan fee
|
|
|
|
uint256 amountIn = amountOut.add(amountOut.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000));
|
|
|
|
uint256 reserveDecimals = _getDecimals(reserveIn);
|
2021-01-25 08:29:07 +00:00
|
|
|
address[] memory path = new address[](1);
|
|
|
|
path[0] = reserveIn;
|
2021-01-27 14:43:34 +00:00
|
|
|
|
2021-02-01 14:13:53 +00:00
|
|
|
return
|
|
|
|
AmountCalc(
|
|
|
|
amountIn,
|
|
|
|
amountOut.mul(10**18).div(amountIn),
|
|
|
|
_calcUsdValue(reserveIn, amountIn, reserveDecimals),
|
|
|
|
_calcUsdValue(reserveIn, amountOut, reserveDecimals),
|
2021-01-25 08:29:07 +00:00
|
|
|
path
|
2021-02-01 14:13:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-02 13:39:05 +00:00
|
|
|
(uint256[] memory amounts, address[] memory path) =
|
2020-12-02 14:30:57 +00:00
|
|
|
_getAmountsInAndPath(reserveIn, reserveOut, amountOut);
|
2020-11-05 14:33:11 +00:00
|
|
|
|
2020-11-13 15:30:14 +00:00
|
|
|
// Add flash loan fee
|
2021-01-12 12:20:32 +00:00
|
|
|
uint256 finalAmountIn = amounts[0].add(amounts[0].mul(FLASHLOAN_PREMIUM_TOTAL).div(10000));
|
2020-11-05 14:33:11 +00:00
|
|
|
|
|
|
|
uint256 reserveInDecimals = _getDecimals(reserveIn);
|
|
|
|
uint256 reserveOutDecimals = _getDecimals(reserveOut);
|
|
|
|
|
2020-11-30 13:14:29 +00:00
|
|
|
uint256 inPerOutPrice =
|
|
|
|
amountOut.mul(10**18).mul(10**reserveInDecimals).div(
|
|
|
|
finalAmountIn.mul(10**reserveOutDecimals)
|
|
|
|
);
|
2020-11-05 14:33:11 +00:00
|
|
|
|
2020-11-30 13:14:29 +00:00
|
|
|
return
|
|
|
|
AmountCalc(
|
|
|
|
finalAmountIn,
|
|
|
|
inPerOutPrice,
|
|
|
|
_calcUsdValue(reserveIn, finalAmountIn, reserveInDecimals),
|
2020-12-02 13:39:05 +00:00
|
|
|
_calcUsdValue(reserveOut, amountOut, reserveOutDecimals),
|
|
|
|
path
|
2020-11-30 13:14:29 +00:00
|
|
|
);
|
2020-11-04 19:51:21 +00:00
|
|
|
}
|
2020-11-24 15:11:13 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-30 13:14:29 +00:00
|
|
|
* @dev Calculates the input asset amount required to buy the given output asset amount
|
|
|
|
* @param reserveIn Address of the asset to be swap from
|
|
|
|
* @param reserveOut Address of the asset to be swap to
|
|
|
|
* @param amountOut Amount of reserveOut
|
|
|
|
* @return uint256[] amounts Array containing the amountIn and amountOut for a swap
|
|
|
|
*/
|
2020-12-02 14:30:57 +00:00
|
|
|
function _getAmountsInAndPath(
|
2020-11-30 13:14:29 +00:00
|
|
|
address reserveIn,
|
|
|
|
address reserveOut,
|
|
|
|
uint256 amountOut
|
2020-12-02 13:39:05 +00:00
|
|
|
) internal view returns (uint256[] memory, address[] memory) {
|
|
|
|
address[] memory simplePath = new address[](2);
|
|
|
|
simplePath[0] = reserveIn;
|
|
|
|
simplePath[1] = reserveOut;
|
|
|
|
|
2020-12-02 17:19:29 +00:00
|
|
|
uint256[] memory amountsWithoutWeth;
|
|
|
|
uint256[] memory amountsWithWeth;
|
2020-12-02 13:39:05 +00:00
|
|
|
address[] memory pathWithWeth = new address[](3);
|
2020-12-03 10:36:15 +00:00
|
|
|
|
2020-12-03 15:24:57 +00:00
|
|
|
if (reserveIn != WETH_ADDRESS && reserveOut != WETH_ADDRESS) {
|
|
|
|
pathWithWeth[0] = reserveIn;
|
|
|
|
pathWithWeth[1] = WETH_ADDRESS;
|
|
|
|
pathWithWeth[2] = reserveOut;
|
|
|
|
|
|
|
|
try UNISWAP_ROUTER.getAmountsIn(amountOut, pathWithWeth) returns (
|
|
|
|
uint256[] memory resultsWithWeth
|
|
|
|
) {
|
|
|
|
amountsWithWeth = resultsWithWeth;
|
|
|
|
} catch {
|
2020-12-03 15:35:05 +00:00
|
|
|
amountsWithWeth = new uint256[](3);
|
2020-12-03 15:24:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
amountsWithWeth = new uint256[](3);
|
2020-12-03 10:36:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 13:39:05 +00:00
|
|
|
try UNISWAP_ROUTER.getAmountsIn(amountOut, simplePath) returns (
|
|
|
|
uint256[] memory resultAmounts
|
|
|
|
) {
|
2020-12-02 17:19:29 +00:00
|
|
|
amountsWithoutWeth = resultAmounts;
|
2020-12-02 13:39:05 +00:00
|
|
|
|
2020-12-02 17:19:29 +00:00
|
|
|
return
|
2021-01-14 14:21:42 +00:00
|
|
|
(amountsWithWeth[0] < amountsWithoutWeth[0] && amountsWithWeth[0] != 0)
|
2020-12-02 17:19:29 +00:00
|
|
|
? (amountsWithWeth, pathWithWeth)
|
|
|
|
: (amountsWithoutWeth, simplePath);
|
|
|
|
} catch {
|
|
|
|
return (amountsWithWeth, pathWithWeth);
|
2020-12-02 14:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Calculates the input asset amount required to buy the given output asset amount
|
|
|
|
* @param reserveIn Address of the asset to be swap from
|
|
|
|
* @param reserveOut Address of the asset to be swap to
|
|
|
|
* @param amountOut Amount of reserveOut
|
|
|
|
* @return uint256[] amounts Array containing the amountIn and amountOut for a swap
|
|
|
|
*/
|
|
|
|
function _getAmountsIn(
|
|
|
|
address reserveIn,
|
|
|
|
address reserveOut,
|
|
|
|
uint256 amountOut,
|
|
|
|
bool useEthPath
|
|
|
|
) internal view returns (uint256[] memory) {
|
|
|
|
address[] memory path;
|
|
|
|
|
|
|
|
if (useEthPath) {
|
|
|
|
path = new address[](3);
|
|
|
|
path[0] = reserveIn;
|
|
|
|
path[1] = WETH_ADDRESS;
|
|
|
|
path[2] = reserveOut;
|
|
|
|
} else {
|
|
|
|
path = new address[](2);
|
|
|
|
path[0] = reserveIn;
|
|
|
|
path[1] = reserveOut;
|
2020-12-02 13:39:05 +00:00
|
|
|
}
|
2020-11-24 15:11:13 +00:00
|
|
|
|
2020-12-02 14:30:57 +00:00
|
|
|
return UNISWAP_ROUTER.getAmountsIn(amountOut, path);
|
2020-11-24 15:11:13 +00:00
|
|
|
}
|
2020-12-03 11:13:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Emergency rescue for token stucked on this contract, as failsafe mechanism
|
|
|
|
* - Funds should never remain in this contract more time than during transactions
|
|
|
|
* - Only callable by the owner
|
|
|
|
**/
|
|
|
|
function rescueTokens(IERC20 token) external onlyOwner {
|
|
|
|
token.transfer(owner(), token.balanceOf(address(this)));
|
|
|
|
}
|
2020-10-27 12:17:33 +00:00
|
|
|
}
|