Skip to content

Commit

Permalink
Add contract and function description comments
Browse files Browse the repository at this point in the history
  • Loading branch information
EridianAlpha committed Jun 21, 2024
1 parent ff0c5c9 commit eeaaca0
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 52 deletions.
148 changes: 120 additions & 28 deletions src/AavePM.sol

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/FunctionChecks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ import {IAavePM} from "./interfaces/IAavePM.sol";
/// @author EridianAlpha
/// @notice This contract contains the functions used inside AavePM modifiers.
contract FunctionChecks {
/// @notice // TODO: Add comment
/// @notice Check if the caller has the `OWNER_ROLE`.
/// @dev This function checks if the caller has the `OWNER_ROLE` and reverts if it does not.
/// @param _owner The address to check if it has the `OWNER_ROLE`.
function _checkOwner(address _owner) internal view {
if (!IAavePM(address(this)).hasRole(keccak256("OWNER_ROLE"), _owner)) {
revert IAavePM.AavePM__AddressNotAnOwner();
}
}

/// @notice // TODO: Add comment
/// @notice Check if manager invocations are within the daily limit.
/// @dev This function checks if the manager invocations are within the daily limit and reverts if the limit is reached.
/// @param managerInvocations The array of manager invocations.
function _checkManagerInvocationLimit(uint64[] memory managerInvocations) internal view {
// If the array is smaller than getManagerDailyInvocationLimit, return
if (managerInvocations.length < IAavePM(address(this)).getManagerDailyInvocationLimit()) return;
Expand Down
57 changes: 48 additions & 9 deletions src/modules/AaveFunctionsModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ contract AaveFunctionsModule is IAaveFunctionsModule {
// ================================================================

/// @notice Deposit all wstETH into Aave.
/// // TODO: Update comment.
/// @dev This function is used to deposit all wstETH into Aave.
/// @param aavePoolAddress The address of the Aave pool contract.
/// @param tokenAddress The address of the token to deposit.
/// @param tokenBalance The contract balance of the token to deposit.
function aaveSupply(address aavePoolAddress, address tokenAddress, uint256 tokenBalance) public onlyAavePM {
// Takes all tokens in the contract and deposits it into Aave
TransferHelper.safeApprove(tokenAddress, aavePoolAddress, tokenBalance);
IPool(aavePoolAddress).deposit(tokenAddress, tokenBalance, address(this), 0);
}

/// @notice Withdraw wstETH from Aave.
/// // TODO: Update comment.
/// @dev This function is used to withdraw wstETH from Aave.
/// @param aavePoolAddress The address of the Aave pool contract.
/// @param tokenAddress The address of the token to withdraw.
/// @param withdrawAmount The amount of the token to withdraw.
function aaveWithdrawCollateral(address aavePoolAddress, address tokenAddress, uint256 withdrawAmount)
public
onlyAavePM
Expand All @@ -74,22 +80,35 @@ contract AaveFunctionsModule is IAaveFunctionsModule {
}

/// @notice Borrow USDC from Aave.
/// // TODO: Update comment.
/// @dev This function is used to borrow USDC from Aave.
/// @param aavePoolAddress The address of the Aave pool contract.
/// @param tokenAddress The address of the token to borrow.
/// @param borrowAmount The amount of USDC to borrow. 8 decimal places to the dollar. e.g. 100000000 = $1.00.
function aaveBorrow(address aavePoolAddress, address tokenAddress, uint256 borrowAmount) public onlyAavePM {
IPool(aavePoolAddress).borrow(tokenAddress, borrowAmount, 2, 0, address(this));
}

/// @notice Repay USDC debt to Aave.
/// // TODO: Update comment.
/// @dev This function is used to repay USDC debt to Aave.
/// @param aavePoolAddress The address of the Aave pool contract.
/// @param tokenAddress The address of the token to repay.
/// @param repayAmount The amount of USDC to repay. 8 decimal places to the dollar. e.g. 100000000 = $1.00.
function aaveRepayDebt(address aavePoolAddress, address tokenAddress, uint256 repayAmount) public onlyAavePM {
TransferHelper.safeApprove(tokenAddress, aavePoolAddress, repayAmount);
IPool(aavePoolAddress).repay(tokenAddress, repayAmount, 2, address(this));
}

/// @notice // TODO: Add comment.
/// @notice Getter function to get the current position values.
/// @dev This function is used to avoid code duplication in the Reinvest and Rebalance contracts.
/// @param aavePM The Aave Position Manager contract.
/// @return initialCollateralBase The initial collateral in USD base unit with 8 decimals to the dollar.
/// @return totalDebtBase The total debt in USD base unit with 8 decimals to the dollar.
/// @return currentLiquidationThreshold The current liquidation threshold.
/// @return initialHealthFactorScaled The initial health factor scaled to 2 decimal places.
/// @return healthFactorTarget The health factor target.
/// @return aavePoolAddress The address of the Aave pool contract.
/// @return wstETHAddress The address of the wstETH token.
/// @return usdcAddress The address of the USDC token.
function getCurrentPositionValues(IAavePM aavePM)
public
view
Expand Down Expand Up @@ -136,7 +155,14 @@ contract AaveFunctionsModule is IAaveFunctionsModule {
);
}

/// @notice // TODO: Add comment.
/// @notice Check if the health factor is above the minimum.
/// @dev This function is used to check if the health factor is above the minimum.
/// @return totalCollateralBase The total collateral in USD base unit with 8 decimals to the dollar.
/// @return totalDebtBase The total debt in USD base unit with 8 decimals to the dollar.
/// @return availableBorrowsBase The available borrows in USD base unit with 8 decimals to the dollar.
/// @return currentLiquidationThreshold The current liquidation threshold.
/// @return ltv The loan to value ratio.
/// @return healthFactor The health factor.
function checkHealthFactorAboveMinimum()
public
view
Expand Down Expand Up @@ -164,7 +190,13 @@ contract AaveFunctionsModule is IAaveFunctionsModule {
(totalCollateralBase, totalDebtBase, availableBorrowsBase, currentLiquidationThreshold, ltv, healthFactor);
}

/// @notice // TODO: Add comment.
/// @notice Getter function to get the total collateral delta.
/// @dev This function is used to calculate the total collateral delta.
/// @param totalCollateralBase The total collateral in USD base unit with 8 decimals to the dollar.
/// @param reinvestedDebtTotal The reinvested debt total in USD base unit with 8 decimals to the dollar.
/// @param suppliedCollateralTotal The supplied collateral total in USD base unit with 8 decimals to the dollar.
/// @return delta The total collateral delta.
/// @return isPositive A boolean to indicate if the delta is positive.
function getTotalCollateralDelta(
uint256 totalCollateralBase,
uint256 reinvestedDebtTotal,
Expand All @@ -181,7 +213,9 @@ contract AaveFunctionsModule is IAaveFunctionsModule {
return (delta, isPositive);
}

/// @notice // TODO: Add comment
/// @notice Convert existing balance to wstETH and supply to Aave.
/// @dev This function is used to convert the existing balance to wstETH and supply to Aave.
/// @return suppliedCollateral The amount of wstETH supplied to Aave.
function convertExistingBalanceToWstETHAndSupplyToAave() public onlyAavePM returns (uint256 suppliedCollateral) {
IAavePM aavePM = IAavePM(address(this));
address aavePoolAddress = aavePM.getContractAddress("aavePool");
Expand Down Expand Up @@ -232,7 +266,12 @@ contract AaveFunctionsModule is IAaveFunctionsModule {
}
}

/// @notice // TODO: Add comment
/// @notice Calculate the minimum amount of tokens received from a Uniswap V3 swap.
/// @dev This function is used to calculate the minimum amount of tokens received from a Uniswap V3 swap.
/// @param totalCollateralBase The total collateral in USD base unit with 8 decimals to the dollar.
/// @param totalDebtBase The total debt in USD base unit with 8 decimals to the dollar.
/// @param currentLiquidationThreshold The current liquidation threshold.
/// @param healthFactorTarget The health factor target.
function calculateMaxBorrowUSDC(
uint256 totalCollateralBase,
uint256 totalDebtBase,
Expand Down
15 changes: 13 additions & 2 deletions src/modules/BorrowAndWithdrawUSDCModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ contract BorrowAndWithdrawUSDCModule is IBorrowAndWithdrawUSDCModule {
// │ MODULE FUNCTIONS │
// ================================================================

/// @notice // TODO: Add comment
/// @notice Borrow USDC from the Aave protocol and withdraw it to the specified owner.
/// @dev This function borrows USDC from the Aave protocol and withdraws it to the specified owner.
/// @param borrowAmountUSDC The amount of USDC to borrow.
/// @param _owner The address to withdraw the USDC to.
/// @return repaidReinvestedDebt The amount of reinvested debt repaid (if any) to increase the Health Factor.
function borrowAndWithdrawUSDC(uint256 borrowAmountUSDC, address _owner)
public
onlyAavePM
Expand Down Expand Up @@ -101,7 +105,14 @@ contract BorrowAndWithdrawUSDCModule is IBorrowAndWithdrawUSDCModule {
return (repaidReinvestedDebt);
}

/// @notice // TODO: Add comment
/// @notice Calculate the amount of reinvested debt to repay to increase the Health Factor.
/// @dev This function calculates the amount of reinvested debt to repay to increase the Health Factor.
/// @param totalCollateralBase The total collateral base in the Aave pool.
/// @param totalDebtBase The total debt base in the Aave pool.
/// @param currentLiquidationThreshold The current liquidation threshold in the Aave pool.
/// @param borrowAmountUSDC The amount of USDC to borrow.
/// @param healthFactorTarget The target Health Factor.
/// @return repaidReinvestedDebt The amount of reinvested debt repaid to increase the Health Factor.
function _borrowCalculation(
uint256 totalCollateralBase,
uint256 totalDebtBase,
Expand Down
10 changes: 9 additions & 1 deletion src/modules/RebalanceModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ contract RebalanceModule is IRebalanceModule {
return (repaymentAmountUSDC);
}

/// @notice // TODO: Add comment
/// @notice Repay debt to increase the health factor.
/// @dev This function repays debt to increase the health factor.
/// @param totalDebtBase The total debt in base units.
/// @param aavePoolAddress The address of the Aave pool.
/// @param usdcAddress The address of the USDC token.
/// @param totalCollateralBase The total collateral in base units.
/// @param currentLiquidationThreshold The current liquidation threshold.
/// @param healthFactorTarget The target health factor.
/// @return repaymentAmountUSDC The amount of USDC repaid.
function _repayDebt(
uint256 totalDebtBase,
address aavePoolAddress,
Expand Down
16 changes: 14 additions & 2 deletions src/modules/ReinvestModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ contract ReinvestModule is IReinvestModule {
// │ MODULE FUNCTIONS │
// ================================================================

/// @notice // TODO: Add comment
/// @notice Reinvest the Aave position.
/// @dev This function reinvests the Aave position by borrowing USDC, swapping to wstETH, and supplying it back to Aave.
/// @return reinvestedDebt The amount of debt reinvested.
function reinvest() public onlyAavePM returns (uint256 reinvestedDebt) {
IAavePM aavePM = IAavePM(address(this));

Expand Down Expand Up @@ -106,7 +108,17 @@ contract ReinvestModule is IReinvestModule {
return (reinvestedDebt);
}

/// @notice // TODO: Add comment
/// @notice Reinvest the Aave position.
/// @dev This function actions the reinvestment of the Aave position.
/// @param aavePM The Aave Position Manager contract.
/// @param totalDebtBase The total debt in base units.
/// @param aavePoolAddress The address of the Aave pool.
/// @param usdcAddress The address of the USDC token.
/// @param wstETHAddress The address of the wstETH token.
/// @param initialCollateralBase The initial collateral in base units.
/// @param currentLiquidationThreshold The current liquidation threshold.
/// @param healthFactorTarget The target health factor.
/// @return borrowAmountUSDC The amount of USDC borrowed.
function _reinvestAction(
IAavePM aavePM,
uint256 totalDebtBase,
Expand Down
32 changes: 25 additions & 7 deletions src/modules/TokenSwapsModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ contract TokenSwapsModule is ITokenSwapsModule {
// │ MODULE FUNCTIONS │
// ================================================================

/// @notice Swaps the contract's entire specified token balance using a UniswapV3 pool.
/// @dev Calculates the minimum amount that should be received based on the current pool's price ratio and a predefined slippage tolerance.
/// Reverts if there are no tokens in the contract or if the transaction doesn't meet the `amountOutMinimum` criteria due to price movements.
/// @notice Swaps the entire specified token balance of the contract using a UniswapV3 pool.
/// @dev Calculates the minimum amount that should be received based on the
/// current pool price ratio and a predefined slippage tolerance.
/// Reverts if there are no tokens in the contract or if the transaction does not
/// meet the `amountOutMinimum` criteria due to price movements.
/// @param _uniswapV3PoolIdentifier The identifier of the UniswapV3 pool to use for the swap.
/// @param _tokenInIdentifier The identifier of the token to swap.
/// @param _tokenOutIdentifier The identifier of the token to receive from the swap.
Expand Down Expand Up @@ -105,7 +107,12 @@ contract TokenSwapsModule is ITokenSwapsModule {
return (_tokenOutIdentifier, approveAndExecuteSwap(aavePM, params, currentBalance));
}

/// @notice // TODO: Add comment
/// @notice Approves and executes the swap using the UniswapV3 router.
/// @dev Approves the swapRouter to spend the tokenIn and executes the swap.
/// @param aavePM The Aave Position Manager contract.
/// @param params The swap parameters.
/// @param currentBalance The current balance of the token to swap.
/// @return amountOut The amount of tokens received from the swap.
function approveAndExecuteSwap(
IAavePM aavePM,
ISwapRouter.ExactInputSingleParams memory params,
Expand All @@ -118,7 +125,8 @@ contract TokenSwapsModule is ITokenSwapsModule {
return amountOut;
}

/// @notice // TODO: Add comment
/// @notice Wraps all ETH in the contract to WETH.
/// @dev Wraps all ETH in the contract to WETH even if the amount is 0.
function wrapETHToWETH() public payable onlyAavePM {
IWETH9(IAavePM(address(this)).getTokenAddress("WETH")).deposit{value: address(this).balance}();
}
Expand All @@ -127,7 +135,14 @@ contract TokenSwapsModule is ITokenSwapsModule {
// │ FUNCTIONS - CALCULATIONS │
// ================================================================

/// @notice // TODO: Add comment
/// @notice Calculates the minimum amount of tokens to receive from a UniswapV3 swap.
/// @dev Uses the current pool price ratio and a predefined slippage tolerance to calculate the minimum amount.
/// @param aavePM The Aave Position Manager contract.
/// @param _currentBalance The current balance of the token to swap.
/// @param _uniswapV3PoolAddress The address of the UniswapV3 pool to use for the swap.
/// @param tokenInAddress The address of the token to swap.
/// @param tokenOutAddress The address of the token to receive from the swap.
/// @return minOut The minimum amount of tokens to receive from the swap.
function uniswapV3CalculateMinOut(
IAavePM aavePM,
uint256 _currentBalance,
Expand All @@ -153,7 +168,10 @@ contract TokenSwapsModule is ITokenSwapsModule {
return minOut = expectedOut - slippageTolerance;
}

/// @notice // TODO: Add comment
/// @notice Checks if the identifier is for ETH.
/// @dev Compares the identifier to the ETH identifier and returns true if they match.
/// @param identifier The identifier to check.
/// @return isETH True if the identifier is for ETH.
function _isIdentifierETH(string memory identifier) private pure returns (bool) {
return (keccak256(abi.encodePacked(identifier)) == keccak256(abi.encodePacked("ETH"))) ? true : false;
}
Expand Down
4 changes: 3 additions & 1 deletion test/testHelperContracts/AavePMUpgradeExample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/U

import {IAavePM} from "../../src/interfaces/IAavePM.sol";

/// @notice //TODO Add comment.
/// @title Aave Position Manager Upgrade Example
/// @author EridianAlpha
/// @notice This contract is an example of an upgradeable contract using the UUPS pattern.
contract AavePMUpgradeExample is Initializable, AccessControlUpgradeable, UUPSUpgradeable {
function test() public {} // Added to remove this whole testing file from coverage report.

Expand Down

0 comments on commit eeaaca0

Please sign in to comment.