PositionsManager

Git Source

Inherits: IPositionsManager, MatchingEngine

Author: Morpho Labs.

Main Logic of Morpho Protocol, implementation of the 5 main functionalities: supply, borrow, withdraw, repay and liquidate.

Functions

supplyLogic

LOGIC ///

Implements supply logic.

function supplyLogic(address _poolToken, address _from, address _onBehalf, uint256 _amount, uint256 _maxGasForMatching)
    external;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the pool token the user wants to interact with.
_fromaddressThe address of the account sending funds.
_onBehalfaddressThe address of the account whose positions will be updated.
_amountuint256The amount of token (in underlying).
_maxGasForMatchinguint256The maximum amount of gas to consume within a matching engine loop.

borrowLogic

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

Implements borrow logic.

function borrowLogic(address _poolToken, uint256 _amount, uint256 _maxGasForMatching) external;

Parameters

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

withdrawLogic

Peer-to-peer borrow /// Pool borrow ///

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.

_supplyToPool

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

Supplies underlying tokens to Compound.

function _supplyToPool(address _poolToken, ERC20 _underlyingToken, uint256 _amount) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the pool token.
_underlyingTokenERC20The underlying token of the market to supply to.
_amountuint256The amount of token (in underlying).

_withdrawFromPool

Withdraws underlying tokens from Compound.

function _withdrawFromPool(address _poolToken, uint256 _amount) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the pool token.
_amountuint256The amount of token (in underlying).

_borrowFromPool

Borrows underlying tokens from Compound.

function _borrowFromPool(address _poolToken, uint256 _amount) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the pool token.
_amountuint256The amount of token (in underlying).

_repayToPool

Repays underlying tokens to Compound.

function _repayToPool(address _poolToken, ERC20 _underlyingToken, uint256 _amount) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the pool token.
_underlyingTokenERC20The underlying token of the market to repay to.
_amountuint256The amount of token (in underlying).

_enterMarketIfNeeded

Enters the user into the market if not already there.

function _enterMarketIfNeeded(address _poolToken, address _user) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market to check.
_useraddressThe address of the user to update.

_leaveMarketIfNeeded

Removes the user from the market if its balances are null.

function _leaveMarketIfNeeded(address _poolToken, address _user) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market to check.
_useraddressThe address of the user to update.

_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
    view
    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

Supplied

EVENTS ///

Emitted when a supply happens.

event Supplied(
    address indexed _supplier,
    address indexed _onBehalf,
    address indexed _poolToken,
    uint256 _amount,
    uint256 _balanceOnPool,
    uint256 _balanceInP2P
);

Borrowed

Emitted when a borrow happens.

event Borrowed(
    address indexed _borrower,
    address indexed _poolToken,
    uint256 _amount,
    uint256 _balanceOnPool,
    uint256 _balanceInP2P
);

Withdrawn

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);

P2PBorrowDeltaUpdated

Emitted when the borrow peer-to-peer delta is updated.

event P2PBorrowDeltaUpdated(address indexed _poolToken, uint256 _p2pBorrowDelta);

P2PSupplyDeltaUpdated

Emitted when the supply peer-to-peer delta is updated.

event P2PSupplyDeltaUpdated(address indexed _poolToken, uint256 _p2pSupplyDelta);

P2PAmountsUpdated

Emitted when the supply and borrow peer-to-peer amounts are updated.

event P2PAmountsUpdated(address indexed _poolToken, uint256 _p2pSupplyAmount, uint256 _p2pBorrowAmount);

Errors

AmountAboveWhatAllowedToRepay

ERRORS ///

Thrown when the amount repaid during the liquidation is above what is allowed to be repaid.

error AmountAboveWhatAllowedToRepay();

BorrowOnCompoundFailed

Thrown when the borrow on Compound failed and throws back the Compound error code.

error BorrowOnCompoundFailed(uint256 errorCode);

RedeemOnCompoundFailed

Thrown when the redeem on Compound failed and throws back the Compound error code.

error RedeemOnCompoundFailed(uint256 errorCode);

RepayOnCompoundFailed

Thrown when the repay on Compound failed and throws back the Compound error code.

error RepayOnCompoundFailed(uint256 errorCode);

MintOnCompoundFailed

Thrown when the mint on Compound failed and throws back the Compound error code.

error MintOnCompoundFailed(uint256 errorCode);

UserNotMemberOfMarket

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();

UnauthorisedBorrow

Thrown when the user does not have enough collateral for the borrow.

error UnauthorisedBorrow();

WithdrawTooSmall

Thrown when the amount desired for a withdrawal is too small.

error WithdrawTooSmall();

AddressIsZero

Thrown when the address is zero.

error AddressIsZero();

AmountIsZero

Thrown when the amount is equal to 0.

error AmountIsZero();

SameBlockBorrowRepay

Thrown when a user tries to repay its debt after borrowing in the same block.

error SameBlockBorrowRepay();

SupplyIsPaused

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

error SupplyIsPaused();

BorrowIsPaused

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

error BorrowIsPaused();

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

SupplyVars

STRUCTS ///

struct SupplyVars {
    uint256 remainingToSupply;
    uint256 poolBorrowIndex;
    uint256 toRepay;
}

WithdrawVars

struct WithdrawVars {
    uint256 remainingGasForMatching;
    uint256 remainingToWithdraw;
    uint256 poolSupplyIndex;
    uint256 p2pSupplyIndex;
    uint256 toWithdraw;
    ERC20 underlyingToken;
}

RepayVars

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

LiquidateVars

struct LiquidateVars {
    uint256 collateralPrice;
    uint256 borrowBalance;
    uint256 supplyBalance;
    uint256 borrowedPrice;
    uint256 amountToSeize;
    uint256 closeFactor;
    bool liquidationAllowed;
}