From 1e1b094c6dabeec8b8897a3f72ba0bd6fa8880a8 Mon Sep 17 00:00:00 2001 From: Samyak Jain Date: Sun, 14 Jul 2019 17:29:31 +0530 Subject: [PATCH] getSave and getLeverage --- contracts/ProxyLogics/InstaCompSave.sol | 51 ++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/contracts/ProxyLogics/InstaCompSave.sol b/contracts/ProxyLogics/InstaCompSave.sol index fe4b46c..d85bb74 100644 --- a/contracts/ProxyLogics/InstaCompSave.sol +++ b/contracts/ProxyLogics/InstaCompSave.sol @@ -211,6 +211,48 @@ contract CompoundHelper is Helpers { ratio = wdiv(totalBorrow, totalSupply); } + function getSave( + address user, + uint ethToFree, + address[] memory cTokenAddr, + uint[] memory ctokenFactor + ) public returns (uint finalColInEth, uint finalDebtInEth, uint daiDebt, bool isOk) + { + (uint totalSupply, uint totalBorrow,,,uint maxWithdraw,) = getCompStats(user, cTokenAddr, ctokenFactor); + uint ethToSwap = ethToFree < maxWithdraw ? ethToFree : maxWithdraw; + (, uint expectedDAI) = SplitSwapInterface(getAddressSplitSwap()).getBest(getAddressETH(), getAddressDAI(), ethToSwap); + uint daiBorrowed = daiBorrowed(user); + uint daiInEth = CompOracleInterface(getCompOracleAddress()).getUnderlyingPrice(getCDAIAddress()); + if (daiBorrowed < expectedDAI) { + finalColInEth = sub(totalSupply, ethToSwap); + finalDebtInEth = sub(totalBorrow, wmul(daiBorrowed, daiInEth)); + daiDebt = 0; + isOk = false; + } else { + finalColInEth = sub(totalSupply, ethToSwap); + finalDebtInEth = sub(totalBorrow, wmul(expectedDAI, daiInEth)); + daiDebt = sub(daiBorrowed, expectedDAI); + isOk = true; + } + } + + function getLeverage( + address user, + uint daiToBorrow, + address[] memory cTokenAddr, + uint[] memory ctokenFactor + ) public returns (uint finalColInEth, uint finalDebtInEth, uint ethCol) + { + (uint totalSupply, uint totalBorrow,, uint borrowRemain,,) = getCompStats(user, cTokenAddr, ctokenFactor); + uint daiToSwap = getDaiRemainBorrow(borrowRemain); + daiToSwap = daiToSwap < daiToBorrow ? daiToSwap : daiToBorrow; + (, uint expectedETH) = SplitSwapInterface(getAddressSplitSwap()).getBest(getAddressDAI(), getAddressETH(), daiToSwap); + uint daiInEth = CompOracleInterface(getCompOracleAddress()).getUnderlyingPrice(getCDAIAddress()); + finalColInEth = add(totalSupply, expectedETH); + finalDebtInEth = add(totalBorrow, wmul(daiToSwap, daiInEth)); + ethCol = add(getEthSupply(user), expectedETH); + } + function getEthSupply(address user) internal returns (uint ethSupply) { CTokenInterface cTokenContract = CTokenInterface(getCETHAddress()); uint cTokenBal = sub(cTokenContract.balanceOf(user), 1); @@ -218,6 +260,11 @@ contract CompoundHelper is Helpers { ethSupply = wmul(cTokenBal, cTokenExchangeRate); } + function daiBorrowed(address user) internal returns (uint daiAmt) { + CTokenInterface cTokenContract = CTokenInterface(getCDAIAddress()); + daiAmt = cTokenContract.borrowBalanceCurrent(user); + } + function getDaiRemainBorrow(uint daiInEth) internal view returns (uint daiAmt) { uint tokenPriceInEth = CompOracleInterface(getCompOracleAddress()).getUnderlyingPrice(getCDAIAddress()); daiAmt = sub(wdiv(daiInEth, tokenPriceInEth), 10); @@ -304,13 +351,13 @@ contract CompoundSave is CompoundResolver { function save( uint ethToFree, - address[] memory cTokenAddr, + address[] memory ctokenAddr, uint[] memory ctokenFactor, uint splitAmt, uint slippageAmt ) public { - (,,,,uint maxWithdraw,) = getCompStats(address(this), cTokenAddr, ctokenFactor); + (,,,,uint maxWithdraw,) = getCompStats(address(this), ctokenAddr, ctokenFactor); uint ethToSwap = ethToFree < maxWithdraw ? ethToFree : maxWithdraw; redeemEth(ethToSwap); uint destAmt = SplitSwapInterface(getAddressSplitSwap()).ethToDaiSwap.value(ethToSwap)(splitAmt, slippageAmt);