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

Review of comments, libraries and interfaces #438

Merged
merged 6 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ contract Morpho is IMorpho {
if (market[id].fee != 0) {
uint256 feeAmount = interest.wMulDown(market[id].fee);
// The fee amount is subtracted from the total supply in this calculation to compensate for the fact
// that total supply is already updated.
// that total supply is already increased by the full interest (including the fee amount).
feeShares =
feeAmount.toSharesDown(market[id].totalSupplyAssets - feeAmount, market[id].totalSupplyShares);
position[id][feeRecipient].supplyShares += feeShares;
Expand Down
48 changes: 30 additions & 18 deletions src/interfaces/IMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,23 @@ interface IMorpho {
function feeRecipient() external view returns (address);

/// @notice The state of the position of `user` on the market corresponding to `id`.
function position(Id id, address user) external view returns (uint256, uint128, uint128);
function position(Id id, address user)
external
view
returns (uint256 supplyShares, uint128 borrowShares, uint128 collateral);

/// @notice The state of the market corresponding to `id`.
function market(Id id) external view returns (uint128, uint128, uint128, uint128, uint128, uint128);
function market(Id id)
external
view
returns (
uint128 totalSupplyAssets,
uint128 totalSupplyShares,
uint128 totalBorrowAssets,
uint128 totalBorrowShares,
uint128 lastUpdate,
uint128 fee
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
);

/// @notice Whether the `irm` is enabled.
function isIrmEnabled(address irm) external view returns (bool);
Expand All @@ -93,14 +106,13 @@ interface IMorpho {

/// @notice Sets `newOwner` as owner of the contract.
/// @dev Warning: No two-step transfer ownership.
/// @dev Warning: The owner can be set to the zero address.
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
function setOwner(address newOwner) external;

/// @notice Enables `irm` as possible IRM for market creation.
/// @notice Enables `irm` as a possible IRM for market creation.
/// @dev Warning: It is not possible to disable an IRM.
function enableIrm(address irm) external;

/// @notice Enables `lltv` as possible LLTV for market creation.
/// @notice Enables `lltv` as a possible LLTV for market creation.
/// @dev Warning: It is not possible to disable a LLTV.
function enableLltv(uint256 lltv) external;

Expand All @@ -126,7 +138,7 @@ interface IMorpho {
/// @param marketParams The market to supply assets to.
/// @param assets The amount of assets to supply.
/// @param shares The amount of shares to mint.
/// @param onBehalf The address that will receive the position.
/// @param onBehalf The address that will own the increased supply position.
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved
/// @param data Arbitrary data to pass to the `onMorphoSupply` callback. Pass empty data if not needed.
/// @return assetsSupplied The amount of assets supplied.
/// @return sharesSupplied The amount of shares minted.
Expand All @@ -145,7 +157,7 @@ interface IMorpho {
/// @param marketParams The market to withdraw assets from.
/// @param assets The amount of assets to withdraw.
/// @param shares The amount of shares to burn.
/// @param onBehalf The address of the owner of the withdrawn assets.
/// @param onBehalf The address of the owner of the supply position.
/// @param receiver The address that will receive the withdrawn assets.
/// @return assetsWithdrawn The amount of assets withdrawn.
/// @return sharesWithdrawn The amount of shares burned.
Expand All @@ -166,8 +178,8 @@ interface IMorpho {
/// @param marketParams The market to borrow assets from.
/// @param assets The amount of assets to borrow.
/// @param shares The amount of shares to mint.
/// @param onBehalf The address of the owner of the debt.
/// @param receiver The address that will receive the debt.
/// @param onBehalf The address that will own the increased borrow position.
/// @param receiver The address that will receive the borrowed assets.
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
/// @return assetsBorrowed The amount of assets borrowed.
/// @return sharesBorrowed The amount of shares minted.
function borrow(
Expand All @@ -185,7 +197,7 @@ interface IMorpho {
/// @param marketParams The market to repay assets to.
/// @param assets The amount of assets to repay.
/// @param shares The amount of shares to burn.
/// @param onBehalf The address of the owner of the debt.
/// @param onBehalf The address of the owner of the debt position.
/// @param data Arbitrary data to pass to the `onMorphoRepay` callback. Pass empty data if not needed.
/// @return assetsRepaid The amount of assets repaid.
/// @return sharesRepaid The amount of shares burned.
Expand All @@ -203,7 +215,7 @@ interface IMorpho {
/// @dev Supplying a large amount can revert for overflow.
/// @param marketParams The market to supply collateral to.
/// @param assets The amount of collateral to supply.
/// @param onBehalf The address that will receive the collateral.
/// @param onBehalf The address that will own the increased collateral position.
/// @param data Arbitrary data to pass to the `onMorphoSupplyCollateral` callback. Pass empty data if not needed.
function supplyCollateral(MarketParams memory marketParams, uint256 assets, address onBehalf, bytes memory data)
external;
Expand All @@ -213,15 +225,15 @@ interface IMorpho {
/// @dev Withdrawing an amount corresponding to more collateral than supplied will revert for underflow.
/// @param marketParams The market to withdraw collateral from.
/// @param assets The amount of collateral to withdraw.
/// @param onBehalf The address of the owner of the collateral.
/// @param receiver The address that will receive the withdrawn collateral.
/// @param onBehalf The address of the owner of the collateral position.
/// @param receiver The address that will receive the collateral assets.
function withdrawCollateral(MarketParams memory marketParams, uint256 assets, address onBehalf, address receiver)
external;

/// @notice Liquidates the given `repaidShares` of debt asset or seize the given `seized` of collateral on the given
/// `market` of the given `borrower`'s position, optionally calling back the caller's `onMorphoLiquidate` function
/// with the given `data`.
/// @dev Either `seized` or `repaidShares` should be zero.
/// @notice Liquidates the given `repaidShares` of debt asset or seize the given `seizedAssets` of collateral on the
/// given market `marketParams` of the given `borrower`'s position, optionally calling back the caller's
/// `onMorphoLiquidate` function with the given `data`.
/// @dev Either `seizedAssets` or `repaidShares` should be zero.
/// @dev Seizing more than the collateral balance will underflow and revert without any error message.
/// @dev Repaying more than the borrow balance will underflow and revert without any error message.
/// @param marketParams The market of the position.
Expand Down Expand Up @@ -259,5 +271,5 @@ interface IMorpho {
function setAuthorizationWithSig(Authorization calldata authorization, Signature calldata signature) external;

/// @notice Returns the data stored on the different `slots`.
function extSloads(bytes32[] memory slots) external view returns (bytes32[] memory res);
function extSloads(bytes32[] memory slots) external view returns (bytes32[] memory);
}
4 changes: 2 additions & 2 deletions src/interfaces/IMorphoCallbacks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pragma solidity >=0.6.2;
interface IMorphoLiquidateCallback {
/// @notice Callback called when a liquidation occurs.
/// @dev The callback is called only if data is not empty.
/// @param assets The amount of repaid assets.
/// @param repaidAssets The amount of repaid assets.
/// @param data Arbitrary data passed to the `liquidate` function.
function onMorphoLiquidate(uint256 assets, bytes calldata data) external;
function onMorphoLiquidate(uint256 repaidAssets, bytes calldata data) external;
}

/// @title IMorphoRepayCallback
Expand Down
16 changes: 8 additions & 8 deletions src/libraries/EventsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ library EventsLib {

/// @notice Emitted when setting a new fee.
/// @param id The market id.
/// @param fee The new fee.
event SetFee(Id indexed id, uint256 fee);
/// @param newFee The new fee.
event SetFee(Id indexed id, uint256 newFee);

/// @notice Emitted when setting a new fee recipient.
/// @param feeRecipient The new fee recipient.
event SetFeeRecipient(address indexed feeRecipient);
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved
/// @param newFeeRecipient The new fee recipient.
event SetFeeRecipient(address indexed newFeeRecipient);

/// @notice Emitted when enabling an IRM.
/// @param irm The IRM that was enabled.
Expand Down Expand Up @@ -103,17 +103,17 @@ library EventsLib {
/// @param id The market id.
/// @param caller The caller.
/// @param borrower The borrower of the position.
/// @param repaid The amount of assets repaid.
/// @param repaidAssets The amount of assets repaid.
/// @param repaidShares The amount of shares burned.
/// @param seized The amount of collateral seized.
/// @param seizedAssets The amount of collateral seized.
/// @param badDebtShares The amount of shares minted as bad debt.
event Liquidate(
Id indexed id,
address indexed caller,
address indexed borrower,
uint256 repaid,
uint256 repaidAssets,
uint256 repaidShares,
uint256 seized,
uint256 seizedAssets,
uint256 badDebtShares
);

Expand Down
16 changes: 8 additions & 8 deletions src/libraries/MathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ library MathLib {
return mulDivUp(x, WAD, y);
}

/// @dev (x * y) / denominator rounded down.
function mulDivDown(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256) {
return (x * y) / denominator;
/// @dev (x * y) / d rounded down.
function mulDivDown(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
return (x * y) / d;
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
}

/// @dev (x * y) / denominator rounded up.
function mulDivUp(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256) {
return (x * y + (denominator - 1)) / denominator;
/// @dev (x * y) / d rounded up.
function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
return (x * y + (d - 1)) / d;
}

/// @dev The sum of the last three terms in a four term taylor series expansion
/// to approximate a continuous compound interest rate: e^(nx) - 1.
/// @dev The first three non-zero terms of a Taylor expansion of e^(nx) - 1,
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved
/// to approximate a continuous compound interest rate.
function wTaylorCompounded(uint256 x, uint256 n) internal pure returns (uint256) {
uint256 firstTerm = x * n;
uint256 secondTerm = mulDivDown(firstTerm, firstTerm, 2 * WAD);
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/UtilsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {ErrorsLib} from "../libraries/ErrorsLib.sol";
/// @notice Library exposing helpers.
/// @dev Inspired by https://github.com/morpho-org/morpho-utils.
library UtilsLib {
/// @dev Returns true if there is exactly one zero.
/// @dev Returns true if there is exactly one zero among `x` and `y`.
function exactlyOneZero(uint256 x, uint256 y) internal pure returns (bool z) {
assembly {
z := xor(iszero(x), iszero(y))
}
}

/// @dev Returns the min of x and y.
/// @dev Returns the min of `x` and `y`.
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
z := xor(x, mul(xor(x, y), lt(y, x)))
Expand Down