Skip to main content

Lens

The lens is a stateless contract that exposes view helpers to format data from the Optimizers' storage. They can be used to ease the integration on top of the Optimizers.

Lens Functions

getTotalSupply

function getTotalSupply()
external
view
returns (
uint256 p2pSupplyAmount,
uint256 poolSupplyAmount,
uint256 totalSupplyAmount
);

Return values:

NameTypeDescription
p2pSupplyAmountuint256The total supplied amount matched peer-to-peer, subtracting the supply delta (in USD, 18 decimals).
poolSupplyAmountuint256The total supplied amount on the underlying pool, adding the supply delta (in USD, 18 decimals).
totalSupplyAmountuint256The total amount supplied through Morpho (in USD, 18 decimals).

getTotalBorrow

 function getTotalBorrow()
external
view
returns (
uint256 p2pBorrowAmount,
uint256 poolBorrowAmount,
uint256 totalBorrowAmount
);

Return values:

NameTypeDescription
p2pBorrowAmountuint256The total borrowed amount matched peer-to-peer, subtracting the borrow delta (in USD, 18 decimals).
poolBorrowAmountuint256The total borrowed amount on the underlying pool, adding the borrow delta (in USD, 18 decimals).
totalBorrowAmountuint256The total amount borrowed through Morpho (in USD, 18 decimals).

Rewards Lens Functions

These functions are specific to Morpho CompoundV2 Optimizer.

getUserUnclaimedRewards

function getUserUnclaimedRewards(address[] calldata _poolTokens, address _user)
external
view
returns (uint256);

Returns the unclaimed COMP rewards for the given cToken addresses.

Parameters:

NameTypeDescription
_poolTokensaddress[]The cToken addresses for which to compute the rewards.
_useraddressThe user to compute the rewards for.

Return values:

NameTypeDescription
unclaimedRewardsuint256The unclaimed COMP rewards for the given cToken addresses.

getAccruedSupplierComp

function getAccruedSupplierComp(
address _supplier,
address _poolToken
) external view returns (uint256);

Returns the accrued COMP rewards of a user since the last update.

Parameters:

NameTypeDescription
_supplieraddressThe user to compute the rewards for.
_poolTokenaddressThe cToken address for which to compute the rewards.

Return values:

NameTypeDescription
accruedSupplierCompuint256The accrued COMP rewards.

getAccruedBorrowerComp

function getAccruedBorrowerComp(
address _supplier,
address _poolToken
) external view returns (uint256);

Returns the accrued COMP rewards of a user since the last update.

Parameters:

NameTypeDescription
_supplieraddressThe user to compute the rewards for.
_poolTokenaddressThe cToken address for which to compute the rewards.

Return values:

NameTypeDescription
accruedBorrowerCompuint256The accrued COMP rewards.

getCurrentCompSupplyIndex

function getCurrentCompSupplyIndex(address _poolToken) external view returns (uint256);

Returns the updated COMP supply index.

Parameters:

NameTypeDescription
_poolTokenaddressThe cToken address for which to compute the rewards.

Return values:

NameTypeDescription
supplyIndexuint256The updated COMP supply index.

getCurrentCompBorrowIndex

function getCurrentCompBorrowIndex(address _poolToken) external view returns (uint256);

Returns the updated COMP borrow index.

Parameters:

NameTypeDescription
_poolTokenaddressThe cToken address for which to compute the rewards.

Return values:

NameTypeDescription
supplyIndexuint256The updated COMP borrow index.

Market Lens Functions

isMarketCreated

function isMarketCreated(address _poolToken) external view returns (bool);

Checks if a market is created.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market to check.

Return values:

NameTypeDescription
isCreatedboolTrue if the market is created.

getAllMarkets

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

Returns the list of all markets created.

Return values:

NameTypeDescription
marketsaddress[]The list of all markets created.

getMainMarketData

  function getMainMarketData(address _poolToken)
