Skip to content

Commit

Permalink
Add deleverage function
Browse files Browse the repository at this point in the history
  • Loading branch information
EridianAlpha committed Jun 10, 2024
1 parent fb2f249 commit 82380ed
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ supply-script:
repay-script:
@forge script script/Interactions.s.sol:Interactions ${NETWORK_ARGS} -vvvv --sig "aaveRepayAavePM()"

deleverage-script:
@forge script script/Interactions.s.sol:Interactions ${NETWORK_ARGS} -vvvv --sig "aaveDeleverageAavePM()"

closePosition-script:
@forge script script/Interactions.s.sol:Interactions ${NETWORK_ARGS} -vvvv --sig "aaveClosePositionAavePM()"

Expand Down Expand Up @@ -164,6 +167,7 @@ update-hft-anvil: anvil-network update-hft
update-st-anvil: anvil-network update-st
rebalance-anvil: anvil-network rebalance-script
reinvest-anvil: anvil-network reinvest-script
deleverage-anvil: anvil-network deleverage-script
closePosition-anvil: anvil-network closePosition-script
supply-anvil: anvil-network supply-script
repay-anvil: anvil-network repay-script
Expand Down
6 changes: 6 additions & 0 deletions script/Interactions.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ contract Interactions is Script {
vm.stopBroadcast();
}

function aaveDeleverageAavePM() public {
vm.startBroadcast();
aavePM.deleverage();
vm.stopBroadcast();
}

function aaveClosePositionAavePM() public {
vm.startBroadcast();
aavePM.aaveClosePosition(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266);
Expand Down
28 changes: 21 additions & 7 deletions src/AavePM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,23 @@ contract AavePM is
// TODO: Emit an event with suppliedCollateral, reinvestedDebt, reinvestedCollateral
}

/// @notice // TODO: Add comment
function deleverage() public onlyRole(MANAGER_ROLE) {
// Update the Health Factor target to the maximum value.
uint16 previousHealthFactorTarget = s_healthFactorTarget;
if (previousHealthFactorTarget != type(uint16).max) {
s_healthFactorTarget = type(uint16).max;
}

// Rebalance position to repay all debt.
rebalance();

// Reset the Health Factor target to the previous value.
if (previousHealthFactorTarget != type(uint16).max) {
s_healthFactorTarget = previousHealthFactorTarget;
}
}

/// @notice // TODO: Add comment
function aaveSupplyFromContractBalance() public onlyRole(MANAGER_ROLE) returns (uint256 suppliedCollateral) {
suppliedCollateral = _convertExistingBalanceToWstETHAndSupplyToAave();
Expand Down Expand Up @@ -421,15 +438,12 @@ contract AavePM is

/// @notice // TODO: Add comment
function aaveClosePosition(address _owner) public onlyRole(MANAGER_ROLE) checkOwner(_owner) {
// Update the Health Factor target to the maximum value.
if (!(s_healthFactorTarget == type(uint16).max)) {
emit HealthFactorTargetUpdated(s_healthFactorTarget, type(uint16).max);
s_healthFactorTarget = type(uint16).max;
// Check if position needs to be deleveraged before closing.
(,,,,, uint256 currentHealthFactor) = IPool(getContractAddress("aavePool")).getUserAccountData(address(this));
if (currentHealthFactor != type(uint256).max) {
deleverage();
}

// Rebalance position to repay all debt.
rebalance();

// Withdraw all wstETH to the owner.
aaveWithdrawWstETH(getContractBalance("awstETH"), _owner);

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IAavePM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ interface IAavePM {
// ================================================================
function rebalance() external;
function reinvest() external returns (uint256 reinvestedDebt);
function deleverage() external;
function aaveSupplyFromContractBalance() external returns (uint256 suppliedCollateral);
function aaveRepayUSDCFromContractBalance() external;

Expand Down

0 comments on commit 82380ed

Please sign in to comment.