Skip to content

Commit

Permalink
Merge pull request #144 from morpho-dao/refactor/move-adding-to-colla…
Browse files Browse the repository at this point in the history
…teral-list

Refactor/move adding to collateral list
  • Loading branch information
pakim249CAL authored Jan 14, 2023
2 parents d5cacec + 95901e4 commit 7b06160
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/MorphoInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ abstract contract MorphoInternal is MorphoStorage {
onPool,
inP2P
);
if (onPool == 0 && inP2P == 0) _userBorrows[user].remove(underlying);
else _userBorrows[user].add(underlying);
}

function _setPauseStatus(address underlying, bool isPaused) internal {
Expand Down
7 changes: 6 additions & 1 deletion src/PositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

import {Permit2Lib} from "./libraries/Permit2Lib.sol";
import {ERC20, SafeTransferLib} from "@solmate/utils/SafeTransferLib.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import {MorphoStorage} from "./MorphoStorage.sol";
import {PositionsManagerInternal} from "./PositionsManagerInternal.sol";
Expand All @@ -24,6 +25,7 @@ contract PositionsManager is IPositionsManager, PositionsManagerInternal {
using Permit2Lib for ERC20;
using SafeTransferLib for ERC20;
using MarketBalanceLib for Types.MarketBalances;
using EnumerableSet for EnumerableSet.AddressSet;

using WadRayMath for uint256;
using PercentageMath for uint256;
Expand Down Expand Up @@ -66,6 +68,7 @@ contract PositionsManager is IPositionsManager, PositionsManagerInternal {
ERC20(underlying).transferFrom2(from, address(this), amount);

_marketBalances[underlying].collateral[onBehalf] += amount.rayDiv(indexes.supply.poolIndex);
_userCollaterals[onBehalf].add(underlying);

_POOL.supplyToPool(underlying, amount);

Expand Down Expand Up @@ -131,7 +134,9 @@ contract PositionsManager is IPositionsManager, PositionsManagerInternal {
// The following check requires storage indexes to be up-to-date.
_validateWithdrawCollateral(underlying, amount, supplier);

_marketBalances[underlying].collateral[supplier] -= amount.rayDiv(indexes.supply.poolIndex);
uint256 newBalance = _marketBalances[underlying].collateral[supplier] - amount.rayDiv(indexes.supply.poolIndex);
_marketBalances[underlying].collateral[supplier] = newBalance;
if (newBalance == 0) _userCollaterals[supplier].remove(underlying);

_POOL.withdrawFromPool(underlying, _market[underlying].aToken, amount);
ERC20(underlying).safeTransfer(receiver, amount);
Expand Down
16 changes: 0 additions & 16 deletions src/PositionsManagerInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ abstract contract PositionsManagerInternal is MatchingEngine {
onPool = marketBalances.scaledPoolSupplyBalance(user);
inP2P = marketBalances.scaledP2PSupplyBalance(user);

_userCollaterals[user].add(underlying);

/// Peer-to-peer supply ///

// Match the peer-to-peer borrow delta.
Expand Down Expand Up @@ -236,7 +234,6 @@ abstract contract PositionsManagerInternal is MatchingEngine {
Types.MarketBalances storage marketBalances = _marketBalances[underlying];
Types.Deltas storage deltas = market.deltas;

_userBorrows[user].add(underlying);
vars.onPool = marketBalances.scaledPoolBorrowBalance(user);
vars.inP2P = marketBalances.scaledP2PBorrowBalance(user);

Expand Down Expand Up @@ -319,11 +316,6 @@ abstract contract PositionsManagerInternal is MatchingEngine {

if (amount == 0) {
_updateBorrowerInDS(underlying, user, onPool, inP2P);

if (inP2P == 0 && onPool == 0) {
_userBorrows[user].remove(underlying);
}

return (onPool, inP2P, 0, toRepay);
}
}
Expand Down Expand Up @@ -392,8 +384,6 @@ abstract contract PositionsManagerInternal is MatchingEngine {
/// Note: Only used in breaking repay. Suppliers should not be able to supply if the pool is supply capped.
toSupply = _handleSupplyCap(underlying, amount);
}

if (inP2P == 0 && onPool == 0) _userBorrows[user].remove(underlying);
}

function _executeWithdraw(
Expand Down Expand Up @@ -421,10 +411,6 @@ abstract contract PositionsManagerInternal is MatchingEngine {
if (amount == 0) {
_updateSupplierInDS(underlying, user, vars.onPool, vars.inP2P);

if (vars.inP2P == 0 && vars.onPool == 0) {
_userCollaterals[user].remove(underlying);
}

return vars;
}
}
Expand Down Expand Up @@ -480,8 +466,6 @@ abstract contract PositionsManagerInternal is MatchingEngine {
emit Events.P2PAmountsUpdated(underlying, deltas.p2pSupplyAmount, deltas.p2pBorrowAmount);
vars.toBorrow = amount;
}

if (vars.inP2P == 0 && vars.onPool == 0) _userCollaterals[user].remove(underlying);
}

function _calculateAmountToSeize(
Expand Down

0 comments on commit 7b06160

Please sign in to comment.