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

feat: added batch aave v3 data request for apy data points #7

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sort_imports = true
solc_version = '0.8.23'
libs = ['node_modules', 'lib']
optimizer_runs = 10_000
optimizer = false

[profile.optimized]
via_ir = true
Expand Down
8 changes: 3 additions & 5 deletions src/contracts/BatchAaveV2DataRequest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ contract BatchAaveV2DataRequest {

for (uint256 i = 0; i < _assets.length; i++) {
DataTypes.ReserveData memory _reserveData = _lendingPool.getReserveData(_assets[i]);
uint256 currentLiquidityRate = _reserveData.currentLiquidityRate;
address aTokenAddress = _reserveData.aTokenAddress;

// asset is the ERC20 supplied or borrowed, eg. DAI, WETH
(, uint256 aEmissionPerSecond,) = _incentivesController.getAssetData(aTokenAddress);
(, uint256 aEmissionPerSecond,) = _incentivesController.getAssetData(_reserveData.aTokenAddress);

_returnData[i] = Data({
currentLiquidityRate: currentLiquidityRate,
aTokenAddress: aTokenAddress,
currentLiquidityRate: _reserveData.currentLiquidityRate,
aTokenAddress: _reserveData.aTokenAddress,
aEmissionPerSecond: aEmissionPerSecond
});
}
Expand Down
41 changes: 41 additions & 0 deletions src/contracts/BatchAaveV3DataRequest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Data, DataTypes, IPool, IPoolDataProvider} from '../interfaces/aave-v3/IAaveV3.sol';

Check warning on line 4 in src/contracts/BatchAaveV3DataRequest.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "IPoolDataProvider" is unused

/**
* To calculate deposit apy/apr from this data
* RAY = 10**27
* SECONDS_PER_YEAR = 31536000
* depositAPR = liquidityRate/RAY
* depositAPY = ((1 + (depositAPR / SECONDS_PER_YEAR)) ^ SECONDS_PER_YEAR) - 1
*/
contract BatchAaveV3DataRequest {
// This contract is used to fetch apy data points from aave
constructor(IPool _pool, address[] memory _assets) {
// create an array to store return data
Data[] memory _returnData = new Data[](_assets.length);

for (uint256 i = 0; i < _assets.length; i++) {
DataTypes.ReserveData memory _reserveData = _pool.getReserveData(_assets[i]);
_returnData[i] = Data({liquidityRate: _reserveData.currentLiquidityRate});
}

// encode return data
bytes memory _data = abi.encode(_returnData);

// force constructor to return data via assembly
assembly {
// abi.encode adds an additional offset (32 bytes) that we need to skip
let _dataStart := add(_data, 32)
// msize() gets the size of active memory in bytes.
// if we subtract msize() from _dataStart, the output will be
// the amount of bytes from _dataStart to the end of memory
// which due to how the data has been laid out in memory, will coincide with
// where our desired data ends.
let _dataEnd := sub(msize(), _dataStart)
// starting from _dataStart, get all the data in memory.
return(_dataStart, _dataEnd)
}
}
}
268 changes: 268 additions & 0 deletions src/interfaces/aave-v3/DataTypes.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.23;

