HyperFunTrading

VaultTrading (HyperFunTrading.sol)

HyperFunTrading.sol executes Hyperliquid L1 actions through precompiles and CoreWriter.

Inheritance

contract HyperFunTrading is
    UUPSUpgradeable,
    OwnableUpgradeable,
    ReentrancyGuardUpgradeable

Precompile Addresses

address public constant PRECOMPILE_PERP_POSITION = 0x0000000000000000000000000000000000000800;
address public constant PRECOMPILE_SPOT = 0x0000000000000000000000000000000000000801;
address public constant PRECOMPILE_WITHDRAWABLE = 0x0000000000000000000000000000000000000803;
address public constant PRECOMPILE_ORACLE = 0x0000000000000000000000000000000000000807;
address public constant PRECOMPILE_PERP_ASSET_INFO = 0x000000000000000000000000000000000000080a;
address public constant PRECOMPILE_ACCOUNT_MARGIN_SUMMARY = 0x000000000000000000000000000000000000080F;
Precompile
Returns

0x800 PERP_POSITION

(int64 szi, uint64 entryNtl, int64 isolatedRawUsd, uint32 leverage, bool isIsolated)

0x801 SPOT

(uint256 total, uint256 hold, uint256 entryNtl) in 8 decimals

0x803 WITHDRAWABLE

uint64 withdrawable from perp

0x807 ORACLE

uint64 oracle price

0x80A PERP_ASSET_INFO

(string name, uint32 marginTableId, uint32 szDecimals, uint32 maxLeverage, bool onlyIsolated)

0x80F ACCOUNT_MARGIN_SUMMARY

(int64 accountValue, uint64 marginUsed, uint64 ntlPos, int64 rawUsd)

CoreWriter Actions

State Variables


Trading Functions

Market Order

Flow:

  1. Validate vault has deposits

  2. Normalize size to asset's szDecimals

  3. Ensure margin in L1 Perp (auto-transfer from Spot)

  4. Send IOC order to L1 with unique cloid

  5. Trigger auto-rebalance

Limit Order

Flow:

  1. Validate vault has deposits

  2. Normalize size

  3. Ensure margin

  4. Send GTC order to L1

  5. Trigger auto-rebalance

Limit Order Raw (No Margin Check)

For split orders where margin is pre-transferred. Only sends order, no other L1 actions.

Batch Limit Orders

Execute multiple limit orders in one transaction (max 20 orders). Margin calculated for total position.

Close Order

Close Position (Auto)

Flow:

  1. Read position from L1 precompile

  2. Get oracle price

  3. Apply slippage

  4. Send reduce-only IOC order

  5. Sweep excess from Perp → Spot

  6. Trigger auto-rebalance

Cancel Order

Cancel a pending limit order by order ID.


HIP-3 Builder Perps (V46)

For HIP-3 assets (index >= 100000), precompiles don't work. Must provide params from API.

Execute Order (HIP-3)

Close Position (HIP-3)

Standard vs HIP-3

Feature
Standard (< 100000)
HIP-3 (>= 100000)

Oracle

Precompile 0x807

Must provide

szDecimals

Precompile 0x80A

Must provide

maxLeverage

Precompile 0x80A

Must provide

Open

executeMarketOrder()

executeOrderBuilder()

Close

closePositionAdvanced()

closePositionBuilder()


Fund Transfer Functions

Spot ↔ Perp

Sweep to Spot

Auto-Rebalance

Maintains 50% EVM / 50% L1 ratio:

  • EVM > 52%: Deposit excess to L1

  • EVM < 48%: Withdraw from L1 Spot to EVM


API Wallet Functions

Add API Wallet (V39)

Only leader can add. Registers on both EVM and L1.

Renew API Wallet

Extend expiration for active wallet.

Remove API Wallet

Removes from EVM tracking. L1 registration is permanent (Hyperliquid limitation), but EVM check prevents unauthorized use.

Get Status


View Functions

L1 Balances

Position Data

Account Margin

Oracle & Asset Info

Size Preview

Debug


Admin Functions

Withdraw from L1

Refill Reserve

Transfer from L1 Perp to Spot (admin only).

Reserve Check


Events


Access Control

Role
Can Execute

Leader

All trading, API wallet management

API Wallet

Trading only (if active & not expired)

Vault (Token)

autoRebalance()

Admin

Withdraw functions, refillReserve()


Internal Mechanisms

Size Normalization

Sizes are normalized to asset's szDecimals precision:

Example: SUI (szDecimals=1) → step = 10^7 → 0.1 SUI minimum

Margin Management

Auto-transfers from Spot to Perp when opening positions:

Return to Spot

After closing positions, excess margin is swept back:

Order Nonce (V41)

Each order has unique cloid to prevent L1 duplicates:


Upgrade Authority


Contract
Role

HyperFunToken

Core vault, calls autoRebalance()

HyperFunFactory

Global settings, upgrade authority

Last updated