Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into certora/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
QGarchery committed Aug 17, 2023
2 parents db4503c + ea5dc96 commit 87eff1c
Show file tree
Hide file tree
Showing 24 changed files with 2,083 additions and 1,162 deletions.
3 changes: 2 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[profile.default]
evm_version = 'paris'
via-ir = true

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
# See more config options https://github.com/foundry-rs/foundry/tree/master/crates/config
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const config: HardhatUserConfig = {
runs: 200,
},
viaIR: true,
evmVersion: "paris",
},
},
],
Expand Down
9 changes: 6 additions & 3 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ contract Morpho is IMorpho {
}

/// @dev Accrues interests for `market`.
/// @dev Assumes the given `market` and `id` match.
function _accrueInterests(Market memory market, Id id) internal {
uint256 elapsed = block.timestamp - lastUpdate[id];

Expand Down Expand Up @@ -463,7 +464,8 @@ contract Morpho is IMorpho {

/* HEALTH CHECK */

/// @notice Returns whether the position of `user` in the given `market` is healthy.
/// @dev Returns whether the position of `user` in the given `market` is healthy.
/// @dev Assumes the given `market` and `id` match.
function _isHealthy(Market memory market, Id id, address user) internal view returns (bool) {
if (borrowShares[id][user] == 0) return true;

Expand All @@ -472,7 +474,8 @@ contract Morpho is IMorpho {
return _isHealthy(market, id, user, collateralPrice);
}

/// @notice Returns whether the position of `user` in the given `market` with the given `collateralPrice` and `priceScale` is healthy.
/// @dev Returns whether the position of `user` in the given `market` with the given `collateralPrice` is healthy.
/// @dev Assumes the given `market` and `id` match.
function _isHealthy(Market memory market, Id id, address user, uint256 collateralPrice)
internal
view
Expand Down Expand Up @@ -504,7 +507,7 @@ contract Morpho is IMorpho {

/* LIQUIDATION INCENTIVE FACTOR */

/// @dev The liquidation incentive factor is min(maxIncentiveFactor, 1/(1 - cursor(1 - lltv))).
/// @dev The liquidation incentive factor is min(maxIncentiveFactor, 1/(1 - cursor*(1 - lltv))).
function liquidationIncentiveFactor(uint256 lltv) private pure returns (uint256) {
return
UtilsLib.min(MAX_LIQUIDATION_INCENTIVE_FACTOR, WAD.wDivDown(WAD - LIQUIDATION_CURSOR.wMulDown(WAD - lltv)));
Expand Down
26 changes: 13 additions & 13 deletions src/interfaces/IMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface IMorpho is IFlashLender {
function supplyShares(Id id, address user) external view returns (uint256);

/// @notice The `user`'s borrow shares on the market `id`.
function borrowShares(Id, address user) external view returns (uint256);
function borrowShares(Id id, address user) external view returns (uint256);

/// @notice The `user`'s collateral balance on the market `id`.
function collateral(Id id, address user) external view returns (uint256);
Expand Down Expand Up @@ -136,12 +136,12 @@ interface IMorpho is IFlashLender {
/// @param onBehalf The address that will receive the position.
/// @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 supplied.
/// @return sharesSupplied The amount of shares minted.
function supply(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes memory data)
external
returns (uint256 assetsSupplied, uint256 sharesSupplied);

/// @notice Withdraws the given `assets` or `shares` from the given `market` on behalf of `onBehalf`.
/// @notice Withdraws the given `assets` or `shares` from the given `market` on behalf of `onBehalf` to `receiver`.
/// @dev Either `assets` or `shares` should be zero.
/// To withdraw the whole position, pass the `shares`'s balance of `onBehalf`.
/// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
Expand All @@ -152,16 +152,16 @@ interface IMorpho is IFlashLender {
/// @param onBehalf The address of the owner of the withdrawn assets.
/// @param receiver The address that will receive the withdrawn assets.
/// @return assetsWithdrawn The amount of assets withdrawn.
/// @return sharesWithdrawn The amount of shares withdrawn.
/// @return sharesWithdrawn The amount of shares burned.
function withdraw(Market memory market, uint256 assets, uint256 shares, address onBehalf, address receiver)
external
returns (uint256 assetsWithdrawn, uint256 sharesWithdrawn);

/// @notice Borrows the given `assets` or `shares` from the given `market` on behalf of `onBehalf`.
/// @notice Borrows the given `assets` or `shares` from the given `market` on behalf of `onBehalf` to `receiver`.
/// @dev Either `assets` or `shares` should be zero.
/// Most usecases should rely on `assets` as an input so the caller
/// is guaranteed to borrow `assets` of tokens,
/// but the possibility to burn a specific amount of shares is given
/// but the possibility to mint a specific amount of shares is given
/// for full compatibility and precision.
/// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
/// @dev Borrowing a large amount can overflow and revert without any error message.
Expand All @@ -171,7 +171,7 @@ interface IMorpho is IFlashLender {
/// @param onBehalf The address of the owner of the debt.
/// @param receiver The address that will receive the debt.
/// @return assetsBorrowed The amount of assets borrowed.
/// @return sharesBorrowed The amount of shares borrowed.
/// @return sharesBorrowed The amount of shares minted.
function borrow(Market memory market, uint256 assets, uint256 shares, address onBehalf, address receiver)
external
returns (uint256 assetsBorrowed, uint256 sharesBorrowed);
Expand All @@ -187,7 +187,7 @@ interface IMorpho is IFlashLender {
/// @param onBehalf The address of the owner of the debt.
/// @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 repaid.
/// @return sharesRepaid The amount of shares burned.
function repay(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes memory data)
external
returns (uint256 assetsRepaid, uint256 sharesRepaid);
Expand All @@ -197,16 +197,16 @@ interface IMorpho is IFlashLender {
/// @dev Interests are not accrued since it's not required and it saves gas.
/// @dev Supplying a large amount can overflow and revert without any error message.
/// @param market The market to supply collateral to.
/// @param assets The assets of collateral to supply.
/// @param assets The amount of collateral to supply.
/// @param onBehalf The address that will receive the collateral.
/// @param data Arbitrary data to pass to the `onMorphoSupplyCollateral` callback. Pass empty data if not needed.
function supplyCollateral(Market memory market, uint256 assets, address onBehalf, bytes memory data) external;

/// @notice Withdraws the given `assets` of collateral from the given `market` on behalf of `onBehalf`.
/// @notice Withdraws the given `assets` of collateral from the given `market` on behalf of `onBehalf` to `receiver`.
/// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
/// @dev Withdrawing an amount corresponding to more collateral than supplied will underflow and revert without any error message.
/// @param market The market to withdraw collateral from.
/// @param assets The assets of collateral to withdraw.
/// @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.
function withdrawCollateral(Market memory market, uint256 assets, address onBehalf, address receiver) external;
Expand All @@ -217,10 +217,10 @@ interface IMorpho is IFlashLender {
/// @dev Repaying more than the borrow balance will underflow and revert without any error message.
/// @param market The market of the position.
/// @param borrower The owner of the position.
/// @param seized The assets of collateral to seize.
/// @param seized The amount of collateral to seize.
/// @param data Arbitrary data to pass to the `onMorphoLiquidate` callback. Pass empty data if not needed.
/// @return assetsRepaid The amount of assets repaid.
/// @return sharesRepaid The amount of shares repaid.
/// @return sharesRepaid The amount of shares burned.
function liquidate(Market memory market, address borrower, uint256 seized, bytes memory data)
external
returns (uint256 assetsRepaid, uint256 sharesRepaid);
Expand Down
14 changes: 7 additions & 7 deletions src/interfaces/IMorphoCallbacks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity >=0.5.0;
interface IMorphoLiquidateCallback {
/// @notice Callback called when a liquidation occurs.
/// @dev The callback is called only if data is not empty.
/// @param assets The assets of repaid assets.
/// @param assets The amount of repaid assets.
/// @param data Arbitrary data passed to the `liquidate` function.
function onMorphoLiquidate(uint256 assets, bytes calldata data) external;
}
Expand All @@ -16,7 +16,7 @@ interface IMorphoLiquidateCallback {
interface IMorphoRepayCallback {
/// @notice Callback called when a repayment occurs.
/// @dev The callback is called only if data is not empty.
/// @param assets The assets of repaid assets.
/// @param assets The amount of repaid assets.
/// @param data Arbitrary data passed to the `repay` function.
function onMorphoRepay(uint256 assets, bytes calldata data) external;
}
Expand All @@ -26,23 +26,23 @@ interface IMorphoRepayCallback {
interface IMorphoSupplyCallback {
/// @notice Callback called when a supply occurs.
/// @dev The callback is called only if data is not empty.
/// @param assets The assets of supplied assets.
/// @param assets The amount of supplied assets.
/// @param data Arbitrary data passed to the `supply` function.
function onMorphoSupply(uint256 assets, bytes calldata data) external;
}

/// @title IMorphoSupplyCollateralCallback
/// @notice Interface that users willing to use `supplyCollateral`'s callback must implement.
interface IMorphoSupplyCollateralCallback {
/// @notice Callback called when a supply occurs.
/// @notice Callback called when a supply of collateral occurs.
/// @dev The callback is called only if data is not empty.
/// @param assets The assets of supplied assets.
/// @param assets The amount of supplied collateral.
/// @param data Arbitrary data passed to the `supplyCollateral` function.
function onMorphoSupplyCollateral(uint256 assets, bytes calldata data) external;
}

/// @title IMorphoWithdrawCallback
/// @notice Interface that users willing to use `withdraw`'s callback must implement.
/// @title IMorphoFlashLoanCallback
/// @notice Interface that users willing to use `flashLoan`'s callback must implement.
interface IMorphoFlashLoanCallback {
/// @notice Callback called when a flash loan occurs.
/// @dev The callback is called only if data is not empty.
Expand Down
24 changes: 12 additions & 12 deletions src/libraries/EventsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ library EventsLib {
/// @notice Emitted on supply of assets.
/// @param id The market id.
/// @param caller The caller.
/// @param onBehalf The address that will receive the position.
/// @param onBehalf The address that received the supply.
/// @param assets The amount of assets supplied.
/// @param shares The amount of shares minted.
event Supply(Id indexed id, address indexed caller, address indexed onBehalf, uint256 assets, uint256 shares);

/// @notice Emitted on withdrawal of assets.
/// @param id The market id.
/// @param caller The caller.
/// @param onBehalf The address from which the assets are withdrawn.
/// @param receiver The address that will receive the withdrawn assets.
/// @param onBehalf The address from which the assets were withdrawn.
/// @param receiver The address that received the withdrawn assets.
/// @param assets The amount of assets withdrawn.
/// @param shares The amount of shares burned.
event Withdraw(
Expand All @@ -61,8 +61,8 @@ library EventsLib {
/// @notice Emitted on borrow of assets.
/// @param id The market id.
/// @param caller The caller.
/// @param onBehalf The address from which the assets are borrowed.
/// @param receiver The address that will receive the borrowed assets.
/// @param onBehalf The address from which the assets were borrowed.
/// @param receiver The address that received the borrowed assets.
/// @param assets The amount of assets borrowed.
/// @param shares The amount of shares minted.
event Borrow(
Expand All @@ -77,23 +77,23 @@ library EventsLib {
/// @notice Emitted on repayment of assets.
/// @param id The market id.
/// @param caller The caller.
/// @param onBehalf The address for which the assets are repaid.
/// @param onBehalf The address for which the assets were repaid.
/// @param assets The amount of assets repaid.
/// @param shares The amount of shares burned.
event Repay(Id indexed id, address indexed caller, address indexed onBehalf, uint256 assets, uint256 shares);

/// @notice Emitted on supply of collateral.
/// @param id The market id.
/// @param caller The caller.
/// @param onBehalf The address that will receive the position.
/// @param onBehalf The address that received the collateral.
/// @param assets The amount of collateral supplied.
event SupplyCollateral(Id indexed id, address indexed caller, address indexed onBehalf, uint256 assets);

/// @notice Emitted on withdrawal of collateral.
/// @param id The market id.
/// @param caller The caller.
/// @param onBehalf The address from which the collateral is withdrawn.
/// @param receiver The address that will receive the withdrawn collateral.
/// @param onBehalf The address from which the collateral was withdrawn.
/// @param receiver The address that received the withdrawn collateral.
/// @param assets The amount of collateral withdrawn.
event WithdrawCollateral(
Id indexed id, address caller, address indexed onBehalf, address indexed receiver, uint256 assets
Expand All @@ -118,9 +118,9 @@ library EventsLib {
);

/// @notice Emitted on flash loan.
/// @param caller The caller..
/// @param caller The caller.
/// @param token The token that was flash loaned.
/// @param assets The assets that was flash loaned.
/// @param assets The amount that was flash loaned.
event FlashLoan(address indexed caller, address indexed token, uint256 assets);

/// @notice Emitted when setting an authorization.
Expand All @@ -141,7 +141,7 @@ library EventsLib {
/// @notice Emitted when accruing interests.
/// @param id The market id.
/// @param prevBorrowRate The previous borrow rate.
/// @param accruedInterests The amount of interests accrued.
/// @param accruedInterests The amount of interest accrued.
/// @param feeShares The amount of shares minted as fee.
event AccrueInterests(Id indexed id, uint256 prevBorrowRate, uint256 accruedInterests, uint256 feeShares);
}
6 changes: 3 additions & 3 deletions src/libraries/MathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ library MathLib {
/// @dev (x * y) / denominator rounded up.
function mulDivUp(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 z) {
// Underflow if denominator == 0.
// Division by 0 if denominator == 0 (this case cannot occure since the above underflow happens before).
// Division by 0 if denominator == 0 (this case cannot occur since the above underflow happens before).
// Overflow if
// x * y + denominator - 1 > type(uint256).max
// <=> x * y > type(uint256).max - denominator - 1
// <=> y > 0 and x > (type(uint256).max - denominator - 1) / y
// <=> x * y > type(uint256).max - denominator + 1
// <=> y > 0 and x > (type(uint256).max - denominator + 1) / y
assembly {
if or(mul(y, gt(x, div(sub(MAX_UINT256, sub(denominator, 1)), y))), iszero(denominator)) { revert(0, 0) }

Expand Down
Loading

0 comments on commit 87eff1c

Please sign in to comment.