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

Commit 2493935

Browse files
authored
Merge pull request #61 from ionicprotocol/feat/lens-hypothetical-repay
created lens function for getting hypothetical health factor
2 parents 2d81f9f + c33f8b0 commit 2493935

File tree

7 files changed

+70
-18
lines changed

7 files changed

+70
-18
lines changed

contracts/PoolLens.sol

+18-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,24 @@ contract PoolLens is Initializable {
584584
}
585585

586586
function getHealthFactor(address user, IonicComptroller pool) external view returns (uint256) {
587-
(uint256 err, uint256 collateralValue, uint256 liquidity, uint256 shortfall) = pool.getAccountLiquidity(user);
587+
return getHealthFactorHypothetical(pool, user, address(0), 0, 0, 0);
588+
}
589+
590+
function getHealthFactorHypothetical(
591+
IonicComptroller pool,
592+
address account,
593+
address cTokenModify,
594+
uint256 redeemTokens,
595+
uint256 borrowAmount,
596+
uint256 repayAmount
597+
) public view returns (uint256) {
598+
(uint256 err, uint256 collateralValue, uint256 liquidity, uint256 shortfall) = pool.getHypotheticalAccountLiquidity(
599+
account,
600+
cTokenModify,
601+
redeemTokens,
602+
borrowAmount,
603+
repayAmount
604+
);
588605

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

contracts/compound/Comptroller.sol

+33-8
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
307307
redeemer,
308308
ICErc20(cToken),
309309
redeemTokens,
310+
0,
310311
0
311312
);
312313
if (err != Error.NO_ERROR) {
@@ -371,6 +372,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
371372
account,
372373
isBorrow ? cTokenModify : ICErc20(address(0)),
373374
0,
375+
0,
374376
0
375377
);
376378
require(err == Error.NO_ERROR, "!liquidity");
@@ -482,7 +484,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
482484
flywheelPreBorrowerAction(cToken, borrower);
483485

484486
// Perform a hypothetical liquidity check to guard against shortfall
485-
(uint256 err, , , uint256 shortfall) = this.getHypotheticalAccountLiquidity(borrower, cToken, 0, borrowAmount);
487+
(uint256 err, , , uint256 shortfall) = this.getHypotheticalAccountLiquidity(borrower, cToken, 0, borrowAmount, 0);
486488
if (err != uint256(Error.NO_ERROR)) {
487489
return err;
488490
}
@@ -573,7 +575,13 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
573575
require(borrowBalance >= repayAmount, "!borrow>repay");
574576
} else {
575577
/* The borrower must have shortfall in order to be liquidateable */
576-
(Error err, , , uint256 shortfall) = getHypotheticalAccountLiquidityInternal(borrower, ICErc20(address(0)), 0, 0);
578+
(Error err, , , uint256 shortfall) = getHypotheticalAccountLiquidityInternal(
579+
borrower,
580+
ICErc20(address(0)),
581+
0,
582+
0,
583+
0
584+
);
577585
if (err != Error.NO_ERROR) {
578586
return uint256(err);
579587
}
@@ -714,6 +722,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
714722
Exp tokensToDenom;
715723
uint256 borrowCapForCollateral;
716724
uint256 borrowedAssetPrice;
725+
uint256 assetAsCollateralValueCap;
717726
}
718727

719728
function getAccountLiquidity(address account)
@@ -732,7 +741,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
732741
uint256 collateralValue,
733742
uint256 liquidity,
734743
uint256 shortfall
735-
) = getHypotheticalAccountLiquidityInternal(account, ICErc20(address(0)), 0, 0);
744+
) = getHypotheticalAccountLiquidityInternal(account, ICErc20(address(0)), 0, 0, 0);
736745
return (uint256(err), collateralValue, liquidity, shortfall);
737746
}
738747

@@ -750,7 +759,8 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
750759
address account,
751760
address cTokenModify,
752761
uint256 redeemTokens,
753-
uint256 borrowAmount
762+
uint256 borrowAmount,
763+
uint256 repayAmount
754764
)
755765
public
756766
view
@@ -766,7 +776,13 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
766776
uint256 collateralValue,
767777
uint256 liquidity,
768778
uint256 shortfall
769-
) = getHypotheticalAccountLiquidityInternal(account, ICErc20(cTokenModify), redeemTokens, borrowAmount);
779+
) = getHypotheticalAccountLiquidityInternal(
780+
account,
781+
ICErc20(cTokenModify),
782+
redeemTokens,
783+
borrowAmount,
784+
repayAmount
785+
);
770786
return (uint256(err), collateralValue, liquidity, shortfall);
771787
}
772788

