Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from ionicprotocol/feat/lens-hypothetical-repay
Browse files Browse the repository at this point in the history
created lens function for getting hypothetical health factor
  • Loading branch information
rhlsthrm authored May 10, 2024
2 parents 2d81f9f + c33f8b0 commit 2493935
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 18 deletions.
19 changes: 18 additions & 1 deletion contracts/PoolLens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,24 @@ contract PoolLens is Initializable {
}

function getHealthFactor(address user, IonicComptroller pool) external view returns (uint256) {
(uint256 err, uint256 collateralValue, uint256 liquidity, uint256 shortfall) = pool.getAccountLiquidity(user);
return getHealthFactorHypothetical(pool, user, address(0), 0, 0, 0);
}

function getHealthFactorHypothetical(
IonicComptroller pool,
address account,
address cTokenModify,
uint256 redeemTokens,
uint256 borrowAmount,
uint256 repayAmount
) public view returns (uint256) {
(uint256 err, uint256 collateralValue, uint256 liquidity, uint256 shortfall) = pool.getHypotheticalAccountLiquidity(
account,
cTokenModify,
redeemTokens,
borrowAmount,
repayAmount
);

if (err != 0) revert ComptrollerError(err);

Expand Down
41 changes: 33 additions & 8 deletions contracts/compound/Comptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
redeemer,
ICErc20(cToken),
redeemTokens,
0,
0
);
if (err != Error.NO_ERROR) {
Expand Down Expand Up @@ -371,6 +372,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
account,
isBorrow ? cTokenModify : ICErc20(address(0)),
0,
0,
0
);
require(err == Error.NO_ERROR, "!liquidity");
Expand Down Expand Up @@ -482,7 +484,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
flywheelPreBorrowerAction(cToken, borrower);

// Perform a hypothetical liquidity check to guard against shortfall
(uint256 err, , , uint256 shortfall) = this.getHypotheticalAccountLiquidity(borrower, cToken, 0, borrowAmount);
(uint256 err, , , uint256 shortfall) = this.getHypotheticalAccountLiquidity(borrower, cToken, 0, borrowAmount, 0);
if (err != uint256(Error.NO_ERROR)) {
return err;
}
Expand Down Expand Up @@ -573,7 +575,13 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
require(borrowBalance >= repayAmount, "!borrow>repay");
} else {
/* The borrower must have shortfall in order to be liquidateable */
(Error err, , , uint256 shortfall) = getHypotheticalAccountLiquidityInternal(borrower, ICErc20(address(0)), 0, 0);
(Error err, , , uint256 shortfall) = getHypotheticalAccountLiquidityInternal(
borrower,
ICErc20(address(0)),
0,
0,
0
);
if (err != Error.NO_ERROR) {
return uint256(err);
}
Expand Down Expand Up @@ -714,6 +722,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
Exp tokensToDenom;
uint256 borrowCapForCollateral;
uint256 borrowedAssetPrice;
uint256 assetAsCollateralValueCap;
}

function getAccountLiquidity(address account)
Expand All @@ -732,7 +741,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
uint256 collateralValue,
uint256 liquidity,
uint256 shortfall
) = getHypotheticalAccountLiquidityInternal(account, ICErc20(address(0)), 0, 0);
) = getHypotheticalAccountLiquidityInternal(account, ICErc20(address(0)), 0, 0, 0);
return (uint256(err), collateralValue, liquidity, shortfall);
}

Expand All @@ -750,7 +759,8 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
address account,
address cTokenModify,
uint256 redeemTokens,
uint256 borrowAmount
uint256 borrowAmount,
uint256 repayAmount
)
public
view
Expand All @@ -766,7 +776,13 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
uint256 collateralValue,
uint256 liquidity,
uint256 shortfall
) = getHypotheticalAccountLiquidityInternal(account, ICErc20(cTokenModify), redeemTokens, borrowAmount);
) = getHypotheticalAccountLiquidityInternal(
account,
ICErc20(cTokenModify),
redeemTokens,
borrowAmount,
repayAmount
);
return (uint256(err), collateralValue, liquidity, shortfall);
}