external
view
returns (
uint256 avgSupplyRatePerBlock,
uint256 avgBorrowRatePerBlock,
uint256 p2pSupplyAmount,
uint256 p2pBorrowAmount,
uint256 poolSupplyAmount,
uint256 poolBorrowAmount
);

For a given market, returns the average supply/borrow rates and amounts of underlying asset supplied and borrowed through Morpho Optimizer, on the underlying pool and matched peer-to-peer.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market to check.

Return values:

NameTypeDescription
avgSupplyRatePerBlockuint256The average supply rate experienced on the given market (in wad).
avgBorrowRatePerBlockuint256The average borrow rate experienced on the given market (in wad).
p2pSupplyAmountuint256The total supplied amount matched peer-to-peer, subtracting the supply delta (in underlying).
p2pBorrowAmountuint256The total borrowed amount matched peer-to-peer, subtracting the borrow delta (in underlying).
poolSupplyAmountuint256The total supplied amount on the underlying pool, adding the supply delta (in underlying).
poolBorrowAmountuint256The total borrowed amount on the underlying pool, adding the borrow delta (in underlying).

getAdvancedMarketData

struct Indexes {
uint256 p2pSupplyIndex; // The peer-to-peer supply index (in wad), used to multiply the peer-to-peer supply scaled balance and get the peer-to-peer supply balance (in underlying).
uint256 p2pBorrowIndex; // The peer-to-peer borrow index (in wad), used to multiply the peer-to-peer borrow scaled balance and get the peer-to-peer borrow balance (in underlying).
uint256 poolSupplyIndex; // The pool supply index (in wad), used to multiply the pool supply scaled balance and get the pool supply balance (in underlying).
uint256 poolBorrowIndex; // The pool borrow index (in wad), used to multiply the pool borrow scaled balance and get the pool borrow balance (in underlying).
}

function getAdvancedMarketData(address _poolToken)
external
view
returns (
Indexes memory indexes,
uint32 lastUpdateBlockNumber,
uint256 p2pSupplyDelta,
uint256 p2pBorrowDelta
);

Returns non-updated indexes, the block at which they were last updated and the total deltas of a given market.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market to check.

Return values:

NameTypeDescription
indexesIndexesThe indexes of the given market.
lastUpdateBlockNumberuint32The block at which pool indexes were last updated.
p2pSupplyDeltauint256The total supply delta (in underlying).
p2pBorrowDeltauint256The total borrow delta (in underlying).

getMarketConfiguration

function getMarketConfiguration(address _poolToken)
external
view
returns (
address underlying,
bool isCreated,
bool isP2PDisabled,
bool isPaused,
bool isPartiallyPaused,
uint16 reserveFactor,
uint16 p2pIndexCursor,
uint256 collateralFactor
);

Returns the given market's configuration.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market of which to get the configuration.

Return values:

The return value is a tuple containing the following values:

NameTypeDescription
underlyingaddressThe underlying token address.
isCreatedboolWhether the market is created or not.
isP2PDisabledboolWhether the peer-to-peer market is enabled or not.
isPausedboolDeprecated
isPartiallyPausedboolDeprecated
reserveFactoruint16The reserve factor applied to this market.
p2pIndexCursoruint16The p2p index cursor applied to this market.
collateralFactoruint256The pool collateral factor also used by Morpho (in wad).

getMarketPauseStatus

struct MarketPauseStatus {
bool isSupplyPaused; // Whether the supply is paused or not.
bool isBorrowPaused; // Whether the borrow is paused or not
bool isWithdrawPaused; // Whether the withdraw is paused or not. Note that a "withdraw" is still possible using a liquidation (if not paused).
bool isRepayPaused; // Whether the repay is paused or not. Note that a "repay" is still possible using a liquidation (if not paused).
bool isLiquidateCollateralPaused; // Whether the liquidation on this market as collateral is paused or not.
bool isLiquidateBorrowPaused; // Whether the liquidatation on this market as borrow is paused or not.
bool isDeprecated; // Whether a market is deprecated or not.
}
function getMarketPauseStatus(address _poolToken)
external
view
returns (MarketPauseStatus memory);

