From 3019d91434208ce39336d80d9018bb83d2006f37 Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Wed, 23 Aug 2023 14:51:37 +0200 Subject: [PATCH] refactor(position): rename user to position --- src/Morpho.sol | 39 ++++++++++++++-------------- src/interfaces/IMorpho.sol | 8 +++--- test/forge/periphery/MorphoLib.t.sol | 6 ++--- test/hardhat/Morpho.spec.ts | 2 +- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/Morpho.sol b/src/Morpho.sol index 8d5e78807..f52eae96b 100644 --- a/src/Morpho.sol +++ b/src/Morpho.sol @@ -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, @@ -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 @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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); @@ -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); @@ -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); @@ -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(); } @@ -457,17 +457,18 @@ 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) @@ -475,10 +476,10 @@ contract Morpho is IMorpho { 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; diff --git a/src/interfaces/IMorpho.sol b/src/interfaces/IMorpho.sol index 3995244c3..097962b4c 100644 --- a/src/interfaces/IMorpho.sol +++ b/src/interfaces/IMorpho.sol @@ -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; @@ -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. diff --git a/test/forge/periphery/MorphoLib.t.sol b/test/forge/periphery/MorphoLib.t.sol index 8ca54c023..7c41a09cd 100644 --- a/test/forge/periphery/MorphoLib.t.sol +++ b/test/forge/periphery/MorphoLib.t.sol @@ -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); } diff --git a/test/hardhat/Morpho.spec.ts b/test/hardhat/Morpho.spec.ts index 38ab0f41f..06ea8f047 100644 --- a/test/hardhat/Morpho.spec.ts +++ b/test/hardhat/Morpho.spec.ts @@ -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;