Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 39 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# aHYPER Leverage Frontend
# ETH/WETH Leverage on Morpho

A single-page DeFi app for leveraged aHYPER positions on Monad, powered by [Enso Finance](https://enso.finance) bundle API and Morpho flashloans.
A single-page DeFi app for leveraged ETH positions on the Morpho WETH/USDC market (Ethereum mainnet), powered by [Enso Finance](https://enso.finance) bundle API and Morpho flashloans.

## How It Works

Users deposit **USDC** or **aHYPER** and select a leverage multiplier (1.5x - 3x). The app builds an atomic bundle that:
Users deposit **ETH** or **WETH** and select a leverage multiplier (1.5x - 3x). The app builds an atomic bundle that:

1. **Flashloan** USDC from Morpho
2. **Deposit** flashloaned USDC into the Hyperithm vault to mint aHYPER
3. **Deposit** all aHYPER (user's + newly minted) as collateral into Morpho
4. **Borrow** USDC from Morpho to repay the flashloan
1. **Wrap** ETH → WETH (skipped in WETH mode)
2. **Flashloan** USDC from Morpho
3. **Swap** flashloaned USDC → WETH via Enso route
4. **Deposit** all WETH (user's + swapped) as collateral into Morpho
5. **Borrow** USDC from Morpho to repay the flashloan

All steps execute in a single transaction via the Enso bundle API.

### USDC Mode
### WETH Mode

When depositing USDC, a preliminary step converts the user's USDC into aHYPER via the Hyperithm vault before entering the flashloan loop.
When depositing WETH, the wrapping step is skipped. An ERC-20 approval for WETH is checked instead.

### Health Factor

Expand All @@ -25,21 +26,20 @@ Since `totalCollateral = leverage * deposit` and `debt = (leverage - 1) * deposi
HF = (leverage * LLTV) / (leverage - 1)
```

HF depends only on the leverage multiplier and LLTV (0.77), not on the deposit size:
HF depends only on the leverage multiplier and LLTV (0.86), not on the deposit size:

| Leverage | Health Factor |
|----------|--------------|
| 1.5x | 2.31 |
| 2x | 1.54 |
| 2.5x | 1.28 |
| 3x | 1.16 |
| 1.5x | 2.58 |
| 2x | 1.72 |
| 2.5x | 1.43 |
| 3x | 1.29 |

Positions with HF <= 1.0 are liquidatable.

### Constraints

- The Hyperithm aHYPER vault requires a **minimum deposit of 100 USDC** (or equivalent aHYPER). Transactions below this threshold will revert on-chain.
- The bundle response includes an approval transaction that the EOA must execute to call `setAuthorization` on the Morpho contract, granting the `ensowallet-v2` contract permission to act on behalf of the user. This is a one-time operation per wallet. See [example tx](https://monadscan.com/tx/0x376e31d72ee4f9be33f9f0514ca029370422930dceba27f43610460c7c5f20ba).
- The bundle response includes an approval transaction that the EOA must execute to call `setAuthorization` on the Morpho contract, granting the `ensowallet-v2` contract permission to act on behalf of the user. This is a one-time operation per wallet.

## Transaction Flow

Expand All @@ -60,12 +60,12 @@ User clicks "Execute Leverage"
+-------------------------------------+
|
v
+--[ Check ERC-20 token allowance ]---+
| allowance(user, spender) |
| >= amountIn? |
| |
| YES --> skip NO --> add |
+-------------------------------------+
+--[ Check WETH allowance (WETH mode only) ]+
| allowance(user, spender) >= amountIn? |
| |
| YES --> skip NO --> add |
| ETH mode --> skip |
+-------------------------------------------+
|
v
Always add main leverage tx
Expand All @@ -82,28 +82,22 @@ User clicks "Execute Leverage"
### UX Cases

```
Case 1: First-time user (3 txs)
[setAuthorization] -> [approve USDC] -> [leverage tx]
Case 1: ETH mode, first-time user (2 txs)
[setAuthorization] -> [leverage tx]

Case 2: Authorized, new token amount (2 txs)
[approve USDC] -> [leverage tx]

Case 3: Authorized + sufficient allowance (1 tx)
Case 2: ETH mode, authorized (1 tx)
[leverage tx]

Case 4: Returning user, same token, higher amount (2 txs)
[approve USDC] -> [leverage tx]

Case 5: aHYPER mode, first time (3 txs)
[setAuthorization] -> [approve aHYPER] -> [leverage tx]
Case 3: WETH mode, first-time user (3 txs)
[setAuthorization] -> [approve WETH] -> [leverage tx]

Case 5b: aHYPER mode, authorized + sufficient allowance (1 tx)
Case 4: WETH mode, authorized + sufficient allowance (1 tx)
[leverage tx]

Case 6: Insufficient balance
Case 5: Insufficient balance
Button greyed out, "Insufficient {token} balance" shown

Case 7: Health factor <= 1
Case 6: Health factor <= 1
Button greyed out
```

Expand All @@ -114,25 +108,26 @@ Case 7: Health factor <= 1
| `POST /api/v1/shortcuts/bundle` | Build and simulate the flashloan bundle |
| `GET /api/v1/wallet/approve` | Check/build ERC-20 approval for the Enso router |
| `GET /api/v1/wallet/balances` | Fetch wallet token balances |
| `GET /api/v1/tokens` | Token metadata (symbol, name, logo) |
| `GET /api/v1/prices/{chainId}/{token}` | Token USD pricing |
| `GET /api/v1/prices/{chainId}/{token}` | ETH USD pricing |

## Contracts

| Contract | Address |
|----------|---------|
| aHYPER (Hyperithm Vault) | `0x7Cd231120a60F500887444a9bAF5e1BD753A5e59` |
| USDC | `0x754704Bc059F8C67012fEd69BC8A327a5aafb603` |
| Morpho Primary | `0xD5D960E8C380B724a48AC59E2DfF1b2CB4a1eAee` |
| WETH | `0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2` |
| USDC | `0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48` |
| Morpho Blue | `0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb` |

Market: [WETH/USDC](https://app.morpho.org/ethereum/market/0x94b823e6bd8ea533b4e33fbc307faea0b307301bc48763acc4d4aa4def7636cd/weth-usdc) (LLTV: 86%)

Chain: **Monad** (chainId: 143)
Chain: **Ethereum** (chainId: 1)

## Setup

```bash
npm install
cp .env.example .env
# Edit .env with your API key and RPC URL
# Edit .env with your API key and Ethereum RPC URL
npm run dev
```

Expand All @@ -141,7 +136,7 @@ npm run dev
| Variable | Description |
|----------|-------------|
| `NEXT_PUBLIC_ENSO_API_KEY` | Enso Finance API key ([get one here](https://www.enso.finance)) |
| `NEXT_PUBLIC_RPC_URL` | Monad RPC endpoint |
| `NEXT_PUBLIC_RPC_URL` | Ethereum mainnet RPC endpoint |

## Tech Stack

Expand Down
4 changes: 2 additions & 2 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Providers } from './providers';
import './globals.css';

export const metadata: Metadata = {
title: 'Enso Flashloan Leverage',
description: 'aHYPER leverage looping via Enso',
title: 'ETH Leverage on Morpho',
description: 'ETH/WETH leverage looping on Morpho WETH/USDC via Enso',
};

export default function RootLayout({
Expand Down
Loading