Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: aave V3.3.0 upgrade #435

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 3 additions & 38 deletions contracts/interfaces/aaveV3/DataTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,8 @@
pragma solidity =0.8.24;

library DataTypes {
/// @dev Legacy reserve data
struct ReserveData {
//stores the reserve configuration
ReserveConfigurationMap configuration;
//the liquidity index. Expressed in ray
uint128 liquidityIndex;
//the current supply rate. Expressed in ray
uint128 currentLiquidityRate;
//variable borrow index. Expressed in ray
uint128 variableBorrowIndex;
// DEPRECATED on v3.2.0
uint128 currentVariableBorrowRate;
//the current stable borrow rate. Expressed in ray
uint128 currentStableBorrowRate;
//timestamp of last update
uint40 lastUpdateTimestamp;
//the id of the reserve. Represents the position in the list of the active reserves
uint16 id;
//aToken address
address aTokenAddress;
// DEPRECATED on v3.2.0
address stableDebtTokenAddress;
//variableDebtToken address
address variableDebtTokenAddress;
//address of the interest rate strategy
address interestRateStrategyAddress;
//the current treasury balance, scaled
uint128 accruedToTreasury;
//the outstanding unbacked aTokens minted through the bridging feature
uint128 unbacked;
//the outstanding debt borrowed against this asset in isolation mode
uint128 isolationModeTotalDebt;
}
struct ReserveDataExtended {
//stores the reserve configuration
ReserveConfigurationMap configuration;
//the liquidity index. Expressed in ray
Expand All @@ -45,17 +14,15 @@ library DataTypes {
uint128 variableBorrowIndex;
//the current variable borrow rate. Expressed in ray
uint128 currentVariableBorrowRate;
//the current stable borrow rate. Expressed in ray
// DEPRECATED on v3.2.0
uint128 currentStableBorrowRate;
//timestamp of last update
uint40 lastUpdateTimestamp;
//the id of the reserve. Represents the position in the list of the active reserves
uint16 id;
//timestamp in the future, until when liquidations are not allowed on the reserve
uint40 liquidationGracePeriodUntil;
//aToken address
address aTokenAddress;
//stableDebtToken address
// DEPRECATED on v3.2.0
address stableDebtTokenAddress;
//variableDebtToken address
address variableDebtTokenAddress;
Expand All @@ -67,8 +34,6 @@ library DataTypes {
uint128 unbacked;
//the outstanding debt borrowed against this asset in isolation mode
uint128 isolationModeTotalDebt;
//the amount of underlying accounted for by the protocol
uint128 virtualUnderlyingBalance;
}

struct ReserveConfigurationMap {
Expand Down
34 changes: 27 additions & 7 deletions contracts/interfaces/aaveV3/IPoolV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,6 @@ interface IPoolV3 {
**/
function getReserveData(address asset) external view returns (DataTypes.ReserveData memory);

/**
* @notice Returns the state and configuration of the reserve
* @param asset The address of the underlying asset of the reserve
* @return The state and configuration data of the reserve
**/
function getReserveDataExtended(address asset) external view returns (DataTypes.ReserveDataExtended memory);

/**
* @notice Validates and finalizes an aToken transfer
* @dev Only callable by the overlying aToken of the `asset`
Expand Down Expand Up @@ -778,4 +771,31 @@ interface IPoolV3 {
*/
function getEModeCategoryBorrowableBitmap(uint8 id) external view returns (uint128);

/**
* @notice Returns the current deficit of a reserve.
* @param asset The address of the underlying asset of the reserve
* @return The current deficit of the reserve
*/
function getReserveDeficit(address asset) external view returns (uint256);

/**
* @notice Returns the aToken address of a reserve.
* @param asset The address of the underlying asset of the reserve
* @return The address of the aToken
*/
function getReserveAToken(address asset) external view returns (address);

/**
* @notice Returns the variableDebtToken address of a reserve.
* @param asset The address of the underlying asset of the reserve
* @return The address of the variableDebtToken
*/
function getReserveVariableDebtToken(address asset) external view returns (address);

/**
* @notice Returns the virtual underlying balance of the reserve
* @param asset The address of the underlying asset of the reserve
* @return The reserve virtual underlying balance
*/
function getVirtualUnderlyingBalance(address asset) external view returns (uint128);
}
45 changes: 22 additions & 23 deletions contracts/views/AaveV3View.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ contract AaveV3View is AaveV3Helper, AaveV3RatioHelper {
_tokenAddresses[i]
);
uint256 collFactor = config.data & ~LTV_MASK;
DataTypes.ReserveData memory reserveData = lendingPool.getReserveData(
_tokenAddresses[i]
);
address aTokenAddr = reserveData.aTokenAddress;
address aTokenAddr = lendingPool.getReserveAToken(_tokenAddresses[i]);
address priceOracleAddress = IPoolAddressesProvider(_market).getPriceOracle();
uint256 price = IAaveV3Oracle(priceOracleAddress).getAssetPrice(_tokenAddresses[i]);
tokens[i] = TokenInfo({
Expand Down Expand Up @@ -539,45 +536,47 @@ contract AaveV3View is AaveV3Helper, AaveV3RatioHelper {
IPoolV3 lendingPool = getLendingPool(_market);
EstimatedRates[] memory estimatedRates = new EstimatedRates[](_reserveParams.length);
for (uint256 i = 0; i < _reserveParams.length; ++i) {
DataTypes.ReserveConfigurationMap memory config = lendingPool.getConfiguration(
_reserveParams[i].reserveAddress
);
DataTypes.ReserveDataExtended memory reserve = lendingPool.getReserveDataExtended(_reserveParams[i].reserveAddress);

EstimatedRates memory estimatedRate;
estimatedRate.reserveAddress = _reserveParams[i].reserveAddress;

uint256 totalVarDebt = IScaledBalanceToken(reserve.variableDebtTokenAddress).scaledTotalSupply().rayMul(_getNextVariableBorrowIndex(reserve));
address asset = _reserveParams[i].reserveAddress;
DataTypes.ReserveConfigurationMap memory config = lendingPool.getConfiguration(asset);
DataTypes.ReserveData memory reserve = lendingPool.getReserveData(asset);

uint256 totalVarDebt = IScaledBalanceToken(reserve.variableDebtTokenAddress)
.scaledTotalSupply()
.rayMul(_getNextVariableBorrowIndex(reserve));

if (_reserveParams[i].isDebtAsset) {
totalVarDebt += _reserveParams[i].liquidityTaken;
totalVarDebt = _reserveParams[i].liquidityAdded >= totalVarDebt ? 0
totalVarDebt = _reserveParams[i].liquidityAdded >= totalVarDebt
? 0
: totalVarDebt - _reserveParams[i].liquidityAdded;
}

(
estimatedRate.supplyRate,
estimatedRate.variableBorrowRate
) = IReserveInterestRateStrategy(reserve.interestRateStrategyAddress).calculateInterestRates(
(uint256 estimatedSupplyRate, uint256 estimatedVariableBorrowRate) = IReserveInterestRateStrategy(
reserve.interestRateStrategyAddress
).calculateInterestRates(
DataTypes.CalculateInterestRatesParams({
unbacked: reserve.unbacked,
unbacked: reserve.unbacked + lendingPool.getReserveDeficit(asset),
liquidityAdded: _reserveParams[i].liquidityAdded,
liquidityTaken: _reserveParams[i].liquidityTaken,
totalDebt: totalVarDebt,
reserveFactor: getReserveFactor(reserve.configuration),
reserve: _reserveParams[i].reserveAddress,
reserve: asset,
usingVirtualBalance: isReserveUsingVirtualAccounting(config),
virtualUnderlyingBalance: reserve.virtualUnderlyingBalance
virtualUnderlyingBalance: lendingPool.getVirtualUnderlyingBalance(asset)
})
);

estimatedRates[i] = estimatedRate;
estimatedRates[i] = EstimatedRates({
reserveAddress: asset,
supplyRate: estimatedSupplyRate,
variableBorrowRate: estimatedVariableBorrowRate
});
}

return estimatedRates;
}

function _getNextVariableBorrowIndex(DataTypes.ReserveDataExtended memory _reserve) internal view returns (uint128 variableBorrowIndex) {
function _getNextVariableBorrowIndex(DataTypes.ReserveData memory _reserve) internal view returns (uint128 variableBorrowIndex) {
uint256 scaledVariableDebt = IScaledBalanceToken(_reserve.variableDebtTokenAddress).scaledTotalSupply();
variableBorrowIndex = _reserve.variableBorrowIndex;
if (scaledVariableDebt > 0) {
Expand Down
1 change: 1 addition & 0 deletions contracts/views/LSVView.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct Position {
uint256 debt;
}

// TODO: On next deployment (assuming Aave v3.3.0 is live at that point), use 'getReserveAToken' and 'getReserveVariableDebtToken' directly on Pool, instead of fetching reserveData
contract LSVView is ActionsUtilHelper, UtilHelper, AaveV3Helper, MorphoAaveV3Helper, CompV3Helper, SparkHelper, MorphoBlueHelper, LSVUtilHelper {
enum Protocol {
AAVE_V3,
Expand Down