The Deleverage function in the contract follows the below flow to deleverage the position on morpho v1. This function can only be executed by the governance of the vault.

Step 1: Borrow Flashloan of x WETH from Aave

  • In this step, the vault initiates a flashloan from the Aave lending protocol, borrowing an amount of Wrapped ETH (WETH) denoted as "x." The "x" amount will be calculated by getborrowedAmountForDeleverage(). This function is designed to calculate the amount of borrowed assets (WETH) needed to repay in order to deleverage the vault. The logic within this function considers the current vault's leverage position and calculates the amount to repay.

Step 2: Repay x WETH to Morpho v1

  • With the borrowed x WETH from the flashloan, the vault repays an equivalent amount of WETH to the Morpho V1 protocol.

Step 3: Withdraw x stETH from Morpho v1

  • The vault initiates a withdrawal of staked Ether (stETH) tokens from the Morpho V1 protocol. The amount withdrawn is calculated as the sum of:

    • The original borrowed amount "x."

    • Aave fees, which are the fees incurred for borrowing the flashloan in the first place.

Step 4: Swap x stETH to WETH through 1inch

  • The vault uses a decentralized exchange aggregator like 1inch to swap the stETH tokens into Wrapped ETH (WETH). The goal here is to convert the stETH tokens obtained from the withdrawal back into WETH.

Step 5: Payback the flashloan of x WETH to Aave

  • Finally, the vault repays the flashloan taken from Aave by returning the x borrowed WETH tokens. This ensures that the flashloan is settled, and the borrowed assets are returned as per the terms of the flashloan.