@@ -785,7 +801,8 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
785801
address account,
786802
ICErc20 cTokenModify,
787803
uint256 redeemTokens,
788-
uint256 borrowAmount
804+
uint256 borrowAmount,
805+
uint256 repayAmount
789806
)
790807
internal
791808
view
@@ -833,7 +850,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
833850
}
834851
{
835852
// Exclude the asset-to-be-borrowed from the liquidity, except for when redeeming
836-
uint256 assetAsCollateralValueCap = asComptrollerExtension().getAssetAsCollateralValueCap(
853+
vars.assetAsCollateralValueCap = asComptrollerExtension().getAssetAsCollateralValueCap(
837854
vars.asset,
838855
cTokenModify,
839856
redeemTokens > 0,
@@ -842,7 +859,8 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
842859

843860
// accumulate the collateral value to sumCollateral
844861
uint256 assetCollateralValue = mul_ScalarTruncate(vars.tokensToDenom, vars.cTokenBalance);
845-
if (assetCollateralValue > assetAsCollateralValueCap) assetCollateralValue = assetAsCollateralValueCap;
862+
if (assetCollateralValue > vars.assetAsCollateralValueCap)
863+
assetCollateralValue = vars.assetAsCollateralValueCap;
846864
vars.sumCollateral += assetCollateralValue;
847865
}
848866

@@ -870,6 +888,13 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
870888
borrowAmount,
871889
vars.sumBorrowPlusEffects
872890
);
891+
892+
uint256 repayEffect = mul_ScalarTruncate(vars.oraclePrice, repayAmount);
893+
if (repayEffect >= vars.sumBorrowPlusEffects) {
894+
vars.sumBorrowPlusEffects = 0;
895+
} else {
896+
vars.sumBorrowPlusEffects -= repayEffect;
897+
}
873898
}
874899
}
875900

contracts/compound/ComptrollerInterface.sol

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ interface ComptrollerInterface {
3939
address account,
4040
address cTokenModify,
4141
uint256 redeemTokens,
42-
uint256 borrowAmount
42+
uint256 borrowAmount,
43+
uint256 repayAmount
4344
)
4445
external
4546
view

contracts/external/compound/IComptroller.sol

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ interface IComptroller {
3535
address account,
3636
address cTokenModify,
3737
uint256 redeemTokens,
38-
uint256 borrowAmount
38+
uint256 borrowAmount,
39+
uint256 repayAmount
3940
)
4041
external
4142
view

contracts/test/LeveredPositionTest.t.sol

+8-5
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,16 @@ contract LeveredPositionLensTest is BaseTest {
8686
}
8787

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

91-
emit log_named_array("markets", markets);
91+
emit log_named_array("accounts", accounts);
9292

93-
for (uint256 j = 0; j < markets.length; j++) {
94-
address[] memory borrowable = factory.getBorrowableMarketsByCollateral(ICErc20(markets[j]));
95-
emit log_named_array("borrowable", borrowable);
93+
for (uint256 j = 0; j < accounts.length; j++) {
94+
address[] memory positions;
95+
bool[] memory closed;
96+
(positions, closed) = factory.getPositionsByAccount(accounts[j]);
97+
emit log_named_array("positions", positions);
98+
//emit log_named_array("closed", closed);
9699
}
97100
}
98101
}

contracts/test/MaxBorrowTest.t.sol

+4-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ contract MaxBorrowTest is WithPool {
162162
someBorrower,
163163
address(marketToBorrow),
164164
0,
165-
borrowAmount
165+
borrowAmount,
166+
0
166167
);
167168
emit log("errBefore");
168169
emit log_uint(errBefore);
@@ -182,7 +183,8 @@ contract MaxBorrowTest is WithPool {
182183
someBorrower,
183184
address(marketToBorrow),
184185
0,
185-
borrowAmount
186+
borrowAmount,
187+
0
186188
);
187189
emit log("errAfter");
188190
emit log_uint(errAfter);

contracts/test/PoolCapsAndBlacklistsTest.t.sol

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ contract PoolCapsAndBlacklistsTest is MarketsTest {
9393
borrower,
9494
address(ankrBNBMkt),
9595
0,
96+
0,
9697
0
9798
);
9899
assertEq(shortFallBefore, 0, "should have no shortfall before");
@@ -105,6 +106,7 @@ contract PoolCapsAndBlacklistsTest is MarketsTest {
105106
borrower,
106107
address(ankrBNBMkt),
107108
0,
109+
0,
108110
0
109111
);
110112
assertGt(liquidityBefore - liquidityAfterBlacklist, 0, "should have lower liquidity after bl");
@@ -121,6 +123,7 @@ contract PoolCapsAndBlacklistsTest is MarketsTest {
121123
borrower,
122124
address(ankrBNBMkt),
123125
0,
126+
0,
124127
0
125128
);
126129
assertEq(shortFallWhitelist, shortFallBefore, "should have the same sf after wl");

0 commit comments

Comments
 (0)