Skip to content

Commit

Permalink
chore: updated batchRequest constructor batching to fetch aave apy da…
Browse files Browse the repository at this point in the history
…ta points
  • Loading branch information
0xNilesh committed May 23, 2024
1 parent ffd73fd commit 0936fd6
Show file tree
Hide file tree
Showing 8 changed files with 637 additions and 54 deletions.
48 changes: 48 additions & 0 deletions src/contracts/BatchAaveV2DataRequest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Data, DataTypes, IIncentivesController, ILendingPool} from '../interfaces/aave-v2/IAaveV2.sol';

/**
* To calculate deposit apy/apr from this data
* https://docs.aave.com/developers/v/2.0/guides/apy-and-apr#compute-data
*/
contract BatchAaveV2DataRequest {
// This contract is used to fetch apy data points from aave
constructor(ILendingPool _lendingPool, IIncentivesController _incentivesController, 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 = _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);

_returnData[i] = Data({
currentLiquidityRate: currentLiquidityRate,
aTokenAddress: aTokenAddress,
aEmissionPerSecond: aEmissionPerSecond
});
}

// 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)
}
}
}
35 changes: 0 additions & 35 deletions src/contracts/BatchRequest.sol

This file was deleted.

19 changes: 0 additions & 19 deletions src/contracts/PoolManager.sol

This file was deleted.

53 changes: 53 additions & 0 deletions src/interfaces/aave-v2/DataTypes.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.8.23;

library DataTypes {
// refer to the whitepaper, section 1.1 basic concepts for a formal description of these properties.
struct ReserveData {
//stores the reserve configuration
ReserveConfigurationMap configuration;
//the liquidity index. Expressed in ray
uint128 liquidityIndex;
//variable borrow index. Expressed in ray
uint128 variableBorrowIndex;
//the current supply rate. Expressed in ray
uint128 currentLiquidityRate;
//the current variable borrow rate. Expressed in ray
uint128 currentVariableBorrowRate;
//the current stable borrow rate. Expressed in ray
uint128 currentStableBorrowRate;
uint40 lastUpdateTimestamp;
//tokens addresses
address aTokenAddress;
address stableDebtTokenAddress;
address variableDebtTokenAddress;
//address of the interest rate strategy
address interestRateStrategyAddress;
//the id of the reserve. Represents the position in the list of the active reserves
uint8 id;
}

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-63: reserved
//bit 64-79: reserve factor
uint256 data;
}

struct UserConfigurationMap {
uint256 data;
}

enum InterestRateMode {

Check warning on line 48 in src/interfaces/aave-v2/DataTypes.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function order is incorrect, enum definition can not go after struct definition (line 44)
NONE,
STABLE,
VARIABLE
}
}
12 changes: 12 additions & 0 deletions src/interfaces/aave-v2/IAaveV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.8.23;

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

Check warning on line 4 in src/interfaces/aave-v2/IAaveV2.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "DataTypes" is unused
import {IIncentivesController} from './IIncentivesController.sol';

Check warning on line 5 in src/interfaces/aave-v2/IAaveV2.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "IIncentivesController" is unused
import {ILendingPool} from './ILendingPool.sol';

Check warning on line 6 in src/interfaces/aave-v2/IAaveV2.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "ILendingPool" is unused

struct Data {
uint256 currentLiquidityRate;
address aTokenAddress;
uint256 aEmissionPerSecond;
}
59 changes: 59 additions & 0 deletions src/interfaces/aave-v2/IIncentivesController.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

interface IIncentivesController {
event AssetConfigUpdated(address indexed asset, uint256 emission);
event AssetIndexUpdated(address indexed asset, uint256 index);
event ClaimerSet(address indexed user, address indexed claimer);
event DistributionEndUpdated(uint256 newDistributionEnd);
event RewardsAccrued(address indexed user, uint256 amount);
event RewardsClaimed(address indexed user, address indexed to, address indexed claimer, uint256 amount);
event UserIndexUpdated(address indexed user, address indexed asset, uint256 index);

function DISTRIBUTION_END() external view returns (uint256);

Check warning on line 13 in src/interfaces/aave-v2/IIncentivesController.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function name must be in mixedCase

function EMISSION_MANAGER() external view returns (address);

Check warning on line 15 in src/interfaces/aave-v2/IIncentivesController.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function name must be in mixedCase

function PRECISION() external view returns (uint8);

Check warning on line 17 in src/interfaces/aave-v2/IIncentivesController.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function name must be in mixedCase

function REVISION() external view returns (uint256);

Check warning on line 19 in src/interfaces/aave-v2/IIncentivesController.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function name must be in mixedCase

function REWARD_TOKEN() external view returns (address);

Check warning on line 21 in src/interfaces/aave-v2/IIncentivesController.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function name must be in mixedCase

function STAKE_TOKEN() external view returns (address);

Check warning on line 23 in src/interfaces/aave-v2/IIncentivesController.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Function name must be in mixedCase

function assets(address) external view returns (uint104 emissionPerSecond, uint104 index, uint40 lastUpdateTimestamp);

function claimRewards(address[] memory assets, uint256 amount, address to) external returns (uint256);

function claimRewardsOnBehalf(
address[] memory assets,
uint256 amount,
address user,
address to
) external returns (uint256);

function claimRewardsToSelf(address[] memory assets, uint256 amount) external returns (uint256);

function configureAssets(address[] memory assets, uint256[] memory emissionsPerSecond) external;

function getAssetData(address asset) external view returns (uint256, uint256, uint256);

function getClaimer(address user) external view returns (address);

function getDistributionEnd() external view returns (uint256);

function getRewardsBalance(address[] memory assets, address user) external view returns (uint256);

function getUserAssetData(address user, address asset) external view returns (uint256);

function getUserUnclaimedRewards(address _user) external view returns (uint256);

function handleAction(address user, uint256 totalSupply, uint256 userBalance) external;

function initialize(address) external;

function setClaimer(address user, address caller) external;

function setDistributionEnd(uint256 distributionEnd) external;
}
Loading

0 comments on commit 0936fd6

Please sign in to comment.