library DataTypes {
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;
//the current variable borrow rate. Expressed in ray
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;
//stableDebtToken address
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 ReserveConfigurationMap {
//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 frozen
//bit 58: borrowing is enabled
//bit 59: stable rate borrowing enabled
//bit 60: asset is paused
//bit 61: borrowing in isolation mode is enabled
//bit 62: siloed borrowing enabled
//bit 63: flashloaning enabled
//bit 64-79: reserve factor
//bit 80-115 borrow cap in whole tokens, borrowCap == 0 => no cap
//bit 116-151 supply cap in whole tokens, supplyCap == 0 => no cap
//bit 152-167 liquidation protocol fee
//bit 168-175 eMode category
//bit 176-211 unbacked mint cap in whole tokens, unbackedMintCap == 0 => minting disabled
//bit 212-251 debt ceiling for isolation mode with (ReserveConfiguration::DEBT_CEILING_DECIMALS) decimals
//bit 252-255 unused
uint256 data;
}

struct UserConfigurationMap {
/**
* @dev Bitmap of the users collaterals and borrows. It is divided in pairs of bits, one pair per asset.
* The first bit indicates if an asset is used as collateral by the user, the second whether an
* asset is borrowed by the user.
*/
uint256 data;
}

struct EModeCategory {
// each eMode category has a custom ltv and liquidation threshold
uint16 ltv;
uint16 liquidationThreshold;
uint16 liquidationBonus;
// each eMode category may or may not have a custom oracle to override the individual assets price oracles
address priceSource;
string label;
}

enum InterestRateMode {
NONE,
STABLE,
VARIABLE
}

struct ReserveCache {
uint256 currScaledVariableDebt;
uint256 nextScaledVariableDebt;
uint256 currPrincipalStableDebt;
uint256 currAvgStableBorrowRate;
uint256 currTotalStableDebt;
uint256 nextAvgStableBorrowRate;
uint256 nextTotalStableDebt;
uint256 currLiquidityIndex;
uint256 nextLiquidityIndex;
uint256 currVariableBorrowIndex;
uint256 nextVariableBorrowIndex;
uint256 currLiquidityRate;
uint256 currVariableBorrowRate;
uint256 reserveFactor;
ReserveConfigurationMap reserveConfiguration;
address aTokenAddress;
address stableDebtTokenAddress;
address variableDebtTokenAddress;
uint40 reserveLastUpdateTimestamp;
uint40 stableDebtLastUpdateTimestamp;
}

struct ExecuteLiquidationCallParams {
uint256 reservesCount;
uint256 debtToCover;
address collateralAsset;
address debtAsset;
address user;
bool receiveAToken;
address priceOracle;
uint8 userEModeCategory;
address priceOracleSentinel;
}

struct ExecuteSupplyParams {
address asset;
uint256 amount;
address onBehalfOf;
uint16 referralCode;
}

struct ExecuteBorrowParams {
address asset;
address user;
address onBehalfOf;
uint256 amount;
InterestRateMode interestRateMode;
uint16 referralCode;
bool releaseUnderlying;
uint256 maxStableRateBorrowSizePercent;
uint256 reservesCount;
address oracle;
uint8 userEModeCategory;
address priceOracleSentinel;
}

struct ExecuteRepayParams {
address asset;
uint256 amount;
InterestRateMode interestRateMode;
address onBehalfOf;
bool useATokens;
}

struct ExecuteWithdrawParams {
address asset;
uint256 amount;
address to;
uint256 reservesCount;
address oracle;
uint8 userEModeCategory;
}

struct ExecuteSetUserEModeParams {
uint256 reservesCount;
address oracle;
uint8 categoryId;
}

struct FinalizeTransferParams {
address asset;
address from;
address to;
uint256 amount;
uint256 balanceFromBefore;
uint256 balanceToBefore;
uint256 reservesCount;
address oracle;
uint8 fromEModeCategory;
}

struct FlashloanParams {
address receiverAddress;
address[] assets;
uint256[] amounts;
uint256[] interestRateModes;
address onBehalfOf;
bytes params;
uint16 referralCode;
uint256 flashLoanPremiumToProtocol;
uint256 flashLoanPremiumTotal;
uint256 maxStableRateBorrowSizePercent;
uint256 reservesCount;
address addressesProvider;
uint8 userEModeCategory;
bool isAuthorizedFlashBorrower;
}

struct FlashloanSimpleParams {
address receiverAddress;
address asset;
uint256 amount;
bytes params;
uint16 referralCode;
uint256 flashLoanPremiumToProtocol;
uint256 flashLoanPremiumTotal;
}

struct FlashLoanRepaymentParams {
uint256 amount;
uint256 totalPremium;
uint256 flashLoanPremiumToProtocol;
address asset;
address receiverAddress;
uint16 referralCode;
}

struct CalculateUserAccountDataParams {
UserConfigurationMap userConfig;
uint256 reservesCount;
address user;
address oracle;
uint8 userEModeCategory;
}

struct ValidateBorrowParams {
ReserveCache reserveCache;
UserConfigurationMap userConfig;
address asset;
address userAddress;
uint256 amount;
InterestRateMode interestRateMode;
uint256 maxStableLoanPercent;
uint256 reservesCount;
address oracle;
uint8 userEModeCategory;
address priceOracleSentinel;
bool isolationModeActive;
address isolationModeCollateralAddress;
uint256 isolationModeDebtCeiling;
}

struct ValidateLiquidationCallParams {
ReserveCache debtReserveCache;
uint256 totalDebt;
uint256 healthFactor;
address priceOracleSentinel;
}

struct CalculateInterestRatesParams {
uint256 unbacked;
uint256 liquidityAdded;
uint256 liquidityTaken;
uint256 totalStableDebt;
uint256 totalVariableDebt;
uint256 averageStableBorrowRate;
uint256 reserveFactor;
address reserve;
address aToken;
}

struct InitReserveParams {
address asset;
address aTokenAddress;
address stableDebtAddress;
address variableDebtAddress;
address interestRateStrategyAddress;
uint16 reservesCount;
uint16 maxNumberReserves;
}
}
10 changes: 10 additions & 0 deletions src/interfaces/aave-v3/IAaveV3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.8.23;

import {DataTypes} from './DataTypes.sol';
import {IPool} from './IPool.sol';
import {IPoolDataProvider} from './IPoolDataProvider.sol';

struct Data {
uint256 liquidityRate; // to calc supply rate, deposit apr, deposit apy
}
Loading
Loading