Expand All @@ -785,7 +801,8 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
address account,
ICErc20 cTokenModify,
uint256 redeemTokens,
uint256 borrowAmount
uint256 borrowAmount,
uint256 repayAmount
)
internal
view
Expand Down Expand Up @@ -833,7 +850,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
}
{
// Exclude the asset-to-be-borrowed from the liquidity, except for when redeeming
uint256 assetAsCollateralValueCap = asComptrollerExtension().getAssetAsCollateralValueCap(
vars.assetAsCollateralValueCap = asComptrollerExtension().getAssetAsCollateralValueCap(
vars.asset,
cTokenModify,
redeemTokens > 0,
Expand All @@ -842,7 +859,8 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR

// accumulate the collateral value to sumCollateral
uint256 assetCollateralValue = mul_ScalarTruncate(vars.tokensToDenom, vars.cTokenBalance);
if (assetCollateralValue > assetAsCollateralValueCap) assetCollateralValue = assetAsCollateralValueCap;
if (assetCollateralValue > vars.assetAsCollateralValueCap)
assetCollateralValue = vars.assetAsCollateralValueCap;
vars.sumCollateral += assetCollateralValue;
}

Expand Down Expand Up @@ -870,6 +888,13 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
borrowAmount,
vars.sumBorrowPlusEffects
);

uint256 repayEffect = mul_ScalarTruncate(vars.oraclePrice, repayAmount);
if (repayEffect >= vars.sumBorrowPlusEffects) {
vars.sumBorrowPlusEffects = 0;
} else {
vars.sumBorrowPlusEffects -= repayEffect;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion contracts/compound/ComptrollerInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ interface ComptrollerInterface {
address account,
address cTokenModify,
uint256 redeemTokens,
uint256 borrowAmount
uint256 borrowAmount,
uint256 repayAmount
)
external
view
Expand Down
3 changes: 2 additions & 1 deletion contracts/external/compound/IComptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ interface IComptroller {
address account,
address cTokenModify,
uint256 redeemTokens,
uint256 borrowAmount
uint256 borrowAmount,
uint256 repayAmount
)
external
view
Expand Down
13 changes: 8 additions & 5 deletions contracts/test/LeveredPositionTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ contract LeveredPositionLensTest is BaseTest {
}

function testPrintLeveredPositions() public debuggingOnly fork(POLYGON_MAINNET) {
address[] memory markets = factory.getWhitelistedCollateralMarkets();
address[] memory accounts = factory.getAccountsWithOpenPositions();

emit log_named_array("markets", markets);
emit log_named_array("accounts", accounts);

for (uint256 j = 0; j < markets.length; j++) {
address[] memory borrowable = factory.getBorrowableMarketsByCollateral(ICErc20(markets[j]));
emit log_named_array("borrowable", borrowable);
for (uint256 j = 0; j < accounts.length; j++) {
address[] memory positions;
bool[] memory closed;
(positions, closed) = factory.getPositionsByAccount(accounts[j]);
emit log_named_array("positions", positions);
//emit log_named_array("closed", closed);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions contracts/test/MaxBorrowTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ contract MaxBorrowTest is WithPool {
someBorrower,
address(marketToBorrow),
0,
borrowAmount
borrowAmount,
0
);
emit log("errBefore");
emit log_uint(errBefore);
Expand All @@ -182,7 +183,8 @@ contract MaxBorrowTest is WithPool {
someBorrower,
address(marketToBorrow),
0,
borrowAmount
borrowAmount,
0
);
emit log("errAfter");
emit log_uint(errAfter);
Expand Down
3 changes: 3 additions & 0 deletions contracts/test/PoolCapsAndBlacklistsTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ contract PoolCapsAndBlacklistsTest is MarketsTest {
borrower,
address(ankrBNBMkt),
0,
0,
0
);
assertEq(shortFallBefore, 0, "should have no shortfall before");
Expand All @@ -105,6 +106,7 @@ contract PoolCapsAndBlacklistsTest is MarketsTest {
borrower,
address(ankrBNBMkt),
0,
0,
0
);
assertGt(liquidityBefore - liquidityAfterBlacklist, 0, "should have lower liquidity after bl");
Expand All @@ -121,6 +123,7 @@ contract PoolCapsAndBlacklistsTest is MarketsTest {
borrower,
address(ankrBNBMkt),
0,
0,
0
);
assertEq(shortFallWhitelist, shortFallBefore, "should have the same sf after wl");
Expand Down

0 comments on commit 2493935

Please sign in to comment.