-
Notifications
You must be signed in to change notification settings - Fork 587
/
Copy pathDefaultReserveInterestRateStrategy.sol
263 lines (221 loc) · 10.1 KB
/
DefaultReserveInterestRateStrategy.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import {IERC20} from '../../dependencies/openzeppelin/contracts/IERC20.sol';
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
import {PercentageMath} from '../libraries/math/PercentageMath.sol';
import {DataTypes} from '../libraries/types/DataTypes.sol';
import {Errors} from '../libraries/helpers/Errors.sol';
import {IDefaultInterestRateStrategy} from '../../interfaces/IDefaultInterestRateStrategy.sol';
import {IReserveInterestRateStrategy} from '../../interfaces/IReserveInterestRateStrategy.sol';
import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.sol';
/**
* @title DefaultReserveInterestRateStrategy contract
* @author Aave
* @notice Implements the calculation of the interest rates depending on the reserve state
* @dev The model of interest rate is based on 2 slopes, one before the `OPTIMAL_USAGE_RATIO`
* point of usage and another from that one to 100%.
* - An instance of this same contract, can't be used across different Aave markets, due to the caching
* of the PoolAddressesProvider
*/
contract DefaultReserveInterestRateStrategy is IDefaultInterestRateStrategy {
using WadRayMath for uint256;
using PercentageMath for uint256;
/// @inheritdoc IDefaultInterestRateStrategy
uint256 public immutable OPTIMAL_USAGE_RATIO;
/// @inheritdoc IDefaultInterestRateStrategy
uint256 public immutable OPTIMAL_STABLE_TO_TOTAL_DEBT_RATIO;
/// @inheritdoc IDefaultInterestRateStrategy
uint256 public immutable MAX_EXCESS_USAGE_RATIO;
/// @inheritdoc IDefaultInterestRateStrategy
uint256 public immutable MAX_EXCESS_STABLE_TO_TOTAL_DEBT_RATIO;
IPoolAddressesProvider public immutable ADDRESSES_PROVIDER;
// Base variable borrow rate when usage rate = 0. Expressed in ray
uint256 internal immutable _baseVariableBorrowRate;
// Slope of the variable interest curve when usage ratio > 0 and <= OPTIMAL_USAGE_RATIO. Expressed in ray
uint256 internal immutable _variableRateSlope1;
// Slope of the variable interest curve when usage ratio > OPTIMAL_USAGE_RATIO. Expressed in ray
uint256 internal immutable _variableRateSlope2;
// Slope of the stable interest curve when usage ratio > 0 and <= OPTIMAL_USAGE_RATIO. Expressed in ray
uint256 internal immutable _stableRateSlope1;
// Slope of the stable interest curve when usage ratio > OPTIMAL_USAGE_RATIO. Expressed in ray
uint256 internal immutable _stableRateSlope2;
// Premium on top of `_variableRateSlope1` for base stable borrowing rate
uint256 internal immutable _baseStableRateOffset;
// Additional premium applied to stable rate when stable debt surpass `OPTIMAL_STABLE_TO_TOTAL_DEBT_RATIO`
uint256 internal immutable _stableRateExcessOffset;
/**
* @dev Constructor.
* @param provider The address of the PoolAddressesProvider contract
* @param optimalUsageRatio The optimal usage ratio
* @param baseVariableBorrowRate The base variable borrow rate
* @param variableRateSlope1 The variable rate slope below optimal usage ratio
* @param variableRateSlope2 The variable rate slope above optimal usage ratio
* @param stableRateSlope1 The stable rate slope below optimal usage ratio
* @param stableRateSlope2 The stable rate slope above optimal usage ratio
* @param baseStableRateOffset The premium on top of variable rate for base stable borrowing rate
* @param stableRateExcessOffset The premium on top of stable rate when there stable debt surpass the threshold
* @param optimalStableToTotalDebtRatio The optimal stable debt to total debt ratio of the reserve
*/
constructor(
IPoolAddressesProvider provider,
uint256 optimalUsageRatio,
uint256 baseVariableBorrowRate,
uint256 variableRateSlope1,
uint256 variableRateSlope2,
uint256 stableRateSlope1,
uint256 stableRateSlope2,
uint256 baseStableRateOffset,
uint256 stableRateExcessOffset,
uint256 optimalStableToTotalDebtRatio
) {
require(WadRayMath.RAY >= optimalUsageRatio, Errors.INVALID_OPTIMAL_USAGE_RATIO);
require(
WadRayMath.RAY >= optimalStableToTotalDebtRatio,
Errors.INVALID_OPTIMAL_STABLE_TO_TOTAL_DEBT_RATIO
);
OPTIMAL_USAGE_RATIO = optimalUsageRatio;
MAX_EXCESS_USAGE_RATIO = WadRayMath.RAY - optimalUsageRatio;
OPTIMAL_STABLE_TO_TOTAL_DEBT_RATIO = optimalStableToTotalDebtRatio;
MAX_EXCESS_STABLE_TO_TOTAL_DEBT_RATIO = WadRayMath.RAY - optimalStableToTotalDebtRatio;
ADDRESSES_PROVIDER = provider;
_baseVariableBorrowRate = baseVariableBorrowRate;
_variableRateSlope1 = variableRateSlope1;
_variableRateSlope2 = variableRateSlope2;
_stableRateSlope1 = stableRateSlope1;
_stableRateSlope2 = stableRateSlope2;
_baseStableRateOffset = baseStableRateOffset;
_stableRateExcessOffset = stableRateExcessOffset;
}
/// @inheritdoc IDefaultInterestRateStrategy
function getVariableRateSlope1() external view returns (uint256) {
return _variableRateSlope1;
}
/// @inheritdoc IDefaultInterestRateStrategy
function getVariableRateSlope2() external view returns (uint256) {
return _variableRateSlope2;
}
/// @inheritdoc IDefaultInterestRateStrategy
function getStableRateSlope1() external view returns (uint256) {
return _stableRateSlope1;
}
/// @inheritdoc IDefaultInterestRateStrategy
function getStableRateSlope2() external view returns (uint256) {
return _stableRateSlope2;
}
/// @inheritdoc IDefaultInterestRateStrategy
function getStableRateExcessOffset() external view returns (uint256) {
return _stableRateExcessOffset;
}
/// @inheritdoc IDefaultInterestRateStrategy
function getBaseStableBorrowRate() public view returns (uint256) {
return _variableRateSlope1 + _baseStableRateOffset;
}
/// @inheritdoc IDefaultInterestRateStrategy
function getBaseVariableBorrowRate() external view override returns (uint256) {
return _baseVariableBorrowRate;
}
/// @inheritdoc IDefaultInterestRateStrategy
function getMaxVariableBorrowRate() external view override returns (uint256) {
return _baseVariableBorrowRate + _variableRateSlope1 + _variableRateSlope2;
}
struct CalcInterestRatesLocalVars {
uint256 availableLiquidity;
uint256 totalDebt;
uint256 currentVariableBorrowRate;
uint256 currentStableBorrowRate;
uint256 currentLiquidityRate;
uint256 borrowUsageRatio;
uint256 supplyUsageRatio;
uint256 stableToTotalDebtRatio;
uint256 availableLiquidityPlusDebt;
}
/// @inheritdoc IReserveInterestRateStrategy
function calculateInterestRates(DataTypes.CalculateInterestRatesParams memory params)
public
view
override
returns (
uint256,
uint256,
uint256
)
{
CalcInterestRatesLocalVars memory vars;
vars.totalDebt = params.totalStableDebt + params.totalVariableDebt;
vars.currentLiquidityRate = 0;
vars.currentVariableBorrowRate = _baseVariableBorrowRate;
vars.currentStableBorrowRate = getBaseStableBorrowRate();
if (vars.totalDebt != 0) {
vars.stableToTotalDebtRatio = params.totalStableDebt.rayDiv(vars.totalDebt);
vars.availableLiquidity =
IERC20(params.reserve).balanceOf(params.aToken) +
params.liquidityAdded -
params.liquidityTaken;
vars.availableLiquidityPlusDebt = vars.availableLiquidity + vars.totalDebt;
vars.borrowUsageRatio = vars.totalDebt.rayDiv(vars.availableLiquidityPlusDebt);
vars.supplyUsageRatio = vars.totalDebt.rayDiv(
vars.availableLiquidityPlusDebt + params.unbacked
);
}
if (vars.borrowUsageRatio > OPTIMAL_USAGE_RATIO) {
uint256 excessBorrowUsageRatio = (vars.borrowUsageRatio - OPTIMAL_USAGE_RATIO).rayDiv(
MAX_EXCESS_USAGE_RATIO
);
vars.currentStableBorrowRate +=
_stableRateSlope1 +
_stableRateSlope2.rayMul(excessBorrowUsageRatio);
vars.currentVariableBorrowRate +=
_variableRateSlope1 +
_variableRateSlope2.rayMul(excessBorrowUsageRatio);
} else {
vars.currentStableBorrowRate += _stableRateSlope1.rayMul(vars.borrowUsageRatio).rayDiv(
OPTIMAL_USAGE_RATIO
);
vars.currentVariableBorrowRate += _variableRateSlope1.rayMul(vars.borrowUsageRatio).rayDiv(
OPTIMAL_USAGE_RATIO
);
}
if (vars.stableToTotalDebtRatio > OPTIMAL_STABLE_TO_TOTAL_DEBT_RATIO) {
uint256 excessStableDebtRatio = (vars.stableToTotalDebtRatio -
OPTIMAL_STABLE_TO_TOTAL_DEBT_RATIO).rayDiv(MAX_EXCESS_STABLE_TO_TOTAL_DEBT_RATIO);
vars.currentStableBorrowRate += _stableRateExcessOffset.rayMul(excessStableDebtRatio);
}
vars.currentLiquidityRate = _getOverallBorrowRate(
params.totalStableDebt,
params.totalVariableDebt,
vars.currentVariableBorrowRate,
params.averageStableBorrowRate
).rayMul(vars.supplyUsageRatio).percentMul(
PercentageMath.PERCENTAGE_FACTOR - params.reserveFactor
);
return (
vars.currentLiquidityRate,
vars.currentStableBorrowRate,
vars.currentVariableBorrowRate
);
}
/**
* @dev Calculates the overall borrow rate as the weighted average between the total variable debt and total stable
* debt
* @param totalStableDebt The total borrowed from the reserve at a stable rate
* @param totalVariableDebt The total borrowed from the reserve at a variable rate
* @param currentVariableBorrowRate The current variable borrow rate of the reserve
* @param currentAverageStableBorrowRate The current weighted average of all the stable rate loans
* @return The weighted averaged borrow rate
*/
function _getOverallBorrowRate(
uint256 totalStableDebt,
uint256 totalVariableDebt,
uint256 currentVariableBorrowRate,
uint256 currentAverageStableBorrowRate
) internal pure returns (uint256) {
uint256 totalDebt = totalStableDebt + totalVariableDebt;
if (totalDebt == 0) return 0;
uint256 weightedVariableRate = totalVariableDebt.wadToRay().rayMul(currentVariableBorrowRate);
uint256 weightedStableRate = totalStableDebt.wadToRay().rayMul(currentAverageStableBorrowRate);
uint256 overallBorrowRate = (weightedVariableRate + weightedStableRate).rayDiv(
totalDebt.wadToRay()
);
return overallBorrowRate;
}
}