Skip to content

Commit

Permalink
refactor(position): rename user to position
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Aug 23, 2023
1 parent 47a0619 commit 3019d91
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
39 changes: 20 additions & 19 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;

import {Id, IMorpho, MarketParams, User, Market, Authorization, Signature} from "./interfaces/IMorpho.sol";
import {Id, IMorpho, MarketParams, Position, Market, Authorization, Signature} from "./interfaces/IMorpho.sol";
import {
IMorphoLiquidateCallback,
IMorphoRepayCallback,
Expand Down Expand Up @@ -46,7 +46,7 @@ contract Morpho is IMorpho {
/// @inheritdoc IMorpho
address public feeRecipient;
/// @inheritdoc IMorpho
mapping(Id => mapping(address => User)) public user;
mapping(Id => mapping(address => Position)) public position;
/// @inheritdoc IMorpho
mapping(Id => Market) public market;
/// @inheritdoc IMorpho
Expand Down Expand Up @@ -166,7 +166,7 @@ contract Morpho is IMorpho {
if (assets > 0) shares = assets.toSharesDown(market[id].totalSupplyAssets, market[id].totalSupplyShares);
else assets = shares.toAssetsUp(market[id].totalSupplyAssets, market[id].totalSupplyShares);

user[id][onBehalf].supplyShares += shares;
position[id][onBehalf].supplyShares += shares;
market[id].totalSupplyShares += shares.toUint128();
market[id].totalSupplyAssets += assets.toUint128();

Expand Down Expand Up @@ -199,7 +199,7 @@ contract Morpho is IMorpho {
if (assets > 0) shares = assets.toSharesUp(market[id].totalSupplyAssets, market[id].totalSupplyShares);
else assets = shares.toAssetsDown(market[id].totalSupplyAssets, market[id].totalSupplyShares);

user[id][onBehalf].supplyShares -= shares;
position[id][onBehalf].supplyShares -= shares;
market[id].totalSupplyShares -= shares.toUint128();
market[id].totalSupplyAssets -= assets.toUint128();

Expand Down Expand Up @@ -234,7 +234,7 @@ contract Morpho is IMorpho {
if (assets > 0) shares = assets.toSharesUp(market[id].totalBorrowAssets, market[id].totalBorrowShares);
else assets = shares.toAssetsDown(market[id].totalBorrowAssets, market[id].totalBorrowShares);

user[id][onBehalf].borrowShares += shares.toUint128();
position[id][onBehalf].borrowShares += shares.toUint128();
market[id].totalBorrowShares += shares.toUint128();
market[id].totalBorrowAssets += assets.toUint128();

Expand Down Expand Up @@ -266,7 +266,7 @@ contract Morpho is IMorpho {
if (assets > 0) shares = assets.toSharesDown(market[id].totalBorrowAssets, market[id].totalBorrowShares);
else assets = shares.toAssetsUp(market[id].totalBorrowAssets, market[id].totalBorrowShares);

user[id][onBehalf].borrowShares -= shares.toUint128();
position[id][onBehalf].borrowShares -= shares.toUint128();
market[id].totalBorrowShares -= shares.toUint128();
market[id].totalBorrowAssets -= assets.toUint128();

Expand All @@ -292,7 +292,7 @@ contract Morpho is IMorpho {

// Don't accrue interest because it's not required and it saves gas.

user[id][onBehalf].collateral += assets.toUint128();
position[id][onBehalf].collateral += assets.toUint128();

emit EventsLib.SupplyCollateral(id, msg.sender, onBehalf, assets);

Expand All @@ -314,7 +314,7 @@ contract Morpho is IMorpho {

_accrueInterest(marketParams, id);

user[id][onBehalf].collateral -= assets.toUint128();
position[id][onBehalf].collateral -= assets.toUint128();

require(_isHealthy(marketParams, id, onBehalf), ErrorsLib.INSUFFICIENT_COLLATERAL);

Expand Down Expand Up @@ -347,21 +347,21 @@ contract Morpho is IMorpho {
assetsRepaid = seized.mulDivUp(collateralPrice, ORACLE_PRICE_SCALE).wDivUp(incentiveFactor);
sharesRepaid = assetsRepaid.toSharesDown(market[id].totalBorrowAssets, market[id].totalBorrowShares);

user[id][borrower].borrowShares -= sharesRepaid.toUint128();
position[id][borrower].borrowShares -= sharesRepaid.toUint128();
market[id].totalBorrowShares -= sharesRepaid.toUint128();
market[id].totalBorrowAssets -= assetsRepaid.toUint128();

user[id][borrower].collateral -= seized.toUint128();
position[id][borrower].collateral -= seized.toUint128();

// Realize the bad debt if needed. Note that it saves ~3k gas to do it.
uint256 badDebtShares;
if (user[id][borrower].collateral == 0) {
badDebtShares = user[id][borrower].borrowShares;
if (position[id][borrower].collateral == 0) {
badDebtShares = position[id][borrower].borrowShares;
uint256 badDebt = badDebtShares.toAssetsUp(market[id].totalBorrowAssets, market[id].totalBorrowShares);
market[id].totalSupplyAssets -= badDebt.toUint128();
market[id].totalBorrowAssets -= badDebt.toUint128();
market[id].totalBorrowShares -= badDebtShares.toUint128();
user[id][borrower].borrowShares = 0;
position[id][borrower].borrowShares = 0;
}

IERC20(marketParams.collateralToken).safeTransfer(msg.sender, seized);
Expand Down Expand Up @@ -444,7 +444,7 @@ contract Morpho is IMorpho {
// that total supply is already updated.
feeShares =
feeAmount.toSharesDown(market[id].totalSupplyAssets - feeAmount, market[id].totalSupplyShares);
user[id][feeRecipient].supplyShares += feeShares;
position[id][feeRecipient].supplyShares += feeShares;
market[id].totalSupplyShares += feeShares.toUint128();
}

Expand All @@ -457,28 +457,29 @@ contract Morpho is IMorpho {

/* HEALTH CHECK */

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

uint256 collateralPrice = IOracle(marketParams.oracle).price();

return _isHealthy(marketParams, id, borrower, collateralPrice);
}

/// @dev Returns whether the position of `user` in the given market `marketParams` with the given `collateralPrice`
/// @dev Returns whether the position of `position` in the given market `marketParams` with the given
/// `collateralPrice`
/// is healthy.
/// @dev Assumes that the inputs `marketParams` and `id` match.
function _isHealthy(MarketParams memory marketParams, Id id, address borrower, uint256 collateralPrice)
internal
view
returns (bool)
{
uint256 borrowed = uint256(user[id][borrower].borrowShares).toAssetsUp(
uint256 borrowed = uint256(position[id][borrower].borrowShares).toAssetsUp(
market[id].totalBorrowAssets, market[id].totalBorrowShares
);
uint256 maxBorrow = uint256(user[id][borrower].collateral).mulDivDown(collateralPrice, ORACLE_PRICE_SCALE)
uint256 maxBorrow = uint256(position[id][borrower].collateral).mulDivDown(collateralPrice, ORACLE_PRICE_SCALE)
.wMulDown(marketParams.lltv);

return maxBorrow >= borrowed;
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/IMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct MarketParams {

/// @dev Warning: For `feeRecipient`, `supplyShares` does not contain the accrued shares since the last interest
/// accrual.
struct User {
struct Position {
uint256 supplyShares;
uint128 borrowShares;
uint128 collateral;
Expand Down Expand Up @@ -64,10 +64,10 @@ interface IMorpho {
/// @dev The recipient receives the fees of a given market through a supply position on that market.
function feeRecipient() external view returns (address);

/// @notice Users' storage for market `id`.
function user(Id id, address user) external view returns (uint256, uint128, uint128);
/// @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);

/// @notice Market' storage for market `id`.
/// @notice The state of the market corresponding to `id`.
function market(Id id) external view returns (uint128, uint128, uint128, uint128, uint128, uint128);

/// @notice Whether the `irm` is enabled.
Expand Down
6 changes: 3 additions & 3 deletions test/forge/periphery/MorphoLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ contract MorphoLibTest is BaseTest {
function testSupplyShares(uint256 amountSupplied, uint256 amountBorrowed, uint256 timestamp, uint256 fee) public {
_testMorphoLibCommon(amountSupplied, amountBorrowed, timestamp, fee);

(uint256 expectedSupplyShares,,) = morpho.user(id, address(this));
(uint256 expectedSupplyShares,,) = morpho.position(id, address(this));
assertEq(morpho.supplyShares(id, address(this)), expectedSupplyShares);
}

function testBorrowShares(uint256 amountSupplied, uint256 amountBorrowed, uint256 timestamp, uint256 fee) public {
_testMorphoLibCommon(amountSupplied, amountBorrowed, timestamp, fee);

(, uint256 expectedBorrowShares,) = morpho.user(id, BORROWER);
(, uint256 expectedBorrowShares,) = morpho.position(id, BORROWER);
assertEq(morpho.borrowShares(id, BORROWER), expectedBorrowShares);
}

function testCollateral(uint256 amountSupplied, uint256 amountBorrowed, uint256 timestamp, uint256 fee) public {
_testMorphoLibCommon(amountSupplied, amountBorrowed, timestamp, fee);

(,, uint256 expectedCollateral) = morpho.user(id, BORROWER);
(,, uint256 expectedCollateral) = morpho.position(id, BORROWER);
assertEq(morpho.collateral(id, BORROWER), expectedCollateral);
}

Expand Down
2 changes: 1 addition & 1 deletion test/hardhat/Morpho.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe("Morpho", () => {

await morpho.connect(liquidator).liquidate(marketParams, borrower.address, seized, "0x");

const remainingCollateral = (await morpho.user(id, borrower.address)).collateral;
const remainingCollateral = (await morpho.position(id, borrower.address)).collateral;

if (closePositions)
expect(remainingCollateral.isZero(), "did not take the whole collateral when closing the position").to.be.true;
Expand Down

0 comments on commit 3019d91

Please sign in to comment.