RatesLens

Git Source

Inherits: UsersLens

Author: Morpho Labs.

Intermediary layer exposing endpoints to query live data related to the Morpho Protocol users and their positions.

Functions

getNextUserSupplyRatePerBlock

EXTERNAL ///

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

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

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

Parameters

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

Returns

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

getNextUserBorrowRatePerBlock

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

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

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

Parameters

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

Returns

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

getCurrentUserSupplyRatePerBlock

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

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

Parameters

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

Returns

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

getCurrentUserBorrowRatePerBlock

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

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

Parameters

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

Returns

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

getAverageSupplyRatePerBlock

PUBLIC ///

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

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

Parameters

NameTypeDescription
_poolTokenaddressThe market address.

Returns

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

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

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

Parameters

NameTypeDescription
_poolTokenaddressThe market address.

Returns

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

getRatesPerBlock

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

Note: prefer using getAverageSupplyRatePerBlock & getAverageBorrowRatePerBlock to get the actual experienced supply/borrow rate.

function getRatesPerBlock(address _poolToken) external view returns (uint256, uint256, uint256, uint256);

Parameters

NameTypeDescription
_poolTokenaddressThe market address.

Returns

NameTypeDescription
<none>uint256p2pSupplyRate The market's peer-to-peer supply rate per block (in wad).
<none>uint256p2pBorrowRate The market's peer-to-peer borrow rate per block (in wad).
<none>uint256poolSupplyRate The market's pool supply rate per block (in wad).
<none>uint256poolBorrowRate The market's pool borrow rate per block (in wad).

_getRatesPerBlock

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

function _getRatesPerBlock(
    address _poolToken,
    uint256 _suppliedOnPool,
    uint256 _borrowedFromPool,
    uint256 _repaidOnPool,
    uint256 _withdrawnFromPool
)
    internal
    view
    returns (uint256 p2pSupplyRate, uint256 p2pBorrowRate, uint256 poolSupplyRate, uint256 poolBorrowRate);

Parameters

NameTypeDescription
_poolTokenaddressThe market address.
_suppliedOnPooluint256The amount hypothetically supplied to the underlying's pool (in underlying).
_borrowedFromPooluint256The amount hypothetically borrowed from the underlying's pool (in underlying).
_repaidOnPooluint256The amount hypothetically repaid to the underlying's pool (in underlying).
_withdrawnFromPooluint256The amount hypothetically withdrawn from the underlying's pool (in underlying).

Returns

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

_getMarketSupply

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

function _getMarketSupply(
    address _poolToken,
    uint256 _p2pSupplyIndex,
    uint256 _poolSupplyIndex,
    Types.Delta memory _delta
) internal view returns (uint256 p2pSupplyAmount, uint256 poolSupplyAmount);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market to check.
_p2pSupplyIndexuint256The given market's peer-to-peer supply index.
_poolSupplyIndexuint256The given market's pool supply index.
_deltaTypes.DeltaThe given market's deltas.

Returns

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

_getMarketBorrow

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

function _getMarketBorrow(
    address _poolToken,
    uint256 _p2pBorrowIndex,
    uint256 _poolBorrowIndex,
    Types.Delta memory _delta
) internal view returns (uint256 p2pBorrowAmount, uint256 poolBorrowAmount);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market to check.
_p2pBorrowIndexuint256The given market's peer-to-peer borrow index.
_poolBorrowIndexuint256The given market's borrow index.
_deltaTypes.DeltaThe given market's deltas.

Returns

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

_getUserSupplyRatePerBlock

Returns the supply rate per block experienced on a market based on a given position distribution. The calculation takes into account the change in pool rates implied by an hypothetical supply and/or repay.

function _getUserSupplyRatePerBlock(
    address _poolToken,
    uint256 _balanceInP2P,
    uint256 _balanceOnPool,
    uint256 _suppliedOnPool,
    uint256 _repaidToPool
) internal view returns (uint256, uint256);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market.
_balanceInP2Puint256The amount of balance matched peer-to-peer (in a unit common to _balanceOnPool).
_balanceOnPooluint256The amount of balance supplied on pool (in a unit common to _balanceInP2P).
_suppliedOnPooluint256The amount hypothetically supplied on pool (in underlying).
_repaidToPooluint256The amount hypothetically repaid to the pool (in underlying).

Returns

NameTypeDescription
<none>uint256The supply rate per block experienced by the given position (in wad).
<none>uint256The sum of peer-to-peer & pool balances.

_getUserBorrowRatePerBlock

Returns the borrow rate per block experienced on a market based on a given position distribution. The calculation takes into account the change in pool rates implied by an hypothetical borrow and/or withdraw.

function _getUserBorrowRatePerBlock(
    address _poolToken,
    uint256 _balanceInP2P,
    uint256 _balanceOnPool,
    uint256 _borrowedFromPool,
    uint256 _withdrawnFromPool
) internal view returns (uint256, uint256);

Parameters

NameTypeDescription
_poolTokenaddressThe address of the market.
_balanceInP2Puint256The amount of balance matched peer-to-peer (in a unit common to _balanceOnPool).
_balanceOnPooluint256The amount of balance supplied on pool (in a unit common to _balanceInP2P).
_borrowedFromPooluint256The amount hypothetically borrowed from the pool (in underlying).
_withdrawnFromPooluint256The amount hypothetically withdrawn from the pool (in underlying).

Returns

NameTypeDescription
<none>uint256The borrow rate per block experienced by the given position (in wad).
<none>uint256The sum of peer-to-peer & pool balances.

_getWeightedRate

Returns the rate experienced based on a given pool & peer-to-peer distribution.

function _getWeightedRate(uint256 _p2pRate, uint256 _poolRate, uint256 _balanceInP2P, uint256 _balanceOnPool)
    internal
    pure
    returns (uint256 weightedRate, uint256 totalBalance);

Parameters

NameTypeDescription
_p2pRateuint256The peer-to-peer rate (in a unit common to _poolRate & weightedRate).
_poolRateuint256The pool rate (in a unit common to _p2pRate & weightedRate).
_balanceInP2Puint256The amount of balance matched peer-to-peer (in a unit common to _balanceOnPool).
_balanceOnPooluint256The amount of balance supplied on pool (in a unit common to _balanceInP2P).

Returns

NameTypeDescription
weightedRateuint256The rate experienced by the given distribution (in a unit common to _p2pRate & _poolRate).
totalBalanceuint256The sum of peer-to-peer & pool balances.

Errors

BorrowRateFailed

ERRORS ///

error BorrowRateFailed();

Structs

PoolRatesVars

INTERNAL ///

struct PoolRatesVars {
    Types.Delta delta;
    Types.LastPoolIndexes lastPoolIndexes;
    Types.Indexes indexes;
    Types.MarketParameters params;
}