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

Commit 544bf46

Browse files
Merge branch 'feat/lens-hypothetical-repay' into feat/pyth-integration
2 parents 5db558a + 090cbcd commit 544bf46

26 files changed

+950
-163
lines changed

contracts/IonicUniV3Liquidator.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ contract IonicUniV3Liquidator is OwnableUpgradeable, ILiquidator, IUniswapV3Flas
156156
uniswapV3FlashCallback(fee0, fee1, data);
157157
}
158158

159+
function algebraFlashCallback(
160+
uint256 fee0,
161+
uint256 fee1,
162+
bytes calldata data
163+
) external {
164+
uniswapV3FlashCallback(fee0, fee1, data);
165+
}
166+
159167
function uniswapV3FlashCallback(
160168
uint256 fee0,
161169
uint256 fee1,

contracts/PoolLens.sol

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ contract PoolLens is Initializable {
110110
*/
111111
function getPublicPoolsWithData()
112112
external
113-
returns (uint256[] memory, PoolDirectory.Pool[] memory, IonicPoolData[] memory, bool[] memory)
113+
returns (
114+
uint256[] memory,
115+
PoolDirectory.Pool[] memory,
116+
IonicPoolData[] memory,
117+
bool[] memory
118+
)
114119
{
115120
(uint256[] memory indexes, PoolDirectory.Pool[] memory publicPools) = directory.getPublicPools();
116121
(IonicPoolData[] memory data, bool[] memory errored) = getPoolsData(publicPools);
@@ -122,9 +127,15 @@ contract PoolLens is Initializable {
122127
* @dev This function is not designed to be called in a transaction: it is too gas-intensive.
123128
* Ideally, we can add the `view` modifier, but many cToken functions potentially modify the state.
124129
*/
125-
function getPublicPoolsByVerificationWithData(
126-
bool whitelistedAdmin
127-
) external returns (uint256[] memory, PoolDirectory.Pool[] memory, IonicPoolData[] memory, bool[] memory) {
130+
function getPublicPoolsByVerificationWithData(bool whitelistedAdmin)
131+
external
132+
returns (
133+
uint256[] memory,
134+
PoolDirectory.Pool[] memory,
135+
IonicPoolData[] memory,
136+
bool[] memory
137+
)
138+
{
128139
(uint256[] memory indexes, PoolDirectory.Pool[] memory publicPools) = directory.getPublicPoolsByVerification(
129140
whitelistedAdmin
130141
);
@@ -137,9 +148,15 @@ contract PoolLens is Initializable {
137148
* @dev This function is not designed to be called in a transaction: it is too gas-intensive.
138149
* Ideally, we can add the `view` modifier, but many cToken functions potentially modify the state.
139150
*/
140-
function getPoolsByAccountWithData(
141-
address account
142-
) external returns (uint256[] memory, PoolDirectory.Pool[] memory, IonicPoolData[] memory, bool[] memory) {
151+
function getPoolsByAccountWithData(address account)
152+
external
153+
returns (
154+
uint256[] memory,
155+
PoolDirectory.Pool[] memory,
156+
IonicPoolData[] memory,
157+
bool[] memory
158+
)
159+
{
143160
(uint256[] memory indexes, PoolDirectory.Pool[] memory accountPools) = directory.getPoolsByAccount(account);
144161
(IonicPoolData[] memory data, bool[] memory errored) = getPoolsData(accountPools);
145162
return (indexes, accountPools, data, errored);
@@ -150,9 +167,15 @@ contract PoolLens is Initializable {
150167
* @dev This function is not designed to be called in a transaction: it is too gas-intensive.
151168
* Ideally, we can add the `view` modifier, but many cToken functions potentially modify the state.
152169
*/
153-
function getPoolsOIonicrWithData(
154-
address user
155-
) external returns (uint256[] memory, PoolDirectory.Pool[] memory, IonicPoolData[] memory, bool[] memory) {
170+
function getPoolsOIonicrWithData(address user)
171+
external
172+
returns (
173+
uint256[] memory,
174+
PoolDirectory.Pool[] memory,
175+
IonicPoolData[] memory,
176+
bool[] memory
177+
)
178+
{
156179
(uint256[] memory indexes, PoolDirectory.Pool[] memory userPools) = directory.getPoolsOfUser(user);
157180
(IonicPoolData[] memory data, bool[] memory errored) = getPoolsData(userPools);
158181
return (indexes, userPools, data, errored);
@@ -187,9 +210,16 @@ contract PoolLens is Initializable {
187210
/**
188211
* @notice Returns total supply balance (in ETH), total borrow balance (in ETH), underlying token addresses, and underlying token symbols of a Ionic pool.
189212
*/
190-
function getPoolSummary(
191-
IonicComptroller comptroller
192-
) external returns (uint256, uint256, address[] memory, string[] memory, bool) {
213+
function getPoolSummary(IonicComptroller comptroller)
214+
external
215+
returns (
216+
uint256,
217+
uint256,
218+
address[] memory,
219+
string[] memory,
220+
bool
221+
)
222+
{
193223
uint256 totalBorrow = 0;
194224
uint256 totalSupply = 0;
195225
ICErc20[] memory cTokens = comptroller.getAllMarkets();
@@ -329,10 +359,7 @@ contract PoolLens is Initializable {
329359
return (detailedAssets);
330360
}
331361

332-
function getBorrowCapsPerCollateral(
333-
ICErc20 borrowedAsset,
334-
IonicComptroller comptroller
335-
)
362+
function getBorrowCapsPerCollateral(ICErc20 borrowedAsset, IonicComptroller comptroller)
336363
internal
337364
view
338365
returns (
@@ -431,9 +458,15 @@ contract PoolLens is Initializable {
431458
* @notice returns the total supply cap for each asset in the pool and the total non-whitelist supplied assets
432459
* @dev This function is not designed to be called in a transaction: it is too gas-intensive.
433460
*/
434-
function getSupplyCapsDataForPool(
435-
IonicComptroller comptroller
436-
) public view returns (address[] memory, uint256[] memory, uint256[] memory) {
461+
function getSupplyCapsDataForPool(IonicComptroller comptroller)
462+
public
463+
view
464+
returns (
465+
address[] memory,
466+
uint256[] memory,
467+
uint256[] memory
468+
)
469+
{
437470
ICErc20[] memory poolMarkets = comptroller.getAllMarkets();
438471

439472
address[] memory assets = new address[](poolMarkets.length);
@@ -455,9 +488,7 @@ contract PoolLens is Initializable {
455488
* @notice returns the total borrow cap and the per collateral borrowing cap/blacklist for the asset
456489
* @dev This function is not designed to be called in a transaction: it is too gas-intensive.
457490
*/
458-
function getBorrowCapsForAsset(
459-
ICErc20 asset
460-
)
491+
function getBorrowCapsForAsset(ICErc20 asset)
461492
public
462493
view
463494
returns (
@@ -476,9 +507,7 @@ contract PoolLens is Initializable {
476507
* @notice returns the total borrow cap, the per collateral borrowing cap/blacklist for the asset and the total non-whitelist borrows
477508
* @dev This function is not designed to be called in a transaction: it is too gas-intensive.
478509
*/
479-
function getBorrowCapsDataForAsset(
480-
ICErc20 asset
481-
)
510+
function getBorrowCapsDataForAsset(ICErc20 asset)
482511
public
483512
view
484513
returns (
@@ -503,9 +532,11 @@ contract PoolLens is Initializable {
503532
* Note that the whitelist does not have to be enforced.
504533
* @dev This function is not designed to be called in a transaction: it is too gas-intensive.
505534
*/
506-
function getWhitelistedPoolsByAccount(
507-
address account
508-
) public view returns (uint256[] memory, PoolDirectory.Pool[] memory) {
535+
function getWhitelistedPoolsByAccount(address account)
536+
public
537+
view
538+
returns (uint256[] memory, PoolDirectory.Pool[] memory)
539+
{
509540
(, PoolDirectory.Pool[] memory pools) = directory.getActivePools();
510541
uint256 arrayLength = 0;
511542

@@ -538,27 +569,22 @@ contract PoolLens is Initializable {
538569
* @dev This function is not designed to be called in a transaction: it is too gas-intensive.
539570
* Ideally, we can add the `view` modifier, but many cToken functions potentially modify the state.
540571
*/
541-
function getWhitelistedPoolsByAccountWithData(
542-
address account
543-
) external returns (uint256[] memory, PoolDirectory.Pool[] memory, IonicPoolData[] memory, bool[] memory) {
572+
function getWhitelistedPoolsByAccountWithData(address account)
573+
external
574+
returns (
575+
uint256[] memory,
576+
PoolDirectory.Pool[] memory,
577+
IonicPoolData[] memory,
578+
bool[] memory
579+
)
580+
{
544581
(uint256[] memory indexes, PoolDirectory.Pool[] memory accountPools) = getWhitelistedPoolsByAccount(account);
545582
(IonicPoolData[] memory data, bool[] memory errored) = getPoolsData(accountPools);
546583
return (indexes, accountPools, data, errored);
547584
}
548585

549586
function getHealthFactor(address user, IonicComptroller pool) external view returns (uint256) {
550-
(uint256 err, uint256 collateralValue, uint256 liquidity, uint256 shortfall) = pool.getAccountLiquidity(user);
551-
552-
if (err != 0) revert ComptrollerError(err);
553-
554-
if (shortfall > 0) {
555-
// HF < 1.0
556-
return (collateralValue * 1e18) / (collateralValue + shortfall);
557-
} else {
558-
// HF >= 1.0
559-
if (collateralValue <= liquidity) return type(uint256).max;
560-
else return (collateralValue * 1e18) / (collateralValue - liquidity);
561-
}
587+
return getHealthFactorHypothetical(pool, user, address(0), 0, 0, 0);
562588
}
563589

564590
function getHealthFactorHypothetical(
@@ -568,7 +594,7 @@ contract PoolLens is Initializable {
568594
uint256 redeemTokens,
569595
uint256 borrowAmount,
570596
uint256 repayAmount
571-
) external view returns (uint256) {
597+
) public view returns (uint256) {
572598
(uint256 err, uint256 collateralValue, uint256 liquidity, uint256 shortfall) = pool.getHypotheticalAccountLiquidity(
573599
account,
574600
cTokenModify,

contracts/compound/Comptroller.sol

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
228228
* @param mintAmount The amount of underlying being supplied to the market in exchange for tokens
229229
* @return 0 if the mint is allowed, otherwise a semi-opaque error code (See ErrorReporter.sol)
230230
*/
231-
function mintAllowed(address cTokenAddress, address minter, uint256 mintAmount) external override returns (uint256) {
231+
function mintAllowed(
232+
address cTokenAddress,
233+
address minter,
234+
uint256 mintAmount
235+
) external override returns (uint256) {
232236
// Pausing is a very serious situation - we revert to sound the alarms
233237
require(!mintGuardianPaused[cTokenAddress], "!mint:paused");
234238

@@ -268,7 +272,11 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
268272
* @param redeemTokens The number of cTokens to exchange for the underlying asset in the market
269273
* @return 0 if the redeem is allowed, otherwise a semi-opaque error code (See ErrorReporter.sol)
270274
*/
271-
function redeemAllowed(address cToken, address redeemer, uint256 redeemTokens) external override returns (uint256) {
275+
function redeemAllowed(
276+
address cToken,
277+
address redeemer,
278+
uint256 redeemTokens
279+
) external override returns (uint256) {
272280
uint256 allowed = redeemAllowedInternal(cToken, redeemer, redeemTokens);
273281
if (allowed != uint256(Error.NO_ERROR)) {
274282
return allowed;
@@ -319,7 +327,12 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
319327
* @param actualMintAmount The amount of the underlying asset being minted
320328
* @param mintTokens The number of tokens being minted
321329
*/
322-
function mintVerify(address cToken, address minter, uint256 actualMintAmount, uint256 mintTokens) external {
330+
function mintVerify(
331+
address cToken,
332+
address minter,
333+
uint256 actualMintAmount,
334+
uint256 mintTokens
335+
) external {
323336
// Add minter to suppliers mapping
324337
suppliers[minter] = true;
325338
}
@@ -417,7 +430,11 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
417430
* @param borrowAmount The amount of underlying the account would borrow
418431
* @return 0 if the borrow is allowed, otherwise a semi-opaque error code (See ErrorReporter.sol)
419432
*/
420-
function borrowAllowed(address cToken, address borrower, uint256 borrowAmount) external override returns (uint256) {
433+
function borrowAllowed(
434+
address cToken,
435+
address borrower,
436+
uint256 borrowAmount
437+
) external override returns (uint256) {
421438
// Pausing is a very serious situation - we revert to sound the alarms
422439
require(!borrowGuardianPaused[cToken], "!borrow:paused");
423440

@@ -558,13 +575,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
558575
require(borrowBalance >= repayAmount, "!borrow>repay");
559576
} else {
560577
/* The borrower must have shortfall in order to be liquidateable */
561-
(Error err, , , uint256 shortfall) = getHypotheticalAccountLiquidityInternal(
562-
borrower,
563-
ICErc20(address(0)),
564-
0,
565-
0,
566-
0
567-
);
578+
(Error err, , , uint256 shortfall) = getHypotheticalAccountLiquidityInternal(borrower, ICErc20(address(0)), 0, 0, 0);
568579
if (err != Error.NO_ERROR) {
569580
return uint256(err);
570581
}
@@ -675,7 +686,11 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
675686
* @param src The account which sources the tokens
676687
* @param dst The account which receives the tokens
677688
*/
678-
function flywheelPreTransferAction(address cToken, address src, address dst) internal {
689+
function flywheelPreTransferAction(
690+
address cToken,
691+
address src,
692+
address dst
693+
) internal {
679694
for (uint256 i = 0; i < rewardsDistributors.length; i++)
680695
IIonicFlywheel(rewardsDistributors[i]).flywheelPreTransferAction(cToken, src, dst);
681696
}
@@ -701,9 +716,20 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
701716
Exp tokensToDenom;
702717
uint256 borrowCapForCollateral;
703718
uint256 borrowedAssetPrice;
719+
uint256 assetAsCollateralValueCap;
704720
}
705721

706-
function getAccountLiquidity(address account) public view override returns (uint256, uint256, uint256, uint256) {
722+
function getAccountLiquidity(address account)
723+
public
724+
view
725+
override
726+
returns (
727+
uint256,
728+
uint256,
729+
uint256,
730+
uint256
731+
)
732+
{
707733
(
708734
Error err,
709735
uint256 collateralValue,
@@ -729,19 +755,22 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
729755
uint256 redeemTokens,
730756
uint256 borrowAmount,
731757
uint256 repayAmount
732-
) public view returns (uint256, uint256, uint256, uint256) {
758+
)
759+
public
760+
view
761+
returns (
762+
uint256,
763+
uint256,
764+
uint256,
765+
uint256
766+
)
767+
{
733768
(
734769
Error err,
735770
uint256 collateralValue,
736771
uint256 liquidity,
737772
uint256 shortfall
738-
) = getHypotheticalAccountLiquidityInternal(
739-
account,
740-
ICErc20(cTokenModify),
741-
redeemTokens,
742-
borrowAmount,
743-
repayAmount
744-
);
773+
) = getHypotheticalAccountLiquidityInternal(account, ICErc20(cTokenModify), redeemTokens, borrowAmount, repayAmount);
745774
return (uint256(err), collateralValue, liquidity, shortfall);
746775
}
747776

@@ -762,7 +791,16 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
762791
uint256 redeemTokens,
763792
uint256 borrowAmount,
764793
uint256 repayAmount
765-
) internal view returns (Error, uint256, uint256, uint256) {
794+
)
795+
internal
796+
view
797+
returns (
798+
Error,
799+
uint256,
800+
uint256,
801+
uint256
802+
)
803+
{
766804
AccountLiquidityLocalVars memory vars; // Holds all our calculation results
767805

768806
if (address(cTokenModify) != address(0)) {
@@ -800,7 +838,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
800838
}
801839
{
802840
// Exclude the asset-to-be-borrowed from the liquidity, except for when redeeming
803-
uint256 assetAsCollateralValueCap = asComptrollerExtension().getAssetAsCollateralValueCap(
841+
vars.assetAsCollateralValueCap = asComptrollerExtension().getAssetAsCollateralValueCap(
804842
vars.asset,
805843
cTokenModify,
806844
redeemTokens > 0,
@@ -809,7 +847,7 @@ contract Comptroller is ComptrollerBase, ComptrollerInterface, ComptrollerErrorR
809847

810848
// accumulate the collateral value to sumCollateral
811849
uint256 assetCollateralValue = mul_ScalarTruncate(vars.tokensToDenom, vars.cTokenBalance);
812-
if (assetCollateralValue > assetAsCollateralValueCap) assetCollateralValue = assetAsCollateralValueCap;
850+
if (assetCollateralValue > vars.assetAsCollateralValueCap) assetCollateralValue = vars.assetAsCollateralValueCap;
813851
vars.sumCollateral += assetCollateralValue;
814852
}
815853

0 commit comments

Comments
 (0)