Merge pull request #17 from aave/fix/eth-eth-repay-w-collateral

Fix eth->eth repay with collateral
This commit is contained in:
Ernesto Boado 2021-02-01 15:18:32 +01:00 committed by GitHub
commit 9b509eadbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 19 deletions

View File

@ -24,6 +24,7 @@ import {IBaseUniswapAdapter} from './interfaces/IBaseUniswapAdapter.sol';
abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapter, Ownable { abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapter, Ownable {
using SafeMath for uint256; using SafeMath for uint256;
using PercentageMath for uint256; using PercentageMath for uint256;
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
// Max slippage percent allowed // Max slippage percent allowed
@ -346,6 +347,18 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
// Subtract flash loan fee // Subtract flash loan fee
uint256 finalAmountIn = amountIn.sub(amountIn.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000)); uint256 finalAmountIn = amountIn.sub(amountIn.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000));
if (reserveIn == reserveOut) {
uint256 reserveDecimals = _getDecimals(reserveIn);
return
AmountCalc(
finalAmountIn,
finalAmountIn.mul(10**18).div(amountIn),
_calcUsdValue(reserveIn, amountIn, reserveDecimals),
_calcUsdValue(reserveIn, finalAmountIn, reserveDecimals),
[reserveIn]
);
}
address[] memory simplePath = new address[](2); address[] memory simplePath = new address[](2);
simplePath[0] = reserveIn; simplePath[0] = reserveIn;
simplePath[1] = reserveOut; simplePath[1] = reserveOut;
@ -420,6 +433,20 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
address reserveOut, address reserveOut,
uint256 amountOut uint256 amountOut
) internal view returns (AmountCalc memory) { ) internal view returns (AmountCalc memory) {
if (reserveIn == reserveOut) {
// Add flash loan fee
uint256 amountIn = amountOut.add(amountOut.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000));
uint256 reserveDecimals = _getDecimals(reserveIn);
return
AmountCalc(
amountIn,
amountOut.mul(10**18).div(amountIn),
_calcUsdValue(reserveIn, amountIn, reserveDecimals),
_calcUsdValue(reserveIn, amountOut, reserveDecimals),
[reserveIn]
);
}
(uint256[] memory amounts, address[] memory path) = (uint256[] memory amounts, address[] memory path) =
_getAmountsInAndPath(reserveIn, reserveOut, amountOut); _getAmountsInAndPath(reserveIn, reserveOut, amountOut);

View File

@ -208,19 +208,22 @@ contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter {
PermitSignature memory permitSignature, PermitSignature memory permitSignature,
bool useEthPath bool useEthPath
) internal { ) internal {
SwapLiquidityLocalVars memory vars; SwapLiquidityLocalVars memory vars;
vars.aToken = _getReserveData(assetFrom).aTokenAddress; vars.aToken = _getReserveData(assetFrom).aTokenAddress;
vars.aTokenInitiatorBalance = IERC20(vars.aToken).balanceOf(initiator); vars.aTokenInitiatorBalance = IERC20(vars.aToken).balanceOf(initiator);
vars.amountToSwap = vars.amountToSwap = swapAllBalance && vars.aTokenInitiatorBalance.sub(premium) <= amount
swapAllBalance && vars.aTokenInitiatorBalance.sub(premium) <= amount
? vars.aTokenInitiatorBalance.sub(premium) ? vars.aTokenInitiatorBalance.sub(premium)
: amount; : amount;
vars.receivedAmount = vars.receivedAmount = _swapExactTokensForTokens(
_swapExactTokensForTokens(assetFrom, assetTo, vars.amountToSwap, minAmountToReceive, useEthPath); assetFrom,
assetTo,
vars.amountToSwap,
minAmountToReceive,
useEthPath
);
// Deposit new reserve // Deposit new reserve
IERC20(assetTo).safeApprove(address(LENDING_POOL), 0); IERC20(assetTo).safeApprove(address(LENDING_POOL), 0);

View File

@ -200,7 +200,13 @@ contract UniswapRepayAdapter is BaseUniswapAdapter {
); );
// Swap collateral asset to the debt asset // Swap collateral asset to the debt asset
_swapTokensForExactTokens(collateralAsset, debtAsset, amounts[0], neededForFlashLoanDebt, useEthPath); _swapTokensForExactTokens(
collateralAsset,
debtAsset,
amounts[0],
neededForFlashLoanDebt,
useEthPath
);
} else { } else {
// Pull aTokens from user // Pull aTokens from user
_pullAToken( _pullAToken(