Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover increase delta and fix safe sub #1362

Merged
merged 22 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 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,47 @@ 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)
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved
{
_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)
)
)
);

deltas.p2pSupplyDelta += _amount.rayDiv(poolIndexes.poolSupplyIndex);
deltas.p2pSupplyDelta = deltas.p2pSupplyDelta;
deltas.p2pBorrowDelta += _amount.rayDiv(poolIndexes.poolBorrowIndex);
deltas.p2pBorrowDelta = deltas.p2pBorrowDelta;
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 Expand Up @@ -498,8 +544,9 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
// No need to subtract p2pBorrowDelta as it is zero.
vars.feeToRepay = Math.zeroFloorSub(
delta.p2pBorrowAmount.rayMul(vars.p2pBorrowIndex),
(delta.p2pSupplyAmount.rayMul(vars.p2pSupplyIndex) -
delta.p2pSupplyDelta.rayMul(vars.poolSupplyIndex))
delta.p2pSupplyAmount.rayMul(vars.p2pSupplyIndex).zeroFloorSub(
delta.p2pSupplyDelta.rayMul(vars.poolSupplyIndex)
)
);

if (vars.feeToRepay > 0) {
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;
}
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);
}
32 changes: 16 additions & 16 deletions contracts/aave-v3/ExitPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -288,35 +288,35 @@ 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 create deltas.
/// @param _amount The amount to add to the deltas (in underlying).
function increaseP2PDeltasLogic(address _poolToken, uint256 _amount) external {
/// @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.Delta memory deltasMem = deltas;
Types.PoolIndexes memory poolIndexes = poolIndexes[_poolToken];
uint256 p2pSupplyIndex = p2pSupplyIndex[_poolToken];
uint256 p2pBorrowIndex = p2pBorrowIndex[_poolToken];

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

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

ERC20 underlyingToken = ERC20(market[_poolToken].underlyingToken);
_borrowFromPool(underlyingToken, _amount);
Expand Down
12 changes: 4 additions & 8 deletions contracts/aave-v3/MorphoGovernance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,12 @@ abstract contract MorphoGovernance is MorphoUtils {
emit DeprecatedStatusSet(_poolToken, _isDeprecated);
}

/// @notice Creates peer-to-peer deltas, to put some liquidity back on the pool.
/// @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 create deltas.
/// @param _amount The amount to add to the deltas (in underlying).
function increaseP2PDeltas(address _poolToken, uint256 _amount)
external
onlyOwner
isMarketCreated(_poolToken)
{
/// @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,
Expand Down
16 changes: 16 additions & 0 deletions contracts/compound/MorphoGovernance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "./MorphoUtils.sol";
/// @notice Governance functions for Morpho.
abstract contract MorphoGovernance is MorphoUtils {
using SafeTransferLib for ERC20;
using DelegateCall for address;

/// EVENTS ///

Expand Down Expand Up @@ -274,6 +275,21 @@ abstract contract MorphoGovernance is MorphoUtils {
emit ClaimRewardsPauseStatusSet(_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(positionsManager).functionDelegateCall(
abi.encodeWithSelector(
IPositionsManager.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
50 changes: 48 additions & 2 deletions contracts/compound/PositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ contract PositionsManager is IPositionsManager, MatchingEngine {
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);

/// @notice Emitted when the borrow peer-to-peer delta is updated.
/// @param _poolToken The address of the market.
/// @param _p2pBorrowDelta The borrow peer-to-peer delta after update.
Expand Down Expand Up @@ -493,6 +498,46 @@ contract PositionsManager is IPositionsManager, MatchingEngine {
);
}

/// @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)
{
_updateP2PIndexes(_poolToken);

Types.Delta storage deltas = deltas[_poolToken];
Types.LastPoolIndexes memory lastPoolIndexes = lastPoolIndexes[_poolToken];

uint256 poolSupplyIndex = ICToken(_poolToken).exchangeRateStored();
_amount = Math.min(
_amount,
Math.min(
deltas.p2pSupplyAmount.mul(p2pSupplyIndex[_poolToken]).safeSub(
deltas.p2pSupplyDelta.mul(poolSupplyIndex)
),
deltas.p2pBorrowAmount.mul(p2pBorrowIndex[_poolToken]).safeSub(
deltas.p2pBorrowDelta.mul(lastPoolIndexes.lastBorrowPoolIndex)
)
)
);

deltas.p2pSupplyDelta += _amount.div(poolSupplyIndex);
deltas.p2pSupplyDelta = deltas.p2pSupplyDelta;
deltas.p2pBorrowDelta += _amount.div(lastPoolIndexes.lastBorrowPoolIndex);
deltas.p2pBorrowDelta = deltas.p2pBorrowDelta;
emit P2PSupplyDeltaUpdated(_poolToken, deltas.p2pSupplyDelta);
emit P2PBorrowDeltaUpdated(_poolToken, deltas.p2pBorrowDelta);

_borrowFromPool(_poolToken, _amount);
_supplyToPool(_poolToken, _getUnderlying(_poolToken), _amount);

emit P2PDeltasIncreased(_poolToken, _amount);
}

/// INTERNAL ///

/// @dev Implements withdraw logic without security checks.
Expand Down Expand Up @@ -754,8 +799,9 @@ contract PositionsManager is IPositionsManager, MatchingEngine {
// No need to subtract p2pBorrowDelta as it is zero.
vars.feeToRepay = CompoundMath.safeSub(
delta.p2pBorrowAmount.mul(vars.p2pBorrowIndex),
(delta.p2pSupplyAmount.mul(vars.p2pSupplyIndex) -
delta.p2pSupplyDelta.mul(ICToken(_poolToken).exchangeRateStored()))
delta.p2pSupplyAmount.mul(vars.p2pSupplyIndex).safeSub(
delta.p2pSupplyDelta.mul(ICToken(_poolToken).exchangeRateStored())
)
);

if (vars.feeToRepay > 0) {
Expand Down
2 changes: 2 additions & 0 deletions contracts/compound/interfaces/IPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ interface IPositionsManager {
address _borrower,
uint256 _amount
) external;

function increaseP2PDeltasLogic(address _poolToken, uint256 _amount) external;
}
Loading