Interest
This library is used for calculating and accruing interest.
many calculations are unchecked because we asset values are stored as uint128. We also limit the max amount amount of interest to ensure that it can not overflow when added to the current assets.
State Variables
BASE
uint128 internal constant BASE = 0;
OPTIMAL_UTILIZATION
uint128 internal constant OPTIMAL_UTILIZATION = 0.8e18;
DANGER_UTILIZATION
uint128 internal constant DANGER_UTILIZATION = 0.925e18;
SLOPE1
uint128 internal constant SLOPE1 = 0.0375e18;
SLOPE2
uint128 internal constant SLOPE2 = 1e18;
SLOPE3
uint128 internal constant SLOPE3 = 20e18;
SECONDS_IN_YEAR
uint128 internal constant SECONDS_IN_YEAR = 365 days;
LENDING_FEE_RATE
uint128 internal constant LENDING_FEE_RATE = 10;
MAX_UINT112
uint256 private constant MAX_UINT112 = type(uint112).max;
LAST_DEPOSIT
uint256 private constant LAST_DEPOSIT = FIRST_DEBT_TOKEN - 1;
Functions
accrueInterestAndUpdateReservesWithAssets
function accrueInterestAndUpdateReservesWithAssets(
uint128[6] storage assets,
AccrueInterestParams memory accrueInterestParams
) internal returns (uint256 interestXForLP, uint256 interestYForLP, uint256[3] memory protocolFeeAssets);
getReservesAtLendingTick
we approximate the reserves based on an average tick value since the last lending state update.
this will never return values greater than uint112 max when used correctly. The reserve values are underestimated due to a tick being an approximate price. We use a smaller value when multiplying and a larger when dividing to ensure that we do not overflow.
function getReservesAtLendingTick(
uint256 activeLiquidityAssets,
int16 lendingStateTick
) internal pure returns (uint256 reserveXAssets, uint256 reserveYAssets);
Parameters
Name | Type | Description |
---|---|---|
activeLiquidityAssets | uint256 | active L where |
lendingStateTick | int16 | Average tick value since last lending state update. |
Returns
Name | Type | Description |
---|---|---|
reserveXAssets | uint256 | approximate average reserve since last lending state update. |
reserveYAssets | uint256 | approximate average reserve since last lending state update. |
getUtilizationsInWads
function getUtilizationsInWads(
uint128[6] memory startingAssets,
uint256 reservesXAssets,
uint256 reservesYAssets
) internal pure returns (uint256[3] memory utilizationInWads);
accrueInterestWithAssets
function accrueInterestWithAssets(
uint128[6] memory assets,
AccrueInterestParams memory params
)
internal
pure
returns (
uint128[6] memory newAssets,
uint256 interestXPortionForLP,
uint256 interestYPortionForLP,
uint256[3] memory protocolFeeAssets
);
getUtilizationInWads
function getUtilizationInWads(
uint256 totalBorrowedAssets,
uint256 totalDepositedAssets
) internal pure returns (uint256 utilization);
computeInterestAssets
function computeInterestAssets(
uint256 duration,
uint256 utilization,
uint256 borrowedAssets,
uint256 depositedAssets
) internal pure returns (uint256);
addInterestToAssets
function addInterestToAssets(uint256 prevAssets, uint256 interest) internal pure returns (uint128);
getInterestRatePerSecond
function getInterestRatePerSecond(
uint256 utilizationInWads
) internal pure returns (uint256 interestRate);
Structs
AccrueInterestParams
struct AccrueInterestParams {
uint256 duration;
int16 lendingStateTick;
uint256 adjustedActiveLiquidity;
uint112[6] shares;
}