Returns the given market's pause statuses.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market of which to get pause statuses.

Return values:

NameTypeDescription
marketPauseStatusMarketPauseStatusThe market status struct.

getTotalMarketSupply

function getTotalMarketSupply(address _poolToken)
external
view
returns (uint256 p2pSupplyAmount, uint256 poolSupplyAmount);

Computes and returns the total distribution of supply for a given market, using virtually updated indexes.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market to check.

Return values:

NameTypeDescription
p2pSupplyAmountuint256The total supplied amount matched peer-to-peer, subtracting the supply delta (in underlying).
poolSupplyAmountuint256The total supplied amount on the underlying pool, adding the supply delta (in underlying).

getTotalMarketBorrow

function getTotalMarketBorrow(address _poolToken)
external
view
returns (uint256 p2pBorrowAmount, uint256 poolBorrowAmount);

Computes and returns the total distribution of borrow for a given market, using virtually updated indexes.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market to check.

Return values:

NameTypeDescription
p2pBorrowAmountuint256The total borrowed amount matched peer-to-peer, subtracting the borrow delta (in underlying).
poolBorrowAmountuint256The total borrowed amount on the underlying pool, adding the borrow delta (in underlying).

Rates Lens Functions

getNextUserSupplyRatePerBlock

function getNextUserSupplyRatePerBlock(
address _poolToken,
address _user,
uint256 _amount
)
external
view
returns (
uint256 nextSupplyRatePerBlock,
uint256 balanceInP2P,
uint256 balanceOnPool,
uint256 totalBalance
);

Returns the supply rate per block experienced on a market after having supplied the given amount on behalf of the given user.

The returned supply rate is a low estimate: when supplying through Morpho CompoundV2 Optimizer, a supplier could be matched more than once instantly or later and thus benefit from a higher supply rate.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market.
_useraddressThe address of the user on behalf of whom to supply.
_amountuint256The amount to supply.

Return values:

NameTypeDescription
nextSupplyRatePerBlockuint256An approximation of the next supply rate per block experienced after having supplied (in wad).
balanceInP2Puint256The total balance matched peer-to-peer after having supplied (in underlying).
balanceOnPooluint256The total balance supplied on pool after having supplied (in underlying).
totalBalanceuint256The total balance supplied through Morpho (in underlying).

getNextUserBorrowRatePerYear

function getNextUserBorrowRatePerBlock(
address _poolToken,
address _user,
uint256 _amount
)
external
view
returns (
uint256 nextBorrowRatePerBlock,
uint256 balanceInP2P,
uint256 balanceOnPool,
uint256 totalBalance
);

Returns the borrow rate per block experienced on a market after having borrowed the given amount on behalf of the given user.

the returned borrow rate is a high estimate: when borrowing through Morpho CompoundV2 Optimizer, a borrower could be matched more than once instantly or later and thus benefit from a lower borrow rate.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market.
_useraddressThe address of the user on behalf of whom to borrow.
_amountuint256The amount to borrow.

Return values:

NameTypeDescription
nextBorrowRatePerBlockuint256An approximation of the next borrow rate per block experienced after having borrowed (in wad).
balanceInP2Puint256The total balance matched peer-to-peer after having borrowed (in underlying).
balanceOnPooluint256The total balance borrowed on pool after having borrowed (in underlying).
totalBalanceuint256The total balance borrowed through Morpho (in underlying).

getCurrentUserSupplyRatePerBlock

function getCurrentUserSupplyRatePerBlock(address _poolToken, address _user)
external
view
returns (uint256 supplyRatePerBlock);

Returns the supply rate per block a given user is currently experiencing on a given market.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market.
_useraddressThe user to compute the supply rate per block for.

Return values:

NameTypeDescription
supplyRatePerBlockuint256The supply rate per block the user is currently experiencing (in wad).

getCurrentUserBorrowRatePerBlock

function getCurrentUserBorrowRatePerBlock(address _poolToken, address _user)
external
view
returns (uint256 borrowRatePerBlock);

