-
Notifications
You must be signed in to change notification settings - Fork 737
/
ReserveConfiguration.sol
267 lines (242 loc) · 8.25 KB
/
ReserveConfiguration.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
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {ReserveLogic} from '../logic/ReserveLogic.sol';
import {WadRayMath} from '../math/WadRayMath.sol';
import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol';
/**
* @title ReserveConfiguration library
* @author Aave
* @notice Implements the bitmap logic to handle the reserve configuration
*/
library ReserveConfiguration {
uint256 constant LTV_MASK = 0xFFFFFFFFFFFFFFFF0000;
uint256 constant LIQUIDATION_THRESHOLD_MASK = 0xFFFFFFFFFFFF0000FFFF;
uint256 constant LIQUIDATION_BONUS_MASK = 0xFFFFFFF0000FFFFFFFF;
uint256 constant DECIMALS_MASK = 0xFFFFFF00FFFFFFFFFFFF;
uint256 constant ACTIVE_MASK = 0xFFFFFEFFFFFFFFFFFFFF;
uint256 constant FROZEN_MASK = 0xFFFFFDFFFFFFFFFFFFFF;
uint256 constant BORROWING_MASK = 0xFFFFFBFFFFFFFFFFFFFF;
uint256 constant STABLE_BORROWING_MASK = 0xFFFF07FFFFFFFFFFFFFF;
uint256 constant RESERVE_FACTOR_MASK = 0xFFFFFFFFFFFFFFFF;
struct Map {
//bit 0-15: LTV
//bit 16-31: Liq. threshold
//bit 32-47: Liq. bonus
//bit 48-55: Decimals
//bit 56: Reserve is active
//bit 57: reserve is freezed
//bit 58: borrowing is enabled
//bit 59: stable rate borrowing enabled
//bit 64-79: reserve factor
uint256 data;
}
/**
* @dev sets the reserve factor of the reserve
* @param self the reserve configuration
* @param reserveFactor the reserve factor
**/
function setReserveFactor(ReserveConfiguration.Map memory self, uint256 reserveFactor) internal pure {
self.data = (self.data & RESERVE_FACTOR_MASK) | reserveFactor << 64;
}
/**
* @dev gets the reserve factor of the reserve
* @param self the reserve configuration
* @return the reserve factor
**/
function getReserveFactor(ReserveConfiguration.Map storage self) internal view returns (uint256) {
return (self.data & ~RESERVE_FACTOR_MASK) >> 64;
}
/**
* @dev sets the Loan to Value of the reserve
* @param self the reserve configuration
* @param ltv the new ltv
**/
function setLtv(ReserveConfiguration.Map memory self, uint256 ltv) internal pure {
self.data = (self.data & LTV_MASK) | ltv;
}
/**
* @dev gets the Loan to Value of the reserve
* @param self the reserve configuration
* @return the loan to value
**/
function getLtv(ReserveConfiguration.Map storage self) internal view returns (uint256) {
return self.data & ~LTV_MASK;
}
/**
* @dev sets the liquidation threshold of the reserve
* @param self the reserve configuration
* @param threshold the new liquidation threshold
**/
function setLiquidationThreshold(ReserveConfiguration.Map memory self, uint256 threshold)
internal
pure
{
self.data = (self.data & LIQUIDATION_THRESHOLD_MASK) | (threshold << 16);
}
/**
* @dev gets the Loan to Value of the reserve
* @param self the reserve configuration
* @return the liquidation threshold
**/
function getLiquidationThreshold(ReserveConfiguration.Map storage self)
internal
view
returns (uint256)
{
return (self.data & ~LIQUIDATION_THRESHOLD_MASK) >> 16;
}
/**
* @dev sets the liquidation bonus of the reserve
* @param self the reserve configuration
* @param bonus the new liquidation bonus
**/
function setLiquidationBonus(ReserveConfiguration.Map memory self, uint256 bonus) internal pure {
self.data = (self.data & LIQUIDATION_BONUS_MASK) | (bonus << 32);
}
/**
* @dev gets the liquidation bonus of the reserve
* @param self the reserve configuration
* @return the liquidation bonus
**/
function getLiquidationBonus(ReserveConfiguration.Map storage self)
internal
view
returns (uint256)
{
return (self.data & ~LIQUIDATION_BONUS_MASK) >> 32;
}
/**
* @dev sets the decimals of the underlying asset of the reserve
* @param self the reserve configuration
* @param decimals the decimals
**/
function setDecimals(ReserveConfiguration.Map memory self, uint256 decimals) internal pure {
self.data = (self.data & DECIMALS_MASK) | (decimals << 48);
}
/**
* @dev gets the decimals of the underlying asset of the reserve
* @param self the reserve configuration
* @return the decimals of the asset
**/
function getDecimals(ReserveConfiguration.Map storage self) internal view returns (uint256) {
return (self.data & ~DECIMALS_MASK) >> 48;
}
/**
* @dev sets the active state of the reserve
* @param self the reserve configuration
* @param active the active state
**/
function setActive(ReserveConfiguration.Map memory self, bool active) internal pure {
self.data = (self.data & ACTIVE_MASK) | (uint256(active ? 1 : 0) << 56);
}
/**
* @dev gets the active state of the reserve
* @param self the reserve configuration
* @return the active state
**/
function getActive(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~ACTIVE_MASK) >> 56) != 0;
}
/**
* @dev sets the frozen state of the reserve
* @param self the reserve configuration
* @param frozen the frozen state
**/
function setFrozen(ReserveConfiguration.Map memory self, bool frozen) internal pure {
self.data = (self.data & FROZEN_MASK) | (uint256(frozen ? 1 : 0) << 57);
}
/**
* @dev gets the frozen state of the reserve
* @param self the reserve configuration
* @return the frozen state
**/
function getFrozen(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~FROZEN_MASK) >> 57) != 0;
}
/**
* @dev enables or disables borrowing on the reserve
* @param self the reserve configuration
* @param enabled true if the borrowing needs to be enabled, false otherwise
**/
function setBorrowingEnabled(ReserveConfiguration.Map memory self, bool enabled) internal pure {
self.data = (self.data & BORROWING_MASK) | (uint256(enabled ? 1 : 0) << 58);
}
/**
* @dev gets the borrowing state of the reserve
* @param self the reserve configuration
* @return the borrowing state
**/
function getBorrowingEnabled(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~BORROWING_MASK) >> 58) != 0;
}
/**
* @dev enables or disables stable rate borrowing on the reserve
* @param self the reserve configuration
* @param enabled true if the stable rate borrowing needs to be enabled, false otherwise
**/
function setStableRateBorrowingEnabled(ReserveConfiguration.Map memory self, bool enabled)
internal pure
{
self.data = (self.data & STABLE_BORROWING_MASK) | (uint256(enabled ? 1 : 0) << 59);
}
/**
* @dev gets the stable rate borrowing state of the reserve
* @param self the reserve configuration
* @return the stable rate borrowing state
**/
function getStableRateBorrowingEnabled(ReserveConfiguration.Map storage self)
internal
view
returns (bool)
{
return ((self.data & ~STABLE_BORROWING_MASK) >> 59) != 0;
}
/**
* @dev gets the configuration flags of the reserve
* @param self the reserve configuration
* @return the state flags representing active, freezed, borrowing enabled, stableRateBorrowing enabled
**/
function getFlags(ReserveConfiguration.Map storage self)
internal
view
returns (
bool,
bool,
bool,
bool
)
{
uint256 dataLocal = self.data;
return (
(dataLocal & ~ACTIVE_MASK) >> 56 != 0,
(dataLocal & ~FROZEN_MASK) >> 57 != 0,
(dataLocal & ~BORROWING_MASK) >> 58 != 0,
(dataLocal & ~STABLE_BORROWING_MASK) >> 59 != 0
);
}
/**
* @dev gets the configuration paramters of the reserve
* @param self the reserve configuration
* @return the state params representing ltv, liquidation threshold, liquidation bonus, the reserve decimals
**/
function getParams(ReserveConfiguration.Map storage self)
internal
view
returns (
uint256,
uint256,
uint256,
uint256
)
{
uint256 dataLocal = self.data;
return (
dataLocal & ~LTV_MASK,
(dataLocal & ~LIQUIDATION_THRESHOLD_MASK) >> 16,
(dataLocal & ~LIQUIDATION_BONUS_MASK) >> 32,
(dataLocal & ~DECIMALS_MASK) >> 48
);
}
}