# Ledge

Borrow USDC against open Hyperliquid positions without closing them. Lend USDC to earn the interest.

Network: HyperEVM Testnet (chain id 998). Contracts: not deployed yet (preview and faucet still work). USDC: `0x2B3370eE501B4a559b57D449569354196457D8Ab`.

## How it works
- A credit account is a contract that owns a Hyperliquid account. You trade it with a trade-only agent key: it can place and cancel orders but can never withdraw.
- You can borrow up to 50% of the account's equity (margin plus open PnL), as long as equity stays above the liquidation line: 1.2 x debt + 60% of margin in use.
- Positions are allowed only in BTC and ETH. Anything else blocks borrowing and makes the account liquidatable.
- Anyone can liquidate an account below the line. The liquidator earns 3% of debt repaid.

## API
Base: https://0.0.0.0:3000. JSON in and out. 60 requests a minute per IP. Nothing here holds keys: transaction endpoints return unsigned transactions that you sign with your own wallet and send in order.

- `GET /api/v1` index
- `GET /api/v1/preview?address=0x..` what a Hyperliquid mainnet account could borrow
- `GET /api/v1/pool` pool size, utilisation, borrow and supply APR
- `GET /api/v1/accounts?owner=0x..` credit accounts of a wallet
- `GET /api/v1/account/{account}` debt, borrowable, withdrawable, health, positions
- `POST /api/v1/tx/{action}` with body fields below, returns `{ transactions: [{ to, data, value, chainId, description }] }`
  - `open` { from, agent } (sends a little HYPE for the account's gas)
  - `deposit` { from, account, amount }
  - `borrow` { from, account, amount }
  - `repay` { from, account, amount }
  - `withdraw` { from, account, amount } then `claim` { from, account } once it lands (a few seconds)
  - `set-agent` { from, account, agent }
  - `lend` { from, amount } and `unlend` { from, amount }
  Amounts are USDC numbers, e.g. 250.5.

## Trading with the agent key
Use any Hyperliquid SDK with the agent private key as the signer. Orders apply to the credit account automatically. Example (TypeScript, @nktkas/hyperliquid):

```ts
const exchange = new ExchangeClient({ transport: new HttpTransport({ isTestnet: true }), wallet: privateKeyToAccount(AGENT_KEY) });
await exchange.order({ orders: [{ a: 0, b: true, p: "100000", s: "0.01", r: false, t: { limit: { tif: "Ioc" } } }], grouping: "na" });
```

## Faucets
- `POST /api/faucet/testnet` { address } test HYPE and test USDC on HyperEVM testnet, once a day.
- `POST /api/faucet/elysium` { address } one-time gas drip of real HYPE on Elysium, for addresses with Hyperliquid trading history and no Elysium balance.
- `GET /api/faucet` which faucets are open.

## Example: borrow against an open account
1. `GET /api/v1/accounts?owner=YOUR_WALLET` to find your account.
2. `GET /api/v1/account/{account}` and read `borrowable`.
3. `POST /api/v1/tx/borrow` { "from": YOUR_WALLET, "account": ACCOUNT, "amount": 100 }, sign and send the returned transaction.
