# Subaccounts

Hyperliquid vaults act as subaccounts, allowing you to isolate positions, manage risk separately, and run multiple strategies from a single master wallet.

## How subaccounts work

* A subaccount is a Hyperliquid **vault** created by a wallet
* Each subaccount has its own positions, margin, and PnL
* The parent wallet can deposit/withdraw funds from subaccounts
* Trade by passing `vaultAddress` to order requests

:::warning
Subaccounts have constraints: minimum 100,000 USD in trading volume to create, and a maximum of 10 per wallet. Check [Hyperliquid's documentation](https://hyperliquid.gitbook.io/hyperliquid-docs/) for current limits.
:::

## Create a subaccount

```ts
import Openfort from '@openfort/openfort-node'
import * as hl from '@nktkas/hyperliquid'

const openfort = new Openfort(process.env.OPENFORT_SECRET_KEY!, {
  walletSecret: process.env.OPENFORT_WALLET_SECRET!,
})
const account = await openfort.accounts.evm.backend.create()

const transport = new hl.HttpTransport({ isTestnet: true })
const exchange = new hl.ExchangeClient({ wallet: account, transport })

await exchange.createSubAccount({
  name: 'ETH Momentum',
})
```

## Fund a subaccount

Transfer USDC from the parent account to the subaccount:

```ts
await exchange.subAccountTransfer({
  subAccountUser: subAccountAddress,
  isDeposit: true,
  usd: 10000, // 10,000 USD
})
```

## Trade with a subaccount

Pass the `vaultAddress` when placing orders to trade within a subaccount:

```ts
await exchange.order({
  orders: [
    {
      a: 4,
      b: true,
      p: '3000.0',
      s: '0.5',
      r: false,
      t: { limit: { tif: 'Gtc' } },
    },
  ],
  grouping: 'na',
  vaultAddress: subAccountAddress,
})
```

## Withdraw from a subaccount

Move funds back to the parent account:

```ts
await exchange.subAccountTransfer({
  subAccountUser: subAccountAddress,
  isDeposit: false,
  usd: 5000, // withdraw 5,000 USD
})
```

## Query subaccount state

Check margin and positions for a subaccount:

```ts
const info = new hl.InfoClient({ transport })

const state = await info.clearinghouseState({ user: subAccountAddress })
console.log('Subaccount margin:', state.marginSummary.accountValue)
console.log('Positions:', state.assetPositions.length)
```

## Combine agents and subaccounts

Use an [agent wallet](/docs/recipes/hyperliquid/agent-wallets) to trade within a subaccount. This pattern provides three layers of control:

1. **Master wallet** — owns funds, creates subaccounts, approves agents
2. **Agent wallet** — authorized to trade on behalf of master
3. **Subaccount** — isolates positions for a specific strategy

```ts
// Agent trading in a specific subaccount
const agentExchange = new hl.ExchangeClient({ wallet: agent, transport })

await agentExchange.order({
  orders: [
    {
      a: 4,
      b: true,
      p: '3000.0',
      s: '0.1',
      r: false,
      t: { limit: { tif: 'Gtc' } },
    },
  ],
  grouping: 'na',
  vaultAddress: subAccountAddress,
})
```

## Strategy isolation example

Run independent strategies with isolated risk:

```ts
// Create subaccounts for each strategy
await exchange.createSubAccount({ name: 'ETH Momentum' })
await exchange.createSubAccount({ name: 'BTC Mean Reversion' })
await exchange.createSubAccount({ name: 'Basis Trade' })

// Fund each independently
await exchange.subAccountTransfer({
  subAccountUser: ethMomentumAddress,
  isDeposit: true,
  usd: 50000,
})

await exchange.subAccountTransfer({
  subAccountUser: btcRevertAddress,
  isDeposit: true,
  usd: 30000,
})

// Each strategy trades in its own subaccount
// Liquidation in one doesn't affect others
```
