Fixed DefaultReserveInterestRateStrategy

This commit is contained in:
The3D 2020-08-21 11:21:50 +02:00
parent d2848105f7
commit b93937f4fd

View File

@ -35,34 +35,34 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy {
LendingPoolAddressesProvider public immutable addressesProvider; LendingPoolAddressesProvider public immutable addressesProvider;
//base variable borrow rate when Utilization rate = 0. Expressed in ray //base variable borrow rate when Utilization rate = 0. Expressed in ray
uint256 internal immutable baseVariableBorrowRate; uint256 internal immutable _baseVariableBorrowRate;
//slope of the variable interest curve when utilization rate > 0 and <= OPTIMAL_UTILIZATION_RATE. Expressed in ray //slope of the variable interest curve when utilization rate > 0 and <= OPTIMAL_UTILIZATION_RATE. Expressed in ray
uint256 internal immutable variableRateSlope1; uint256 internal immutable _variableRateSlope1;
//slope of the variable interest curve when utilization rate > OPTIMAL_UTILIZATION_RATE. Expressed in ray //slope of the variable interest curve when utilization rate > OPTIMAL_UTILIZATION_RATE. Expressed in ray
uint256 internal immutable variableRateSlope2; uint256 internal immutable _variableRateSlope2;
//slope of the stable interest curve when utilization rate > 0 and <= OPTIMAL_UTILIZATION_RATE. Expressed in ray //slope of the stable interest curve when utilization rate > 0 and <= OPTIMAL_UTILIZATION_RATE. Expressed in ray
uint256 internal immutable stableRateSlope1; uint256 internal immutable _stableRateSlope1;
//slope of the stable interest curve when utilization rate > OPTIMAL_UTILIZATION_RATE. Expressed in ray //slope of the stable interest curve when utilization rate > OPTIMAL_UTILIZATION_RATE. Expressed in ray
uint256 internal immutable stableRateSlope2; uint256 internal immutable _stableRateSlope2;
constructor( constructor(
LendingPoolAddressesProvider _provider, LendingPoolAddressesProvider provider,
uint256 _baseVariableBorrowRate, uint256 baseVariableBorrowRate,
uint256 _variableRateSlope1, uint256 variableRateSlope1,
uint256 _variableRateSlope2, uint256 variableRateSlope2,
uint256 _stableRateSlope1, uint256 stableRateSlope1,
uint256 _stableRateSlope2 uint256 stableRateSlope2
) public { ) public {
addressesProvider = _provider; addressesProvider = provider;
baseVariableBorrowRate = _baseVariableBorrowRate; _baseVariableBorrowRate = baseVariableBorrowRate;
variableRateSlope1 = _variableRateSlope1; _variableRateSlope1 = variableRateSlope1;
variableRateSlope2 = _variableRateSlope2; _variableRateSlope2 = variableRateSlope2;
stableRateSlope1 = _stableRateSlope1; _stableRateSlope1 = stableRateSlope1;
stableRateSlope2 = _stableRateSlope2; _stableRateSlope2 = stableRateSlope2;
} }
/** /**
@ -70,115 +70,122 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy {
*/ */
function getVariableRateSlope1() external view returns (uint256) { function getVariableRateSlope1() external view returns (uint256) {
return variableRateSlope1; return _variableRateSlope1;
} }
function getVariableRateSlope2() external view returns (uint256) { function getVariableRateSlope2() external view returns (uint256) {
return variableRateSlope2; return _variableRateSlope2;
} }
function getStableRateSlope1() external view returns (uint256) { function getStableRateSlope1() external view returns (uint256) {
return stableRateSlope1; return _stableRateSlope1;
} }
function getStableRateSlope2() external view returns (uint256) { function getStableRateSlope2() external view returns (uint256) {
return stableRateSlope2; return _stableRateSlope2;
} }
function getBaseVariableBorrowRate() external override view returns (uint256) { function getBaseVariableBorrowRate() external override view returns (uint256) {
return baseVariableBorrowRate; return _baseVariableBorrowRate;
} }
/** /**
* @dev calculates the interest rates depending on the available liquidity and the total borrowed. * @dev calculates the interest rates depending on the available liquidity and the total borrowed.
* @param _reserve the address of the reserve * @param reserve the address of the reserve
* @param _availableLiquidity the liquidity available in the reserve * @param availableLiquidity the liquidity available in the reserve
* @param _totalBorrowsStable the total borrowed from the reserve a stable rate * @param totalBorrowsStable the total borrowed from the reserve a stable rate
* @param _totalBorrowsVariable the total borrowed from the reserve at a variable rate * @param totalBorrowsVariable the total borrowed from the reserve at a variable rate
* @param _averageStableBorrowRate the weighted average of all the stable rate borrows * @param averageStableBorrowRate the weighted average of all the stable rate borrows
* @return currentLiquidityRate the liquidity rate * @return currentLiquidityRate the liquidity rate
* @return currentStableBorrowRate stable borrow rate * @return currentStableBorrowRate stable borrow rate
* @return currentVariableBorrowRate variable borrow rate * @return currentVariableBorrowRate variable borrow rate
**/ **/
function calculateInterestRates( function calculateInterestRates(
address _reserve, address reserve,
uint256 _availableLiquidity, uint256 availableLiquidity,
uint256 _totalBorrowsStable, uint256 totalBorrowsStable,
uint256 _totalBorrowsVariable, uint256 totalBorrowsVariable,
uint256 _averageStableBorrowRate uint256 averageStableBorrowRate
) )
external external
override override
view view
returns ( returns (
uint256 currentLiquidityRate, uint256,
uint256 currentStableBorrowRate, uint256,
uint256 currentVariableBorrowRate uint256
) )
{ {
uint256 totalBorrows = _totalBorrowsStable.add(_totalBorrowsVariable); uint256 totalBorrows = totalBorrowsStable.add(totalBorrowsVariable);
uint256 currentVariableBorrowRate = 0;
uint256 currentStableBorrowRate = 0;
uint256 currentLiquidityRate = 0;
uint256 utilizationRate = (totalBorrows == 0 && _availableLiquidity == 0) uint256 utilizationRate = (totalBorrows == 0 && availableLiquidity == 0)
? 0 ? 0
: totalBorrows.rayDiv(_availableLiquidity.add(totalBorrows)); : totalBorrows.rayDiv(availableLiquidity.add(totalBorrows));
currentStableBorrowRate = ILendingRateOracle(addressesProvider.getLendingRateOracle()) currentStableBorrowRate = ILendingRateOracle(addressesProvider.getLendingRateOracle())
.getMarketBorrowRate(_reserve); .getMarketBorrowRate(reserve);
if (utilizationRate > OPTIMAL_UTILIZATION_RATE) { if (utilizationRate > OPTIMAL_UTILIZATION_RATE) {
uint256 excessUtilizationRateRatio = utilizationRate.sub(OPTIMAL_UTILIZATION_RATE).rayDiv( uint256 excessUtilizationRateRatio = utilizationRate.sub(OPTIMAL_UTILIZATION_RATE).rayDiv(
EXCESS_UTILIZATION_RATE EXCESS_UTILIZATION_RATE
); );
currentStableBorrowRate = currentStableBorrowRate.add(stableRateSlope1).add( currentStableBorrowRate = currentStableBorrowRate.add(_stableRateSlope1).add(
stableRateSlope2.rayMul(excessUtilizationRateRatio) _stableRateSlope2.rayMul(excessUtilizationRateRatio)
); );
currentVariableBorrowRate = baseVariableBorrowRate.add(variableRateSlope1).add( currentVariableBorrowRate = _baseVariableBorrowRate.add(_variableRateSlope1).add(
variableRateSlope2.rayMul(excessUtilizationRateRatio) _variableRateSlope2.rayMul(excessUtilizationRateRatio)
); );
} else { } else {
currentStableBorrowRate = currentStableBorrowRate.add( currentStableBorrowRate = currentStableBorrowRate.add(
stableRateSlope1.rayMul(utilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE)) _stableRateSlope1.rayMul(utilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE))
); );
currentVariableBorrowRate = baseVariableBorrowRate.add( currentVariableBorrowRate = _baseVariableBorrowRate.add(
utilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE).rayMul(variableRateSlope1) utilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE).rayMul(_variableRateSlope1)
); );
} }
currentLiquidityRate = getOverallBorrowRateInternal( currentLiquidityRate = getOverallBorrowRateInternal(
_totalBorrowsStable, totalBorrowsStable,
_totalBorrowsVariable, totalBorrowsVariable,
currentVariableBorrowRate, currentVariableBorrowRate,
_averageStableBorrowRate averageStableBorrowRate
) )
.rayMul(utilizationRate); .rayMul(utilizationRate);
return (currentLiquidityRate,
currentStableBorrowRate,
currentVariableBorrowRate);
} }
/** /**
* @dev calculates the overall borrow rate as the weighted average between the total variable borrows and total stable borrows. * @dev calculates the overall borrow rate as the weighted average between the total variable borrows and total stable borrows.
* @param _totalBorrowsStable the total borrowed from the reserve a stable rate * @param totalBorrowsStable the total borrowed from the reserve a stable rate
* @param _totalBorrowsVariable the total borrowed from the reserve at a variable rate * @param totalBorrowsVariable the total borrowed from the reserve at a variable rate
* @param _currentVariableBorrowRate the current variable borrow rate * @param currentVariableBorrowRate the current variable borrow rate
* @param _currentAverageStableBorrowRate the weighted average of all the stable rate borrows * @param currentAverageStableBorrowRate the weighted average of all the stable rate borrows
* @return the weighted averaged borrow rate * @return the weighted averaged borrow rate
**/ **/
function getOverallBorrowRateInternal( function getOverallBorrowRateInternal(
uint256 _totalBorrowsStable, uint256 totalBorrowsStable,
uint256 _totalBorrowsVariable, uint256 totalBorrowsVariable,
uint256 _currentVariableBorrowRate, uint256 currentVariableBorrowRate,
uint256 _currentAverageStableBorrowRate uint256 currentAverageStableBorrowRate
) internal pure returns (uint256) { ) internal pure returns (uint256) {
uint256 totalBorrows = _totalBorrowsStable.add(_totalBorrowsVariable); uint256 totalBorrows = totalBorrowsStable.add(totalBorrowsVariable);
if (totalBorrows == 0) return 0; if (totalBorrows == 0) return 0;
uint256 weightedVariableRate = _totalBorrowsVariable.wadToRay().rayMul( uint256 weightedVariableRate = totalBorrowsVariable.wadToRay().rayMul(
_currentVariableBorrowRate currentVariableBorrowRate
); );
uint256 weightedStableRate = _totalBorrowsStable.wadToRay().rayMul( uint256 weightedStableRate = totalBorrowsStable.wadToRay().rayMul(
_currentAverageStableBorrowRate currentAverageStableBorrowRate
); );
uint256 overallBorrowRate = weightedVariableRate.add(weightedStableRate).rayDiv( uint256 overallBorrowRate = weightedVariableRate.add(weightedStableRate).rayDiv(