add withFlash flage to getAmountIn/Out

This commit is contained in:
andyk 2020-12-09 13:29:25 +04:00
parent eadaf066f2
commit 3d2a877770
3 changed files with 50 additions and 39 deletions

View File

@ -63,7 +63,8 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
function getAmountsOut( function getAmountsOut(
uint256 amountIn, uint256 amountIn,
address reserveIn, address reserveIn,
address reserveOut address reserveOut,
bool withFlash
) )
external external
view view
@ -76,7 +77,7 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
address[] memory address[] memory
) )
{ {
AmountCalc memory results = _getAmountsOutData(reserveIn, reserveOut, amountIn); AmountCalc memory results = _getAmountsOutData(reserveIn, reserveOut, amountIn, withFlash);
return ( return (
results.calculatedAmount, results.calculatedAmount,
@ -100,7 +101,8 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
function getAmountsIn( function getAmountsIn(
uint256 amountOut, uint256 amountOut,
address reserveIn, address reserveIn,
address reserveOut address reserveOut,
bool withFlash
) )
external external
view view
@ -113,7 +115,7 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
address[] memory address[] memory
) )
{ {
AmountCalc memory results = _getAmountsInData(reserveIn, reserveOut, amountOut); AmountCalc memory results = _getAmountsInData(reserveIn, reserveOut, amountOut, withFlash);
return ( return (
results.calculatedAmount, results.calculatedAmount,
@ -316,6 +318,14 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
return amount.mul(reservePrice).div(10**decimals).mul(ethUsdPrice).div(10**18); return amount.mul(reservePrice).div(10**decimals).mul(ethUsdPrice).div(10**18);
} }
struct AmountOutVars {
uint256 finalAmountIn;
address[] simplePath;
uint256[] amountsWithoutWeth;
uint256[] amountsWithWeth;
address[] pathWithWeth;
}
/** /**
* @dev Given an input asset amount, returns the maximum output amount of the other asset * @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 reserveIn Address of the asset to be swap from
@ -330,54 +340,55 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
function _getAmountsOutData( function _getAmountsOutData(
address reserveIn, address reserveIn,
address reserveOut, address reserveOut,
uint256 amountIn uint256 amountIn,
bool withFlash
) internal view returns (AmountCalc memory) { ) internal view returns (AmountCalc memory) {
AmountOutVars memory vars;
// Subtract flash loan fee // Subtract flash loan fee
uint256 finalAmountIn = amountIn.sub(amountIn.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000)); vars.finalAmountIn = amountIn.sub(
withFlash ? amountIn.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000) : 0
);
address[] memory simplePath = new address[](2); vars.simplePath = new address[](2);
simplePath[0] = reserveIn; vars.simplePath[0] = reserveIn;
simplePath[1] = reserveOut; vars.simplePath[1] = reserveOut;
uint256[] memory amountsWithoutWeth; vars.pathWithWeth = new address[](3);
uint256[] memory amountsWithWeth;
address[] memory pathWithWeth = new address[](3);
if (reserveIn != WETH_ADDRESS && reserveOut != WETH_ADDRESS) { if (reserveIn != WETH_ADDRESS && reserveOut != WETH_ADDRESS) {
pathWithWeth[0] = reserveIn; vars.pathWithWeth[0] = reserveIn;
pathWithWeth[1] = WETH_ADDRESS; vars.pathWithWeth[1] = WETH_ADDRESS;
pathWithWeth[2] = reserveOut; vars.pathWithWeth[2] = reserveOut;
try UNISWAP_ROUTER.getAmountsOut(finalAmountIn, pathWithWeth) returns ( try UNISWAP_ROUTER.getAmountsOut(vars.finalAmountIn, vars.pathWithWeth) returns (
uint256[] memory resultsWithWeth uint256[] memory resultsWithWeth
) { ) {
amountsWithWeth = resultsWithWeth; vars.amountsWithWeth = resultsWithWeth;
} catch { } catch {
amountsWithWeth = new uint256[](3); vars.amountsWithWeth = new uint256[](3);
} }
} else { } else {
amountsWithWeth = new uint256[](3); vars.amountsWithWeth = new uint256[](3);
} }
uint256 bestAmountOut; uint256 bestAmountOut;
try UNISWAP_ROUTER.getAmountsOut(finalAmountIn, simplePath) returns ( try UNISWAP_ROUTER.getAmountsOut(vars.finalAmountIn, vars.simplePath) returns (
uint256[] memory resultAmounts uint256[] memory resultAmounts
) { ) {
amountsWithoutWeth = resultAmounts; vars.amountsWithoutWeth = resultAmounts;
bestAmountOut = (amountsWithWeth[2] > amountsWithoutWeth[1]) bestAmountOut = (vars.amountsWithWeth[2] > vars.amountsWithoutWeth[1])
? amountsWithWeth[2] ? vars.amountsWithWeth[2]
: amountsWithoutWeth[1]; : vars.amountsWithoutWeth[1];
} catch { } catch {
amountsWithoutWeth = new uint256[](2); vars.amountsWithoutWeth = new uint256[](2);
bestAmountOut = amountsWithWeth[2]; bestAmountOut = vars.amountsWithWeth[2];
} }
uint256 reserveInDecimals = _getDecimals(reserveIn); uint256 reserveInDecimals = _getDecimals(reserveIn);
uint256 reserveOutDecimals = _getDecimals(reserveOut); uint256 reserveOutDecimals = _getDecimals(reserveOut);
uint256 outPerInPrice = uint256 outPerInPrice =
finalAmountIn.mul(10**18).mul(10**reserveOutDecimals).div( vars.finalAmountIn.mul(10**18).mul(10**reserveOutDecimals).div(
bestAmountOut.mul(10**reserveInDecimals) bestAmountOut.mul(10**reserveInDecimals)
); );
@ -387,9 +398,9 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
outPerInPrice, outPerInPrice,
_calcUsdValue(reserveIn, amountIn, reserveInDecimals), _calcUsdValue(reserveIn, amountIn, reserveInDecimals),
_calcUsdValue(reserveOut, bestAmountOut, reserveOutDecimals), _calcUsdValue(reserveOut, bestAmountOut, reserveOutDecimals),
(bestAmountOut == 0) ? new address[](2) : (bestAmountOut == amountsWithoutWeth[1]) (bestAmountOut == 0) ? new address[](2) : (bestAmountOut == vars.amountsWithoutWeth[1])
? simplePath ? vars.simplePath
: pathWithWeth : vars.pathWithWeth
); );
} }
@ -407,13 +418,15 @@ abstract contract BaseUniswapAdapter is FlashLoanReceiverBase, IBaseUniswapAdapt
function _getAmountsInData( function _getAmountsInData(
address reserveIn, address reserveIn,
address reserveOut, address reserveOut,
uint256 amountOut uint256 amountOut,
bool withFlash
) internal view returns (AmountCalc memory) { ) internal view returns (AmountCalc memory) {
(uint256[] memory amounts, address[] memory path) = (uint256[] memory amounts, address[] memory path) =
_getAmountsInAndPath(reserveIn, reserveOut, amountOut); _getAmountsInAndPath(reserveIn, reserveOut, amountOut);
// Add flash loan fee // Add flash loan fee
uint256 finalAmountIn = amounts[0].add(amounts[0].mul(FLASHLOAN_PREMIUM_TOTAL).div(10000)); uint256 finalAmountIn =
amounts[0].add(withFlash ? amounts[0].mul(FLASHLOAN_PREMIUM_TOTAL).div(10000) : 0);
uint256 reserveInDecimals = _getDecimals(reserveIn); uint256 reserveInDecimals = _getDecimals(reserveIn);
uint256 reserveOutDecimals = _getDecimals(reserveOut); uint256 reserveOutDecimals = _getDecimals(reserveOut);

View File

@ -50,7 +50,8 @@ interface IBaseUniswapAdapter {
function getAmountsOut( function getAmountsOut(
uint256 amountIn, uint256 amountIn,
address reserveIn, address reserveIn,
address reserveOut address reserveOut,
bool withFlash
) )
external external
view view
@ -76,7 +77,8 @@ interface IBaseUniswapAdapter {
function getAmountsIn( function getAmountsIn(
uint256 amountOut, uint256 amountOut,
address reserveIn, address reserveIn,
address reserveOut address reserveOut,
bool withFlash
) )
external external
view view

View File

@ -19,10 +19,6 @@ task(`deploy-${CONTRACT_NAME}`, `Deploys the ${CONTRACT_NAME} contract`)
} }
console.log(`\n- ${CONTRACT_NAME} deployment`); console.log(`\n- ${CONTRACT_NAME} deployment`);
// const args = [
// '0x88757f2f99175387aB4C6a4b3067c77A695b0349', // lending provider kovan address
// '0xfcd87315f0e4067070ade8682fcdbc3006631441', // uniswap router address
// ];
const uniswapRepayAdapter = await new UniswapRepayAdapterFactory(await getFirstSigner()).deploy( const uniswapRepayAdapter = await new UniswapRepayAdapterFactory(await getFirstSigner()).deploy(
provider, provider,
router, router,