HyperFunTrading API

HyperFunTrading API Documentation

Overview

HyperFunTrading is the trading module for HyperFun vaults on Hyperliquid. It handles:

  • Perpetual trading (open/close positions)

  • L1 fund transfers (Spot ↔ Perp)

  • Auto-rebalancing (EVM ↔ L1)

  • API wallet management

Contract Address: Deployed as a proxy for each vault (get from vault.tradingModule())


Constants

Name
Value
Description

USDC

0xb88339CB7199b77E23DB6E890353E22632Ba630f

USDC token on HyperEVM

CORE_DEPOSIT_WALLET

0x6B9E773128f453f5c2C60935Ee2DE2CBc5390A24

For EVM → L1 deposits

Asset Indices (Hyperliquid Perps)

Asset
Index
szDecimals
Max Leverage

BTC

0

5

50x

ETH

1

4

50x

SOL

5

2

20x

SUI

57

1

20x

HYPE

132

1

20x


Authorization

Role
Can Call

Leader

All trading functions, API wallet management

API Wallet

All trading functions (if active & not expired)

Admin

Fund withdrawals, refill reserve

Vault

autoRebalance() only


Trading Functions

executeMarketOrder

Execute a market order with IOC (Immediate-Or-Cancel).

With custom leverage:

Example (Python/JS):


executeLimitOrder

Execute a limit order with GTC (Good-Till-Cancel).

With custom leverage:


executeLimitOrderRaw

Execute limit order WITHOUT margin check. Use for split orders to avoid multiple L1 actions per TX.

Important: Caller must ensure margin is pre-transferred to L1 Perp before calling.


batchLimitOrders

Execute multiple limit orders in one transaction (for DCA / split orders).

Limits: 1-20 orders per batch.

Example:


closePositionAdvanced

Close a position using oracle price with slippage.

Example:


executeCloseOrderAdvanced

Close with custom size and price (for partial closes).


cancelOrder

Cancel a pending limit order.


Fund Transfer Functions

transferToPerp

Transfer USDC from L1 Spot to L1 Perp.

transferFromPerp

Transfer USDC from L1 Perp to L1 Spot.

sweepToSpot

Sweep all withdrawable funds from Perp to Spot.

forceReturnToSpot

Force return all withdrawable from Perp to Spot (use if sweepToSpot doesn't work).


View Functions

getL1Position

Get current position size for an asset.

Returns: Position size in szDecimals format.

  • Positive = LONG

  • Negative = SHORT

  • For SOL (szDecimals=2): szi=150 means 1.5 SOL

getL1PositionFull

Get full position data.

getL1SpotBalance

Get L1 Spot USDC balance.

getL1AccountValue

Get L1 Perp account value (includes unrealized PnL).

getAccountMarginSummary

Get detailed margin info.

getOraclePrice

Get current oracle price for an asset.

Format: price_USD * 10^(6-szDecimals)

getSzDecimals

Get size decimals for an asset.

getMaxLeverage

Get maximum leverage for an asset.

previewNormalizedSize

Preview how size will be normalized (rounded).

getTotalAssets

Get total vault assets (EVM + L1 Spot + L1 Perp - Pending Sells).

checkReserve

Check if EVM reserve is low.


API Wallet Setup (Complete Flow)

Overview

AI/Bot 要交易需要完成以下步骤:

Step 1: addApiWallet (Required)

Leader 添加 API wallet,同时在 EVM 和 L1 上注册。

Example:

权限: 仅 Leader 可调用

L1 Action: 自动发送 ACTION_ADD_API_WALLET 到 Hyperliquid


Step 2: approveBuilderFee (Optional)

设置交易手续费返佣(如果需要)。这是在 Token 合约 上调用,不是 Trading。

谁可以调用:

  • Admin

  • Factory Owner

  • Factory 合约本身

Note: 如果 Factory 设置了 defaultBuilder,新创建的 vault 会自动 approve。


Step 3: API Wallet 开始交易

API Wallet 现在可以调用所有交易函数:


API Wallet Management Functions

renewApiWallet

续期 API wallet(Leader only)。

removeApiWallet

移除 API wallet 授权(Leader only)。

Note: L1 上的注册无法移除(Hyperliquid 限制),但 EVM 上的检查会阻止交易。

getApiWalletStatus

获取 API wallet 状态。

isApiWalletValid

检查 API wallet 是否有效。


Complete Setup Example


Admin Functions (Admin Only)

withdrawPerpToEVM

Withdraw USDC from L1 Perp to EVM.

withdrawAllPerpToEVM

Withdraw all withdrawable from L1 Perp to EVM.

withdrawAllSpotToEVM

Send all L1 Spot USDC to EVM.

refillReserve

Refill EVM reserve from L1 Perp.


Size & Price Formatting

Size Format (1e8)

All sizes are in 1e8 format (8 decimal places):

Amount
Size Value

1.0 SOL

100000000

0.5 ETH

50000000

0.001 BTC

100000

Important: Sizes are normalized based on szDecimals:

  • SOL (szDecimals=2): Minimum = 0.01 SOL = 1000000

  • ETH (szDecimals=4): Minimum = 0.0001 ETH = 10000

  • BTC (szDecimals=5): Minimum = 0.00001 BTC = 1000

Price Format (1e8)

All prices are in 1e8 format:

Price
Price Value

$150.00

15000000000

$3000.50

300050000000

$95000.00

9500000000000


Events


Example: Full Trading Flow


Error Codes

Code
Meaning

Not authorized

Caller is not leader/API wallet

Not admin

Caller is not admin

No deposits

Vault has no deposits yet

Size too small

Size below minimum for asset

No L1 position

No position to close

Slippage 1-50%

Invalid slippage value

Array length mismatch

Batch order arrays different lengths

Invalid order count

Batch must be 1-20 orders


Contract Version

Current: V44 (includes pending sell fix)

Last updated