aave-protocol-v2/contracts/misc/AaveProtocolDataProvider.sol

181 lines
6.4 KiB
Solidity
Raw Normal View History

// SPDX-License-Identifier: agpl-3.0
2020-11-20 10:45:20 +00:00
pragma solidity 0.6.12;
2020-06-08 15:36:40 +00:00
pragma experimental ABIEncoderV2;
2020-10-15 13:25:27 +00:00
import {IERC20Detailed} from '../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol';
import {ILendingPool} from '../interfaces/ILendingPool.sol';
import {IStableDebtToken} from '../interfaces/IStableDebtToken.sol';
import {IVariableDebtToken} from '../interfaces/IVariableDebtToken.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';
2020-06-08 15:36:40 +00:00
contract AaveProtocolDataProvider {
2020-11-24 15:17:27 +00:00
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using UserConfiguration for DataTypes.UserConfigurationMap;
address constant MKR = 0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2;
address constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
2020-07-13 08:54:08 +00:00
struct TokenData {
string symbol;
address tokenAddress;
}
2020-06-08 15:36:40 +00:00
2020-07-13 08:54:08 +00:00
ILendingPoolAddressesProvider public immutable ADDRESSES_PROVIDER;
2020-06-08 15:36:40 +00:00
2020-07-13 08:54:08 +00:00
constructor(ILendingPoolAddressesProvider addressesProvider) public {
ADDRESSES_PROVIDER = addressesProvider;
}
2020-06-08 15:36:40 +00:00
2020-07-13 08:54:08 +00:00
function getAllReservesTokens() external view returns (TokenData[] memory) {
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool());
2020-10-06 13:51:48 +00:00
address[] memory reserves = pool.getReservesList();
2020-07-13 08:54:08 +00:00
TokenData[] memory reservesTokens = new TokenData[](reserves.length);
for (uint256 i = 0; i < reserves.length; i++) {
if (reserves[i] == MKR) {
reservesTokens[i] = TokenData({symbol: 'MKR', tokenAddress: reserves[i]});
continue;
}
if (reserves[i] == ETH) {
reservesTokens[i] = TokenData({symbol: 'ETH', tokenAddress: reserves[i]});
continue;
}
2020-07-13 08:54:08 +00:00
reservesTokens[i] = TokenData({
symbol: IERC20Detailed(reserves[i]).symbol(),
2020-07-13 08:54:08 +00:00
tokenAddress: reserves[i]
});
}
2020-07-13 08:54:08 +00:00
return reservesTokens;
}
2020-07-13 08:54:08 +00:00
function getAllATokens() external view returns (TokenData[] memory) {
ILendingPool pool = ILendingPool(ADDRESSES_PROVIDER.getLendingPool());
2020-10-06 13:51:48 +00:00
address[] memory reserves = pool.getReservesList();
2020-07-13 08:54:08 +00:00
TokenData[] memory aTokens = new TokenData[](reserves.length);
for (uint256 i = 0; i < reserves.length; i++) {
DataTypes.ReserveData memory reserveData = pool.getReserveData(reserves[i]);
aTokens[i] = TokenData({
symbol: IERC20Detailed(reserveData.aTokenAddress).symbol(),
tokenAddress: reserveData.aTokenAddress
});
2020-06-08 15:36:40 +00:00
}
2020-07-13 08:54:08 +00:00
return aTokens;
}
function getReserveConfigurationData(address asset)
external
view
returns (
uint256 decimals,
uint256 ltv,
uint256 liquidationThreshold,
uint256 liquidationBonus,
uint256 reserveFactor,
bool usageAsCollateralEnabled,
bool borrowingEnabled,
bool stableBorrowRateEnabled,
bool isActive,
bool isFrozen
)
{
2020-11-24 15:17:27 +00:00
DataTypes.ReserveConfigurationMap memory configuration =
ILendingPool(ADDRESSES_PROVIDER.getLendingPool()).getConfiguration(asset);
(ltv, liquidationThreshold, liquidationBonus, decimals, reserveFactor) = configuration
.getParamsMemory();
(isActive, isFrozen, borrowingEnabled, stableBorrowRateEnabled) = configuration
.getFlagsMemory();
2020-10-12 18:07:17 +00:00
usageAsCollateralEnabled = liquidationThreshold > 0;
}
function getReserveData(address asset)
external
view
returns (
uint256 availableLiquidity,
uint256 totalStableDebt,
uint256 totalVariableDebt,
uint256 liquidityRate,
uint256 variableBorrowRate,
uint256 stableBorrowRate,
uint256 averageStableBorrowRate,
uint256 liquidityIndex,
uint256 variableBorrowIndex,
uint40 lastUpdateTimestamp
)
{
DataTypes.ReserveData memory reserve =
ILendingPool(ADDRESSES_PROVIDER.getLendingPool()).getReserveData(asset);
return (
IERC20Detailed(asset).balanceOf(reserve.aTokenAddress),
IERC20Detailed(reserve.stableDebtTokenAddress).totalSupply(),
IERC20Detailed(reserve.variableDebtTokenAddress).totalSupply(),
reserve.currentLiquidityRate,
reserve.currentVariableBorrowRate,
reserve.currentStableBorrowRate,
IStableDebtToken(reserve.stableDebtTokenAddress).getAverageStableRate(),
reserve.liquidityIndex,
reserve.variableBorrowIndex,
reserve.lastUpdateTimestamp
);
}
function getUserReserveData(address asset, address user)
external
view
returns (
uint256 currentATokenBalance,
uint256 currentStableDebt,
uint256 currentVariableDebt,
uint256 principalStableDebt,
uint256 scaledVariableDebt,
uint256 stableBorrowRate,
uint256 liquidityRate,
uint40 stableRateLastUpdated,
bool usageAsCollateralEnabled
)
{
DataTypes.ReserveData memory reserve =
ILendingPool(ADDRESSES_PROVIDER.getLendingPool()).getReserveData(asset);
2020-11-24 15:17:27 +00:00
DataTypes.UserConfigurationMap memory userConfig =
ILendingPool(ADDRESSES_PROVIDER.getLendingPool()).getUserConfiguration(user);
currentATokenBalance = IERC20Detailed(reserve.aTokenAddress).balanceOf(user);
currentVariableDebt = IERC20Detailed(reserve.variableDebtTokenAddress).balanceOf(user);
currentStableDebt = IERC20Detailed(reserve.stableDebtTokenAddress).balanceOf(user);
principalStableDebt = IStableDebtToken(reserve.stableDebtTokenAddress).principalBalanceOf(user);
scaledVariableDebt = IVariableDebtToken(reserve.variableDebtTokenAddress).scaledBalanceOf(user);
liquidityRate = reserve.currentLiquidityRate;
stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(user);
stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated(
user
);
usageAsCollateralEnabled = userConfig.isUsingAsCollateral(reserve.id);
}
2020-10-12 18:07:17 +00:00
function getReserveTokensAddresses(address asset)
external
view
returns (
address aTokenAddress,
address stableDebtTokenAddress,
address variableDebtTokenAddress
)
{
DataTypes.ReserveData memory reserve =
ILendingPool(ADDRESSES_PROVIDER.getLendingPool()).getReserveData(asset);
2020-10-12 18:07:17 +00:00
return (
reserve.aTokenAddress,
reserve.stableDebtTokenAddress,
reserve.variableDebtTokenAddress
);
}
2020-07-13 08:54:08 +00:00
}