MorphoUtils

Git Source

Inherits: MorphoStorage

Author: Morpho Labs.

Modifiers, getters and other util functions for Morpho.

Functions

isMarketCreated

MODIFIERS ///

Prevents to update a market not created yet.

modifier isMarketCreated(address _poolToken);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market to check.

getMarketsCreated

EXTERNAL ///

Returns all created markets.

function getMarketsCreated() external view returns (address[] memory);

Returns

NameTypeDescription
<none>address[]marketsCreated_ The list of market addresses.

getHead

Gets the head of the data structure on a specific market (for UI).

function getHead(address _poolToken, Types.PositionType _positionType) external view returns (address head);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market from which to get the head.
_positionTypeTypes.PositionTypeThe type of user from which to get the head.

Returns

NameTypeDescription
headaddressThe head in the data structure.

getNext

Gets the next user after _user in the data structure on a specific market (for UI).

Beware that this function does not give the account with the highest liquidity.

function getNext(address _poolToken, Types.PositionType _positionType, address _user)
    external
    view
    returns (address next);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market from which to get the user.
_positionTypeTypes.PositionTypeThe type of user from which to get the next user.
_useraddressThe address of the user from which to get the next user.

Returns

NameTypeDescription
nextaddressThe next user in the data structure.

updateIndexes

Updates the peer-to-peer indexes and pool indexes (only stored locally).

function updateIndexes(address _poolToken) external isMarketCreated(_poolToken);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market to update.

_isSupplyingOrBorrowing

INTERNAL ///

Returns if a user has been borrowing or supplying on a given market.

function _isSupplyingOrBorrowing(bytes32 _userMarkets, bytes32 _borrowMask) internal pure returns (bool);

Parameters

NameTypeDescription
_userMarketsbytes32The bitmask encoding the markets entered by the user.
_borrowMaskbytes32The borrow mask of the market to check.

Returns

NameTypeDescription
<none>boolTrue if the user has been supplying or borrowing on this market, false otherwise.

_isBorrowing

Returns if a user is borrowing on a given market.

function _isBorrowing(bytes32 _userMarkets, bytes32 _borrowMask) internal pure returns (bool);

Parameters

NameTypeDescription
_userMarketsbytes32The bitmask encoding the markets entered by the user.
_borrowMaskbytes32The borrow mask of the market to check.

Returns

NameTypeDescription
<none>boolTrue if the user has been borrowing on this market, false otherwise.

_isSupplying

Returns if a user is supplying on a given market.

function _isSupplying(bytes32 _userMarkets, bytes32 _borrowMask) internal pure returns (bool);

Parameters

NameTypeDescription
_userMarketsbytes32The bitmask encoding the markets entered by the user.
_borrowMaskbytes32The borrow mask of the market to check.

Returns

NameTypeDescription
<none>boolTrue if the user has been supplying on this market, false otherwise.

_isBorrowingAny

Returns if a user has been borrowing from any market.

function _isBorrowingAny(bytes32 _userMarkets) internal pure returns (bool);

Parameters

NameTypeDescription
_userMarketsbytes32The bitmask encoding the markets entered by the user.

Returns

NameTypeDescription
<none>boolTrue if the user has been borrowing on any market, false otherwise.

_isBorrowingAndSupplying

Returns if a user is borrowing on a given market and supplying on another given market.

function _isBorrowingAndSupplying(bytes32 _userMarkets, bytes32 _borrowedBorrowMask, bytes32 _suppliedBorrowMask)
    internal
    pure
    returns (bool);

Parameters

NameTypeDescription
_userMarketsbytes32The bitmask encoding the markets entered by the user.
_borrowedBorrowMaskbytes32The borrow mask of the market to check whether the user is borrowing.
_suppliedBorrowMaskbytes32The borrow mask of the market to check whether the user is supplying.

Returns

NameTypeDescription
<none>boolTrue if the user is borrowing on the given market and supplying on the other given market, false otherwise.

