-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBorrowLogic.sol
235 lines (208 loc) · 8.42 KB
/
BorrowLogic.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;
import {GPv2SafeERC20} from '../../../dependencies/gnosis/contracts/GPv2SafeERC20.sol';
import {SafeCast} from '../../../dependencies/openzeppelin/contracts/SafeCast.sol';
import {IERC20} from '../../../dependencies/openzeppelin/contracts/IERC20.sol';
import {IVariableDebtToken} from '../../../interfaces/IVariableDebtToken.sol';
import {IAToken} from '../../../interfaces/IAToken.sol';
import {UserConfiguration} from '../configuration/UserConfiguration.sol';
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {DataTypes} from '../types/DataTypes.sol';
import {ValidationLogic} from './ValidationLogic.sol';
import {ReserveLogic} from './ReserveLogic.sol';
import {IsolationModeLogic} from './IsolationModeLogic.sol';
/**
* @title BorrowLogic library
* @author Aave
* @notice Implements the base logic for all the actions related to borrowing
*/
library BorrowLogic {
using ReserveLogic for DataTypes.ReserveCache;
using ReserveLogic for DataTypes.ReserveData;
using GPv2SafeERC20 for IERC20;
using UserConfiguration for DataTypes.UserConfigurationMap;
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using SafeCast for uint256;
// See `IPool` for descriptions
event Borrow(
address indexed reserve,
address user,
address indexed onBehalfOf,
uint256 amount,
DataTypes.InterestRateMode interestRateMode,
uint256 borrowRate,
uint16 indexed referralCode
);
event Repay(
address indexed reserve,
address indexed user,
address indexed repayer,
uint256 amount,
bool useATokens
);
event IsolationModeTotalDebtUpdated(address indexed asset, uint256 totalDebt);
event ReserveUsedAsCollateralDisabled(address indexed reserve, address indexed user);
/**
* @notice Implements the borrow feature. Borrowing allows users that provided collateral to draw liquidity from the
* Aave protocol proportionally to their collateralization power. For isolated positions, it also increases the
* isolated debt.
* @dev Emits the `Borrow()` event
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param eModeCategories The configuration of all the efficiency mode categories
* @param userConfig The user configuration mapping that tracks the supplied/borrowed assets
* @param params The additional parameters needed to execute the borrow function
*/
function executeBorrow(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories,
DataTypes.UserConfigurationMap storage userConfig,
DataTypes.ExecuteBorrowParams memory params
) external {
DataTypes.ReserveData storage reserve = reservesData[params.asset];
DataTypes.ReserveCache memory reserveCache = reserve.cache();
reserve.updateState(reserveCache);
(
bool isolationModeActive,
address isolationModeCollateralAddress,
uint256 isolationModeDebtCeiling
) = userConfig.getIsolationModeState(reservesData, reservesList);
ValidationLogic.validateBorrow(
reservesData,
reservesList,
eModeCategories,
DataTypes.ValidateBorrowParams({
reserveCache: reserveCache,
userConfig: userConfig,
asset: params.asset,
userAddress: params.onBehalfOf,
amount: params.amount,
interestRateMode: params.interestRateMode,
reservesCount: params.reservesCount,
oracle: params.oracle,
userEModeCategory: params.userEModeCategory,
priceOracleSentinel: params.priceOracleSentinel,
isolationModeActive: isolationModeActive,
isolationModeCollateralAddress: isolationModeCollateralAddress,
isolationModeDebtCeiling: isolationModeDebtCeiling
})
);
bool isFirstBorrowing = false;
(isFirstBorrowing, reserveCache.nextScaledVariableDebt) = IVariableDebtToken(
reserveCache.variableDebtTokenAddress
).mint(params.user, params.onBehalfOf, params.amount, reserveCache.nextVariableBorrowIndex);
if (isFirstBorrowing) {
userConfig.setBorrowing(reserve.id, true);
}
if (isolationModeActive) {
uint256 nextIsolationModeTotalDebt = reservesData[isolationModeCollateralAddress]
.isolationModeTotalDebt += (params.amount /
10 **
(reserveCache.reserveConfiguration.getDecimals() -
ReserveConfiguration.DEBT_CEILING_DECIMALS)).toUint128();
emit IsolationModeTotalDebtUpdated(
isolationModeCollateralAddress,
nextIsolationModeTotalDebt
);
}
reserve.updateInterestRatesAndVirtualBalance(
reserveCache,
params.asset,
0,
params.releaseUnderlying ? params.amount : 0
);
if (params.releaseUnderlying) {
IAToken(reserveCache.aTokenAddress).transferUnderlyingTo(params.user, params.amount);
}
emit Borrow(
params.asset,
params.user,
params.onBehalfOf,
params.amount,
DataTypes.InterestRateMode.VARIABLE,
reserve.currentVariableBorrowRate,
params.referralCode
);
}
/**
* @notice Implements the repay feature. Repaying transfers the underlying back to the aToken and clears the
* equivalent amount of debt for the user by burning the corresponding debt token. For isolated positions, it also
* reduces the isolated debt.
* @dev Emits the `Repay()` event
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param userConfig The user configuration mapping that tracks the supplied/borrowed assets
* @param params The additional parameters needed to execute the repay function
* @return The actual amount being repaid
*/
function executeRepay(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
DataTypes.UserConfigurationMap storage userConfig,
DataTypes.ExecuteRepayParams memory params
) external returns (uint256) {
DataTypes.ReserveData storage reserve = reservesData[params.asset];
DataTypes.ReserveCache memory reserveCache = reserve.cache();
reserve.updateState(reserveCache);
uint256 variableDebt = IERC20(reserveCache.variableDebtTokenAddress).balanceOf(
params.onBehalfOf
);
ValidationLogic.validateRepay(
reserveCache,
params.amount,
params.interestRateMode,
params.onBehalfOf,
variableDebt
);
uint256 paybackAmount = variableDebt;
// Allows a user to repay with aTokens without leaving dust from interest.
if (params.useATokens && params.amount == type(uint256).max) {
params.amount = IAToken(reserveCache.aTokenAddress).balanceOf(msg.sender);
}
if (params.amount < paybackAmount) {
paybackAmount = params.amount;
}
reserveCache.nextScaledVariableDebt = IVariableDebtToken(reserveCache.variableDebtTokenAddress)
.burn(params.onBehalfOf, paybackAmount, reserveCache.nextVariableBorrowIndex);
reserve.updateInterestRatesAndVirtualBalance(
reserveCache,
params.asset,
params.useATokens ? 0 : paybackAmount,
0
);
if (variableDebt - paybackAmount == 0) {
userConfig.setBorrowing(reserve.id, false);
}
IsolationModeLogic.updateIsolatedDebtIfIsolated(
reservesData,
reservesList,
userConfig,
reserveCache,
paybackAmount
);
// in case of aToken repayment the msg.sender must always repay on behalf of itself
if (params.useATokens) {
IAToken(reserveCache.aTokenAddress).burn(
msg.sender,
reserveCache.aTokenAddress,
paybackAmount,
reserveCache.nextLiquidityIndex
);
bool isCollateral = userConfig.isUsingAsCollateral(reserve.id);
if (isCollateral && IAToken(reserveCache.aTokenAddress).scaledBalanceOf(msg.sender) == 0) {
userConfig.setUsingAsCollateral(reserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.asset, msg.sender);
}
} else {
IERC20(params.asset).safeTransferFrom(msg.sender, reserveCache.aTokenAddress, paybackAmount);
IAToken(reserveCache.aTokenAddress).handleRepayment(
msg.sender,
params.onBehalfOf,
paybackAmount
);
}
emit Repay(params.asset, params.onBehalfOf, msg.sender, paybackAmount, params.useATokens);
return paybackAmount;
}
}