Skip to content

Commit

Permalink
Refactor healthFactor checks
Browse files Browse the repository at this point in the history
  • Loading branch information
EridianAlpha committed May 30, 2024
1 parent eabb3fe commit 11ab771
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions test/unit/AavePMRebalanceTest.t.sol
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {console} from "forge-std/Test.sol";
import {AavePMTestSetup} from "test/unit/AavePMTestSetupTest.t.sol";

import {AavePM} from "src/AavePM.sol";

import {IPool} from "@aave/aave-v3-core/contracts/interfaces/IPool.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

// ================================================================
// │ REBALANCE TESTS │
// ================================================================
contract RebalanceTests is AavePMTestSetup {
function test_Rebalance() public {
vm.startPrank(manager1);
// Send some ETH to the contract
(bool success,) = address(aavePM).call{value: SEND_VALUE}("");
require(success, "Failed to send ETH to AavePM contract");

aavePM.rebalance();

(,,,,, uint256 endHealthFactor) =
IPool(aavePM.getContractAddress("aavePool")).getUserAccountData(address(aavePM));
function checkEndHealthFactor(address _address) public view {
(,,,,, uint256 endHealthFactor) = IPool(aavePM.getContractAddress("aavePool")).getUserAccountData(_address);
uint256 endHealthFactorScaled = endHealthFactor / 1e16;

require(endHealthFactorScaled <= (aavePM.getHealthFactorTarget() + REBALANCED_HEALTH_FACTOR_TOLERANCE));
require(endHealthFactorScaled >= (aavePM.getHealthFactorTarget() - REBALANCED_HEALTH_FACTOR_TOLERANCE));
}

function test_Rebalance() public {
vm.startPrank(manager1);
sendEth(address(aavePM), SEND_VALUE);
aavePM.rebalance();
checkEndHealthFactor(address(aavePM));
vm.stopPrank();
}

Expand All @@ -34,33 +37,37 @@ contract RebalanceTests is AavePMTestSetup {
aavePM.updateHealthFactorTarget(HEALTH_FACTOR_TARGET_MINIMUM);

aavePM.rebalance();

(,,,,, uint256 endHealthFactor) =
IPool(aavePM.getContractAddress("aavePool")).getUserAccountData(address(aavePM));
uint256 endHealthFactorScaled = endHealthFactor / 1e16;

require(endHealthFactorScaled <= (aavePM.getHealthFactorTarget() + REBALANCED_HEALTH_FACTOR_TOLERANCE));
require(endHealthFactorScaled >= (aavePM.getHealthFactorTarget() - REBALANCED_HEALTH_FACTOR_TOLERANCE));
checkEndHealthFactor(address(aavePM));
vm.stopPrank();
}

// TODO: Add additional tests for the rebalance function for non-empty Aave accounts

function test_RebalanceHealthFactorIncrease() public {
test_Rebalance();

vm.startPrank(manager1);
// Increase the health factor target
aavePM.updateHealthFactorTarget(aavePM.getHealthFactorTarget() + HEALTH_FACTOR_TARGET_CHANGE);
aavePM.rebalance();
checkEndHealthFactor(address(aavePM));
vm.stopPrank();
}

(,,,,, uint256 endHealthFactor) =
IPool(aavePM.getContractAddress("aavePool")).getUserAccountData(address(aavePM));
uint256 endHealthFactorScaled = endHealthFactor / 1e16;
function test_Exposed_RebalanceWithUSDC() public {
// Send ETH from manager1 to the contract
vm.startPrank(manager1);
sendEth(address(this), SEND_VALUE);
vm.stopPrank();

// TODO: These ranges should be set as a global test variable
require(endHealthFactorScaled <= (aavePM.getHealthFactorTarget() + REBALANCED_HEALTH_FACTOR_TOLERANCE));
require(endHealthFactorScaled >= (aavePM.getHealthFactorTarget() - REBALANCED_HEALTH_FACTOR_TOLERANCE));
// Call internal functions as the current test contract (no prank)
_rebalance();

// Send ETH and convert it to USDC
vm.startPrank(manager1);
sendEth(address(this), SEND_VALUE);
vm.stopPrank();

_swapTokens("USDC/ETH", "ETH", "USDC");
_rebalance();
checkEndHealthFactor(address(this));
}
}

0 comments on commit 11ab771

Please sign in to comment.