The withdraw function in the contract follows the above flow to close the Leveraged position on morpho v1. In the above flow diagram, we are assuming that the vault's current leverage is 3x. But Vault can work on other leverages like 1x, 2x, 3x.

Step 1: User sends x magETH to the vault via withdraw

  • In this step, the user initiates a withdrawal of a certain amount of magETH tokens (denoted as "x") from the Sharpe Magnum vault. The withdrawal operation allows the user to retrieve their assets from the vault.

Step 2: Flashloan 2x WETH on Aave

  • Similar to the deposit process, the user initiates a flashloan from the Aave lending protocol, borrowing 2 times the amount of Wrapped ETH (2x WETH) in this case. Flashloans are instant and require no collateral but must be repaid within the same transaction.

Step 3: Repay 2x WETH on Morpho V1

  • With the borrowed 2x WETH from the flashloan, the vault repays any outstanding borrowings on Morpho V1. This ensures that WETH borrowed assets are returned to Morpho V1 as required.

Step 4: Withdraw all stETH (~3x stETH) from Morpho V1

  • The vault proceeds to withdraw all their staked Ether (stETH) tokens from the Morpho V1 protocol. The amount of stETH withdrawn is approximately three times their initial stETH deposit (plus profit/loss amount if any).

Step 5: Swap ~3x stETH to WETH via 1inch

  • The vault utilizes the 1inch decentralized exchange aggregator to swap their stETH tokens for Wrapped ETH (WETH). This step involves converting all amounts of stETH from the previous step into WETH.

Step 6: Unwrap and send the ~1x ETH to the user

  • After obtaining WETH, the user unwraps it, converting it back into Ether (ETH). From the total amount obtained, the user retains approximately one times their initial deposit or the amount that represents their initial deposit plus profit/loss.

Step 7: Pay the flashloan of 2x WETH to Aave

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

It's important to note above explanation assumes that the ratio of magETH share to ETH is 1:1 (But it will change as per the vault's performance).

Last updated