ExitPositionsManager

Git Source

Inherits: IExitPositionsManager, PositionsManagerUtils

Author: Morpho Labs.

Morpho's exit points: withdraw, repay and liquidate.

Functions

withdrawLogic

LOGIC ///

Implements withdraw logic with security checks.

function withdrawLogic(
    address _poolToken,
    uint256 _amount,
    address _supplier,
    address _receiver,
    uint256 _maxGasForMatching
) external;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market the user wants to interact with.
_amountuint256The amount of token (in underlying).
_supplieraddressThe address of the supplier.
_receiveraddressThe address of the user who will receive the tokens.
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

repayLogic

Implements repay logic with security checks.

function repayLogic(
    address _poolToken,
    address _repayer,
    address _onBehalf,
    uint256 _amount,
    uint256 _maxGasForMatching
) external;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market the user wants to interact with.
_repayeraddressThe address of the account repaying the debt.
_onBehalfaddressThe address of the account whose debt is repaid.
_amountuint256The amount of token (in underlying).
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

liquidateLogic

Liquidates a position.

function liquidateLogic(address _poolTokenBorrowed, address _poolTokenCollateral, address _borrower, uint256 _amount)
    external;

Parameters

NameTypeDescription
_poolTokenBorrowedaddressThe address of the pool token the liquidator wants to repay.
_poolTokenCollateraladdressThe address of the collateral pool token the liquidator wants to seize.
_borroweraddressThe address of the borrower to liquidate.
_amountuint256The amount of token (in underlying) to repay.

increaseP2PDeltasLogic

Implements increaseP2PDeltas logic.

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.

function increaseP2PDeltasLogic(address _poolToken, uint256 _amount) external isMarketCreated(_poolToken);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market on which to increase deltas.
_amountuint256The maximum amount to add to the deltas (in underlying).

_unsafeWithdrawLogic

INTERNAL ///

Implements withdraw logic without security checks.

function _unsafeWithdrawLogic(
    address _poolToken,
    uint256 _amount,
    address _supplier,
    address _receiver,
    uint256 _maxGasForMatching
) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market the user wants to interact with.
_amountuint256The amount of token (in underlying).
_supplieraddressThe address of the supplier.
_receiveraddressThe address of the user who will receive the tokens.
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

_unsafeRepayLogic

Pool withdraw /// Transfer withdraw /// Breaking withdraw ///

Implements repay logic without security checks.

function _unsafeRepayLogic(
    address _poolToken,
    address _repayer,
    address _onBehalf,
    uint256 _amount,
    uint256 _maxGasForMatching
) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market the user wants to interact with.
_repayeraddressThe address of the account repaying the debt.
_onBehalfaddressThe address of the account whose debt is repaid.
_amountuint256The amount of token (in underlying).
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

_getUserHealthFactor

Pool repay /// Transfer repay /// Breaking repay ///

Returns the health factor of the user.

function _getUserHealthFactor(address _user, address _poolToken, uint256 _withdrawnAmount) internal returns (uint256);

Parameters

NameTypeDescription
_useraddressThe user to determine liquidity for.
_poolTokenaddressThe market to hypothetically withdraw from.
_withdrawnAmountuint256The number of tokens to hypothetically withdraw (in underlying).

Returns

NameTypeDescription
<none>uint256The health factor of the user.

_withdrawAllowed

Checks whether the user can withdraw or not.

function _withdrawAllowed(address _user, address _poolToken, uint256 _withdrawnAmount) internal returns (bool);

Parameters

NameTypeDescription
_useraddressThe user to determine liquidity for.
_poolTokenaddressThe market to hypothetically withdraw/borrow in.
_withdrawnAmountuint256The number of tokens to hypothetically withdraw (in underlying).

Returns

NameTypeDescription
<none>boolWhether the withdraw is allowed or not.

_liquidationAllowed

Returns whether a given user is liquidatable and the applicable close factor, given the deprecated status of the borrowed market.

function _liquidationAllowed(address _user, bool _isDeprecated)
    internal
    returns (bool liquidationAllowed, uint256 closeFactor);

Parameters

NameTypeDescription
_useraddressThe user to check.
_isDeprecatedboolWhether the borrowed market is deprecated or not.

Returns

NameTypeDescription
liquidationAllowedboolWhether the liquidation is allowed or not.
closeFactoruint256The close factor to apply.

Events

Withdrawn

EVENTS ///

Emitted when a withdrawal happens.

event Withdrawn(
    address indexed _supplier,
    address indexed _receiver,
    address indexed _poolToken,
    uint256 _amount,
    uint256 _balanceOnPool,
    uint256 _balanceInP2P
);

Repaid

Emitted when a repayment happens.

event Repaid(
    address indexed _repayer,
    address indexed _onBehalf,
    address indexed _poolToken,
    uint256 _amount,
    uint256 _balanceOnPool,
    uint256 _balanceInP2P
);

Liquidated

Emitted when a liquidation happens.

event Liquidated(
    address _liquidator,
    address indexed _liquidated,
    address indexed _poolTokenBorrowed,
    uint256 _amountRepaid,
    address indexed _poolTokenCollateral,
    uint256 _amountSeized
);

P2PDeltasIncreased

Emitted when the peer-to-peer deltas are increased by the governance.

event P2PDeltasIncreased(address indexed _poolToken, uint256 _amount);

Errors

UserNotMemberOfMarket

ERRORS ///

Thrown when user is not a member of the market.

error UserNotMemberOfMarket();

UnauthorisedWithdraw

Thrown when the user does not have enough remaining collateral to withdraw.

error UnauthorisedWithdraw();

UnauthorisedLiquidate

Thrown when the positions of the user is not liquidatable.

error UnauthorisedLiquidate();

WithdrawIsPaused

Thrown when someone tries to withdraw but the withdraw is paused.

error WithdrawIsPaused();

RepayIsPaused

Thrown when someone tries to repay but the repay is paused.

error RepayIsPaused();

LiquidateCollateralIsPaused

Thrown when someone tries to liquidate but the liquidation with this asset as collateral is paused.

error LiquidateCollateralIsPaused();

LiquidateBorrowIsPaused

Thrown when someone tries to liquidate but the liquidation with this asset as debt is paused.

error LiquidateBorrowIsPaused();

Structs

WithdrawVars

STRUCTS ///

struct WithdrawVars {
    uint256 remainingGasForMatching;
    uint256 remainingToWithdraw;
    uint256 poolSupplyIndex;
    uint256 p2pSupplyIndex;
    uint256 onPoolSupply;
    uint256 toWithdraw;
}

RepayVars

struct RepayVars {
    uint256 remainingGasForMatching;
    uint256 remainingToRepay;
    uint256 poolSupplyIndex;
    uint256 poolBorrowIndex;
    uint256 p2pSupplyIndex;
    uint256 p2pBorrowIndex;
    uint256 borrowedOnPool;
    uint256 feeToRepay;
    uint256 toRepay;
}

LiquidateVars

struct LiquidateVars {
    uint256 liquidationBonus;
    uint256 collateralReserveDecimals;
    uint256 collateralTokenUnit;
    uint256 collateralBalance;
    uint256 collateralPrice;
    uint256 amountToSeize;
    uint256 borrowedReserveDecimals;
    uint256 borrowedTokenUnit;
    uint256 borrowedTokenPrice;
    uint256 amountToLiquidate;
    uint256 closeFactor;
    bool liquidationAllowed;
}