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

getNextUserSupplyRatePerYear

EXTERNAL ///

Returns the supply rate per year 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-Aave, a supplier could be matched more than once instantly or later and thus benefit from a higher supply rate.

function getNextUserSupplyRatePerYear(address _poolToken, address _user, uint256 _amount)
    external
    view
    returns (uint256 nextSupplyRatePerYear, uint256 balanceInP2P, uint256 balanceOnPool, 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
nextSupplyRatePerYearuint256An approximation of the next supply rate per year experienced after having supplied (in ray).
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

Returns the borrow rate per year 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-Aave, a borrower could be matched more than once instantly or later and thus benefit from a lower borrow rate.

function getNextUserBorrowRatePerYear(address _poolToken, address _user, uint256 _amount)
    external
    view
    returns (uint256 nextBorrowRatePerYear, uint256 balanceInP2P, uint256 balanceOnPool, 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
nextBorrowRatePerYearuint256An approximation of the next borrow rate per year experienced after having supplied (in ray).
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).

getCurrentUserSupplyRatePerYear

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

function getCurrentUserSupplyRatePerYear(address _poolToken, address _user)
    external
    view
    returns (uint256 supplyRatePerYear);

Parameters

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

Returns

NameTypeDescription
supplyRatePerYearuint256The supply rate per year the user is currently experiencing (in wad).

getCurrentUserBorrowRatePerYear

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

function getCurrentUserBorrowRatePerYear(address _poolToken, address _user)
    external
    view
    returns (uint256 borrowRatePerYear);

Parameters

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

Returns

NameTypeDescription
borrowRatePerYearuint256The borrow rate per year the user is currently experiencing (in wad).

getAverageSupplyRatePerYear

PUBLIC ///

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

function getAverageSupplyRatePerYear(address _poolToken)
    public
    view
    returns (uint256 avgSupplyRatePerYear, uint256 p2pSupplyAmount, uint256 poolSupplyAmount);

Parameters

NameTypeDescription
_poolTokenaddressThe market address.

Returns

NameTypeDescription
avgSupplyRatePerYearuint256The market's average supply rate per year (in ray).
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).

getAverageBorrowRatePerYear

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

function getAverageBorrowRatePerYear(address _poolToken)
    public
    view
    returns (uint256 avgBorrowRatePerYear, uint256 p2pBorrowAmount, uint256 poolBorrowAmount);

Parameters

NameTypeDescription
_poolTokenaddressThe market address.

Returns

NameTypeDescription
avgBorrowRatePerYearuint256The market's average borrow rate per year (in ray).
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

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

Note: prefer using getAverageSupplyRatePerYear & getAverageBorrowRatePerYear to get the actual experienced supply/borrow rate.

function getRatesPerYear(address _poolToken) public view returns (uint256, uint256, uint256, uint256);

Parameters

NameTypeDescription
_poolTokenaddressThe market address.

Returns

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

_getRatesPerYear

INTERNAL ///

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

function _getRatesPerYear(
    address _poolToken,
    uint256 _suppliedOnPool,
    uint256 _borrowedFromPool,
    uint256 _withdrawnFromPool,
    uint256 _repaidOnPool
)
    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).
_withdrawnFromPooluint256The amount hypothetically withdrawn from the underlying's pool (in underlying).
_repaidOnPooluint256The amount hypothetically repaid to the underlying's pool (in underlying).

Returns

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

_getPoolRatesPerYear

Computes and returns the underlying pool rates for a specific market.

function _getPoolRatesPerYear(
    address _underlying,
    uint256 _supplied,
    uint256 _borrowed,
    uint256 _withdrawn,
    uint256 _repaid
) internal view returns (uint256 poolSupplyRate, uint256 poolBorrowRate);

Parameters

NameTypeDescription
_underlyingaddressThe underlying pool market address.
_supplieduint256The amount hypothetically supplied (in underlying).
_borroweduint256The amount hypothetically borrowed (in underlying).
_withdrawnuint256The amount hypothetically withdrawn (in underlying).
_repaiduint256The amount hypothetically repaid (in underlying).

Returns

NameTypeDescription
poolSupplyRateuint256The market's pool supply rate per year (in ray).
poolBorrowRateuint256The market's pool borrow rate per year (in ray).

_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(
    DataTypes.ReserveData memory reserve,
    uint256 _p2pBorrowIndex,
    uint256 _poolBorrowIndex,
    Types.Delta memory _delta
) internal view returns (uint256 p2pBorrowAmount, uint256 poolBorrowAmount);

Parameters

NameTypeDescription
reserveDataTypes.ReserveDataThe reserve data of the underlying pool.
_p2pBorrowIndexuint256The given market's peer-to-peer borrow index.
_poolBorrowIndexuint256The given market's pool 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).

_getUserSupplyRatePerYear

Returns the supply rate per year 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 _getUserSupplyRatePerYear(
    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 year experienced by the given position (in ray).
<none>uint256The sum of peer-to-peer & pool balances.

_getUserBorrowRatePerYear

Returns the borrow rate per year 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 _getUserBorrowRatePerYear(
    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 year experienced by the given position (in ray).
<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.