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 various #419

Merged
merged 11 commits into from
Aug 23, 2023
17 changes: 9 additions & 8 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ contract Morpho is IMorpho {
/// @param newOwner The new owner of the contract.
constructor(address newOwner) {
require(newOwner != address(0), ErrorsLib.ZERO_ADDRESS);
owner = newOwner;

owner = newOwner;
DOMAIN_SEPARATOR = keccak256(abi.encode(DOMAIN_TYPEHASH, keccak256("Morpho"), block.chainid, address(this)));
}

Expand All @@ -97,6 +97,7 @@ contract Morpho is IMorpho {
/// @inheritdoc IMorpho
function setOwner(address newOwner) external onlyOwner {
require(newOwner != owner, ErrorsLib.ALREADY_SET);

owner = newOwner;

emit EventsLib.SetOwner(newOwner);
Expand All @@ -105,6 +106,7 @@ contract Morpho is IMorpho {
/// @inheritdoc IMorpho
function enableIrm(address irm) external onlyOwner {
require(!isIrmEnabled[irm], ErrorsLib.ALREADY_SET);

isIrmEnabled[irm] = true;

emit EventsLib.EnableIrm(address(irm));
Expand All @@ -113,7 +115,8 @@ contract Morpho is IMorpho {
/// @inheritdoc IMorpho
function enableLltv(uint256 lltv) external onlyOwner {
require(!isLltvEnabled[lltv], ErrorsLib.ALREADY_SET);
require(lltv < WAD, ErrorsLib.LLTV_TOO_HIGH);
require(lltv < WAD, ErrorsLib.MAX_LLTV_EXCEEDED);

isLltvEnabled[lltv] = true;

emit EventsLib.EnableLltv(lltv);
Expand All @@ -138,6 +141,7 @@ contract Morpho is IMorpho {
/// @inheritdoc IMorpho
function setFeeRecipient(address newFeeRecipient) external onlyOwner {
require(newFeeRecipient != feeRecipient, ErrorsLib.ALREADY_SET);

feeRecipient = newFeeRecipient;

emit EventsLib.SetFeeRecipient(newFeeRecipient);
Expand Down Expand Up @@ -409,9 +413,6 @@ contract Morpho is IMorpho {
}

/// @inheritdoc IMorpho
/// @dev Warning: Reverts if the signature has already been submitted.
/// @dev The signature is malleable, but it has no impact on the security here.
/// @dev The nonce is passed as argument to be able to revert with a different error message.
function setAuthorizationWithSig(Authorization memory authorization, Signature calldata signature) external {
require(block.timestamp < authorization.deadline, ErrorsLib.SIGNATURE_EXPIRED);
require(authorization.nonce == nonce[authorization.authorizer]++, ErrorsLib.INVALID_NONCE);
Expand Down Expand Up @@ -444,6 +445,9 @@ contract Morpho is IMorpho {

if (elapsed == 0) return;

// Safe "unchecked" cast.
market[id].lastUpdate = uint128(block.timestamp);
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved

if (market[id].totalBorrowAssets != 0) {
uint256 borrowRate = IIrm(marketParams.irm).borrowRate(marketParams);
uint256 interest = market[id].totalBorrowAssets.wMulDown(borrowRate.wTaylorCompounded(elapsed));
Expand All @@ -463,9 +467,6 @@ contract Morpho is IMorpho {

emit EventsLib.AccrueInterest(id, borrowRate, interest, feeShares);
}

// Safe "unchecked" cast.
market[id].lastUpdate = uint128(block.timestamp);
}

/* HEALTH CHECK */
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/IMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ interface IMorpho {

/// @notice Sets `newFeeRecipient` as recipient of the fee.
/// @dev Warning: The fee recipient can be set to the zero address.
/// @dev Warning: The fee to be accrued on each market won't belong to the old fee recipient after calling this
/// function.
function setFeeRecipient(address newFeeRecipient) external;

/// @notice Creates the market `marketParams`.
Expand Down Expand Up @@ -242,6 +244,9 @@ interface IMorpho {
function setAuthorization(address authorized, bool newIsAuthorized) external;

/// @notice Sets the authorization for `authorization.authorized` to manage `authorization.authorizer`'s positions.
/// @dev Warning: Reverts if the signature has already been submitted.
/// @dev The signature is malleable, but it has no impact on the security here.
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
/// @dev The nonce is passed as argument to be able to revert with a different error message.
/// @param authorization The `Authorization` struct.
/// @param signature The signature.
function setAuthorizationWithSig(Authorization calldata authorization, Signature calldata signature) external;
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ library ErrorsLib {
/// @notice Thrown when the caller is not the owner.
string internal constant NOT_OWNER = "not owner";

/// @notice Thrown when the LLTV to enable is too high.
string internal constant LLTV_TOO_HIGH = "LLTV too high";
/// @notice Thrown when the LLTV to enable exceeds the maximum LLTV.
string internal constant MAX_LLTV_EXCEEDED = "max LLTV exceeded";

/// @notice Thrown when the fee to set exceeds the maximum fee.
string internal constant MAX_FEE_EXCEEDED = "MAX_FEE exceeded";
string internal constant MAX_FEE_EXCEEDED = "max fee exceeded";

/// @notice Thrown when the value is already set.
string internal constant ALREADY_SET = "already set";
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/SharesMathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ library SharesMathLib {
/// high precision computations.
uint256 internal constant VIRTUAL_SHARES = 1e6;

/// @dev A number of virtual assets of 1 enforces a conversion rate between shares and assets when a market is
/// empty.
uint256 internal constant VIRTUAL_ASSETS = 1;

/// @dev Calculates the value of `assets` quoted in shares, rounding down.
Expand Down
1 change: 1 addition & 0 deletions src/libraries/UtilsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ library UtilsLib {
}
}

/// @dev Returns `x` safely cast to uint128.
function toUint128(uint256 x) internal pure returns (uint128) {
require(x <= type(uint128).max, ErrorsLib.MAX_UINT128_EXCEEDED);
return uint128(x);
Expand Down
21 changes: 21 additions & 0 deletions test/forge/integration/TestIntegrationCreateMarket.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,25 @@ contract IntegrationCreateMarketTest is BaseTest {
morpho.createMarket(marketParamsFuzz);
vm.stopPrank();
}

function testIdToMarketParams(MarketParams memory marketParamsFuzz) public {
marketParamsFuzz.lltv = _boundValidLltv(marketParamsFuzz.lltv);
Id marketParamsFuzzId = marketParamsFuzz.id();

vm.startPrank(OWNER);
if (marketParamsFuzz.irm != marketParams.irm) morpho.enableIrm(marketParamsFuzz.irm);
if (marketParamsFuzz.lltv != LLTV) morpho.enableLltv(marketParamsFuzz.lltv);

morpho.createMarket(marketParamsFuzz);
vm.stopPrank();

(address borrowableToken, address collateralToken, address oracle, address irm, uint256 lltv) =
morpho.idToMarketParams(marketParamsFuzzId);

assertEq(marketParamsFuzz.borrowableToken, borrowableToken, "borrowableToken != borrowableToken");
assertEq(marketParamsFuzz.collateralToken, collateralToken, "collateralToken != collateralToken");
assertEq(marketParamsFuzz.oracle, oracle, "oracle != oracle");
assertEq(marketParamsFuzz.irm, irm, "irm != irm");
assertEq(marketParamsFuzz.lltv, lltv, "lltv != lltv");
}
}
2 changes: 1 addition & 1 deletion test/forge/integration/TestIntegrationOnlyOwner.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ contract IntegrationOnlyOwnerTest is BaseTest {
lltvFuzz = _boundInvalidLltv(lltvFuzz);

vm.prank(OWNER);
vm.expectRevert(bytes(ErrorsLib.LLTV_TOO_HIGH));
vm.expectRevert(bytes(ErrorsLib.MAX_LLTV_EXCEEDED));
morpho.enableLltv(lltvFuzz);
}

Expand Down
2 changes: 1 addition & 1 deletion test/morpho_tests.tree
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
│ └── revert with NOT_OWNER
└── when msg.sender is owner
├── when lltv >= WAD
│ └── revert with LLTV_TOO_HIGH
│ └── revert with MAX_LLTV_EXCEEDED
└── when lltv < WAD
├── it should set isLltvEnabled[lltv] to true
└── it should emit EnableLltv(lltv)
Expand Down