Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: later requires in constructor, for readability #50

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/PreLiquidation.sol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's more readable, although I'm generally against using immutable values in the contructor, as it's not sure if they are initialised yet or not. Also I like to have the require at the top of functions.

An other possibility would be to remove the struct (would be curious to see the code without it)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried here I'm not fond of it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you @adhusson.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally against using immutable values in the contructor, as it's not sure if they are initialised yet or not.

Are you referring to this part of the via_ir spec? It is a difficult area but I don't think it applies here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is that you could do something like

require(ABC ...);

ABC = abc;

without noticing

Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,10 @@ contract PreLiquidation is IPreLiquidation, IMorphoRepayCallback {
/// @dev The pre-liquidation incentive factor (preLIF) corresponds to the factor which is multiplied by the repaid
/// debt to compute the seized collateral. It increases linearly from preLIF1 at preLltv to preLIF2 at LLTV.
constructor(address morpho, Id id, PreLiquidationParams memory _preLiquidationParams) {
require(IMorpho(morpho).market(id).lastUpdate != 0, ErrorsLib.NonexistentMarket());
MarketParams memory _marketParams = IMorpho(morpho).idToMarketParams(id);
require(_preLiquidationParams.preLltv < _marketParams.lltv, ErrorsLib.PreLltvTooHigh());
require(_preLiquidationParams.preCF2 >= _preLiquidationParams.preCF1, ErrorsLib.CloseFactorDecreasing());
require(_preLiquidationParams.preLIF1 >= WAD, ErrorsLib.preLIFTooLow());
require(_preLiquidationParams.preLIF2 >= _preLiquidationParams.preLIF1, ErrorsLib.preLIFDecreasing());

MORPHO = IMorpho(morpho);
require(MORPHO.market(id).lastUpdate != 0, ErrorsLib.NonexistentMarket());

ID = id;

Expand All @@ -107,6 +103,11 @@ contract PreLiquidation is IPreLiquidation, IMorphoRepayCallback {
PRE_LIF_2 = _preLiquidationParams.preLIF2;
PRE_LIQUIDATION_ORACLE = _preLiquidationParams.preLiquidationOracle;

require(PRE_LLTV < LLTV, ErrorsLib.PreLltvTooHigh());
require(PRE_CF_1 <= PRE_CF_2, ErrorsLib.CloseFactorDecreasing());
require(WAD <= PRE_LIF_1, ErrorsLib.preLIFTooLow());
require(PRE_LIF_1 <= PRE_LIF_2, ErrorsLib.preLIFDecreasing());

ERC20(LOAN_TOKEN).safeApprove(morpho, type(uint256).max);
}

Expand Down