From 12896ff4125379d28684bd4fa55978f1c63356c7 Mon Sep 17 00:00:00 2001 From: andyk Date: Thu, 17 Jun 2021 16:53:59 +0300 Subject: [PATCH 1/4] add more functions to the UI data provider --- contracts/misc/UiPoolDataProvider.sol | 183 +++++++++++++++++- .../misc/interfaces/IUiPoolDataProvider.sol | 22 +++ 2 files changed, 203 insertions(+), 2 deletions(-) diff --git a/contracts/misc/UiPoolDataProvider.sol b/contracts/misc/UiPoolDataProvider.sol index f06d0183..8eff3e4e 100644 --- a/contracts/misc/UiPoolDataProvider.sol +++ b/contracts/misc/UiPoolDataProvider.sol @@ -51,6 +51,185 @@ contract UiPoolDataProvider is IUiPoolDataProvider { ); } + function getReservesList(ILendingPoolAddressesProvider provider) + public + view + override + returns (address[] memory) + { + ILendingPool lendingPool = ILendingPool(provider.getLendingPool()); + return lendingPool.getReservesList(); + } + + function getSimpleReservesData(ILendingPoolAddressesProvider provider) + public + view + override + returns ( + AggregatedReserveData[] memory, + uint256, + uint256 + ) + { + ILendingPool lendingPool = ILendingPool(provider.getLendingPool()); + address[] memory reserves = lendingPool.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.ReserveData memory baseData = + lendingPool.getReserveData(reserveData.underlyingAsset); + reserveData.liquidityIndex = baseData.liquidityIndex; + reserveData.variableBorrowIndex = baseData.variableBorrowIndex; + reserveData.liquidityRate = baseData.currentLiquidityRate; + reserveData.variableBorrowRate = baseData.currentVariableBorrowRate; + reserveData.stableBorrowRate = baseData.currentStableBorrowRate; + reserveData.lastUpdateTimestamp = baseData.lastUpdateTimestamp; + reserveData.aTokenAddress = baseData.aTokenAddress; + reserveData.stableDebtTokenAddress = baseData.stableDebtTokenAddress; + reserveData.variableDebtTokenAddress = baseData.variableDebtTokenAddress; + reserveData.interestRateStrategyAddress = baseData.interestRateStrategyAddress; + reserveData.priceInEth = oracle.getAssetPrice(reserveData.underlyingAsset); + + reserveData.availableLiquidity = IERC20Detailed(reserveData.underlyingAsset).balanceOf( + reserveData.aTokenAddress + ); + ( + reserveData.totalPrincipalStableDebt, + , + reserveData.averageStableRate, + reserveData.stableDebtLastUpdateTimestamp + ) = IStableDebtToken(reserveData.stableDebtTokenAddress).getSupplyData(); + reserveData.totalScaledVariableDebt = IVariableDebtToken(reserveData.variableDebtTokenAddress) + .scaledTotalSupply(); + + // reserve configuration + + // we're getting this info from the aToken, because some of assets can be not compliant with ETC20Detailed + reserveData.symbol = IERC20Detailed(reserveData.aTokenAddress).symbol(); + reserveData.name = ''; + + ( + reserveData.baseLTVasCollateral, + reserveData.reserveLiquidationThreshold, + reserveData.reserveLiquidationBonus, + reserveData.decimals, + reserveData.reserveFactor + ) = baseData.configuration.getParamsMemory(); + ( + reserveData.isActive, + reserveData.isFrozen, + reserveData.borrowingEnabled, + reserveData.stableBorrowRateEnabled + ) = baseData.configuration.getFlagsMemory(); + reserveData.usageAsCollateralEnabled = reserveData.baseLTVasCollateral != 0; + ( + reserveData.variableRateSlope1, + reserveData.variableRateSlope2, + reserveData.stableRateSlope1, + reserveData.stableRateSlope2 + ) = getInterestRateStrategySlopes( + DefaultReserveInterestRateStrategy(reserveData.interestRateStrategyAddress) + ); + + // incentives + if (address(0) != address(incentivesController)) { + ( + reserveData.aEmissionPerSecond, + reserveData.aIncentivesLastUpdateTimestamp, + reserveData.aTokenIncentivesIndex + ) = incentivesController.getAssetData(reserveData.aTokenAddress); + + ( + reserveData.sEmissionPerSecond, + reserveData.sIncentivesLastUpdateTimestamp, + reserveData.sTokenIncentivesIndex + ) = incentivesController.getAssetData(reserveData.stableDebtTokenAddress); + + ( + reserveData.vEmissionPerSecond, + reserveData.vIncentivesLastUpdateTimestamp, + reserveData.vTokenIncentivesIndex + ) = incentivesController.getAssetData(reserveData.variableDebtTokenAddress); + } + } + + uint256 emissionEndTimestamp; + if (address(0) != address(incentivesController)) { + emissionEndTimestamp = incentivesController.DISTRIBUTION_END(); + } + + return (reservesData, oracle.getAssetPrice(MOCK_USD_ADDRESS), emissionEndTimestamp); + } + + function getUserReservesData(ILendingPoolAddressesProvider provider, address user) + external + view + override + returns (UserReserveData[] memory, uint256) + { + ILendingPool lendingPool = ILendingPool(provider.getLendingPool()); + address[] memory reserves = lendingPool.getReservesList(); + DataTypes.UserConfigurationMap memory userConfig = lendingPool.getUserConfiguration(user); + + UserReserveData[] memory userReservesData = + new UserReserveData[](user != address(0) ? reserves.length : 0); + + for (uint256 i = 0; i < reserves.length; i++) { + DataTypes.ReserveData memory baseData = lendingPool.getReserveData(reserves[i]); + // incentives + if (address(0) != address(incentivesController)) { + userReservesData[i].aTokenincentivesUserIndex = incentivesController.getUserAssetData( + user, + baseData.aTokenAddress + ); + userReservesData[i].vTokenincentivesUserIndex = incentivesController.getUserAssetData( + user, + baseData.variableDebtTokenAddress + ); + userReservesData[i].sTokenincentivesUserIndex = incentivesController.getUserAssetData( + user, + baseData.stableDebtTokenAddress + ); + } + // 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); + userReservesData[i].principalStableDebt = IStableDebtToken(baseData.stableDebtTokenAddress) + .principalBalanceOf(user); + if (userReservesData[i].principalStableDebt != 0) { + userReservesData[i].stableBorrowRate = IStableDebtToken(baseData.stableDebtTokenAddress) + .getUserStableRate(user); + userReservesData[i].stableBorrowLastUpdateTimestamp = IStableDebtToken( + baseData + .stableDebtTokenAddress + ) + .getUserLastUpdated(user); + } + } + } + + uint256 userUnclaimedRewards; + if (address(0) != address(incentivesController)) { + userUnclaimedRewards = incentivesController.getUserUnclaimedRewards(user); + } + + return (userReservesData, userUnclaimedRewards); + } + function getReservesData(ILendingPoolAddressesProvider provider, address user) external view @@ -200,12 +379,12 @@ contract UiPoolDataProvider is IUiPoolDataProvider { } } - IncentivesControllerData memory incentivesControllerData; if (address(0) != address(incentivesController)) { if (user != address(0)) { - incentivesControllerData.userUnclaimedRewards = incentivesController.getUserUnclaimedRewards(user); + incentivesControllerData.userUnclaimedRewards = incentivesController + .getUserUnclaimedRewards(user); } incentivesControllerData.emissionEndTimestamp = incentivesController.DISTRIBUTION_END(); } diff --git a/contracts/misc/interfaces/IUiPoolDataProvider.sol b/contracts/misc/interfaces/IUiPoolDataProvider.sol index 15c9bd07..998e79ed 100644 --- a/contracts/misc/interfaces/IUiPoolDataProvider.sol +++ b/contracts/misc/interfaces/IUiPoolDataProvider.sol @@ -73,7 +73,29 @@ interface IUiPoolDataProvider { uint256 emissionEndTimestamp; } + function getReservesList(ILendingPoolAddressesProvider provider) + external + view + returns (address[] memory); + function getSimpleReservesData(ILendingPoolAddressesProvider provider) + external + view + returns ( + AggregatedReserveData[] memory, + uint256, // usd price eth + uint256 // emission end timestamp + ); + + function getUserReservesData(ILendingPoolAddressesProvider provider, address user) + external + view + returns ( + UserReserveData[] memory, + uint256 // user unclaimed rewards + ); + + // generic method with full data function getReservesData(ILendingPoolAddressesProvider provider, address user) external view From 7849be11f5d97ce104e0ca82fc9a0d05ddc7a76e Mon Sep 17 00:00:00 2001 From: andyk Date: Thu, 17 Jun 2021 17:01:19 +0300 Subject: [PATCH 2/4] fix params order in getAssetData --- contracts/misc/UiPoolDataProvider.sol | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contracts/misc/UiPoolDataProvider.sol b/contracts/misc/UiPoolDataProvider.sol index 8eff3e4e..86876aab 100644 --- a/contracts/misc/UiPoolDataProvider.sol +++ b/contracts/misc/UiPoolDataProvider.sol @@ -138,21 +138,21 @@ contract UiPoolDataProvider is IUiPoolDataProvider { // incentives if (address(0) != address(incentivesController)) { ( + reserveData.aTokenIncentivesIndex, reserveData.aEmissionPerSecond, - reserveData.aIncentivesLastUpdateTimestamp, - reserveData.aTokenIncentivesIndex + reserveData.aIncentivesLastUpdateTimestamp ) = incentivesController.getAssetData(reserveData.aTokenAddress); ( + reserveData.sTokenIncentivesIndex, reserveData.sEmissionPerSecond, - reserveData.sIncentivesLastUpdateTimestamp, - reserveData.sTokenIncentivesIndex + reserveData.sIncentivesLastUpdateTimestamp ) = incentivesController.getAssetData(reserveData.stableDebtTokenAddress); ( + reserveData.vTokenIncentivesIndex, reserveData.vEmissionPerSecond, - reserveData.vIncentivesLastUpdateTimestamp, - reserveData.vTokenIncentivesIndex + reserveData.vIncentivesLastUpdateTimestamp ) = incentivesController.getAssetData(reserveData.variableDebtTokenAddress); } } From d51ac1d6fea00bb49ed850390c504c58ebe1172e Mon Sep 17 00:00:00 2001 From: andyk Date: Wed, 14 Jul 2021 15:56:41 +0200 Subject: [PATCH 3/4] temp fix for UIPoolDataProvider on polygon --- .../interfaces/IAaveIncentivesController.sol | 15 +++++++++++++++ contracts/misc/UiPoolDataProvider.sol | 18 ++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/contracts/interfaces/IAaveIncentivesController.sol b/contracts/interfaces/IAaveIncentivesController.sol index bae75967..0006e31e 100644 --- a/contracts/interfaces/IAaveIncentivesController.sol +++ b/contracts/interfaces/IAaveIncentivesController.sol @@ -30,6 +30,21 @@ interface IAaveIncentivesController { uint256 ); + /* + * LEGACY ************************** + * @dev Returns the configuration of the distribution for a certain asset + * @param asset The address of the reference asset of the distribution + * @return The asset index, the emission per second and the last updated timestamp + **/ + function assets(address asset) + external + view + returns ( + uint128, + uint128, + uint256 + ); + /** * @dev Whitelists an address to claim the rewards on behalf of another address * @param user The address of the user diff --git a/contracts/misc/UiPoolDataProvider.sol b/contracts/misc/UiPoolDataProvider.sol index 86876aab..5be19954 100644 --- a/contracts/misc/UiPoolDataProvider.sol +++ b/contracts/misc/UiPoolDataProvider.sol @@ -141,19 +141,22 @@ contract UiPoolDataProvider is IUiPoolDataProvider { reserveData.aTokenIncentivesIndex, reserveData.aEmissionPerSecond, reserveData.aIncentivesLastUpdateTimestamp - ) = incentivesController.getAssetData(reserveData.aTokenAddress); + // ) = incentivesController.getAssetData(reserveData.aTokenAddress); TODO: temp fix + ) = incentivesController.assets(reserveData.aTokenAddress); ( reserveData.sTokenIncentivesIndex, reserveData.sEmissionPerSecond, reserveData.sIncentivesLastUpdateTimestamp - ) = incentivesController.getAssetData(reserveData.stableDebtTokenAddress); + // ) = incentivesController.getAssetData(reserveData.stableDebtTokenAddress); TODO: temp fix + ) = incentivesController.assets(reserveData.stableDebtTokenAddress); ( reserveData.vTokenIncentivesIndex, reserveData.vEmissionPerSecond, reserveData.vIncentivesLastUpdateTimestamp - ) = incentivesController.getAssetData(reserveData.variableDebtTokenAddress); + // ) = incentivesController.getAssetData(reserveData.variableDebtTokenAddress); TODO: temp fix + ) = incentivesController.assets(reserveData.variableDebtTokenAddress); } } @@ -315,19 +318,22 @@ contract UiPoolDataProvider is IUiPoolDataProvider { reserveData.aTokenIncentivesIndex, reserveData.aEmissionPerSecond, reserveData.aIncentivesLastUpdateTimestamp - ) = incentivesController.getAssetData(reserveData.aTokenAddress); + // ) = incentivesController.getAssetData(reserveData.aTokenAddress); TODO: temp fix + ) = incentivesController.assets(reserveData.aTokenAddress); ( reserveData.sTokenIncentivesIndex, reserveData.sEmissionPerSecond, reserveData.sIncentivesLastUpdateTimestamp - ) = incentivesController.getAssetData(reserveData.stableDebtTokenAddress); + // ) = incentivesController.getAssetData(reserveData.stableDebtTokenAddress); TODO: temp fix + ) = incentivesController.assets(reserveData.stableDebtTokenAddress); ( reserveData.vTokenIncentivesIndex, reserveData.vEmissionPerSecond, reserveData.vIncentivesLastUpdateTimestamp - ) = incentivesController.getAssetData(reserveData.variableDebtTokenAddress); + // ) = incentivesController.getAssetData(reserveData.variableDebtTokenAddress); TODO: temp fix + ) = incentivesController.assets(reserveData.variableDebtTokenAddress); } if (user != address(0)) { From 025a988a13208f3a84b1ed926465fb4f5a1e3fe2 Mon Sep 17 00:00:00 2001 From: andyk Date: Mon, 26 Jul 2021 18:06:36 +0200 Subject: [PATCH 4/4] add poligon related temp hacks --- contracts/misc/UiPoolDataProvider.sol | 14 +++++++------- contracts/misc/interfaces/IUiPoolDataProvider.sol | 2 ++ helper-hardhat-config.ts | 5 ++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/contracts/misc/UiPoolDataProvider.sol b/contracts/misc/UiPoolDataProvider.sol index 5be19954..e9f40fe5 100644 --- a/contracts/misc/UiPoolDataProvider.sol +++ b/contracts/misc/UiPoolDataProvider.sol @@ -25,7 +25,7 @@ contract UiPoolDataProvider is IUiPoolDataProvider { using UserConfiguration for DataTypes.UserConfigurationMap; address public constant MOCK_USD_ADDRESS = 0x10F7Fc1F91Ba351f9C629c5947AD69bD03C05b96; - IAaveIncentivesController public immutable incentivesController; + IAaveIncentivesController public immutable override incentivesController; IPriceOracleGetter public immutable oracle; constructor(IAaveIncentivesController _incentivesController, IPriceOracleGetter _oracle) public { @@ -138,23 +138,23 @@ contract UiPoolDataProvider is IUiPoolDataProvider { // incentives if (address(0) != address(incentivesController)) { ( - reserveData.aTokenIncentivesIndex, reserveData.aEmissionPerSecond, - reserveData.aIncentivesLastUpdateTimestamp + reserveData.aIncentivesLastUpdateTimestamp, + reserveData.aTokenIncentivesIndex // ) = incentivesController.getAssetData(reserveData.aTokenAddress); TODO: temp fix ) = incentivesController.assets(reserveData.aTokenAddress); ( - reserveData.sTokenIncentivesIndex, reserveData.sEmissionPerSecond, - reserveData.sIncentivesLastUpdateTimestamp + reserveData.sIncentivesLastUpdateTimestamp, + reserveData.sTokenIncentivesIndex // ) = incentivesController.getAssetData(reserveData.stableDebtTokenAddress); TODO: temp fix ) = incentivesController.assets(reserveData.stableDebtTokenAddress); ( - reserveData.vTokenIncentivesIndex, reserveData.vEmissionPerSecond, - reserveData.vIncentivesLastUpdateTimestamp + reserveData.vIncentivesLastUpdateTimestamp, + reserveData.vTokenIncentivesIndex // ) = incentivesController.getAssetData(reserveData.variableDebtTokenAddress); TODO: temp fix ) = incentivesController.assets(reserveData.variableDebtTokenAddress); } diff --git a/contracts/misc/interfaces/IUiPoolDataProvider.sol b/contracts/misc/interfaces/IUiPoolDataProvider.sol index 998e79ed..db7f3093 100644 --- a/contracts/misc/interfaces/IUiPoolDataProvider.sol +++ b/contracts/misc/interfaces/IUiPoolDataProvider.sol @@ -78,6 +78,8 @@ interface IUiPoolDataProvider { view returns (address[] memory); + function incentivesController() external view returns (IAaveIncentivesController); + function getSimpleReservesData(ILendingPoolAddressesProvider provider) external view diff --git a/helper-hardhat-config.ts b/helper-hardhat-config.ts index 3df38000..2b1e9d01 100644 --- a/helper-hardhat-config.ts +++ b/helper-hardhat-config.ts @@ -47,7 +47,10 @@ export const NETWORKS_RPC_URL: iParamsPerNetwork = { [eEthereumNetwork.buidlerevm]: 'http://localhost:8545', [eEthereumNetwork.tenderlyMain]: `https://rpc.tenderly.co/fork/${TENDERLY_FORK_ID}`, [ePolygonNetwork.mumbai]: 'https://rpc-mumbai.maticvigil.com', - [ePolygonNetwork.matic]: 'https://rpc-mainnet.matic.network', + [ePolygonNetwork.matic]: + // 'https://rpc-mainnet.maticvigil.com/v1/e616b9ddc7598ffae92629f8145614d55094c722', + 'https://polygon-mainnet.g.alchemy.com/v2/6NUmfWDZw6lC3RPAphj0p_2vm7ElOn2U', + // [ePolygonNetwork.matic]: 'https://rpc-mainnet.matic.network', [eXDaiNetwork.xdai]: 'https://rpc.xdaichain.com/', };