AaveFunctionsModule

Git Source

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

NameTypeDescription
_aavePMProxyAddressaddressThe 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

NameTypeDescription
aavePoolAddressaddressThe address of the Aave pool contract.
tokenAddressaddressThe address of the token to deposit.
tokenBalanceuint256The 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

NameTypeDescription
aavePoolAddressaddressThe address of the Aave pool contract.
tokenAddressaddressThe address of the token to withdraw.
withdrawAmountuint256The 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

NameTypeDescription
aavePoolAddressaddressThe address of the Aave pool contract.
tokenAddressaddressThe address of the token to borrow.
borrowAmountuint256The 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

NameTypeDescription
aavePoolAddressaddressThe address of the Aave pool contract.
tokenAddressaddressThe address of the token to repay.
repayAmountuint256The 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

NameTypeDescription
aavePMIAavePMThe Aave Position Manager contract.

Returns

NameTypeDescription
initialCollateralBaseuint256The initial collateral in USD base unit with 8 decimals to the dollar.
totalDebtBaseuint256The total debt in USD base unit with 8 decimals to the dollar.
currentLiquidationThresholduint256The current liquidation threshold.
initialHealthFactorScaleduint256The initial health factor scaled to 2 decimal places.
healthFactorTargetuint16The health factor target.
aavePoolAddressaddressThe address of the Aave pool contract.
wstETHAddressaddressThe address of the wstETH token.
usdcAddressaddressThe 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

NameTypeDescription
totalCollateralBaseuint256The total collateral in USD base unit with 8 decimals to the dollar.
totalDebtBaseuint256The total debt in USD base unit with 8 decimals to the dollar.
availableBorrowsBaseuint256The available borrows in USD base unit with 8 decimals to the dollar.
currentLiquidationThresholduint256The current liquidation threshold.
ltvuint256The loan to value ratio.
healthFactoruint256The 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

NameTypeDescription
totalCollateralBaseuint256The total collateral in USD base unit with 8 decimals to the dollar.
reinvestedDebtTotaluint256The reinvested debt total in USD base unit with 8 decimals to the dollar.
suppliedCollateralTotaluint256The supplied collateral total in USD base unit with 8 decimals to the dollar.

Returns

NameTypeDescription
deltauint256The total collateral delta.
isPositiveboolA 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

NameTypeDescription
suppliedCollateraluint256The 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

NameTypeDescription
totalCollateralBaseuint256The total collateral in USD base unit with 8 decimals to the dollar.
totalDebtBaseuint256The total debt in USD base unit with 8 decimals to the dollar.
currentLiquidationThresholduint256The current liquidation threshold.
healthFactorTargetuint16The health factor target.