Skip to content

Commit

Permalink
Merge pull request #1448 from morpho-dao/feat/increase-delta
Browse files Browse the repository at this point in the history
Increase delta
  • Loading branch information
MerlinEgalite authored Nov 26, 2022
2 parents a023e38 + b4b531b commit b45de24
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 107 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ You can also send an email to [[email protected]](mailto:[email protected])
### Morpho-Aave-V2 Ethereum

- Morpho Proxy: [0x299ff2534c6f11624d6a65463b8b40c958ab668f](https://etherscan.io/address/0x299ff2534c6f11624d6a65463b8b40c958ab668f)
- Morpho Implementation: [0x299ff2534c6f11624d6a65463b8b40c958ab668f](https://etherscan.io/address/0x299ff2534c6f11624d6a65463b8b40c958ab668f)
- Morpho Implementation: [0x497897080d90bc0418f896f4bfcdddd06fe8a9ea](https://etherscan.io/address/0x497897080d90bc0418f896f4bfcdddd06fe8a9ea)
- EntryPositionsManager: [0xdf93cf1ca3acf96bc26783e6fab89400d362d0b4](https://etherscan.io/address/0xdf93cf1ca3acf96bc26783e6fab89400d362d0b4)
- ExitPositionsManager: [0xf6998f72b92b81c8f683d30ed8678d348fe9754b](https://etherscan.io/address/0xf6998f72b92b81c8f683d30ed8678d348fe9754b)
- ExitPositionsManager: [0x51ae58e71fed9b1162810898bbd68301206786b2](https://etherscan.io/address/0x51ae58e71fed9b1162810898bbd68301206786b2)
- InterestRatesManager: [0x91b23044d4a8089670309852c7f0a93e5ca8efb7](https://etherscan.io/address/0x91b23044d4a8089670309852c7f0a93e5ca8efb7)
- Lens Proxy: [0x507fa343d0a90786d86c7cd885f5c49263a91ff4](https://etherscan.io/address/0x507fa343d0a90786d86c7cd885f5c49263a91ff4)
- Lens: [0x8706256509684e9cd93b7f19254775ce9324c226](https://etherscan.io/address/0x8706256509684e9cd93b7f19254775ce9324c226)
Expand Down
44 changes: 44 additions & 0 deletions contracts/aave-v2/ExitPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
uint256 _amountSeized
);

/// @notice Emitted when the peer-to-peer deltas are increased by the governance.
/// @param _poolToken The address of the market on which the deltas were increased.
/// @param _amount The amount that has been added to the deltas (in underlying).
event P2PDeltasIncreased(address indexed _poolToken, uint256 _amount);

/// ERRORS ///

/// @notice Thrown when user is not a member of the market.
Expand Down Expand Up @@ -250,6 +255,45 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
);
}

/// @notice Implements increaseP2PDeltas logic.
/// @dev The current Morpho supply on the pool might not be enough to borrow `_amount` before resupplying it.
/// In this case, consider calling this function multiple times.
/// @param _poolToken The address of the market on which to increase deltas.
/// @param _amount The maximum amount to add to the deltas (in underlying).
function increaseP2PDeltasLogic(address _poolToken, uint256 _amount)
external
isMarketCreated(_poolToken)
{
_updateIndexes(_poolToken);

Types.Delta storage deltas = deltas[_poolToken];
Types.PoolIndexes memory poolIndexes = poolIndexes[_poolToken];

_amount = Math.min(
_amount,
Math.min(
deltas.p2pSupplyAmount.rayMul(p2pSupplyIndex[_poolToken]).zeroFloorSub(
deltas.p2pSupplyDelta.rayMul(poolIndexes.poolSupplyIndex)
),
deltas.p2pBorrowAmount.rayMul(p2pBorrowIndex[_poolToken]).zeroFloorSub(
deltas.p2pBorrowDelta.rayMul(poolIndexes.poolBorrowIndex)
)
)
);
if (_amount == 0) revert AmountIsZero();

deltas.p2pSupplyDelta += _amount.rayDiv(poolIndexes.poolSupplyIndex);
deltas.p2pBorrowDelta += _amount.rayDiv(poolIndexes.poolBorrowIndex);
emit P2PSupplyDeltaUpdated(_poolToken, deltas.p2pSupplyDelta);
emit P2PBorrowDeltaUpdated(_poolToken, deltas.p2pBorrowDelta);

ERC20 underlyingToken = ERC20(market[_poolToken].underlyingToken);
_borrowFromPool(underlyingToken, _amount);
_supplyToPool(underlyingToken, _amount);

emit P2PDeltasIncreased(_poolToken, _amount);
}

/// INTERNAL ///

/// @dev Implements withdraw logic without security checks.
Expand Down
16 changes: 16 additions & 0 deletions contracts/aave-v2/MorphoGovernance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract contract MorphoGovernance is MorphoUtils {
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using PercentageMath for uint256;
using SafeTransferLib for ERC20;
using DelegateCall for address;
using WadRayMath for uint256;

/// EVENTS ///
Expand Down Expand Up @@ -327,6 +328,21 @@ abstract contract MorphoGovernance is MorphoUtils {
pool.setUserUseReserveAsCollateral(market[_poolToken].underlyingToken, _newStatus);
}

/// @notice Increases peer-to-peer deltas, to put some liquidity back on the pool.
/// @dev The current Morpho supply on the pool might not be enough to borrow `_amount` before resuppling it.
/// In this case, consider calling multiple times this function.
/// @param _poolToken The address of the market on which to increase deltas.
/// @param _amount The maximum amount to add to the deltas (in underlying).
function increaseP2PDeltas(address _poolToken, uint256 _amount) external onlyOwner {
address(exitPositionsManager).functionDelegateCall(
abi.encodeWithSelector(
IExitPositionsManager.increaseP2PDeltasLogic.selector,
_poolToken,
_amount
)
);
}

/// @notice Transfers the protocol reserve fee to the DAO.
/// @param _poolTokens The addresses of the pool token addresses on which to claim the reserve fee.
/// @param _amounts The list of amounts of underlying tokens to claim on each market.
Expand Down
2 changes: 2 additions & 0 deletions contracts/aave-v2/interfaces/IExitPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ interface IExitPositionsManager {
address _borrower,
uint256 _amount
) external;

function increaseP2PDeltasLogic(address _poolToken, uint256 _amount) external;
}
1 change: 1 addition & 0 deletions contracts/aave-v2/interfaces/IMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ interface IMorpho {
function setInterestRatesManager(IInterestRatesManager _interestRatesManager) external;
function claimToTreasury(address[] calldata _poolTokens, uint256[] calldata _amounts) external;
function createMarket(address _underlyingToken, uint16 _reserveFactor, uint16 _p2pIndexCursor) external;
function increaseP2PDeltas(address _poolToken, uint256 _amount) external;

/// USERS ///

Expand Down
6 changes: 6 additions & 0 deletions contracts/aave-v2/interfaces/aave/IVariableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,10 @@ interface IVariableDebtToken is IScaledBalanceToken {
uint256 amount,
uint256 index
) external;

/**
* @dev Returns the debt balance of the user
* @return The debt balance of the user.
**/
function balanceOf(address user) external view returns (uint256);
}
Loading

0 comments on commit b45de24

Please sign in to comment.