Returns the borrow rate per block a given user is currently experiencing on a given market.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market.
_useraddressThe user to compute the borrow rate per block for.

Return values:

NameTypeDescription
borrowRatePerBlockuint256The borrow rate per block the user is currently experiencing (in wad).

getAverageSupplyRatePerYear

function getAverageSupplyRatePerBlock(address _poolToken)
public
view
returns (
uint256 avgSupplyRatePerBlock,
uint256 p2pSupplyAmount,
uint256 poolSupplyAmount
);

Computes and returns the current supply rate per block experienced on average on a given market.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market.

Return values:

NameTypeDescription
avgSupplyRatePerBlockuint256The market's average supply rate per block (in wad).
p2pSupplyAmountuint256The total supplied amount matched peer-to-peer, subtracting the supply delta (in underlying).
poolSupplyAmountuint256The total supplied amount on the underlying pool, adding the supply delta (in underlying).

getAverageBorrowRatePerBlock

function getAverageBorrowRatePerBlock(address _poolToken)
public
view
returns (
uint256 avgBorrowRatePerBlock,
uint256 p2pBorrowAmount,
uint256 poolBorrowAmount
);

Computes and returns the current borrow rate per block experienced on average on a given market.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market.

Return values:

NameTypeDescription
avgBorrowRatePerBlockuint256The market's average borrow rate per block (in wad).
p2pBorrowAmountuint256The total borrowed amount matched peer-to-peer, subtracting the borrow delta (in underlying).
poolBorrowAmountuint256The total borrowed amount on the underlying pool, adding the borrow delta (in underlying).

getRatesPerYear

function getRatesPerBlock(address _poolToken)
public
view
returns (
uint256 p2pSupplyRate,
uint256 p2pBorrowRate,
uint256 poolSupplyRate,
uint256 poolBorrowRate
);

Computes and returns peer-to-peer and pool rates for a specific market.

Prefer using getAverageSupplyRatePerBlock & getAverageBorrowRatePerBlock to get the actual experienced supply/borrow rate.

Parameters:

NameTypeDescription
_poolTokenaddressThe address of the market.

Return values:

NameTypeDescription
p2pSupplyRateuint256The market's peer-to-peer supply rate per block (in wad).
p2pBorrowRateuint256The market's peer-to-peer borrow rate per block (in wad).
poolSupplyRateuint256The market's pool supply rate per block (in wad).
poolBorrowRateuint256The market's pool borrow rate per block (in wad).

User Lens Functions

getEnteredMarkets

function getEnteredMarkets(address _user)
external
view
returns (address[] memory enteredMarkets);

Returns all markets entered by a given user.

Parameters:

NameTypeDescription
_useraddressThe address of the user to check.

Return values:

NameTypeDescription
enteredMarketsaddress[]The list of all markets entered by the user.

getUserMaxCapacitiesForAsset

function getUserMaxCapacitiesForAsset(address _user, address _poolToken)
external
view
returns (uint256 withdrawable, uint256 borrowable);

Returns the maximum amount available to withdraw & borrow for a given user, on a given market.

Parameters:

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

Return values:

NameTypeDescription
withdrawableuint256The maximum amount available to withdraw.
borrowableuint256The maximum amount available to borrow.

computeLiquidationRepayAmount

function computeLiquidationRepayAmount(
address _user,
address _poolTokenBorrowed,
address _poolTokenCollateral
) external view returns (uint256);

Computes the maximum repayable amount for a potential liquidation.

Parameters:

NameTypeDescription
_useraddressThe address of the user to check.
_poolTokenBorrowedaddressThe address of the market to repay.
_poolTokenCollateraladdressThe address of the market to seize.

Return values:

NameTypeDescription
repayAmountuint256The maximum repayable amount (in underlying).

getCurrentSupplyBalanceInOf

function getCurrentSupplyBalanceInOf(address _poolToken, address _user)
external
view
returns (
uint256 balanceInP2P,
uint256 balanceOnPool,
uint256 totalBalance
);

Returns the balance in underlying of a given user in a given market.

Parameters:

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

Return values:

