AaveFunctionsModule
Inherits: IAaveFunctionsModule
Author: EridianAlpha
This contract contains the functions for AavePM to interact with the Aave protocol.
State Variables
VERSION
The version of the contract.
Contract is upgradeable so the version is a constant set on each implementation contract.
string public constant VERSION = "0.0.1";
aavePMProxyAddress
The address of the AavePM proxy contract.
The AavePM proxy address is set on deployment and is immutable.
address public immutable aavePMProxyAddress;
HFT_MINIMUM_BUFFER
The buffer for the Health Factor Target minimum calculation
uint16 public constant HFT_MINIMUM_BUFFER = 1;
Functions
constructor
Contract constructor to set the AavePM proxy address.
The AavePM proxy address is set on deployment and is immutable.
constructor(address _aavePMProxyAddress);
Parameters
Name | Type | Description |
---|---|---|
_aavePMProxyAddress | address | The address of the AavePM proxy contract. |
onlyAavePM
Modifier to check that only the AavePM contract is the caller.
Uses address(this)
since this contract is called by the AavePM contract using delegatecall.
modifier onlyAavePM();
aaveSupply
Deposit all wstETH into Aave.
This function is used to deposit all wstETH into Aave.
function aaveSupply(address aavePoolAddress, address tokenAddress, uint256 tokenBalance) public onlyAavePM;
Parameters
Name | Type | Description |
---|---|---|
aavePoolAddress | address | The address of the Aave pool contract. |
tokenAddress | address | The address of the token to deposit. |
tokenBalance | uint256 | The contract balance of the token to deposit. |
aaveWithdrawCollateral
Withdraw wstETH from Aave.
This function is used to withdraw wstETH from Aave.
function aaveWithdrawCollateral(address aavePoolAddress, address tokenAddress, uint256 withdrawAmount)
public
onlyAavePM;
Parameters
Name | Type | Description |
---|---|---|
aavePoolAddress | address | The address of the Aave pool contract. |
tokenAddress | address | The address of the token to withdraw. |
withdrawAmount | uint256 | The amount of the token to withdraw. |
aaveBorrow
Borrow USDC from Aave.
This function is used to borrow USDC from Aave.
function aaveBorrow(address aavePoolAddress, address tokenAddress, uint256 borrowAmount) public onlyAavePM;
Parameters
Name | Type | Description |
---|---|---|
aavePoolAddress | address | The address of the Aave pool contract. |
tokenAddress | address | The address of the token to borrow. |
borrowAmount | uint256 | The amount of USDC to borrow. 8 decimal places to the dollar. e.g. 100000000 = $1.00. |
aaveRepayDebt
Repay USDC debt to Aave.
This function is used to repay USDC debt to Aave.
function aaveRepayDebt(address aavePoolAddress, address tokenAddress, uint256 repayAmount) public onlyAavePM;
Parameters
Name | Type | Description |
---|---|---|
aavePoolAddress | address | The address of the Aave pool contract. |
tokenAddress | address | The address of the token to repay. |
repayAmount | uint256 | The amount of USDC to repay. 8 decimal places to the dollar. e.g. 100000000 = $1.00. |
getCurrentPositionValues
Getter function to get the current position values.
This function is used to avoid code duplication in the Reinvest and Rebalance contracts.
function getCurrentPositionValues(IAavePM aavePM)
public
view
returns (
uint256 initialCollateralBase,
uint256 totalDebtBase,
uint256 currentLiquidationThreshold,
uint256 initialHealthFactorScaled,
uint16 healthFactorTarget,
address aavePoolAddress,
address wstETHAddress,
address usdcAddress
);
Parameters
Name | Type | Description |
---|---|---|
aavePM | IAavePM | The Aave Position Manager contract. |
Returns
Name | Type | Description |
---|---|---|
initialCollateralBase | uint256 | The initial collateral in USD base unit with 8 decimals to the dollar. |
totalDebtBase | uint256 | The total debt in USD base unit with 8 decimals to the dollar. |
currentLiquidationThreshold | uint256 | The current liquidation threshold. |
initialHealthFactorScaled | uint256 | The initial health factor scaled to 2 decimal places. |
healthFactorTarget | uint16 | The health factor target. |
aavePoolAddress | address | The address of the Aave pool contract. |
wstETHAddress | address | The address of the wstETH token. |
usdcAddress | address | The address of the USDC token. |
checkHealthFactorAboveMinimum
Check if the health factor is above the minimum.
This function is used to check if the health factor is above the minimum.
function checkHealthFactorAboveMinimum()
public
view
returns (
uint256 totalCollateralBase,
uint256 totalDebtBase,
uint256 availableBorrowsBase,
uint256 currentLiquidationThreshold,
uint256 ltv,
uint256 healthFactor
);
Returns
Name | Type | Description |
---|---|---|
totalCollateralBase | uint256 | The total collateral in USD base unit with 8 decimals to the dollar. |
totalDebtBase | uint256 | The total debt in USD base unit with 8 decimals to the dollar. |
availableBorrowsBase | uint256 | The available borrows in USD base unit with 8 decimals to the dollar. |
currentLiquidationThreshold | uint256 | The current liquidation threshold. |
ltv | uint256 | The loan to value ratio. |
healthFactor | uint256 | The health factor. |
getTotalCollateralDelta
Getter function to get the total collateral delta.
This function is used to calculate the total collateral delta.
function getTotalCollateralDelta(
uint256 totalCollateralBase,
uint256 reinvestedDebtTotal,
uint256 suppliedCollateralTotal
) public pure returns (uint256 delta, bool isPositive);
Parameters
Name | Type | Description |
---|---|---|
totalCollateralBase | uint256 | The total collateral in USD base unit with 8 decimals to the dollar. |
reinvestedDebtTotal | uint256 | The reinvested debt total in USD base unit with 8 decimals to the dollar. |
suppliedCollateralTotal | uint256 | The supplied collateral total in USD base unit with 8 decimals to the dollar. |
Returns
Name | Type | Description |
---|---|---|
delta | uint256 | The total collateral delta. |
isPositive | bool | A boolean to indicate if the delta is positive. |
convertExistingBalanceToWstETHAndSupplyToAave
Convert existing balance to wstETH and supply to Aave.
This function is used to convert the existing balance to wstETH and supply to Aave.
function convertExistingBalanceToWstETHAndSupplyToAave() public onlyAavePM returns (uint256 suppliedCollateral);
Returns
Name | Type | Description |
---|---|---|
suppliedCollateral | uint256 | The amount of wstETH supplied to Aave. |
calculateMaxBorrowUSDC
Calculate the minimum amount of tokens received from a Uniswap V3 swap.
This function is used to calculate the minimum amount of tokens received from a Uniswap V3 swap.
function calculateMaxBorrowUSDC(
uint256 totalCollateralBase,
uint256 totalDebtBase,
uint256 currentLiquidationThreshold,
uint16 healthFactorTarget
) public pure returns (uint256 maxBorrowUSDC);
Parameters
Name | Type | Description |
---|---|---|
totalCollateralBase | uint256 | The total collateral in USD base unit with 8 decimals to the dollar. |
totalDebtBase | uint256 | The total debt in USD base unit with 8 decimals to the dollar. |
currentLiquidationThreshold | uint256 | The current liquidation threshold. |
healthFactorTarget | uint16 | The health factor target. |