_setBorrowing

Sets if the user is borrowing on a market.

function _setBorrowing(address _user, bytes32 _borrowMask, bool _borrowing) internal;

Parameters

NameTypeDescription
_useraddressThe user to set for.
_borrowMaskbytes32The borrow mask of the market to mark as borrowed.
_borrowingboolTrue if the user is borrowing, false otherwise.

_setSupplying

Sets if the user is supplying on a market.

function _setSupplying(address _user, bytes32 _borrowMask, bool _supplying) internal;

Parameters

NameTypeDescription
_useraddressThe user to set for.
_borrowMaskbytes32The borrow mask of the market to mark as supplied.
_supplyingboolTrue if the user is supplying, false otherwise.

_updateIndexes

Updates the peer-to-peer indexes and pool indexes (only stored locally).

function _updateIndexes(address _poolToken) internal;

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market to update.

_getUserSupplyBalanceInOf

Returns the supply balance of _user in the _poolToken market.

Note: Computes the result with the stored indexes, which are not always the most up to date ones.

function _getUserSupplyBalanceInOf(address _poolToken, address _user) internal view returns (uint256);

Parameters

NameTypeDescription
_poolTokenaddressThe market where to get the supply amount.
_useraddressThe address of the user.

Returns

NameTypeDescription
<none>uint256The supply balance of the user (in underlying).

_getUserBorrowBalanceInOf

Returns the borrow balance of _user in the _poolToken market.

Note: Computes the result with the stored indexes, which are not always the most up to date ones.

function _getUserBorrowBalanceInOf(address _poolToken, address _user) internal view returns (uint256);

Parameters

NameTypeDescription
_poolTokenaddressThe market where to get the borrow amount.
_useraddressThe address of the user.

Returns

NameTypeDescription
<none>uint256The borrow balance of the user (in underlying).

_collateralValue

Calculates the value of the collateral.

function _collateralValue(address _poolToken, address _user, uint256 _underlyingPrice, uint256 _tokenUnit)
    internal
    view
    returns (uint256 collateral);

Parameters

NameTypeDescription
_poolTokenaddressThe pool token to calculate the value for.
_useraddressThe user address.
_underlyingPriceuint256The underlying price.
_tokenUnituint256The token unit.

_debtValue

Calculates the value of the debt.

function _debtValue(address _poolToken, address _user, uint256 _underlyingPrice, uint256 _tokenUnit)
    internal
    view
    returns (uint256 debt);

Parameters

NameTypeDescription
_poolTokenaddressThe pool token to calculate the value for.
_useraddressThe user address.
_underlyingPriceuint256The underlying price.
_tokenUnituint256The token unit.

_liquidityData

Calculates the total value of the collateral, debt, and LTV/LT value depending on the calculation type.

Expects the given user's entered markets to include the given market.

Expects the given market's pool & peer-to-peer indexes to have been updated.

Expects _amountWithdrawn to be less than or equal to the given user's supply on the given market.

function _liquidityData(address _user, address _poolToken, uint256 _amountWithdrawn, uint256 _amountBorrowed)
    internal
    returns (Types.LiquidityData memory values);

Parameters

NameTypeDescription
_useraddressThe user address.
_poolTokenaddressThe pool token that is being borrowed or withdrawn.
_amountWithdrawnuint256The amount that is being withdrawn.
_amountBorroweduint256The amount that is being borrowed.

Returns

NameTypeDescription
valuesTypes.LiquidityDataThe struct containing health factor, collateral, debt, ltv, liquidation threshold values.

Errors

MarketNotCreated

ERRORS ///

Thrown when the market is not created yet.

error MarketNotCreated();

Structs

LiquidityVars

STRUCTS ///

struct LiquidityVars {
    address poolToken;
    uint256 poolTokensLength;
    bytes32 userMarkets;
    bytes32 borrowMask;
    address underlyingToken;
    uint256 underlyingPrice;
}