@@ -96,10 +96,22 @@ library MorphoLib {
9696 }
9797
9898 /// @notice Returns this contract's collateral balance in the given market, in raw collateral-token units.
99+ /// @dev Convenience wrapper over `collateral(morpho, market, address(this))`.
99100 /// @param morpho The Morpho Blue singleton.
100101 /// @param market Morpho market parameters identifying the position.
101102 function collateral (IMorpho morpho , MarketParams memory market ) internal view returns (uint256 ) {
102- return uint256 (morpho.position (market.id (), address (this )).collateral);
103+ return collateral (morpho, market, address (this ));
104+ }
105+
106+ /// @notice Returns `user`'s collateral balance in the given market, in raw collateral-token units.
107+ /// @dev Overload that reads an arbitrary account's position instead of `address(this)`'s, so it is safe to call
108+ /// from a context (e.g. a test or periphery) that is not the position owner.
109+ /// CAUTION: Call `accrueInterest(market)` first if an up-to-the-block value is required.
110+ /// @param morpho The Morpho Blue singleton.
111+ /// @param market Morpho market parameters identifying the position.
112+ /// @param user The account whose collateral balance is being read.
113+ function collateral (IMorpho morpho , MarketParams memory market , address user ) internal view returns (uint256 ) {
114+ return uint256 (morpho.position (market.id (), user).collateral);
103115 }
104116
105117 /// @notice Returns this contract's current debt in the given Morpho market, denominated in raw loan-token units.
@@ -117,7 +129,19 @@ library MorphoLib {
117129 /// @param morpho The Morpho Blue singleton.
118130 /// @param market Morpho market parameters identifying the position.
119131 function debt (IMorpho morpho , MarketParams memory market ) internal view returns (uint256 ) {
120- Position memory pos = morpho.position (market.id (), address (this ));
132+ return debt (morpho, market, address (this ));
133+ }
134+
135+ /// @notice Returns `user`'s current debt in the given Morpho market, denominated in raw loan-token units.
136+ /// @dev Overload of `debt(IMorpho,MarketParams)` that reads an arbitrary account's borrow position instead of
137+ /// `address(this)`'s, so it is safe to call from a context that is not the position owner. Conversion math and
138+ /// virtual shares/assets handling are identical to `debt(IMorpho,MarketParams)`.
139+ /// CAUTION: Call `accrueInterest(market)` first if an up-to-the-block value is required.
140+ /// @param morpho The Morpho Blue singleton.
141+ /// @param market Morpho market parameters identifying the position.
142+ /// @param user The account whose debt is being read.
143+ function debt (IMorpho morpho , MarketParams memory market , address user ) internal view returns (uint256 ) {
144+ Position memory pos = morpho.position (market.id (), user);
121145 if (pos.borrowShares == 0 ) return 0 ;
122146 Market memory mkt = morpho.market (market.id ());
123147 return uint256 (pos.borrowShares)
@@ -169,11 +193,21 @@ library MorphoLib {
169193 }
170194
171195 /// @notice Returns the maximum loan-token amount borrowable against this contract's current collateral balance.
172- /// @dev Convenience wrapper over `maxBorrowFor(collateral( market))`.
196+ /// @dev Convenience wrapper over `maxBorrow(morpho, market, address(this ))`.
173197 /// @param morpho The Morpho Blue singleton.
174198 /// @param market Morpho market parameters identifying the position.
175199 function maxBorrow (IMorpho morpho , MarketParams memory market ) internal view returns (uint256 ) {
176- return maxBorrowFor (market, collateral (morpho, market));
200+ return maxBorrow (morpho, market, address (this ));
201+ }
202+
203+ /// @notice Returns the maximum loan-token amount borrowable against `user`'s current collateral balance.
204+ /// @dev Convenience wrapper over `maxBorrowFor(collateral(market, user))`. Reads `user`'s position instead of
205+ /// `address(this)`'s, so it is safe to call from a context that is not the position owner.
206+ /// @param morpho The Morpho Blue singleton.
207+ /// @param market Morpho market parameters identifying the position.
208+ /// @param user The account whose collateral is being borrowed against.
209+ function maxBorrow (IMorpho morpho , MarketParams memory market , address user ) internal view returns (uint256 ) {
210+ return maxBorrowFor (market, collateral (morpho, market, user));
177211 }
178212
179213 /// @notice Returns this contract's health factor in the given market, scaled by WAD (1e18).
@@ -186,9 +220,23 @@ library MorphoLib {
186220 /// @param morpho The Morpho Blue singleton.
187221 /// @param market Morpho market parameters identifying the position.
188222 function healthFactor (IMorpho morpho , MarketParams memory market ) internal view returns (uint256 ) {
189- uint256 debtAmount = debt (morpho, market);
223+ return healthFactor (morpho, market, address (this ));
224+ }
225+
226+ /// @notice Returns `user`'s health factor in the given market, scaled by WAD (1e18).
227+ /// @dev Same definition as `healthFactor(IMorpho,MarketParams)` but computed against `user`'s collateral and debt
228+ /// (both read via the `address` overloads), so it is safe to call from a context that is not the position owner.
229+ /// The collateral and debt MUST refer to the same account: mixing `user`'s debt with `address(this)`'s collateral
230+ /// would understate the health factor whenever the caller holds no collateral itself.
231+ /// Returns `type(uint256).max` when `user` has no debt, since an unborrowed position cannot be liquidated.
232+ /// CAUTION: Call `accrueInterest(market)` first if an up-to-the-block value is required.
233+ /// @param morpho The Morpho Blue singleton.
234+ /// @param market Morpho market parameters identifying the position.
235+ /// @param user The account whose health factor is being read.
236+ function healthFactor (IMorpho morpho , MarketParams memory market , address user ) internal view returns (uint256 ) {
237+ uint256 debtAmount = debt (morpho, market, user);
190238 if (debtAmount == 0 ) return type (uint256 ).max;
191- return maxBorrow (morpho, market).mulDiv (WAD, debtAmount);
239+ return maxBorrow (morpho, market, user ).mulDiv (WAD, debtAmount);
192240 }
193241
194242 /// @notice Returns the additional loan-token amount this contract can borrow to reach `targetHealthFactor`.
0 commit comments