mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
code improvements
This commit is contained in:
parent
cf9c8855c3
commit
b0d9dbe2a7
|
@ -24,14 +24,6 @@ contract BaseUniswapAdapter {
|
||||||
using PercentageMath for uint256;
|
using PercentageMath for uint256;
|
||||||
using SafeERC20 for IERC20;
|
using SafeERC20 for IERC20;
|
||||||
|
|
||||||
struct PermitParams {
|
|
||||||
uint256[] amount;
|
|
||||||
uint256[] deadline;
|
|
||||||
uint8[] v;
|
|
||||||
bytes32[] r;
|
|
||||||
bytes32[] s;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PermitSignature {
|
struct PermitSignature {
|
||||||
uint256 amount;
|
uint256 amount;
|
||||||
uint256 deadline;
|
uint256 deadline;
|
||||||
|
@ -140,9 +132,9 @@ contract BaseUniswapAdapter {
|
||||||
uint256 toAssetPrice = _getPrice(assetToSwapTo);
|
uint256 toAssetPrice = _getPrice(assetToSwapTo);
|
||||||
|
|
||||||
uint256 expectedMinAmountOut = amountToSwap
|
uint256 expectedMinAmountOut = amountToSwap
|
||||||
.mul(fromAssetPrice.mul(10**toAssetDecimals))
|
.mul(fromAssetPrice.mul(10**toAssetDecimals))
|
||||||
.div(toAssetPrice.mul(10**fromAssetDecimals))
|
.div(toAssetPrice.mul(10**fromAssetDecimals))
|
||||||
.percentMul(PercentageMath.PERCENTAGE_FACTOR.sub(MAX_SLIPPAGE_PERCENT));
|
.percentMul(PercentageMath.PERCENTAGE_FACTOR.sub(MAX_SLIPPAGE_PERCENT));
|
||||||
|
|
||||||
require(expectedMinAmountOut < minAmountOut, 'minAmountOut exceed max slippage');
|
require(expectedMinAmountOut < minAmountOut, 'minAmountOut exceed max slippage');
|
||||||
|
|
||||||
|
@ -183,9 +175,9 @@ contract BaseUniswapAdapter {
|
||||||
uint256 toAssetPrice = _getPrice(assetToSwapTo);
|
uint256 toAssetPrice = _getPrice(assetToSwapTo);
|
||||||
|
|
||||||
uint256 expectedMaxAmountToSwap = amountToReceive
|
uint256 expectedMaxAmountToSwap = amountToReceive
|
||||||
.mul(toAssetPrice.mul(10**fromAssetDecimals))
|
.mul(toAssetPrice.mul(10**fromAssetDecimals))
|
||||||
.div(fromAssetPrice.mul(10**toAssetDecimals))
|
.div(fromAssetPrice.mul(10**toAssetDecimals))
|
||||||
.percentMul(PercentageMath.PERCENTAGE_FACTOR.add(MAX_SLIPPAGE_PERCENT));
|
.percentMul(PercentageMath.PERCENTAGE_FACTOR.add(MAX_SLIPPAGE_PERCENT));
|
||||||
|
|
||||||
require(maxAmountToSwap < expectedMaxAmountToSwap, 'maxAmountToSwap exceed max slippage');
|
require(maxAmountToSwap < expectedMaxAmountToSwap, 'maxAmountToSwap exceed max slippage');
|
||||||
|
|
||||||
|
@ -267,8 +259,7 @@ contract BaseUniswapAdapter {
|
||||||
* @return whether or not permit should be called
|
* @return whether or not permit should be called
|
||||||
*/
|
*/
|
||||||
function _usePermit(PermitSignature memory signature) internal pure returns (bool) {
|
function _usePermit(PermitSignature memory signature) internal pure returns (bool) {
|
||||||
return !(uint256(signature.deadline) == uint256(signature.v) &&
|
return !(uint256(signature.deadline) == uint256(signature.v) && uint256(signature.deadline) == 0);
|
||||||
uint256(signature.deadline) == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -283,10 +274,10 @@ contract BaseUniswapAdapter {
|
||||||
uint256 reservePrice = _getPrice(reserve);
|
uint256 reservePrice = _getPrice(reserve);
|
||||||
|
|
||||||
return amount
|
return amount
|
||||||
.mul(reservePrice)
|
.mul(reservePrice)
|
||||||
.div(10**decimals)
|
.div(10**decimals)
|
||||||
.mul(ethUsdPrice)
|
.mul(ethUsdPrice)
|
||||||
.div(10**18);
|
.div(10**18);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -314,9 +305,9 @@ contract BaseUniswapAdapter {
|
||||||
uint256 reserveOutDecimals = _getDecimals(reserveOut);
|
uint256 reserveOutDecimals = _getDecimals(reserveOut);
|
||||||
|
|
||||||
uint256 outPerInPrice = finalAmountIn
|
uint256 outPerInPrice = finalAmountIn
|
||||||
.mul(10**18)
|
.mul(10**18)
|
||||||
.mul(10**reserveOutDecimals)
|
.mul(10**reserveOutDecimals)
|
||||||
.div(amounts[1].mul(10**reserveInDecimals));
|
.div(amounts[1].mul(10**reserveInDecimals));
|
||||||
|
|
||||||
return AmountCalc(
|
return AmountCalc(
|
||||||
amounts[1],
|
amounts[1],
|
||||||
|
@ -351,9 +342,9 @@ contract BaseUniswapAdapter {
|
||||||
uint256 reserveOutDecimals = _getDecimals(reserveOut);
|
uint256 reserveOutDecimals = _getDecimals(reserveOut);
|
||||||
|
|
||||||
uint256 inPerOutPrice = amountOut
|
uint256 inPerOutPrice = amountOut
|
||||||
.mul(10**18)
|
.mul(10**18)
|
||||||
.mul(10**reserveInDecimals)
|
.mul(10**reserveInDecimals)
|
||||||
.div(finalAmountIn.mul(10**reserveOutDecimals));
|
.div(finalAmountIn.mul(10**reserveOutDecimals));
|
||||||
|
|
||||||
return AmountCalc(
|
return AmountCalc(
|
||||||
finalAmountIn,
|
finalAmountIn,
|
||||||
|
|
|
@ -15,6 +15,14 @@ import {IERC20} from '../dependencies/openzeppelin/contracts/IERC20.sol';
|
||||||
**/
|
**/
|
||||||
contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
|
contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
|
||||||
|
|
||||||
|
struct PermitParams {
|
||||||
|
uint256[] amount;
|
||||||
|
uint256[] deadline;
|
||||||
|
uint8[] v;
|
||||||
|
bytes32[] r;
|
||||||
|
bytes32[] s;
|
||||||
|
}
|
||||||
|
|
||||||
struct SwapParams {
|
struct SwapParams {
|
||||||
address[] assetToSwapToList;
|
address[] assetToSwapToList;
|
||||||
uint256[] minAmountsToReceive;
|
uint256[] minAmountsToReceive;
|
||||||
|
@ -26,8 +34,8 @@ contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
|
||||||
ILendingPoolAddressesProvider addressesProvider,
|
ILendingPoolAddressesProvider addressesProvider,
|
||||||
IUniswapV2Router02 uniswapRouter
|
IUniswapV2Router02 uniswapRouter
|
||||||
)
|
)
|
||||||
public
|
public
|
||||||
BaseUniswapAdapter(addressesProvider, uniswapRouter)
|
BaseUniswapAdapter(addressesProvider, uniswapRouter)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -191,19 +199,15 @@ contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
|
||||||
uint256 flashLoanDebt = amount.add(premium);
|
uint256 flashLoanDebt = amount.add(premium);
|
||||||
uint256 amountToPull = swapAllBalance ? aTokenInitiatorBalance : flashLoanDebt;
|
uint256 amountToPull = swapAllBalance ? aTokenInitiatorBalance : flashLoanDebt;
|
||||||
|
|
||||||
_pullATokenAndRepayFlashLoan(
|
_pullAToken(assetFrom, aToken, initiator, amountToPull, permitSignature);
|
||||||
assetFrom,
|
|
||||||
aToken,
|
// Repay flashloan
|
||||||
initiator,
|
IERC20(assetFrom).approve(address(POOL), flashLoanDebt);
|
||||||
amountToPull,
|
|
||||||
flashLoanDebt,
|
|
||||||
permitSignature
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Decodes debt information encoded in flashloan params
|
* @dev Decodes debt information encoded in flashloan params
|
||||||
* @param params Additional variadic field to include extra params. Expected parameters:
|
* @param params Additional variadic field to include extra params. Expected parameters:
|
||||||
* address[] assetToSwapToList List of the addresses of the reserve to be swapped to and deposited
|
* address[] assetToSwapToList List of the addresses of the reserve to be swapped to and deposited
|
||||||
* uint256[] minAmountsToReceive List of min amounts to be received from the swap
|
* uint256[] minAmountsToReceive List of min amounts to be received from the swap
|
||||||
* bool[] swapAllBalance Flag indicating if all the user balance should be swapped
|
* bool[] swapAllBalance Flag indicating if all the user balance should be swapped
|
||||||
|
@ -212,8 +216,8 @@ contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
|
||||||
* uint8[] v List of v param for the permit signature
|
* uint8[] v List of v param for the permit signature
|
||||||
* bytes32[] r List of r param for the permit signature
|
* bytes32[] r List of r param for the permit signature
|
||||||
* bytes32[] s List of s param for the permit signature
|
* bytes32[] s List of s param for the permit signature
|
||||||
* @return SwapParams struct containing decoded params
|
* @return SwapParams struct containing decoded params
|
||||||
*/
|
*/
|
||||||
function _decodeParams(bytes memory params) internal pure returns (SwapParams memory) {
|
function _decodeParams(bytes memory params) internal pure returns (SwapParams memory) {
|
||||||
(
|
(
|
||||||
address[] memory assetToSwapToList,
|
address[] memory assetToSwapToList,
|
||||||
|
@ -228,27 +232,4 @@ contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
|
||||||
|
|
||||||
return SwapParams(assetToSwapToList, minAmountsToReceive, swapAllBalance, PermitParams(permitAmount, deadline, v, r, s));
|
return SwapParams(assetToSwapToList, minAmountsToReceive, swapAllBalance, PermitParams(permitAmount, deadline, v, r, s));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Pull the ATokens from the user and use them to repay the flashloan
|
|
||||||
* @param reserve address of the asset
|
|
||||||
* @param reserveAToken address of the aToken of the reserve
|
|
||||||
* @param user address
|
|
||||||
* @param amountToPull amount to be pulled from the user
|
|
||||||
* @param flashLoanDebt need to be repaid
|
|
||||||
* @param permitSignature struct containing the permit signature
|
|
||||||
*/
|
|
||||||
function _pullATokenAndRepayFlashLoan(
|
|
||||||
address reserve,
|
|
||||||
address reserveAToken,
|
|
||||||
address user,
|
|
||||||
uint256 amountToPull,
|
|
||||||
uint256 flashLoanDebt,
|
|
||||||
PermitSignature memory permitSignature
|
|
||||||
) internal {
|
|
||||||
_pullAToken(reserve, reserveAToken, user, amountToPull, permitSignature);
|
|
||||||
|
|
||||||
// Repay flashloan
|
|
||||||
IERC20(reserve).approve(address(POOL), flashLoanDebt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,8 @@ contract UniswapRepayAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
|
||||||
ILendingPoolAddressesProvider addressesProvider,
|
ILendingPoolAddressesProvider addressesProvider,
|
||||||
IUniswapV2Router02 uniswapRouter
|
IUniswapV2Router02 uniswapRouter
|
||||||
)
|
)
|
||||||
public
|
public
|
||||||
BaseUniswapAdapter(addressesProvider, uniswapRouter)
|
BaseUniswapAdapter(addressesProvider, uniswapRouter)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user