-
Notifications
You must be signed in to change notification settings - Fork 45
/
UiPoolDataProviderV3.sol
265 lines (234 loc) · 11.2 KB
/
UiPoolDataProviderV3.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;
import {IERC20Detailed} from '../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
import {IPoolAddressesProvider} from '../interfaces/IPoolAddressesProvider.sol';
import {IPool} from '../interfaces/IPool.sol';
import {IAaveOracle} from '../interfaces/IAaveOracle.sol';
import {IAToken} from '../interfaces/IAToken.sol';
import {IVariableDebtToken} from '../interfaces/IVariableDebtToken.sol';
import {IDefaultInterestRateStrategyV2} from '../interfaces/IDefaultInterestRateStrategyV2.sol';
import {AaveProtocolDataProvider} from './AaveProtocolDataProvider.sol';
import {WadRayMath} from '../protocol/libraries/math/WadRayMath.sol';
import {ReserveConfiguration} from '../protocol/libraries/configuration/ReserveConfiguration.sol';
import {UserConfiguration} from '../protocol/libraries/configuration/UserConfiguration.sol';
import {DataTypes} from '../protocol/libraries/types/DataTypes.sol';
import {IEACAggregatorProxy} from './interfaces/IEACAggregatorProxy.sol';
import {IERC20DetailedBytes} from './interfaces/IERC20DetailedBytes.sol';
import {IUiPoolDataProviderV3} from './interfaces/IUiPoolDataProviderV3.sol';
contract UiPoolDataProviderV3 is IUiPoolDataProviderV3 {
using WadRayMath for uint256;
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using UserConfiguration for DataTypes.UserConfigurationMap;
IEACAggregatorProxy public immutable networkBaseTokenPriceInUsdProxyAggregator;
IEACAggregatorProxy public immutable marketReferenceCurrencyPriceInUsdProxyAggregator;
uint256 public constant ETH_CURRENCY_UNIT = 1 ether;
address public constant MKR_ADDRESS = 0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2;
constructor(
IEACAggregatorProxy _networkBaseTokenPriceInUsdProxyAggregator,
IEACAggregatorProxy _marketReferenceCurrencyPriceInUsdProxyAggregator
) {
networkBaseTokenPriceInUsdProxyAggregator = _networkBaseTokenPriceInUsdProxyAggregator;
marketReferenceCurrencyPriceInUsdProxyAggregator = _marketReferenceCurrencyPriceInUsdProxyAggregator;
}
function getReservesList(
IPoolAddressesProvider provider
) external view override returns (address[] memory) {
IPool pool = IPool(provider.getPool());
return pool.getReservesList();
}
function getReservesData(
IPoolAddressesProvider provider
) external view override returns (AggregatedReserveData[] memory, BaseCurrencyInfo memory) {
IAaveOracle oracle = IAaveOracle(provider.getPriceOracle());
IPool pool = IPool(provider.getPool());
AaveProtocolDataProvider poolDataProvider = AaveProtocolDataProvider(
provider.getPoolDataProvider()
);
address[] memory reserves = pool.getReservesList();
AggregatedReserveData[] memory reservesData = new AggregatedReserveData[](reserves.length);
for (uint256 i = 0; i < reserves.length; i++) {
AggregatedReserveData memory reserveData = reservesData[i];
reserveData.underlyingAsset = reserves[i];
// reserve current state
DataTypes.ReserveDataLegacy memory baseData = pool.getReserveData(
reserveData.underlyingAsset
);
//the liquidity index. Expressed in ray
reserveData.liquidityIndex = baseData.liquidityIndex;
//variable borrow index. Expressed in ray
reserveData.variableBorrowIndex = baseData.variableBorrowIndex;
//the current supply rate. Expressed in ray
reserveData.liquidityRate = baseData.currentLiquidityRate;
//the current variable borrow rate. Expressed in ray
reserveData.variableBorrowRate = baseData.currentVariableBorrowRate;
reserveData.lastUpdateTimestamp = baseData.lastUpdateTimestamp;
reserveData.aTokenAddress = baseData.aTokenAddress;
reserveData.variableDebtTokenAddress = baseData.variableDebtTokenAddress;
//address of the interest rate strategy
reserveData.interestRateStrategyAddress = baseData.interestRateStrategyAddress;
reserveData.priceInMarketReferenceCurrency = oracle.getAssetPrice(
reserveData.underlyingAsset
);
reserveData.priceOracle = oracle.getSourceOfAsset(reserveData.underlyingAsset);
reserveData.availableLiquidity = IERC20Detailed(reserveData.underlyingAsset).balanceOf(
reserveData.aTokenAddress
);
reserveData.totalScaledVariableDebt = IVariableDebtToken(reserveData.variableDebtTokenAddress)
.scaledTotalSupply();
// Due we take the symbol from underlying token we need a special case for $MKR as symbol() returns bytes32
if (address(reserveData.underlyingAsset) == address(MKR_ADDRESS)) {
bytes32 symbol = IERC20DetailedBytes(reserveData.underlyingAsset).symbol();
bytes32 name = IERC20DetailedBytes(reserveData.underlyingAsset).name();
reserveData.symbol = bytes32ToString(symbol);
reserveData.name = bytes32ToString(name);
} else {
reserveData.symbol = IERC20Detailed(reserveData.underlyingAsset).symbol();
reserveData.name = IERC20Detailed(reserveData.underlyingAsset).name();
}
//stores the reserve configuration
DataTypes.ReserveConfigurationMap memory reserveConfigurationMap = baseData.configuration;
(
reserveData.baseLTVasCollateral,
reserveData.reserveLiquidationThreshold,
reserveData.reserveLiquidationBonus,
reserveData.decimals,
reserveData.reserveFactor
) = reserveConfigurationMap.getParams();
reserveData.usageAsCollateralEnabled = reserveData.baseLTVasCollateral != 0;
(
reserveData.isActive,
reserveData.isFrozen,
reserveData.borrowingEnabled,
reserveData.isPaused
) = reserveConfigurationMap.getFlags();
// interest rates
try
IDefaultInterestRateStrategyV2(reserveData.interestRateStrategyAddress).getInterestRateData(
reserveData.underlyingAsset
)
returns (IDefaultInterestRateStrategyV2.InterestRateDataRay memory res) {
reserveData.baseVariableBorrowRate = res.baseVariableBorrowRate;
reserveData.variableRateSlope1 = res.variableRateSlope1;
reserveData.variableRateSlope2 = res.variableRateSlope2;
reserveData.optimalUsageRatio = res.optimalUsageRatio;
} catch {}
// v3 only
reserveData.debtCeiling = reserveConfigurationMap.getDebtCeiling();
reserveData.debtCeilingDecimals = poolDataProvider.getDebtCeilingDecimals();
(reserveData.borrowCap, reserveData.supplyCap) = reserveConfigurationMap.getCaps();
try poolDataProvider.getFlashLoanEnabled(reserveData.underlyingAsset) returns (
bool flashLoanEnabled
) {
reserveData.flashLoanEnabled = flashLoanEnabled;
} catch (bytes memory) {
reserveData.flashLoanEnabled = true;
}
reserveData.isSiloedBorrowing = reserveConfigurationMap.getSiloedBorrowing();
reserveData.unbacked = baseData.unbacked;
reserveData.isolationModeTotalDebt = baseData.isolationModeTotalDebt;
reserveData.accruedToTreasury = baseData.accruedToTreasury;
reserveData.borrowableInIsolation = reserveConfigurationMap.getBorrowableInIsolation();
try poolDataProvider.getIsVirtualAccActive(reserveData.underlyingAsset) returns (
bool virtualAccActive
) {
reserveData.virtualAccActive = virtualAccActive;
} catch (bytes memory) {
reserveData.virtualAccActive = false;
}
try pool.getVirtualUnderlyingBalance(reserveData.underlyingAsset) returns (
uint128 virtualUnderlyingBalance
) {
reserveData.virtualUnderlyingBalance = virtualUnderlyingBalance;
} catch (bytes memory) {
reserveData.virtualUnderlyingBalance = 0;
}
}
BaseCurrencyInfo memory baseCurrencyInfo;
baseCurrencyInfo.networkBaseTokenPriceInUsd = networkBaseTokenPriceInUsdProxyAggregator
.latestAnswer();
baseCurrencyInfo.networkBaseTokenPriceDecimals = networkBaseTokenPriceInUsdProxyAggregator
.decimals();
try oracle.BASE_CURRENCY_UNIT() returns (uint256 baseCurrencyUnit) {
baseCurrencyInfo.marketReferenceCurrencyUnit = baseCurrencyUnit;
baseCurrencyInfo.marketReferenceCurrencyPriceInUsd = int256(baseCurrencyUnit);
} catch (bytes memory /*lowLevelData*/) {
baseCurrencyInfo.marketReferenceCurrencyUnit = ETH_CURRENCY_UNIT;
baseCurrencyInfo
.marketReferenceCurrencyPriceInUsd = marketReferenceCurrencyPriceInUsdProxyAggregator
.latestAnswer();
}
return (reservesData, baseCurrencyInfo);
}
/// @inheritdoc IUiPoolDataProviderV3
function getEModes(IPoolAddressesProvider provider) external view returns (Emode[] memory) {
IPool pool = IPool(provider.getPool());
Emode[] memory tempCategories = new Emode[](256);
uint8 eModesFound = 0;
uint8 missCounter = 0;
for (uint8 i = 1; i < 256; i++) {
DataTypes.CollateralConfig memory cfg = pool.getEModeCategoryCollateralConfig(i);
if (cfg.liquidationThreshold != 0) {
tempCategories[eModesFound] = Emode({
eMode: DataTypes.EModeCategory({
ltv: cfg.ltv,
liquidationThreshold: cfg.liquidationThreshold,
liquidationBonus: cfg.liquidationBonus,
label: pool.getEModeCategoryLabel(i),
collateralBitmap: pool.getEModeCategoryCollateralBitmap(i),
borrowableBitmap: pool.getEModeCategoryBorrowableBitmap(i)
}),
id: i
});
++eModesFound;
missCounter = 0;
} else {
++missCounter;
}
// assumes there will never be a gap > 2 when setting eModes
if (missCounter > 2) break;
}
Emode[] memory categories = new Emode[](eModesFound);
for (uint8 i = 0; i < eModesFound; i++) {
categories[i] = tempCategories[i];
}
return categories;
}
function getUserReservesData(
IPoolAddressesProvider provider,
address user
) external view override returns (UserReserveData[] memory, uint8) {
IPool pool = IPool(provider.getPool());
address[] memory reserves = pool.getReservesList();
DataTypes.UserConfigurationMap memory userConfig = pool.getUserConfiguration(user);
uint8 userEmodeCategoryId = uint8(pool.getUserEMode(user));
UserReserveData[] memory userReservesData = new UserReserveData[](
user != address(0) ? reserves.length : 0
);
for (uint256 i = 0; i < reserves.length; i++) {
DataTypes.ReserveDataLegacy memory baseData = pool.getReserveData(reserves[i]);
// user reserve data
userReservesData[i].underlyingAsset = reserves[i];
userReservesData[i].scaledATokenBalance = IAToken(baseData.aTokenAddress).scaledBalanceOf(
user
);
userReservesData[i].usageAsCollateralEnabledOnUser = userConfig.isUsingAsCollateral(i);
if (userConfig.isBorrowing(i)) {
userReservesData[i].scaledVariableDebt = IVariableDebtToken(
baseData.variableDebtTokenAddress
).scaledBalanceOf(user);
}
}
return (userReservesData, userEmodeCategoryId);
}
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory) {
uint8 i = 0;
while (i < 32 && _bytes32[i] != 0) {
i++;
}
bytes memory bytesArray = new bytes(i);
for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
bytesArray[i] = _bytes32[i];
}
return string(bytesArray);
}
}