NameTypeDescription
balanceInP2Puint256The total balance matched peer-to-peer (in underlying).
balanceOnPooluint256The total balance on the underlying pool (in underlying).
totalBalanceuint256The total balance supplied through Morpho (in underlying).

getCurrentBorrowBalanceInOf

function getCurrentBorrowBalanceInOf(address _poolToken, address _user)
external
view
returns (
uint256 balanceInP2P,
uint256 balanceOnPool,
uint256 totalBalance
);

Returns the balance in underlying of a given user in a given market.

Parameters:

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

Return values:

NameTypeDescription
balanceInP2Puint256The total balance matched peer-to-peer (in underlying).
balanceOnPooluint256The total balance on the underlying pool (in underlying).
totalBalanceuint256The total balance borrowed through Morpho (in underlying).

getUserBalanceStates

struct LiquidityData {
uint256 collateralEth; // The collateral value (in USD, 18 decimals).
uint256 borrowableEth; // The maximum debt value allowed to borrow (in USD, 18 decimals).
uint256 maxDebtEth; // The maximum debt value allowed before being liquidatable (in USD, 18 decimals).
uint256 debtEth; // The debt value (in USD, 18 decimals).
}

function getUserBalanceStates(address _user) public view returns (LiquidityData memory);

Returns the collateral value, debt value and max debt value of a given user.

Parameters:

NameTypeDescription
_useraddressThe address of the user to check.

Return values:

NameTypeDescription
liquidityDataLiquidityDataThe liquidity data struct.

getUserHypotheticalBalanceStates


struct LiquidityData {
uint256 collateralEth; // The collateral value (in USD, 18 decimals).
uint256 borrowableEth; // The maximum debt value allowed to borrow (in USD, 18 decimals).
uint256 maxDebtEth; // The maximum debt value allowed before being liquidatable (in USD, 18 decimals).
uint256 debtEth; // The debt value (in USD, 18 decimals).
}

function getUserHypotheticalBalanceStates(
address _user,
address _poolToken,
uint256 _withdrawnAmount,
uint256 _borrowedAmount
) public view returns (LiquidityData memory liquidityData);

Returns the aggregated position of a given user, following an hypothetical borrow/withdraw on a given market, using virtually updated pool & peer-to-peer indexes for all markets.

Parameters:

NameTypeDescription
_useraddressThe address of the user to check.
_poolTokenaddressThe address of the market.
_withdrawnAmountuint256The number of tokens to hypothetically withdraw from the given market (in underlying).
_borrowedAmountuint256The amount of tokens to hypothetically borrow from the given market (in underlying).

Return values:

NameTypeDescription
liquidityDataLiquidityDataThe liquidity data struct.

getUserHealthFactor

function getUserHealthFactor(address _user) public view returns (uint256);

Returns the health factor of a given user.

Parameters:

NameTypeDescription
_useraddressThe address of the user to check.

Return values:

NameTypeDescription
healthFactoruint256The health factor of the given user.

getUserHypotheticalHealthFactor

function getUserHypotheticalHealthFactor(
address _user,
address _poolToken,
uint256 _withdrawnAmount,
uint256 _borrowedAmount
) public view returns (uint256 healthFactor);

Returns the hypothetical health factor of a user, following an hypothetical borrow/withdraw on a given market, using virtually updated pool & peer-to-peer indexes for all markets.

Parameters:

NameTypeDescription
_useraddressThe address of the user to check.
_poolTokenaddressThe address of the market.
_withdrawnAmountuint256The number of tokens to hypothetically withdraw from the given market (in underlying).
_borrowedAmountuint256The amount of tokens to hypothetically borrow from the given market (in underlying).

Return values:

NameTypeDescription
healthFactoruint256The health factor of the given user.

isLiquidatable

 function isLiquidatable(address _user) public view returns (bool);

Checks if a given user is liquidatable.

Parameters:

NameTypeDescription
_useraddressThe address of the user to check.

Return values:

NameTypeDescription
isLiquidatableboolTrue if the user is liquidatable.