-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReserveLogic.sol
314 lines (282 loc) · 12.3 KB
/
ReserveLogic.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;
import {IERC20} from '../../../dependencies/openzeppelin/contracts/IERC20.sol';
import {GPv2SafeERC20} from '../../../dependencies/gnosis/contracts/GPv2SafeERC20.sol';
import {IVariableDebtToken} from '../../../interfaces/IVariableDebtToken.sol';
import {IReserveInterestRateStrategy} from '../../../interfaces/IReserveInterestRateStrategy.sol';
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {MathUtils} from '../math/MathUtils.sol';
import {WadRayMath} from '../math/WadRayMath.sol';
import {PercentageMath} from '../math/PercentageMath.sol';
import {Errors} from '../helpers/Errors.sol';
import {DataTypes} from '../types/DataTypes.sol';
import {SafeCast} from '../../../dependencies/openzeppelin/contracts/SafeCast.sol';
/**
* @title ReserveLogic library
* @author Aave
* @notice Implements the logic to update the reserves state
*/
library ReserveLogic {
using WadRayMath for uint256;
using PercentageMath for uint256;
using SafeCast for uint256;
using GPv2SafeERC20 for IERC20;
using ReserveLogic for DataTypes.ReserveData;
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
// See `IPool` for descriptions
event ReserveDataUpdated(
address indexed reserve,
uint256 liquidityRate,
uint256 stableBorrowRate,
uint256 variableBorrowRate,
uint256 liquidityIndex,
uint256 variableBorrowIndex
);
/**
* @notice Returns the ongoing normalized income for the reserve.
* @dev A value of 1e27 means there is no income. As time passes, the income is accrued
* @dev A value of 2*1e27 means for each unit of asset one unit of income has been accrued
* @param reserve The reserve object
* @return The normalized income, expressed in ray
*/
function getNormalizedIncome(
DataTypes.ReserveData storage reserve
) internal view returns (uint256) {
uint40 timestamp = reserve.lastUpdateTimestamp;
//solium-disable-next-line
if (timestamp == block.timestamp) {
//if the index was updated in the same block, no need to perform any calculation
return reserve.liquidityIndex;
} else {
return
MathUtils.calculateLinearInterest(reserve.currentLiquidityRate, timestamp).rayMul(
reserve.liquidityIndex
);
}
}
/**
* @notice Returns the ongoing normalized variable debt for the reserve.
* @dev A value of 1e27 means there is no debt. As time passes, the debt is accrued
* @dev A value of 2*1e27 means that for each unit of debt, one unit worth of interest has been accumulated
* @param reserve The reserve object
* @return The normalized variable debt, expressed in ray
*/
function getNormalizedDebt(
DataTypes.ReserveData storage reserve
) internal view returns (uint256) {
uint40 timestamp = reserve.lastUpdateTimestamp;
//solium-disable-next-line
if (timestamp == block.timestamp) {
//if the index was updated in the same block, no need to perform any calculation
return reserve.variableBorrowIndex;
} else {
return
MathUtils.calculateCompoundedInterest(reserve.currentVariableBorrowRate, timestamp).rayMul(
reserve.variableBorrowIndex
);
}
}
/**
* @notice Updates the liquidity cumulative index and the variable borrow index.
* @param reserve The reserve object
* @param reserveCache The caching layer for the reserve data
*/
function updateState(
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache
) internal {
// If time didn't pass since last stored timestamp, skip state update
//solium-disable-next-line
if (reserveCache.reserveLastUpdateTimestamp == uint40(block.timestamp)) {
return;
}
_updateIndexes(reserve, reserveCache);
_accrueToTreasury(reserve, reserveCache);
//solium-disable-next-line
reserve.lastUpdateTimestamp = uint40(block.timestamp);
reserveCache.reserveLastUpdateTimestamp = uint40(block.timestamp);
}
/**
* @notice Accumulates a predefined amount of asset to the reserve as a fixed, instantaneous income. Used for example
* to accumulate the flashloan fee to the reserve, and spread it between all the suppliers.
* @param reserve The reserve object
* @param totalLiquidity The total liquidity available in the reserve
* @param amount The amount to accumulate
* @return The next liquidity index of the reserve
*/
function cumulateToLiquidityIndex(
DataTypes.ReserveData storage reserve,
uint256 totalLiquidity,
uint256 amount
) internal returns (uint256) {
//next liquidity index is calculated this way: `((amount / totalLiquidity) + 1) * liquidityIndex`
//division `amount / totalLiquidity` done in ray for precision
uint256 result = (amount.wadToRay().rayDiv(totalLiquidity.wadToRay()) + WadRayMath.RAY).rayMul(
reserve.liquidityIndex
);
reserve.liquidityIndex = result.toUint128();
return result;
}
/**
* @notice Initializes a reserve.
* @param reserve The reserve object
* @param aTokenAddress The address of the overlying atoken contract
* @param variableDebtTokenAddress The address of the overlying variable debt token contract
* @param interestRateStrategyAddress The address of the interest rate strategy contract
*/
function init(
DataTypes.ReserveData storage reserve,
address aTokenAddress,
address variableDebtTokenAddress,
address interestRateStrategyAddress
) internal {
require(reserve.aTokenAddress == address(0), Errors.RESERVE_ALREADY_INITIALIZED);
reserve.liquidityIndex = uint128(WadRayMath.RAY);
reserve.variableBorrowIndex = uint128(WadRayMath.RAY);
reserve.aTokenAddress = aTokenAddress;
reserve.variableDebtTokenAddress = variableDebtTokenAddress;
reserve.interestRateStrategyAddress = interestRateStrategyAddress;
}
/**
* @notice Updates the reserve current variable borrow rate and the current liquidity rate.
* @param reserve The reserve reserve to be updated
* @param reserveCache The caching layer for the reserve data
* @param reserveAddress The address of the reserve to be updated
* @param liquidityAdded The amount of liquidity added to the protocol (supply or repay) in the previous action
* @param liquidityTaken The amount of liquidity taken from the protocol (redeem or borrow)
*/
function updateInterestRatesAndVirtualBalance(
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache,
address reserveAddress,
uint256 liquidityAdded,
uint256 liquidityTaken
) internal {
uint256 totalVariableDebt = reserveCache.nextScaledVariableDebt.rayMul(
reserveCache.nextVariableBorrowIndex
);
(uint256 nextLiquidityRate, uint256 nextVariableRate) = IReserveInterestRateStrategy(
reserve.interestRateStrategyAddress
).calculateInterestRates(
DataTypes.CalculateInterestRatesParams({
unbacked: reserve.unbacked + reserve.deficit,
liquidityAdded: liquidityAdded,
liquidityTaken: liquidityTaken,
totalDebt: totalVariableDebt,
reserveFactor: reserveCache.reserveFactor,
reserve: reserveAddress,
usingVirtualBalance: reserveCache.reserveConfiguration.getIsVirtualAccActive(),
virtualUnderlyingBalance: reserve.virtualUnderlyingBalance
})
);
reserve.currentLiquidityRate = nextLiquidityRate.toUint128();
reserve.currentVariableBorrowRate = nextVariableRate.toUint128();
// Only affect virtual balance if the reserve uses it
if (reserveCache.reserveConfiguration.getIsVirtualAccActive()) {
if (liquidityAdded > 0) {
reserve.virtualUnderlyingBalance += liquidityAdded.toUint128();
}
if (liquidityTaken > 0) {
reserve.virtualUnderlyingBalance -= liquidityTaken.toUint128();
}
}
emit ReserveDataUpdated(
reserveAddress,
nextLiquidityRate,
0,
nextVariableRate,
reserveCache.nextLiquidityIndex,
reserveCache.nextVariableBorrowIndex
);
}
/**
* @notice Mints part of the repaid interest to the reserve treasury as a function of the reserve factor for the
* specific asset.
* @param reserve The reserve to be updated
* @param reserveCache The caching layer for the reserve data
*/
function _accrueToTreasury(
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache
) internal {
if (reserveCache.reserveFactor == 0) {
return;
}
//calculate the total variable debt at moment of the last interaction
uint256 prevTotalVariableDebt = reserveCache.currScaledVariableDebt.rayMul(
reserveCache.currVariableBorrowIndex
);
//calculate the new total variable debt after accumulation of the interest on the index
uint256 currTotalVariableDebt = reserveCache.currScaledVariableDebt.rayMul(
reserveCache.nextVariableBorrowIndex
);
//debt accrued is the sum of the current debt minus the sum of the debt at the last update
uint256 totalDebtAccrued = currTotalVariableDebt - prevTotalVariableDebt;
uint256 amountToMint = totalDebtAccrued.percentMul(reserveCache.reserveFactor);
if (amountToMint != 0) {
reserve.accruedToTreasury += amountToMint.rayDiv(reserveCache.nextLiquidityIndex).toUint128();
}
}
/**
* @notice Updates the reserve indexes and the timestamp of the update.
* @param reserve The reserve reserve to be updated
* @param reserveCache The cache layer holding the cached protocol data
*/
function _updateIndexes(
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache
) internal {
// Only cumulating on the supply side if there is any income being produced
// The case of Reserve Factor 100% is not a problem (currentLiquidityRate == 0),
// as liquidity index should not be updated
if (reserveCache.currLiquidityRate != 0) {
uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest(
reserveCache.currLiquidityRate,
reserveCache.reserveLastUpdateTimestamp
);
reserveCache.nextLiquidityIndex = cumulatedLiquidityInterest.rayMul(
reserveCache.currLiquidityIndex
);
reserve.liquidityIndex = reserveCache.nextLiquidityIndex.toUint128();
}
// Variable borrow index only gets updated if there is any variable debt.
// reserveCache.currVariableBorrowRate != 0 is not a correct validation,
// because a positive base variable rate can be stored on
// reserveCache.currVariableBorrowRate, but the index should not increase
if (reserveCache.currScaledVariableDebt != 0) {
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserveCache.currVariableBorrowRate,
reserveCache.reserveLastUpdateTimestamp
);
reserveCache.nextVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(
reserveCache.currVariableBorrowIndex
);
reserve.variableBorrowIndex = reserveCache.nextVariableBorrowIndex.toUint128();
}
}
/**
* @notice Creates a cache object to avoid repeated storage reads and external contract calls when updating state and
* interest rates.
* @param reserve The reserve object for which the cache will be filled
* @return The cache object
*/
function cache(
DataTypes.ReserveData storage reserve
) internal view returns (DataTypes.ReserveCache memory) {
DataTypes.ReserveCache memory reserveCache;
reserveCache.reserveConfiguration = reserve.configuration;
reserveCache.reserveFactor = reserveCache.reserveConfiguration.getReserveFactor();
reserveCache.currLiquidityIndex = reserveCache.nextLiquidityIndex = reserve.liquidityIndex;
reserveCache.currVariableBorrowIndex = reserveCache.nextVariableBorrowIndex = reserve
.variableBorrowIndex;
reserveCache.currLiquidityRate = reserve.currentLiquidityRate;
reserveCache.currVariableBorrowRate = reserve.currentVariableBorrowRate;
reserveCache.aTokenAddress = reserve.aTokenAddress;
reserveCache.variableDebtTokenAddress = reserve.variableDebtTokenAddress;
reserveCache.reserveLastUpdateTimestamp = reserve.lastUpdateTimestamp;
reserveCache.currScaledVariableDebt = reserveCache.nextScaledVariableDebt = IVariableDebtToken(
reserveCache.variableDebtTokenAddress
).scaledTotalSupply();
return reserveCache;
}
}