
Hyperliquid
- 8.4k installs
- 18 repo stars
- Updated July 27, 2026
- starchild-ai-agent/official-skills
hyperliquid is an agent skill that |.
About
| --- name: hyperliquid version: 1.5.0 description: | Trade perp futures, spot, and RWA on Hyperliquid DEX with up to asset max leverage. Use when placing perp or spot orders, setting TP/SL, or moving funds on Hyperliquid (e.g. long BTC 5x, sell ETH, deposit USDC, set stop). delivery: script tools: - hl_account - hl_balances - hl_total_balance - hl_open_orders - hl_market - hl_orderbook - hl_fills - hl_candles - hl_funding - hl_order - hl_spot_order - hl_tpsl_order - hl_cancel - hl_cancel_all - hl_modify - hl_leverage - hl_transfer_usd - hl_withdraw - hl_deposit metadata: starchild: emoji: "📈" skillKey: hyperliquid requires: env: [WALLET_SERVICE_URL] user-invocable: true disable-model-invocation: false --- # Hyperliquid Trading Trade perpetual futures and spot tokens on Hyperliquid, a fully on-chain decentralized exchange. Orders are signed using this agent's EVM wallet and submitted directly to the Hyperliquid L1. ## Prerequisites Before trading, the wallet policy must be active. Load the **wallet-policy** skill and propose the standard wildcard policy (deny key export + allow `*`). This covers all Hyperliquid operations - USDC deposits, EIP-712 order signing, and withdrawals.
- Best for: human-in-the-loop operations, ad-hoc trades, monitoring flows, orchestration across multiple skills
- Strength: fastest integration with built-in verification workflow (`check → execute → verify`)
- Limitation: `hl_*` tools are tool-runtime capabilities, **not normal Python imports**
- Use `skills/hyperliquid/client.py` (`HyperliquidClient`)
- This is the recommended path for always-on bots (grid/maker/rebalancer) that should not depend on localhost agent-chat b
Hyperliquid by the numbers
- 8,446 all-time installs (skills.sh)
- +69 installs in the week ending Jul 28, 2026 (Skillselion tracking)
- Ranked #181 of 2,184 Testing & QA skills by installs in the Skillselion catalog
- Security screen: MEDIUM risk (skills.sh audit)
- Data as of Jul 28, 2026 (Skillselion catalog sync)
hyperliquid capabilities & compatibility
- Capabilities
- best for: human in the loop operations, ad hoc t · strength: fastest integration with built in veri · limitation: `hl_*` tools are tool runtime capabi · use `skills/hyperliquid/client.py` (`hyperliquid · this is the recommended path for always on bots
- Use cases
- documentation
What hyperliquid says it does
--- name: hyperliquid version: 1.5.0 description: | Trade perp futures, spot, and RWA on Hyperliquid DEX with up to asset max leverage.
Use when placing perp or spot orders, setting TP/SL, or moving funds on Hyperliquid (e.g.
long BTC 5x, sell ETH, deposit USDC, set stop).
Orders are signed using this agent's EVM wallet and submitted directly to the Hyperliquid L1.
npx skills add https://github.com/starchild-ai-agent/official-skills --skill hyperliquidAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 8.4k |
|---|---|
| repo stars | ★ 18 |
| Security audit | 1 / 3 scanners passed |
| Last updated | July 27, 2026 |
| Repository | starchild-ai-agent/official-skills ↗ |
What problem does hyperliquid solve for developers using this skill?
|
Who is it for?
Developers who need hyperliquid patterns described in the cached skill documentation.
Skip if: Skip when docs are empty or the task is outside the skill's documented scope.
When should I use this skill?
|
What you get
Actionable workflows and conventions from SKILL.md for hyperliquid.
- Executed trade orders
- Account balance reports
- Funding and fill history
By the numbers
- Exposes 16 Hyperliquid trading and account tools
- Skill version 1.4.0 with script delivery mode
Files
Hyperliquid Trading
Trade perpetual futures and spot tokens on Hyperliquid, a fully on-chain decentralized exchange. Orders are signed using this agent's EVM wallet and submitted directly to the Hyperliquid L1.
Prerequisites
Before trading, the wallet policy must be active. Load the wallet-policy skill and propose the standard wildcard policy (deny key export + allow *). This covers all Hyperliquid operations — USDC deposits, EIP-712 order signing, and withdrawals.
Runtime Model: Agent Tools vs Service Scripts (Important)
Hyperliquid has two execution modes. Use the right one for your workflow:
1) Agent tool mode (chat/task runtime)
Use hl_* tools directly in agent conversations and task scripts that run inside the Starchild tool runtime.
- Best for: human-in-the-loop operations, ad-hoc trades, monitoring flows, orchestration across multiple skills
- Strength: fastest integration with built-in verification workflow (
check → execute → verify) - Limitation:
hl_*tools are tool-runtime capabilities, not normal Python imports
2) Service script mode (FastAPI/worker/bot process)
For standalone services (FastAPI bots, daemons, web backends), call Hyperliquid directly via the bundled client:
- Use
skills/hyperliquid/client.py(HyperliquidClient) - This is the recommended path for always-on bots (grid/maker/rebalancer) that should not depend on localhost agent-chat bridging
hl_*tools are not importable asfrom ... import hl_orderin plain Python services
Service Integration Pattern (recommended)
1. Keep strategy/state machine in your own service process (FastAPI, worker loop, queue consumer) 2. Use HyperliquidClient for order, cancel, open orders, fills, account 3. Persist bot state locally (orders, fills cursor, grid map, PnL) 4. Build admin APIs (/start, /stop, /status, /history) around that state
Minimal service example (direct client)
from skills.hyperliquid.client import HyperliquidClient
client = HyperliquidClient()
address = await client._get_address() # wallet address used for read endpoints
# Query
account = await client.get_account_state(address)
opens = await client.get_open_orders(address)
fills = await client.get_user_fills(address)
# Place order (example)
res = await client.place_order(
coin="BTC",
is_buy=True,
size=0.001,
price=95000,
order_type="limit",
)
# Cancel all BTC orders
await client.cancel_all("BTC")Available Tools
Account & Market Info
| Tool | What it does |
|---|---|
hl_total_balance | Check how much you can trade with (use this for balance checks!) |
hl_account | See your open positions and PnL |
hl_balances | See your token holdings (USDC, HYPE, etc.) |
hl_market | Get current prices for crypto or stocks |
hl_orderbook | Check order book depth and liquidity |
hl_fills | See recent trade fills and execution prices |
hl_candles | Get price charts (1m, 5m, 1h, 4h, 1d) |
hl_funding | Check funding rates for perps |
hl_open_orders | See pending orders |
Trading
| Tool | What it does |
|---|---|
hl_order | Buy or sell perps (crypto/stocks) |
hl_spot_order | Buy or sell spot tokens |
hl_tpsl_order | Place stop loss or take profit orders |
hl_leverage | Set leverage (1x to asset max) |
hl_cancel | Cancel a specific order |
hl_cancel_all | Cancel all open orders |
hl_modify | Change order price or size |
Funds
| Tool | What it does |
|---|---|
hl_deposit | Add USDC from Arbitrum (min $5) |
hl_withdraw | Send USDC to Arbitrum (1 USDC fee, ~5 min) |
hl_transfer_usd | Move USDC between spot/perp (rarely needed) |
---
Quick Start
Just tell the agent what you want to trade - it handles everything automatically.
Examples:
User: "Buy $20 of Bitcoin with 5x leverage"
Agent: [checks balance → sets leverage → places order → reports fill]
Result: "✓ Bought 0.0002 BTC at $95,432 with 5x leverage. Position opened."
User: "Long NVIDIA with $50, 10x"
Agent: [auto-detects NVIDIA = xyz:NVDA → executes → verifies]
Result: "✓ Bought 0.25 NVDA at $198.50 with 10x leverage. Filled at $198.62."
User: "Sell my ETH position"
Agent: [checks position size → closes → reports PnL]
Result: "✓ Sold 0.5 ETH at $3,421. Realized PnL: +$12.50"You don't need to:
- Understand account modes or fund transfers
- Check balances manually (agent does it)
- Calculate position sizes (agent does it)
- Verify fills (agent does it)
Just say what you want, the agent handles the rest.
---
Agent Behavior Guidelines
🤖 As the agent, you should ALWAYS do these automatically (never ask the user):
1. Check available funds - Use hl_total_balance before EVERY trade to see total available margin 2. Detect asset type - Recognize if user wants crypto (BTC, ETH, SOL) or stocks/RWA (NVIDIA→xyz:NVDA, TESLA→xyz:TSLA) 3. Set leverage - Always call hl_leverage before placing orders (unless user specifies not to) 4. Verify fills - After placing ANY order, immediately call hl_fills to check if it filled 5. Report results - Tell user the outcome: filled price, size, and any PnL 6. Suggest risk management - For leveraged positions, remind users about stop losses or offer to set them
🎯 User just says: "buy X" or "sell Y" or "long Z with $N"
🔧 You figure out:
- Current balance (hl_total_balance)
- Asset resolution (crypto vs RWA)
- Leverage settings (hl_leverage)
- Order sizing (calculate from user's $ amount or size)
- Execution (hl_order)
- Verification (hl_fills)
- Final report to user
📊 Balance checking hierarchy:
- ✅ Use
hl_total_balance- shows ACTUAL available margin regardless of account mode - ❌ Don't use
hl_accountfor balance - may show $0 even if funds available - ❌ Don't use
hl_balancesfor margin - only shows spot tokens
🚀 Be proactive, not reactive:
- Don't wait for user to ask "did it fill?" - check automatically
- Don't ask "should I check your balance?" - just do it
- Don't explain account modes - user doesn't care, just execute
---
Tool Usage Examples
Check Account State
hl_account() # Default crypto perps account
hl_account(dex="xyz") # Builder dex (RWA/stock perps) accountReturns marginSummary (accountValue, totalMarginUsed, withdrawable) and assetPositions array with each position's coin, szi (signed size), entryPx, unrealizedPnl, leverage.
Important: Builder perps (xyz:NVDA, xyz:TSLA, etc.) have separate clearinghouses. Always check the correct dex when trading RWA/stock perps.
Check Spot Balances
hl_balances()Returns balances array with coin, hold, total for USDC and all spot tokens.
Check Market Prices
hl_market() # All mid prices
hl_market(coin="BTC") # BTC price + metadata (maxLeverage, szDecimals)Side Parameter Convention (read this first)
All order tools (hl_order, hl_spot_order, hl_tpsl_order, hl_modify) use the same side parameter. Use `"buy"` or `"sell"` — these are the documented values and should be your default.
For safety, the tools also accept these aliases so a model guess doesn't reverse direction on a leveraged order:
- Buy family:
"buy","B","bid","long","L",1,true - Sell family:
"sell","S","A","ask","short",0,false
An unrecognized value will fail the call with a clear error — the tool never defaults to sell (or buy) when side is ambiguous. This is intentional: silently reversing direction on a leveraged position is the worst failure mode.
Note: Hyperliquid's L1 wire protocol uses "B" and "A" internally, but the tool interface here is buy/sell. Stick to buy/sell in your calls and you will never be surprised.
Place a Perp Limit Order
hl_order(coin="BTC", side="buy", size=0.01, price=95000)Places a GTC limit buy for 0.01 BTC at $95,000.
Place a Perp Market Order
hl_order(coin="ETH", side="sell", size=0.1)Omitting price submits an IoC order at mid price +/- 3% slippage.
Parameter format behavior:
- Preferred: pass correct JSON types (
sizeas number,reduce_onlyas boolean) - Hyperliquid tools now include tolerant coercion for common LLM formatting mistakes:
- numeric strings like
"0.01"→0.01 - boolean strings like
"true"/"false"→true/false - integer strings like
"5"/"5.0"→5 - Invalid/empty/non-finite values still fail with explicit validation errors
Place a Post-Only Order
hl_order(coin="BTC", side="buy", size=0.01, price=94000, order_type="alo")ALO (Add Liquidity Only) = post-only. Rejected if it would immediately fill.
Practical guardrail for bots: If your ALO price is too close to mid (often within ~0.1% on liquid pairs), Hyperliquid may reject it. For market-making/grid bots, compute current mid first and skip or shift levels that sit inside your no-cross buffer zone.
Place a Stop Loss Order
hl_tpsl_order(coin="BTC", side="sell", size=0.01, trigger_px=90000, tpsl="sl")Automatically sells 0.01 BTC if the price drops to $90,000. Executes as market order when triggered.
For a limit order when triggered (instead of market):
hl_tpsl_order(coin="BTC", side="sell", size=0.01, trigger_px=90000, tpsl="sl", is_market=false, limit_px=89900)Place a Take Profit Order
hl_tpsl_order(coin="ETH", side="sell", size=0.5, trigger_px=3500, tpsl="tp")Automatically sells 0.5 ETH if the price rises to $3,500. Executes as market order when triggered.
Close a Perp Position
hl_order(coin="BTC", side="sell", size=0.01, reduce_only=true)Use reduce_only=true to ensure it only closes, never opens a new position.
Place a Spot Order
hl_spot_order(coin="HYPE", side="buy", size=10, price=25.0)Spot orders use the same interface — just specify the token name.
Cancel an Order
hl_cancel(coin="BTC", order_id=12345678)Get order_id from hl_open_orders.
Cancel All Orders
hl_cancel_all() # Cancel everything
hl_cancel_all(coin="BTC") # Cancel only BTC ordersModify an Order
hl_modify(order_id=12345678, coin="BTC", side="buy", size=0.02, price=94500)Set Leverage
hl_leverage(coin="BTC", leverage=10) # 10x cross margin
hl_leverage(coin="ETH", leverage=5, cross=false) # 5x isolated marginTransfer USDC (rarely needed)
hl_transfer_usd(amount=1000, to_perp=true) # Spot → Perp
hl_transfer_usd(amount=500, to_perp=false) # Perp → SpotNote: Usually not needed - funds are automatically shared. Only use if you get an error saying you need to transfer.
Withdraw USDC to Arbitrum
hl_withdraw(amount=100) # Withdraw to own wallet
hl_withdraw(amount=50, destination="0xABC...") # Withdraw to specific addressFee: 1 USDC deducted by Hyperliquid. Processing takes ~5 minutes.
Deposit USDC from Arbitrum
hl_deposit(amount=500)Sends USDC from the agent's Arbitrum wallet to the Hyperliquid bridge contract. Minimum deposit: 5 USDC. Requires USDC balance on Arbitrum.
Get Candles
hl_candles(coin="BTC", interval="1h", lookback=48)Intervals: 1m, 5m, 15m, 1h, 4h, 1d. Lookback in hours.
Check Funding Rates
hl_funding() # All predicted fundings
hl_funding(coin="BTC") # BTC predicted + 24h historyGet Recent Fills
hl_fills(limit=10)---
Coin vs RWA Resolution
When a user asks to trade a ticker, you need to determine whether it's a native crypto perp (use plain name) or an RWA/stock perp (use xyz:TICKER prefix).
Decision Workflow
1. Known crypto → use plain name: "BTC", "ETH", "SOL", "DOGE", "HYPE", etc. 2. Known stock/commodity/forex → use xyz: prefix: "xyz:NVDA", "xyz:TSLA", "xyz:GOLD", etc. 3. Unsure → resolve with tool calls:
- First try
hl_market(coin="X")— if it returns a price, it's a crypto perp - If not found, try
hl_market(dex="xyz")to list all RWA markets and search the results - Use whichever returns a match
Common RWA Categories (all use xyz: prefix)
| Category | Examples |
|---|---|
| US Stocks | xyz:NVDA, xyz:TSLA, xyz:AAPL, xyz:MSFT, xyz:AMZN, xyz:GOOG, xyz:META, xyz:TSM |
| Commodities — Metals | xyz:GOLD, xyz:SILVER, xyz:COPPER, xyz:PLATINUM, xyz:PALLADIUM, xyz:ALUMINIUM |
| Commodities — Energy | xyz:CL (WTI), xyz:BRENTOIL, xyz:NATGAS, xyz:TTF (EU Gas) |
| Commodities — Agriculture | xyz:CORN, xyz:WHEAT |
| Commodities — Other | xyz:URANIUM |
| Indices | xyz:SPY |
| Forex | xyz:EUR, xyz:GBP, xyz:JPY |
If a user says "buy NVDA" or "trade GOLD", usexyz:NVDA/xyz:GOLD. These are real-world assets, not crypto.
⚠️ HIP-3 Commodity Price Lookup
All 13 commodity markets are HIP-3 builder-deployed perps. Their symbols use the xyz: prefix (e.g. xyz:GOLD, xyz:CL), NOT standard formats like XAU, XAG, or WTI.
Full commodity list: GOLD, SILVER, COPPER, PLATINUM, PALLADIUM, ALUMINIUM, CL (WTI crude), BRENTOIL, NATGAS, TTF (EU gas), CORN, WHEAT, URANIUM.
Key gotcha: HIP-3 assets are NOT included in `allMids` (the standard price feed). This means:
hl_market(coin="xyz:GOLD")may return no price or fail to find the assethl_market(dex="xyz")lists all builder markets but may not include mid prices
The reliable way to get commodity prices is `hl_candles`:
# Get latest gold price (use 1h candles, lookback=24 for 24h data)
hl_candles(coin="xyz:GOLD", interval="1h", lookback=24)
# Get latest copper price
hl_candles(coin="xyz:COPPER", interval="1h", lookback=24)
# Get latest silver price
hl_candles(coin="xyz:SILVER", interval="1h", lookback=24)The close field of the most recent candle = current price. The oldest candle's open vs latest close gives 24h change.
Prefixed Name — Same Tools
All existing tools work with xyz:TICKER — just pass the prefixed coin name:
hl_market(coin="xyz:NVDA") # Check NVIDIA stock perp price
hl_market(dex="xyz") # List ALL available RWA/stock perps
hl_orderbook(coin="xyz:NVDA") # Check liquidity
hl_leverage(coin="xyz:NVDA", leverage=3) # Set leverage (auto-isolated)
hl_order(coin="xyz:NVDA", side="buy", size=0.5, price=188) # Limit buy 0.5 NVDA
hl_order(coin="xyz:TSM", side="buy", size=1) # Market buy 1 TSM
hl_cancel(coin="xyz:NVDA", order_id=12345678) # Cancel orderExample: User Says "Buy NVIDIA"
1. Recognize NVIDIA = stock → use xyz:NVDA 2. hl_market(coin="xyz:NVDA") — Check current price, leverage limits 3. hl_leverage(coin="xyz:NVDA", leverage=3) — Set leverage (builder perps use isolated margin) 4. hl_order(coin="xyz:NVDA", side="buy", size=0.5, price=188) — Place limit buy 5. hl_fills() — Check if filled
Notes
- Builder perps (HIP-3) use isolated margin only —
hl_leveragehandles this automatically - The
dexprefix (e.g.xyz) identifies which builder deployed the perp - All tools (candles, orderbook, funding, etc.) work the same way with prefixed names
---
Common Workflows
Query Commodity Prices
User: "What's the gold price?" or "Show me commodity prices" or "Oil price?"
Name → Symbol mapping:
- Metals: GOLD→
xyz:GOLD, SILVER→xyz:SILVER, COPPER→xyz:COPPER, PLATINUM→xyz:PLATINUM, PALLADIUM→xyz:PALLADIUM, ALUMINIUM→xyz:ALUMINIUM - Energy: WTI/Crude Oil→
xyz:CL, Brent→xyz:BRENTOIL, Natural Gas→xyz:NATGAS, EU Gas→xyz:TTF - Agriculture: Corn→
xyz:CORN, Wheat→xyz:WHEAT - Other: Uranium→
xyz:URANIUM
Steps: 1. Map commodity name → HIP-3 symbol using the table above 2. hl_candles(coin="xyz:GOLD", interval="1h", lookback=24) — Get 24h of hourly candles 3. Current price = last candle's close 4. 24h change = (last_close - first_open) / first_open * 100 5. Repeat for each commodity as needed
Do NOT use `hl_market()` for commodities — HIP-3 assets are not in allMids. Always use hl_candles.
Liquidity note: CL (WTI) and BRENTOIL have the highest volume. ALUMINIUM, URANIUM, CORN, WHEAT, TTF may have zero or very low liquidity — warn the user before trading these.
Trade Crypto Perps (BTC, ETH, SOL, etc.)
User: "Buy BTC" or "Long ETH with 5x"
Agent workflow: 1. hl_total_balance() → Check available funds 2. hl_leverage(coin="BTC", leverage=5) → Set leverage 3. hl_order(...) → Place order 4. hl_fills() → Verify fill and report result
Trade Stocks/RWA (NVIDIA, TESLA, GOLD, etc.)
User: "Buy NVIDIA" or "Short TESLA"
Agent workflow: 1. Detect asset type → Convert "NVIDIA" to "xyz:NVDA" 2. hl_total_balance() → Check available funds 3. hl_leverage(coin="xyz:NVDA", leverage=10) → Set leverage 4. hl_order(coin="xyz:NVDA", ...) → Place order 5. hl_fills() → Verify fill and report result
Close Positions
User: "Close my BTC position"
Agent workflow: 1. hl_account() → Get current position size 2. hl_order(coin="BTC", side="sell", size=X, reduce_only=true) → Close position 3. hl_fills() → Report PnL
Grid / Market-Making Bot Loop (service mode)
For always-on bots running inside FastAPI/worker services:
1. Read open orders via get_open_orders(address) 2. Read fills via get_user_fills(address) 3. Use fills as source of truth for "order executed" events 4. On fill:
- buy fill → place paired sell at next grid level
- sell fill → place paired buy at previous grid level
5. Keep periodic reconciliation: local state vs exchange open orders
Important: Do not treat "order disappeared from open orders" as guaranteed fill. It can also mean cancel/reject/expired. Always confirm with get_user_fills (or get_order_status when needed).
Spot Trading
User: "Buy 100 HYPE tokens"
Agent workflow: 1. hl_total_balance() → Check available USDC 2. hl_spot_order(coin="HYPE", side="buy", size=100) → Buy tokens 3. hl_balances() → Verify purchase
Deposit/Withdraw Funds
Deposit: User: "Deposit $500 USDC to Hyperliquid" Agent: hl_deposit(amount=500) → Done
Withdraw: User: "Withdraw $100 to my Arbitrum wallet" Agent: hl_withdraw(amount=100) → Done (5 min, 1 USDC fee)
---
Order Types
| Type | Parameter | Behavior |
|---|---|---|
| Limit (GTC) | order_type="limit" | Rests on book until filled or cancelled |
| Market (IoC) | omit price | Immediate-or-Cancel at mid +/- 3% slippage |
| Post-Only (ALO) | order_type="alo" | Rejected if it would cross the spread |
| Fill-or-Kill | order_type="ioc" + explicit price | Fill immediately at price or cancel |
| Stop Loss | hl_tpsl_order with tpsl="sl" | Triggers when price drops to limit losses |
| Take Profit | hl_tpsl_order with tpsl="tp" | Triggers when price rises to lock gains |
---
Stop Loss & Take Profit Orders
Stop loss and take profit orders are trigger orders that automatically execute when the market reaches a specified price level. Use these to manage risk and lock in profits without monitoring positions 24/7.
How They Work
1. Order Placement: Place a dormant trigger order with a trigger price 2. Monitoring: Order sits inactive, watching the market price 3. Trigger: When market price reaches trigger_px, order activates 4. Execution: Order executes immediately (as market or limit order)
Stop Loss (SL)
Use case: Limit losses on a position by automatically exiting if price moves against you.
Example: You're long BTC at $95,000 and want to exit if it drops below $90,000.
hl_tpsl_order(coin="BTC", side="sell", size=0.1, trigger_px=90000, tpsl="sl")- trigger_px=90000: Activates when BTC drops to $90k
- side="sell": Closes your long position
- tpsl="sl": Marks this as a stop loss order
- Default: Executes as market order when triggered (instant exit)
Take Profit (TP)
Use case: Lock in gains by automatically exiting when price reaches your profit target.
Example: You're long ETH at $3,000 and want to take profit at $3,500.
hl_tpsl_order(coin="ETH", side="sell", size=1.0, trigger_px=3500, tpsl="tp")- trigger_px=3500: Activates when ETH rises to $3,500
- side="sell": Closes your long position
- tpsl="tp": Marks this as a take profit order
- Default: Executes as market order when triggered (instant exit)
Market vs Limit Execution
By default, TP/SL orders execute as market orders when triggered (instant fill, possible slippage).
For more control, use a limit order when triggered:
hl_tpsl_order(
coin="BTC",
side="sell",
size=0.1,
trigger_px=90000,
tpsl="sl",
is_market=false,
limit_px=89900
)- trigger_px=90000: Activates at $90k
- is_market=false: Use limit order (not market)
- limit_px=89900: Limit price when triggered ($89,900)
Trade-off: Limit orders avoid slippage but may not fill in fast-moving markets.
Short Positions
For short positions, reverse the side parameter:
Stop loss on short (exit if price rises):
hl_tpsl_order(coin="BTC", side="buy", size=0.1, trigger_px=98000, tpsl="sl")Take profit on short (exit if price drops):
hl_tpsl_order(coin="BTC", side="buy", size=0.1, trigger_px=92000, tpsl="tp")Best Practices
1. Always use `reduce_only=true` (default) - ensures TP/SL only closes positions, never opens new ones 2. Match size to position - TP/SL size should equal or be less than your position size 3. Set both TP and SL - protect both upside (take profit) and downside (stop loss) 4. Account for volatility - don't set stops too tight or they'll trigger on normal price swings 5. Check open orders - use hl_open_orders to verify TP/SL orders are active
Common Mistakes
| Mistake | Problem | Solution |
|---|---|---|
| Wrong side | SL buys instead of sells | Long position → side="sell" for SL/TP |
| Size too large | TP/SL opens new position | Set size ≤ position size, use reduce_only=true |
| Trigger = limit | Confusion about prices | trigger_px = when to activate, limit_px = execution price |
| No SL on leverage | Liquidation risk | Always set stop loss on leveraged positions |
---
Perps vs Spot
| Aspect | Perps | Spot |
|---|---|---|
| Tool | hl_order | hl_spot_order |
| Leverage | Yes (up to asset max) | No |
| Funding | Paid/received every hour | None |
| Short selling | Yes (native) | Must own tokens to sell |
| Check positions | hl_account | hl_balances |
---
Risk Management
- Always check account state before trading — know your margin usage and existing positions
- Set leverage explicitly before opening new positions (default may vary)
- Use reduce_only when closing to avoid accidentally opening the opposite direction
- Monitor funding rates — high positive funding means longs are expensive to hold
- Start with small sizes — Hyperliquid has minimum order sizes per asset (check szDecimals)
- Post-only (ALO) orders save on fees (maker vs taker rates)
- Check fills after market orders — IoC orders may partially fill or not fill at all
---
Common Errors
| Error | Fix |
|---|---|
| "Unknown perp asset" | Check coin name. Crypto: "BTC", "ETH". Stocks: "xyz:NVDA", "xyz:TSLA" |
| "Insufficient margin" | Use hl_total_balance to check funds. Reduce size or add more USDC |
| "Order must have minimum value of $10" | Increase size. Formula: size × price ≥ $10 |
| "Size too small" | BTC min is typically 0.001. Check asset's szDecimals |
| "Order would cross" | ALO order rejected. Use regular limit order instead |
| "User or wallet does not exist" | Deposit USDC first with hl_deposit(amount=500) |
| "Minimum deposit is 5 USDC" | Hyperliquid requires at least $5 per deposit |
| "Policy violation" | Load wallet-policy skill and propose wildcard policy |
| "Action disabled when unified account is active" | Transfers blocked in unified mode (default). Just place orders directly |
| "'side' must be one of: buy/sell ..." | You passed an unrecognized direction. Use "buy" or "sell". See Side Parameter Convention above |
"""
Hyperliquid Extension — Perpetual & Spot Trading on Hyperliquid DEX
Provides 19 tools for trading on Hyperliquid:
- 8 info tools: account state, balances, orders, market data, orderbook, fills, candles, funding
- 11 exchange tools: order, spot order, TP/SL order, cancel, cancel all, modify, leverage, USDC transfer, withdraw, deposit, set abstraction
Environment Variables:
- WALLET_SERVICE_URL: Privy wallet service URL (required for signing)
Usage:
This extension is auto-loaded by the ExtensionLoader.
"""
import logging
from typing import List
logger = logging.getLogger(__name__)
def register(api) -> List[str]:
"""
Extension entry point — register all Hyperliquid tools.
Args:
api: ExtensionApi instance with registry and config
Returns:
List of registered tool names
"""
registered = []
try:
from .tools import (
# Info tools (8)
HLAccountTool,
HLBalancesTool,
HLOpenOrdersTool,
HLMarketTool,
HLOrderbookTool,
HLFillsTool,
HLCandlesTool,
HLFundingTool,
# Exchange tools (11)
HLOrderTool,
HLSpotOrderTool,
HLTPSLOrderTool,
HLCancelTool,
HLCancelAllTool,
HLModifyTool,
HLLeverageTool,
HLTransferUsdTool,
HLWithdrawTool,
HLDepositTool,
HLSetAbstractionTool,
)
# Info tools
api.register_tool(HLAccountTool())
api.register_tool(HLBalancesTool())
api.register_tool(HLOpenOrdersTool())
api.register_tool(HLMarketTool())
api.register_tool(HLOrderbookTool())
api.register_tool(HLFillsTool())
api.register_tool(HLCandlesTool())
api.register_tool(HLFundingTool())
# Exchange tools
api.register_tool(HLOrderTool())
api.register_tool(HLSpotOrderTool())
api.register_tool(HLTPSLOrderTool())
api.register_tool(HLCancelTool())
api.register_tool(HLCancelAllTool())
api.register_tool(HLModifyTool())
api.register_tool(HLLeverageTool())
api.register_tool(HLTransferUsdTool())
api.register_tool(HLWithdrawTool())
api.register_tool(HLDepositTool())
api.register_tool(HLSetAbstractionTool())
registered = [
# Info (8)
"hl_account",
"hl_balances",
"hl_open_orders",
"hl_market",
"hl_orderbook",
"hl_fills",
"hl_candles",
"hl_funding",
# Exchange (11)
"hl_order",
"hl_spot_order",
"hl_tpsl_order",
"hl_cancel",
"hl_cancel_all",
"hl_modify",
"hl_leverage",
"hl_transfer_usd",
"hl_withdraw",
"hl_deposit",
"hl_set_abstraction",
]
logger.info(f"Registered Hyperliquid tools ({len(registered)} tools)")
except Exception as e:
logger.warning(f"Failed to load Hyperliquid tools: {e}")
return registered
# Extension metadata
EXTENSION_INFO = {
"name": "hyperliquid",
"version": "1.0.0",
"description": "Hyperliquid DEX trading — perpetual futures and spot",
"tools": [
"hl_account",
"hl_balances",
"hl_open_orders",
"hl_market",
"hl_orderbook",
"hl_fills",
"hl_candles",
"hl_funding",
"hl_order",
"hl_spot_order",
"hl_tpsl_order",
"hl_cancel",
"hl_cancel_all",
"hl_modify",
"hl_leverage",
"hl_transfer_usd",
"hl_withdraw",
"hl_deposit",
"hl_set_abstraction",
],
"env_vars": [
"WALLET_SERVICE_URL",
],
}
"""
Hyperliquid API Client — wraps /info (read-only) and /exchange (signed) endpoints.
Uses the hyperliquid-python-sdk Info class for reads, and custom signing
(via wallet service) for exchange actions.
"""
import logging
import os
import time
from decimal import Decimal
from typing import Any, Dict, Optional
import aiohttp
from .signing import sign_l1_action, sign_user_action, sign_user_set_abstraction
logger = logging.getLogger(__name__)
DEFAULT_API_URL = "https://api.hyperliquid.xyz"
DEFAULT_SLIPPAGE = 0.03 # 3% for market orders
def float_to_wire(x: float) -> str:
"""Convert a float to a string matching Hyperliquid's server-side normalization.
Matches the official Hyperliquid Python SDK: rounds to 8 decimal places,
then strips trailing zeros via Decimal.normalize().
"""
rounded = f"{x:.8f}"
if abs(float(rounded) - x) >= 1e-12:
raise ValueError(f"float_to_wire: rounding loses precision for {x}")
if rounded == "-0.00000000":
rounded = "0.00000000"
normalized = Decimal(rounded).normalize()
return f"{normalized:f}"
# Hyperliquid bridge contract (for USDC deposits)
BRIDGE_ADDRESS = "0x2Df1c51E09aECF9cacB7bc98cB1742757f163dF7"
# USDC on Arbitrum
USDC_ADDRESS = "0xaf88d065e77c8cC2239327C5EDb3A432268e5831"
USDC_DECIMALS = 6
ARBITRUM_CHAIN_ID = 42161
MIN_DEPOSIT_USDC = 5
class HyperliquidClient:
"""
Async Hyperliquid client.
- Info methods: POST to /info (no auth)
- Exchange methods: POST to /exchange (signed)
"""
def __init__(self, api_url: Optional[str] = None):
self.api_url = api_url or os.environ.get(
"HYPERLIQUID_API_URL", DEFAULT_API_URL
)
# Cached metadata
self._meta: Optional[dict] = None
self._spot_meta: Optional[dict] = None
self._name_to_index: Dict[str, int] = {}
self._spot_name_to_index: Dict[str, int] = {}
self._spot_mid_key: Dict[str, str] = {} # user name → allMids key
self._sz_decimals: Dict[str, int] = {}
# Builder dex (HIP-3) metadata — lazy loaded
self._perp_dexs: Optional[list] = None # perpDexs API response
self._dex_offsets: Dict[str, int] = {} # dex_name -> asset index offset
self._builder_meta: Dict[str, dict] = {} # dex_name -> meta response
# ── Internal helpers ─────────────────────────────────────────────────
async def _post(self, endpoint: str, payload: dict) -> Any:
"""POST to Hyperliquid API."""
url = f"{self.api_url}{endpoint}"
async with aiohttp.ClientSession() as session:
async with session.post(
url,
json=payload,
timeout=aiohttp.ClientTimeout(total=15),
) as resp:
if resp.status >= 400:
body = await resp.text()
raise Exception(f"Hyperliquid API {resp.status}: {body}")
data = await resp.json()
# Hyperliquid returns HTTP 200 with {"status": "err", "response": "..."} for API-level errors
if isinstance(data, dict) and data.get("status") == "err":
raise Exception(f"Hyperliquid error: {data.get('response', data)}")
return data
async def _info(self, req_type: str, **kwargs) -> Any:
"""Query the /info endpoint."""
payload = {"type": req_type, **kwargs}
return await self._post("/info", payload)
async def _exchange(
self,
action: dict,
nonce: Optional[int] = None,
vault_address: Optional[str] = None,
) -> Any:
"""Submit a signed action to the /exchange endpoint.
Matches SDK's Exchange._post_action() behavior.
"""
if nonce is None:
nonce = int(time.time() * 1000)
action_type = action.get("type", "unknown")
wallet_addr = self._cached_address or "unknown"
logger.info(
f"_exchange: action_type={action_type}, wallet={wallet_addr}, nonce={nonce}"
)
signature = await sign_l1_action(action, nonce, vault_address)
# Match SDK payload structure exactly
# vaultAddress: included for most actions, None for usdClassTransfer/sendAsset
# expiresAfter: None by default (matches SDK's expires_after = None)
payload = {
"action": action,
"nonce": nonce,
"signature": signature,
"vaultAddress": vault_address if action_type not in ["usdClassTransfer", "sendAsset"] else None,
"expiresAfter": None, # matches SDK default
}
try:
return await self._post("/exchange", payload)
except Exception as e:
logger.error(
f"_exchange FAILED: action_type={action_type}, wallet={wallet_addr}, "
f"nonce={nonce}, error={e}"
)
raise
async def _ensure_meta(self) -> None:
"""Fetch and cache perp + spot metadata for asset index resolution."""
if self._meta is None:
self._meta = await self._info("meta")
for i, asset in enumerate(self._meta.get("universe", [])):
name = asset["name"]
self._name_to_index[name] = i
self._sz_decimals[name] = asset.get("szDecimals", 0)
if self._spot_meta is None:
try:
self._spot_meta = await self._info("spotMeta")
for pair in self._spot_meta.get("universe", []):
# universe entries: {"tokens": [1,0], "name": "PURR/USDC", "index": 0}
# Non-canonical entries have names like "@1", "@2"
pair_name = pair.get("name", "")
idx = pair.get("index", 0)
if "/" in pair_name:
# Canonical pair like "PURR/USDC" — map base name
base = pair_name.split("/")[0]
self._spot_name_to_index[base] = idx
self._spot_mid_key[base] = pair_name # allMids uses "PURR/USDC"
elif pair_name:
# Non-canonical like "@1" — map as-is
self._spot_name_to_index[pair_name] = idx
self._spot_mid_key[pair_name] = pair_name
except Exception:
self._spot_meta = {}
async def _ensure_builder_dex(self, dex_name: str) -> None:
"""Lazy-load builder dex metadata (HIP-3 perps like xyz:NVDA).
Called only when a coin with ':' prefix is encountered.
Fetches perpDexs once to discover all builder dexes and compute offsets,
then fetches meta for the specific builder dex.
"""
# Already loaded this dex
if dex_name in self._builder_meta:
return
# Fetch perpDexs list once to compute offsets
if self._perp_dexs is None:
raw = await self._info("perpDexs")
# perpDexs returns [null, {"name": "xyz", ...}, {"name": "rage", ...}, ...]
# First element is null (default dex), rest are builder dex objects.
# Offset formula matches SDK: skip index 0, then 110000 + i * 10000
self._perp_dexs = raw
idx = 0
for entry in raw:
if entry is None:
continue
name = entry if isinstance(entry, str) else entry.get("name", "")
if name:
self._dex_offsets[name] = 110000 + idx * 10000
idx += 1
if dex_name not in self._dex_offsets:
raise ValueError(
f"Unknown builder dex: {dex_name}. "
f"Available: {list(self._dex_offsets.keys())}"
)
# Fetch meta for this specific builder dex
meta = await self._info("meta", dex=dex_name)
self._builder_meta[dex_name] = meta
offset = self._dex_offsets[dex_name]
for i, asset in enumerate(meta.get("universe", [])):
asset_name = asset["name"]
# meta(dex="xyz") returns names already prefixed: "xyz:NVDA"
# Use the name as-is if it contains ':', otherwise prefix it
full_name = asset_name if ":" in asset_name else f"{dex_name}:{asset_name}"
self._name_to_index[full_name] = offset + i
self._sz_decimals[full_name] = asset.get("szDecimals", 0)
async def _resolve_asset(self, coin: str) -> int:
"""Resolve coin name to perp asset index.
Supports builder perps via 'dex:COIN' format (e.g. 'xyz:NVDA').
"""
# Try direct match first
idx = self._name_to_index.get(coin)
if idx is not None:
return idx
# Case-insensitive fallback (e.g. "ai16z" -> "AI16Z")
coin_upper = coin.upper()
for name, i in self._name_to_index.items():
if name.upper() == coin_upper:
return i
# If coin contains ':', try loading the builder dex
if ":" in coin:
dex_name = coin.split(":")[0]
await self._ensure_builder_dex(dex_name)
idx = self._name_to_index.get(coin)
if idx is not None:
return idx
raise ValueError(
f"Unknown perp asset: {coin}. "
f"Available: {list(self._name_to_index.keys())[:20]}..."
)
async def _resolve_any_asset(self, coin: str) -> int:
"""Resolve coin name to asset index — tries perps, builder perps, then spot."""
await self._ensure_meta()
try:
return await self._resolve_asset(coin)
except ValueError:
pass
# Try spot (exact then case-insensitive)
idx = self._spot_name_to_index.get(coin)
if idx is not None:
return 10000 + idx
coin_upper = coin.upper()
for name, i in self._spot_name_to_index.items():
if name.upper() == coin_upper:
return 10000 + i
raise ValueError(f"Unknown asset: {coin}")
def _resolve_spot_asset(self, coin: str) -> int:
"""Resolve coin name to spot asset index (10000 + idx)."""
idx = self._spot_name_to_index.get(coin)
if idx is None:
raise ValueError(
f"Unknown spot asset: {coin}. "
f"Available: {list(self._spot_name_to_index.keys())[:20]}..."
)
return 10000 + idx
def _format_size(self, coin: str, size: float) -> str:
"""Format size using float_to_wire for exact Hyperliquid normalization."""
return float_to_wire(size)
def _format_price(self, price: float, coin: str = "", is_spot: bool = False) -> str:
"""Format price matching the official SDK's rounding rules.
Prices can have up to 5 significant figures, but no more than
(MAX_DECIMALS - szDecimals) decimal places where MAX_DECIMALS is 6
for perps and 8 for spot. Integer prices are always allowed.
"""
# Step 1: round to 5 significant figures
rounded = float(f"{price:.5g}")
# Step 2: constrain decimal places based on szDecimals
sz_decimals = self._sz_decimals.get(coin, 0)
max_decimals = 8 if is_spot else 6
max_dp = max_decimals - sz_decimals
if max_dp >= 0:
rounded = round(rounded, max_dp)
return float_to_wire(rounded)
async def _validate_order_margin(
self,
coin: str,
size: float,
price: float,
leverage: int = 1,
is_spot: bool = False,
) -> tuple[bool, str]:
"""Validate if account has sufficient margin for an order.
Returns: (is_valid, error_message)
"""
try:
# Spot orders don't use leverage/margin
if is_spot:
return (True, "")
# Calculate order notional value
order_value = size * price
# Check minimum order value ($10)
if order_value < 10:
return (
False,
f"Order value ${order_value:.2f} is below minimum $10. "
f"Increase size to at least {10 / price:.4f} {coin}",
)
# Calculate required margin (notional / leverage)
required_margin = order_value / max(leverage, 1)
# CRITICAL: Check abstraction mode FIRST to determine which account to query
# Unified account mode keeps funds in SPOT, not PERP
address = await self._get_address()
current_mode = "default"
try:
abstraction_state = await self.get_user_abstraction_state(address)
if isinstance(abstraction_state, str):
current_mode = abstraction_state
elif isinstance(abstraction_state, dict):
current_mode = abstraction_state.get("type", abstraction_state.get("state", "default"))
except Exception:
current_mode = "default"
# Get account state based on abstraction mode
if current_mode == "unifiedAccount":
# Unified account: funds are in SPOT (shared collateral)
spot_state = await self.get_spot_state(address)
# Find USDC balance in spot
usdc_balance = 0.0
for bal in spot_state.get("balances", []):
if bal.get("coin") == "USDC":
usdc_balance = float(bal.get("total", 0))
break
# Use SPOT balance as available margin
account_value = usdc_balance
total_margin_used = 0.0 # Unified account shares collateral
available_margin = account_value
logger.info(
f"Margin validation for {coin} (UNIFIED ACCOUNT): "
f"spot_usdc=${usdc_balance:.2f}, "
f"available=${available_margin:.2f}"
)
else:
# Default/disabled mode: check PERP account
state = await self.get_account_state(address)
margin_summary = state.get("marginSummary", {})
account_value = float(margin_summary.get("accountValue", 0))
total_margin_used = float(margin_summary.get("totalMarginUsed", 0))
available_margin = account_value - total_margin_used
logger.info(
f"Margin validation for {coin} (PERP ACCOUNT): "
f"accountValue=${account_value:.2f}, "
f"totalMarginUsed=${total_margin_used:.2f}, "
f"available=${available_margin:.2f}"
)
# Require a safety buffer (5%)
safe_available = available_margin * 0.95
if required_margin > safe_available:
error_msg = (
f"Insufficient margin for {coin} order. "
f"Required: ${required_margin:.2f}, "
f"Available: ${safe_available:.2f} "
f"(Account value: ${account_value:.2f}, Used: ${total_margin_used:.2f}). "
)
# Add mode-specific guidance
if current_mode == "unifiedAccount":
error_msg += (
f"\n\n⚠️ UNIFIED ACCOUNT MODE ACTIVE: Checked SPOT balance (${account_value:.2f}) where your collateral is. "
f"If you think you have more funds, use `hl_total_balance` tool to see the complete picture. "
f"DO NOT use `hl_account` or `hl_balances` alone - they only show partial balances!"
)
elif account_value == 0:
error_msg += (
"\n\n💡 NOTE: Showing $0 balance in PERP account."
" If you have funds in SPOT, enable unified"
" account mode to share collateral across"
" spot/perp, or manually transfer USDC to"
" perp using `hl_transfer_usd`."
)
if ":" in coin:
# Builder perp specific guidance
dex_name = coin.split(":")[0]
error_msg += (
f"\n\n🏗️ BUILDER PERP ({dex_name}): "
f"DEX abstraction auto-transfers funds from main account → {dex_name} dex when order is placed. "
f"Validation checks main account available margin (not {dex_name} dex balance)."
)
error_msg += (
f"\n\n✅ SOLUTIONS:\n"
f"1) Use `hl_total_balance` to check actual available funds\n"
f"2) Reduce size to {safe_available * leverage / price:.4f} {coin} or less\n"
f"3) Increase leverage (if below max for this asset)\n"
f"4) Deposit more USDC to your Hyperliquid account\n"
f"5) Close other positions to free up margin"
)
return (False, error_msg)
return (True, "")
except Exception as e:
logger.warning(f"Order validation failed: {e}")
# Don't block order on validation errors - let exchange validate
return (True, "")
# ── Info Methods (read-only, no auth) ────────────────────────────────
async def get_account_state(self, address: str, dex: Optional[str] = None) -> dict:
"""Get perp positions, margin, account value.
Args:
address: Wallet address
dex: Builder dex name (e.g. 'xyz') for HIP-3 perp positions.
None for default perp positions.
"""
if dex:
return await self._info("clearinghouseState", user=address, dex=dex)
return await self._info("clearinghouseState", user=address)
async def get_spot_state(self, address: str) -> dict:
"""Get spot token balances."""
return await self._info("spotClearinghouseState", user=address)
async def get_open_orders(self, address: str) -> list:
"""Get all open orders."""
return await self._info("openOrders", user=address)
async def get_all_mids(self, dex: Optional[str] = None) -> dict:
"""Get current mid prices for all assets.
Args:
dex: Builder dex name (e.g. 'xyz') for builder perp mid prices.
None for default perp/spot mid prices.
"""
if dex:
return await self._info("allMids", dex=dex)
return await self._info("allMids")
async def get_l2_book(self, coin: str) -> dict:
"""Get L2 orderbook snapshot."""
return await self._info("l2Book", coin=coin)
async def get_meta(self) -> dict:
"""Get perp universe metadata."""
await self._ensure_meta()
return self._meta
async def get_spot_meta(self) -> dict:
"""Get spot universe metadata."""
await self._ensure_meta()
return self._spot_meta
async def get_candles(
self, coin: str, interval: str, start: int, end: int
) -> list:
"""Get OHLCV candlestick data."""
return await self._info(
"candleSnapshot",
req={"coin": coin, "interval": interval, "startTime": start, "endTime": end},
)
async def get_user_fills(self, address: str) -> list:
"""Get recent trade fills."""
return await self._info("userFills", user=address)
async def get_funding_history(
self, coin: str, start: int
) -> list:
"""Get historical funding rates."""
return await self._info(
"fundingHistory", coin=coin, startTime=start
)
async def get_predicted_fundings(self) -> list:
"""Get predicted next funding rates for all assets."""
return await self._info("predictedFundings")
async def get_user_fees(self, address: str) -> dict:
"""Get user fee schedule."""
return await self._info("userFees", user=address)
async def get_user_abstraction_state(self, address: str) -> dict:
"""Query current abstraction state for a user.
Returns abstraction mode: "default", "unifiedAccount", "portfolioMargin", or "disabled"
"""
return await self._info("userAbstraction", user=address)
async def get_order_status(self, address: str, oid: int) -> dict:
"""Look up a single order by oid."""
return await self._info("orderStatus", user=address, oid=oid)
# ── Exchange Methods (require signing) ───────────────────────────────
async def place_order(
self,
coin: str,
is_buy: bool,
size: float,
price: Optional[float] = None,
order_type: str = "limit",
reduce_only: bool = False,
cloid: Optional[str] = None,
is_spot: bool = False,
trigger_px: Optional[float] = None,
tpsl: Optional[str] = None,
) -> dict:
"""
Place a perp or spot order.
Args:
coin: Asset name (e.g. "BTC", "ETH")
is_buy: True for buy, False for sell
size: Order size in base asset
price: Limit price. None = market order (IoC with slippage)
order_type: "limit" (GTC), "ioc", "alo" (post-only)
reduce_only: If True, only reduces position
cloid: Optional client order ID
is_spot: If True, use spot asset index
trigger_px: Trigger price for stop loss / take profit orders
tpsl: "tp" for take profit, "sl" for stop loss
"""
await self._ensure_meta()
# Builder perps (HIP-3) need DEX abstraction for collateral
# Enable BEFORE validation so we can check actual available margin
logger.info(f"place_order DEX check: coin={coin}, is_spot={is_spot}, has_colon={':' in coin}")
if not is_spot and ":" in coin:
logger.info(f"place_order: triggering DEX abstraction for builder perp {coin}")
await self.ensure_dex_abstraction()
else:
logger.info(f"place_order: skipping DEX abstraction (is_spot={is_spot}, coin={coin})")
asset_idx = (
self._resolve_spot_asset(coin)
if is_spot
else await self._resolve_asset(coin)
)
# Market order: fetch mid price and apply slippage
# IMPORTANT: Skip this for trigger orders - they use trigger_px, not current mid
if price is None and trigger_px is None:
# Regular market order (non-trigger)
# Builder perps use dex-specific allMids
if not is_spot and ":" in coin:
dex_name = coin.split(":")[0]
mids = await self.get_all_mids(dex=dex_name)
# allMids(dex="xyz") returns keys like "xyz:NVDA", use full coin name
mid_key = coin
else:
mids = await self.get_all_mids()
# Spot uses pair name in allMids (e.g. "PURR/USDC" or "@1")
if is_spot:
mid_key = self._spot_mid_key.get(coin, coin)
else:
mid_key = coin
mid_str = mids.get(mid_key)
if not mid_str:
raise ValueError(f"No mid price for {coin} (looked up '{mid_key}')")
mid = float(mid_str)
slippage = DEFAULT_SLIPPAGE
price = mid * (1 + slippage) if is_buy else mid * (1 - slippage)
order_type = "ioc" # Force IoC for market orders
# Pre-flight validation: check margin requirements
# CRITICAL: When unified account is active, funds are shared across spot/perp/builder-dexes
# Check SPOT balance instead of perp, since that's where the collateral actually is
if not is_spot and not reduce_only:
try:
address = await self._get_address()
# Check if unified account is active
try:
abstraction_state = await self.get_user_abstraction_state(address)
if isinstance(abstraction_state, str):
current_abstraction = abstraction_state
elif isinstance(abstraction_state, dict):
current_abstraction = abstraction_state.get("type", abstraction_state.get("state", "default"))
else:
current_abstraction = "default"
except Exception:
current_abstraction = "default"
# Get margin state - check SPOT if unified, otherwise check perp
if current_abstraction == "unifiedAccount":
# Unified account: check SPOT balance (where collateral actually is)
spot_state = await self.get_spot_state(address)
# Find USDC balance in spot
usdc_balance = 0.0
for bal in spot_state.get("balances", []):
if bal.get("coin") == "USDC":
usdc_balance = float(bal.get("total", 0))
break
# Create a synthetic margin summary from spot balance
state = {
"marginSummary": {
"accountValue": str(usdc_balance),
"totalMarginUsed": "0.0", # Unified account shares collateral
"totalNtlPos": "0.0",
"totalRawUsd": str(usdc_balance),
},
"assetPositions": [], # Will check metadata for leverage
}
logger.info(f"Unified account active: using SPOT balance ${usdc_balance:.2f} for margin validation")
else:
# Default mode: check perp clearinghouse
state = await self.get_account_state(address)
# Extract leverage from assetPositions or use default
leverage = 1
for pos in state.get("assetPositions", []):
position = pos.get("position", pos)
if position.get("coin") == coin:
lev_info = position.get("leverage", {})
leverage = lev_info.get("value", 1)
break
# If no position, check metadata for default/max leverage
if leverage == 1:
if ":" in coin:
dex_name = coin.split(":")[0]
meta = self._builder_meta.get(dex_name, {})
for asset in meta.get("universe", []):
if asset["name"] == coin or asset["name"] == coin.split(":", 1)[-1]:
# Use max leverage as estimate if no position exists
leverage = asset.get("maxLeverage", 1)
break
else:
if self._meta:
for asset in self._meta.get("universe", []):
if asset["name"] == coin:
leverage = asset.get("maxLeverage", 1)
break
# Validate margin
is_valid, error_msg = await self._validate_order_margin(
coin=coin,
size=size,
price=price,
leverage=leverage,
is_spot=is_spot,
)
if not is_valid:
raise ValueError(error_msg)
except ValueError:
# Re-raise validation errors
raise
except Exception as e:
# Don't block order on validation errors
logger.warning(f"Order validation error (non-blocking): {e}")
# Build order type spec
if trigger_px is not None:
# Trigger order - use float_to_wire directly (matches SDK)
# This ensures consistent serialization when price == trigger_px
formatted_trigger = float_to_wire(trigger_px)
formatted_price = float_to_wire(price)
is_market_trigger = (price == trigger_px) if price is not None else True
# CRITICAL: Field order must match SDK exactly for msgpack hash consistency
# SDK uses: isMarket, triggerPx, tpsl (not triggerPx first!)
trigger_spec = {
"isMarket": is_market_trigger,
"triggerPx": formatted_trigger,
"tpsl": tpsl or "tp",
}
tif_spec = {"trigger": trigger_spec}
elif order_type == "limit":
# Regular orders - keep existing validation
formatted_price = self._format_price(price, coin=coin, is_spot=is_spot)
tif_spec = {"limit": {"tif": "Gtc"}}
elif order_type == "ioc":
formatted_price = self._format_price(price, coin=coin, is_spot=is_spot)
tif_spec = {"limit": {"tif": "Ioc"}}
elif order_type == "alo":
formatted_price = self._format_price(price, coin=coin, is_spot=is_spot)
tif_spec = {"limit": {"tif": "Alo"}}
else:
formatted_price = self._format_price(price, coin=coin, is_spot=is_spot)
tif_spec = {"limit": {"tif": "Gtc"}}
order = {
"a": asset_idx,
"b": is_buy,
"p": formatted_price,
"s": self._format_size(coin, size),
"r": reduce_only,
"t": tif_spec,
}
if cloid:
order["c"] = cloid
action = {
"type": "order",
"orders": [order],
"grouping": "na",
}
logger.info(
f"place_order: coin={coin}, is_buy={is_buy}, size={size}, "
f"price={formatted_price}, order_type={order_type}, "
f"asset_idx={asset_idx}"
)
# DEBUG: Log the full action structure for trigger orders
if trigger_px is not None:
import json
logger.info(f"TRIGGER ORDER ACTION: {json.dumps(action, indent=2)}")
result = await self._exchange(action)
logger.info(f"place_order result: {result}")
return result
async def cancel_order(self, coin: str, oid: int) -> dict:
"""Cancel an order by oid."""
await self._ensure_meta()
asset_idx = await self._resolve_any_asset(coin)
action = {
"type": "cancel",
"cancels": [{"a": asset_idx, "o": oid}],
}
return await self._exchange(action)
async def cancel_by_cloid(self, coin: str, cloid: str) -> dict:
"""Cancel an order by client order ID."""
await self._ensure_meta()
asset_idx = await self._resolve_any_asset(coin)
action = {
"type": "cancelByCloid",
"cancels": [{"asset": asset_idx, "cloid": cloid}],
}
return await self._exchange(action)
async def cancel_all(self, coin: Optional[str] = None) -> list:
"""
Cancel all open orders, optionally filtered by coin.
Returns list of cancel results.
"""
# No batch cancel action in HL — cancel individually
address = await self._get_address()
orders = await self.get_open_orders(address)
if coin:
orders = [o for o in orders if o.get("coin") == coin]
results = []
for order in orders:
try:
r = await self.cancel_order(
order["coin"], order["oid"]
)
results.append(r)
except Exception as e:
results.append({"error": str(e), "oid": order.get("oid")})
return results
async def modify_order(
self,
oid: int,
coin: str,
is_buy: bool,
size: float,
price: float,
order_type: str = "limit",
) -> dict:
"""Modify an existing order."""
await self._ensure_meta()
asset_idx = await self._resolve_any_asset(coin)
if order_type == "limit":
tif_spec = {"limit": {"tif": "Gtc"}}
elif order_type == "ioc":
tif_spec = {"limit": {"tif": "Ioc"}}
elif order_type == "alo":
tif_spec = {"limit": {"tif": "Alo"}}
else:
tif_spec = {"limit": {"tif": "Gtc"}}
action = {
"type": "modify",
"oid": oid,
"order": {
"a": asset_idx,
"b": is_buy,
"p": self._format_price(price, coin=coin),
"s": self._format_size(coin, size),
"r": False,
"t": tif_spec,
},
}
return await self._exchange(action)
# ── DEX Abstraction (HIP-3 builder perps collateral) ─────────────────
_dex_abstraction_enabled: bool = False
async def ensure_dex_abstraction(self) -> dict:
"""Enable DEX abstraction so collateral auto-transfers to builder dexes.
Required for trading HIP-3 builder perps (xyz:NVDA, xyz:TSLA, etc.).
Without this, the wallet has no collateral in builder dex clearinghouses
and all orders fail with "Insufficient margin".
Checks current abstraction state first, then tries multiple methods:
1. agentSetAbstraction("u") - modern agent-signed approach
2. agentEnableDexAbstraction - legacy agent-signed approach
3. userSetAbstraction("unifiedAccount") - user-signed fallback
Safe to call multiple times — Hyperliquid handles idempotency.
"""
address = await self._get_address()
# ALWAYS check current state first - don't trust cache
# The cache might be stale if the state was changed externally or if enable failed
try:
state_result = await self.get_user_abstraction_state(address)
# API may return string ("default") or dict ({"type": "default"})
if isinstance(state_result, str):
current_state = state_result
elif isinstance(state_result, dict):
current_state = state_result.get("type", state_result.get("state", "unknown"))
else:
current_state = "unknown"
logger.info(f"DEX abstraction: current state for {address} = {current_state}")
# If already in unifiedAccount mode, we're done
if current_state == "unifiedAccount":
logger.info("DEX abstraction: already enabled (unifiedAccount mode)")
self._dex_abstraction_enabled = True
return {"status": "already_enabled", "state": current_state}
# If cache says enabled but state check shows otherwise, reset cache
if self._dex_abstraction_enabled and current_state != "unifiedAccount":
logger.warning(
f"DEX abstraction: cache says enabled but state is '{current_state}'. "
f"Resetting cache and retrying enable."
)
self._dex_abstraction_enabled = False
# SDK comment says: "the account must be in 'default' mode to succeed"
if current_state not in ("default", "disabled"):
logger.warning(
f"DEX abstraction: account in {current_state} mode. "
f"Transitions may fail if not in 'default' mode."
)
except Exception as e:
logger.warning(f"DEX abstraction: failed to query current state: {e}")
# Continue anyway, state check is informational
# Try Method 1: agentSetAbstraction with "u" (unified account)
logger.info(f"DEX abstraction: trying agentSetAbstraction(u) for {address}")
action = {
"type": "agentSetAbstraction",
"abstraction": "u", # "u" = unified account (DEX abstraction enabled)
}
try:
result = await self._exchange(action, vault_address=None)
logger.info(f"DEX abstraction: agentSetAbstraction result = {result}")
self._dex_abstraction_enabled = True
return result
except Exception as e:
err_str = str(e).lower()
logger.warning(f"DEX abstraction: agentSetAbstraction failed: {e}")
# If it says "already" or "enabled", that's success
if "already" in err_str or "enabled" in err_str:
self._dex_abstraction_enabled = True
return {"status": "ok", "detail": str(e)}
# Try Method 2: agentEnableDexAbstraction (legacy)
logger.info(f"DEX abstraction: trying agentEnableDexAbstraction for {address}")
action = {
"type": "agentEnableDexAbstraction",
}
try:
result = await self._exchange(action, vault_address=None)
logger.info(f"DEX abstraction: agentEnableDexAbstraction result = {result}")
self._dex_abstraction_enabled = True
return result
except Exception as e:
err_str = str(e).lower()
logger.warning(f"DEX abstraction: agentEnableDexAbstraction failed: {e}")
if "already" in err_str or "enabled" in err_str:
self._dex_abstraction_enabled = True
return {"status": "ok", "detail": str(e)}
# Try Method 3: userSetAbstraction (user-signed, not agent-signed)
# This uses a different signing domain (HyperliquidSignTransaction vs Agent)
# SDK example shows this works when account is in "default" mode
logger.info(f"DEX abstraction: trying userSetAbstraction(unifiedAccount) for {address}")
try:
result = await self._user_set_abstraction(address, "unifiedAccount")
logger.info(f"DEX abstraction: userSetAbstraction result = {result}")
# Verify the state actually changed
try:
new_state_result = await self.get_user_abstraction_state(address)
if isinstance(new_state_result, str):
new_state = new_state_result
elif isinstance(new_state_result, dict):
new_state = new_state_result.get("type", new_state_result.get("state", "unknown"))
else:
new_state = "unknown"
logger.info(f"DEX abstraction: state AFTER userSetAbstraction = {new_state}")
if new_state == "unifiedAccount":
logger.info("DEX abstraction: successfully enabled (verified)")
self._dex_abstraction_enabled = True
return result
else:
logger.warning(
f"DEX abstraction: userSetAbstraction returned success but state is still '{new_state}', not 'unifiedAccount'. "
f"This may indicate the account has restrictions. Full result: {result}"
)
except Exception as verify_err:
logger.warning(f"DEX abstraction: failed to verify state after userSetAbstraction: {verify_err}")
self._dex_abstraction_enabled = True
return result
except Exception as e:
err_str = str(e).lower()
logger.error(f"DEX abstraction: userSetAbstraction failed: {e}")
if "already" in err_str or "enabled" in err_str:
self._dex_abstraction_enabled = True
return {"status": "ok", "detail": str(e)}
# All methods failed
raise RuntimeError(
f"Failed to enable DEX abstraction for {address}. "
f"Your account may have DEX abstraction manually disabled. "
f"Please enable it at app.hyperliquid.xyz:\n"
f"1. Go to Settings (top right)\n"
f"2. Find 'Disable HIP-3 Dex Abstraction' checkbox\n"
f"3. UNCHECK it to enable DEX abstraction\n"
f"4. Then retry your order"
)
async def _user_set_abstraction(self, user: str, abstraction: str) -> dict:
"""Set abstraction mode for user (user-signed action).
Matches SDK's user_set_abstraction exactly.
Args:
user: User wallet address
abstraction: "unifiedAccount", "portfolioMargin", or "disabled"
Returns: API response
"""
nonce = int(time.time() * 1000)
# Action payload must include signatureChainId and hyperliquidChain
# These are required for all user-signed actions
action = {
"type": "userSetAbstraction",
"user": user.lower(),
"abstraction": abstraction,
"nonce": nonce,
"signatureChainId": "0xa4b1", # 42161 in hex (Arbitrum mainnet)
"hyperliquidChain": "Mainnet",
}
signature = await sign_user_set_abstraction(
user=user,
abstraction=abstraction,
nonce=nonce,
)
payload = {
"action": action,
"nonce": nonce,
"signature": signature,
}
return await self._post("/exchange", payload)
async def update_leverage(
self, coin: str, leverage: int, is_cross: bool = True
) -> dict:
"""Set leverage for a perp."""
await self._ensure_meta()
# Builder perps need DEX abstraction
logger.info(f"update_leverage DEX check: coin={coin}, has_colon={':' in coin}")
if ":" in coin:
logger.info(f"update_leverage: triggering DEX abstraction for builder perp {coin}")
await self.ensure_dex_abstraction()
else:
logger.info(f"update_leverage: skipping DEX abstraction for {coin}")
asset_idx = await self._resolve_asset(coin)
action = {
"type": "updateLeverage",
"asset": asset_idx,
"isCross": is_cross,
"leverage": leverage,
}
return await self._exchange(action)
async def market_open(
self,
coin: str,
is_buy: bool,
size: float,
slippage: float = DEFAULT_SLIPPAGE,
is_spot: bool = False,
) -> dict:
"""Convenience: open a position with an IoC market order."""
return await self.place_order(
coin=coin,
is_buy=is_buy,
size=size,
price=None, # triggers market order logic
order_type="ioc",
is_spot=is_spot,
)
async def market_close(
self, coin: str, address: str, slippage: float = DEFAULT_SLIPPAGE
) -> dict:
"""Close entire perp position for a coin."""
# Builder perps (HIP-3) have separate clearinghouse state per dex
if ":" in coin:
dex_name = coin.split(":")[0]
state = await self._info("clearinghouseState", user=address, dex=dex_name)
else:
state = await self.get_account_state(address)
positions = state.get("assetPositions", [])
pos = None
for p in positions:
position = p.get("position", p)
if position.get("coin") == coin:
pos = position
break
if not pos:
raise ValueError(f"No open position for {coin}")
size = abs(float(pos["szi"]))
is_buy = float(pos["szi"]) < 0 # Close short = buy, close long = sell
return await self.place_order(
coin=coin,
is_buy=is_buy,
size=size,
price=None,
order_type="ioc",
reduce_only=True,
)
async def transfer_usd(
self, amount: float, to_perp: bool = True
) -> dict:
"""
Transfer USDC between spot and perp.
Uses user-signed action (HyperliquidSignTransaction domain).
"""
address = await self._get_address()
# Query balances BEFORE transfer for verification
try:
perp_state_before = await self.get_account_state(address)
spot_state_before = await self.get_spot_state(address)
perp_value_before = float(perp_state_before.get("marginSummary", {}).get("accountValue", 0))
spot_balances_before = spot_state_before.get("balances", [])
logger.info(
f"transfer_usd: BEFORE transfer - "
f"perp_value=${perp_value_before:.2f}, "
f"spot_balances={spot_balances_before}"
)
except Exception as e:
logger.warning(f"transfer_usd: failed to query balances before transfer: {e}")
nonce = int(time.time() * 1000)
action = {
"type": "usdClassTransfer",
"amount": str(amount),
"toPerp": to_perp,
"nonce": nonce,
"signatureChainId": "0xa4b1", # 42161 in hex (Arbitrum mainnet)
"hyperliquidChain": "Mainnet",
}
types = {
"HyperliquidTransaction:UsdClassTransfer": [
{"name": "hyperliquidChain", "type": "string"},
{"name": "amount", "type": "string"},
{"name": "toPerp", "type": "bool"},
{"name": "nonce", "type": "uint64"},
]
}
signature = await sign_user_action(
action={
"hyperliquidChain": "Mainnet",
"amount": str(amount),
"toPerp": to_perp,
"nonce": nonce,
},
types=types,
primary_type="HyperliquidTransaction:UsdClassTransfer",
)
payload = {
"action": action,
"nonce": nonce,
"signature": signature,
}
logger.info(f"transfer_usd: submitting ${amount} transfer ({'spot→perp' if to_perp else 'perp→spot'})")
try:
result = await self._post("/exchange", payload)
logger.info(f"transfer_usd: API response SUCCESS = {result}")
except Exception as api_error:
logger.error(f"transfer_usd: API call FAILED: {api_error}", exc_info=True)
# Re-raise so caller sees the error
raise
# Query balances AFTER transfer for verification
try:
perp_state_after = await self.get_account_state(address)
spot_state_after = await self.get_spot_state(address)
perp_value_after = float(perp_state_after.get("marginSummary", {}).get("accountValue", 0))
spot_balances_after = spot_state_after.get("balances", [])
logger.info(
f"transfer_usd: AFTER transfer - "
f"perp_value=${perp_value_after:.2f}, "
f"spot_balances={spot_balances_after}"
)
except Exception as e:
logger.warning(f"transfer_usd: failed to query balances after transfer: {e}")
return result
async def withdraw_from_bridge(
self, amount: float, destination: Optional[str] = None
) -> dict:
"""
Withdraw USDC from Hyperliquid to an Arbitrum wallet (L1 bridge withdrawal).
Fee: 1 USDC (deducted by Hyperliquid). Processing: ~5 minutes.
Args:
amount: USDC amount to withdraw
destination: Target wallet address (defaults to agent's own wallet)
"""
if not destination:
destination = await self._get_address()
nonce = int(time.time() * 1000)
action = {
"type": "withdraw3",
"hyperliquidChain": "Mainnet",
"signatureChainId": "0xa4b1", # 42161 in hex (Arbitrum mainnet)
"destination": destination,
"amount": str(amount),
"time": nonce,
}
types = {
"HyperliquidTransaction:Withdraw": [
{"name": "hyperliquidChain", "type": "string"},
{"name": "destination", "type": "string"},
{"name": "amount", "type": "string"},
{"name": "time", "type": "uint64"},
]
}
signature = await sign_user_action(
action={
"hyperliquidChain": "Mainnet",
"destination": destination,
"amount": str(amount),
"time": nonce,
},
types=types,
primary_type="HyperliquidTransaction:Withdraw",
)
payload = {
"action": action,
"nonce": nonce,
"signature": signature,
}
return await self._post("/exchange", payload)
async def deposit_usdc(self, amount: float) -> dict:
"""
Deposit USDC from agent's Arbitrum wallet into Hyperliquid.
Sends an ERC-20 transfer of USDC to the Hyperliquid bridge contract.
Minimum deposit: 5 USDC.
Args:
amount: USDC amount to deposit (minimum 5)
"""
from eth_utils import keccak
from core.wallet_runtime import wallet_request as _wallet_request, is_fly_machine as _is_fly_machine
if not _is_fly_machine():
raise RuntimeError("Not running on a Fly Machine — wallet unavailable")
if amount < MIN_DEPOSIT_USDC:
raise ValueError(f"Minimum deposit is {MIN_DEPOSIT_USDC} USDC")
amount_base = int(amount * (10 ** USDC_DECIMALS))
# Step 1: ERC-20 approve(bridge, amount)
approve_selector = keccak(b"approve(address,uint256)")[:4]
bridge_bytes = bytes.fromhex(BRIDGE_ADDRESS.replace("0x", "")).rjust(32, b"\x00")
amount_bytes = amount_base.to_bytes(32, "big")
approve_data = "0x" + (approve_selector + bridge_bytes + amount_bytes).hex()
logger.info(f"HL deposit: approve TX (amount_base={amount_base})")
approve_result = await _wallet_request("POST", "/agent/transfer", {
"to": USDC_ADDRESS,
"amount": "0",
"data": approve_data,
"chain_id": ARBITRUM_CHAIN_ID,
})
approve_tx = approve_result.get("tx_hash", approve_result.get("hash", "unknown"))
logger.info(f"HL deposit: approve TX = {approve_tx}")
# Step 2: ERC-20 transfer(bridge, amount)
transfer_selector = keccak(b"transfer(address,uint256)")[:4]
transfer_data = "0x" + (transfer_selector + bridge_bytes + amount_bytes).hex()
logger.info(f"HL deposit: transfer TX to bridge {BRIDGE_ADDRESS}")
transfer_result = await _wallet_request("POST", "/agent/transfer", {
"to": USDC_ADDRESS,
"amount": "0",
"data": transfer_data,
"chain_id": ARBITRUM_CHAIN_ID,
})
transfer_tx = transfer_result.get("tx_hash", transfer_result.get("hash", "unknown"))
logger.info(f"HL deposit: transfer TX = {transfer_tx}")
return {
"approve_tx_hash": approve_tx,
"transfer_tx_hash": transfer_tx,
"amount_deposited": amount,
"amount_base_units": amount_base,
"bridge_contract": BRIDGE_ADDRESS,
"chain_id": ARBITRUM_CHAIN_ID,
}
# ── Address helper ───────────────────────────────────────────────────
_cached_address: Optional[str] = None
async def _get_address(self) -> str:
"""Get the agent's EVM address from wallet service (cached)."""
if self._cached_address:
return self._cached_address
from core.wallet_runtime import wallet_request as _wallet_request, is_fly_machine as _is_fly_machine
if not _is_fly_machine():
raise RuntimeError("Not running on Fly — wallet unavailable")
data = await _wallet_request("GET", "/agent/wallet")
wallets = data if isinstance(data, list) else data.get("wallets", [])
for w in wallets:
if w.get("chain_type") == "ethereum":
self._cached_address = w["wallet_address"]
return self._cached_address
raise RuntimeError("No ethereum wallet found")
"""
Hyperliquid skill exports — read-only tools, sync via requests.
Bypasses the async client entirely. Uses direct POST to /info endpoint.
Wallet address resolved via Fly OIDC → wallet-service.
Write operations (hl_order, hl_cancel, etc.) are NOT exported — they require
the agent's signing pipeline. Use /chat/stream to invoke those from task scripts.
Usage in task scripts:
from core.skill_tools import hyperliquid
account = hyperliquid.hl_account()
mids = hyperliquid.hl_market()
candles = hyperliquid.hl_candles(coin="BTC", interval="1h", hours_back=24)
"""
import os
import json
import time
import http.client
import socket
import requests
HL_API = os.environ.get("HYPERLIQUID_API_URL", "https://api.hyperliquid.xyz")
FLY_API_SOCKET = "/.fly/api"
WALLET_SERVICE_URL = os.environ.get("WALLET_SERVICE_URL", "https://wallet-service-dev.fly.dev")
OIDC_AUDIENCE = os.environ.get("WALLET_OIDC_AUDIENCE", WALLET_SERVICE_URL)
_cached_address = None
def _get_oidc_token():
"""Get OIDC token from Fly unix socket."""
conn = http.client.HTTPConnection("localhost")
conn.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
conn.sock.connect(FLY_API_SOCKET)
body = json.dumps({"aud": OIDC_AUDIENCE}).encode()
conn.request("POST", "/v1/tokens/oidc", body=body,
headers={"Host": "localhost", "Content-Type": "application/json"})
resp = conn.getresponse()
token = resp.read().decode().strip()
conn.close()
return token
def _get_address():
"""Get agent's EVM wallet address (cached)."""
global _cached_address
if _cached_address:
return _cached_address
if not os.path.exists(FLY_API_SOCKET):
raise RuntimeError("Not on Fly machine — wallet unavailable")
token = _get_oidc_token()
r = requests.get(f"{WALLET_SERVICE_URL}/agent/wallet",
headers={"Authorization": f"Bearer {token}"}, timeout=15)
r.raise_for_status()
data = r.json()
for w in (data if isinstance(data, list) else data.get("wallets", [])):
if w.get("chain_type") == "ethereum":
_cached_address = w["wallet_address"]
return _cached_address
raise RuntimeError("No ethereum wallet found")
def _info(req_type, **kwargs):
"""POST to Hyperliquid /info endpoint."""
payload = {"type": req_type, **kwargs}
r = requests.post(f"{HL_API}/info", json=payload, timeout=15)
r.raise_for_status()
data = r.json()
if isinstance(data, dict) and data.get("status") == "err":
raise Exception(f"Hyperliquid error: {data.get('response', data)}")
return data
# ── Exported read-only tools (names match SKILL.md) ──
def hl_account(dex=None):
"""Get perp account state: positions, margin, PnL."""
addr = _get_address()
if dex:
return _info("clearinghouseState", user=addr, dex=dex)
return _info("clearinghouseState", user=addr)
def hl_balances():
"""Get spot token balances."""
return _info("spotClearinghouseState", user=_get_address())
def hl_total_balance():
"""Get total available balance across spot + perp, aware of abstraction mode.
Mirrors the `hl_total_balance` runtime tool. Use this for "how much can I
trade with" checks — hl_account shows perp only (often $0 under unified
account) and hl_balances shows spot only.
Returns dict:
totalAvailable: USDC available for trading (rounded 2dp)
abstractionMode: "unifiedAccount" | "disabled" | "default" | ...
note: human-readable explanation of how the total was derived
breakdown: {spot:{usdc}, perp:{accountValue, marginUsed, available}}
"""
addr = _get_address()
# Abstraction mode — tolerate string or dict shapes, default on any error.
try:
abstraction_result = _info("userAbstraction", user=addr)
if isinstance(abstraction_result, str):
abstraction_mode = abstraction_result
elif isinstance(abstraction_result, dict):
abstraction_mode = abstraction_result.get(
"type", abstraction_result.get("state", "default")
)
else:
abstraction_mode = "default"
except Exception:
abstraction_mode = "default"
spot_state = _info("spotClearinghouseState", user=addr)
perp_state = _info("clearinghouseState", user=addr)
# Spot USDC
spot_usdc = 0.0
for bal in spot_state.get("balances", []):
if bal.get("coin") == "USDC":
spot_usdc = float(bal.get("total", 0))
break
# Perp margin
perp_margin = perp_state.get("marginSummary", {})
perp_value = float(perp_margin.get("accountValue", 0))
perp_used = float(perp_margin.get("totalMarginUsed", 0))
perp_available = perp_value - perp_used
if abstraction_mode == "unifiedAccount":
total_available = spot_usdc + perp_available
note = "Unified account: funds are shared across spot/perp/builder-dexes"
else:
total_available = perp_available
note = "Disabled mode: perp and spot are separate"
return {
"totalAvailable": round(total_available, 2),
"abstractionMode": abstraction_mode,
"note": note,
"breakdown": {
"spot": {"usdc": round(spot_usdc, 2)},
"perp": {
"accountValue": round(perp_value, 2),
"marginUsed": round(perp_used, 2),
"available": round(perp_available, 2),
},
},
}
def hl_open_orders():
"""Get all open orders."""
return _info("openOrders", user=_get_address())
def hl_market(dex=None):
"""Get current mid prices for all assets."""
if dex:
return _info("allMids", dex=dex)
return _info("allMids")
def hl_orderbook(coin):
"""Get L2 orderbook snapshot for a coin."""
return _info("l2Book", coin=coin)
def hl_fills():
"""Get recent trade fills for this wallet."""
return _info("userFills", user=_get_address())
def hl_candles(coin, interval="1h", hours_back=24, start=None, end=None):
"""Get OHLCV candlestick data.
Args:
coin: e.g. "BTC", "ETH"
interval: "1m","5m","15m","1h","4h","1d"
hours_back: lookback period in hours (default 24)
start/end: explicit timestamps in ms (override hours_back)
"""
if end is None:
end = int(time.time() * 1000)
if start is None:
start = end - hours_back * 3600 * 1000
return _info("candleSnapshot", req={"coin": coin, "interval": interval, "startTime": start, "endTime": end})
def hl_funding(coin, hours_back=24, start=None):
"""Get historical funding rates for a coin.
Args:
coin: e.g. "BTC"
hours_back: lookback in hours (default 24)
start: explicit start timestamp in ms (overrides hours_back)
"""
if start is None:
start = int((time.time() - hours_back * 3600) * 1000)
return _info("fundingHistory", coin=coin, startTime=start)
def hl_predicted_funding():
"""Get predicted next funding rates for all assets."""
return _info("predictedFundings")
def hl_order_status(oid):
"""Look up a single order by oid."""
return _info("orderStatus", user=_get_address(), oid=oid)
def hl_user_fees():
"""Get user fee schedule."""
return _info("userFees", user=_get_address())
"""
Hyperliquid L1 Action Signing — phantom agent EIP-712 pattern.
Hyperliquid trading actions (orders, cancels, leverage, modify) require an EIP-712
"Agent" signature over a keccak256 hash of the msgpack-serialized action.
Since we don't have direct private key access (Privy server wallet), we delegate
EIP-712 signing to the wallet service's /agent/sign-typed-data endpoint.
Two signing schemes:
1. L1 Action (orders, cancels, leverage) — msgpack → keccak → Agent struct
2. User-Signed (USDC transfers, withdrawals) — direct EIP-712 over action fields
The connectionId field (bytes32) encoding must match what the Privy wallet service
expects. We try multiple encodings and verify each with local ecrecover.
"""
import logging
from typing import Optional
import msgpack
from eth_utils import keccak
from core.wallet_runtime import wallet_request as _wallet_request
logger = logging.getLogger(__name__)
# Hyperliquid chain constants
MAINNET_SOURCE = "a"
# EIP-712 domains
L1_DOMAIN = {
"name": "Exchange",
"version": "1",
"chainId": 1337,
"verifyingContract": "0x0000000000000000000000000000000000000000",
}
USER_SIGNED_DOMAIN = {
"name": "HyperliquidSignTransaction",
"version": "1",
"chainId": 42161, # Arbitrum mainnet
"verifyingContract": "0x0000000000000000000000000000000000000000",
}
# EIP-712 types for Agent struct
AGENT_TYPES = {
"Agent": [
{"name": "source", "type": "string"},
{"name": "connectionId", "type": "bytes32"},
]
}
# ── Wallet address cache ─────────────────────────────────────────────────
_cached_wallet_address: Optional[str] = None
async def _get_wallet_address() -> Optional[str]:
"""Get the Privy wallet address for signature verification (cached)."""
global _cached_wallet_address
if _cached_wallet_address:
return _cached_wallet_address
try:
from core.wallet_runtime import is_fly_machine as _is_fly_machine
if not _is_fly_machine():
return None
data = await _wallet_request("GET", "/agent/wallet")
wallets = data if isinstance(data, list) else data.get("wallets", [])
for w in wallets:
if w.get("chain_type") == "ethereum":
_cached_wallet_address = w["wallet_address"].lower()
return _cached_wallet_address
except Exception as e:
logger.debug(f"Could not fetch wallet address for verification: {e}")
return None
# ── EIP-712 hash computation (manual, no encode_typed_data dependency) ────
def _eip712_domain_separator(domain: dict) -> bytes:
"""Compute EIP-712 domain separator hash."""
type_hash = keccak(
b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
)
encoded = type_hash
encoded += keccak(domain["name"].encode())
encoded += keccak(domain["version"].encode())
encoded += domain["chainId"].to_bytes(32, "big")
addr = domain["verifyingContract"].replace("0x", "")
encoded += bytes.fromhex(addr).rjust(32, b"\x00")
return keccak(encoded)
def _eip712_encode_field(field_type: str, value) -> bytes:
"""ABI-encode a single EIP-712 field value to 32 bytes."""
if field_type == "string":
return keccak(value.encode() if isinstance(value, str) else value)
elif field_type == "bytes32":
if isinstance(value, bytes):
return value.ljust(32, b"\x00")
elif isinstance(value, str):
return bytes.fromhex(value.replace("0x", "")).ljust(32, b"\x00")
elif isinstance(value, list):
return bytes(value).ljust(32, b"\x00")
elif field_type in ("uint256", "uint64"):
return int(value).to_bytes(32, "big")
elif field_type == "bool":
return int(bool(value)).to_bytes(32, "big")
elif field_type == "address":
return bytes.fromhex(value.replace("0x", "")).rjust(32, b"\x00")
raise ValueError(f"Unsupported EIP-712 type: {field_type}")
def _eip712_hash_struct(primary_type: str, types: dict, message: dict) -> bytes:
"""Compute hashStruct for an EIP-712 message."""
fields = types[primary_type]
type_string = (
primary_type
+ "("
+ ",".join(f"{f['type']} {f['name']}" for f in fields)
+ ")"
)
encoded = keccak(type_string.encode())
for field in fields:
encoded += _eip712_encode_field(field["type"], message[field["name"]])
return keccak(encoded)
def _eip712_digest(domain: dict, types: dict, primary_type: str, message: dict) -> bytes:
"""Compute the full EIP-712 digest: keccak256(0x1901 || domainSep || structHash)."""
domain_sep = _eip712_domain_separator(domain)
struct_hash = _eip712_hash_struct(primary_type, types, message)
return keccak(b"\x19\x01" + domain_sep + struct_hash)
# ── Local ecrecover verification ──────────────────────────────────────────
def _verify_signature_locally(
domain: dict,
types: dict,
primary_type: str,
message: dict,
signature_hex: str,
expected_address: Optional[str],
) -> bool:
"""
Verify an EIP-712 signature locally using ecrecover.
Computes the EIP-712 hash manually (no encode_typed_data dependency)
and recovers the signer address from the signature.
Returns True if the recovered address matches expected_address.
"""
if not expected_address:
return True # Can't verify without address, assume OK
try:
from eth_keys import keys
digest = _eip712_digest(domain, types, primary_type, message)
sig_bytes = bytes.fromhex(signature_hex.replace("0x", ""))
r = int.from_bytes(sig_bytes[:32], "big")
s = int.from_bytes(sig_bytes[32:64], "big")
v = sig_bytes[64]
if v >= 27:
v -= 27
sig = keys.Signature(vrs=(v, r, s))
public_key = sig.recover_public_key_from_msg_hash(digest)
recovered = public_key.to_checksum_address()
recovered_lower = recovered.lower()
expected_lower = expected_address.lower()
if recovered_lower == expected_lower:
logger.info(f"EIP-712 ecrecover OK: {recovered}")
return True
else:
logger.warning(
f"EIP-712 ecrecover MISMATCH: recovered={recovered}, expected={expected_address}"
)
return False
except Exception as e:
logger.warning(f"Local ecrecover verification failed: {e}")
return False
def _signature_to_hex(sig: dict) -> str:
"""Convert {r, s, v} dict to flat hex signature string."""
r = sig["r"].replace("0x", "").zfill(64)
s = sig["s"].replace("0x", "").zfill(64)
v = sig["v"]
return "0x" + r + s + format(v, "02x")
# ── Core hashing ──────────────────────────────────────────────────────────
def action_hash(action: dict, vault_address: Optional[str], nonce: int) -> bytes:
"""
Compute keccak256 hash of a Hyperliquid L1 action.
Steps:
1. msgpack serialize the action dict
2. Append nonce as 8-byte big-endian
3. Append vault flag: 0x01 if vault_address else 0x00
4. If vault, append 20 bytes of vault address
5. keccak256 the whole blob
"""
data = msgpack.packb(action)
data += nonce.to_bytes(8, "big")
if vault_address:
data += b"\x01"
# Remove 0x prefix if present, decode hex to bytes
addr_bytes = bytes.fromhex(vault_address.replace("0x", ""))
data += addr_bytes
else:
data += b"\x00"
return keccak(data)
# ── L1 Action Signing (orders, cancels, leverage) ────────────────────────
async def sign_l1_action(
action: dict,
nonce: int,
vault_address: Optional[str] = None,
) -> dict:
"""
Sign an L1 action (order, cancel, leverage, modify) via wallet service.
Tries multiple connectionId encodings and verifies each with local ecrecover.
Uses the first encoding that produces a signature matching the wallet address.
Returns: {"r": "0x...", "s": "0x...", "v": 27|28}
"""
hash_bytes = action_hash(action, vault_address, nonce)
source = MAINNET_SOURCE
expected_address = await _get_wallet_address()
# Multiple connectionId encodings to try
encodings = [
("hex_with_0x", "0x" + hash_bytes.hex()), # current: hex string with prefix
("byte_array", list(hash_bytes)), # byte array as list of ints
("hex_no_prefix", hash_bytes.hex()), # hex string without 0x prefix
]
last_error = None
for encoding_name, connection_id_value in encodings:
try:
payload = {
"domain": L1_DOMAIN,
"types": AGENT_TYPES,
"primaryType": "Agent",
"message": {
"source": source,
"connectionId": connection_id_value,
},
}
logger.info(
f"sign_l1_action: trying encoding={encoding_name}, "
f"connectionId type={type(connection_id_value).__name__}"
)
result = await _wallet_request("POST", "/agent/sign-typed-data", payload)
sig = _parse_signature(result)
# Verify locally with ecrecover
# For verification, connectionId must be the raw bytes32
verify_message = {
"source": source,
"connectionId": hash_bytes,
}
sig_hex = _signature_to_hex(sig)
match = _verify_signature_locally(
domain=L1_DOMAIN,
types=AGENT_TYPES,
primary_type="Agent",
message=verify_message,
signature_hex=sig_hex,
expected_address=expected_address,
)
if match:
logger.info(f"sign_l1_action: encoding={encoding_name} VERIFIED")
return sig
else:
logger.warning(
f"sign_l1_action: encoding={encoding_name} signature mismatch, trying next"
)
except Exception as e:
logger.warning(f"sign_l1_action: encoding={encoding_name} failed: {e}")
last_error = e
raise RuntimeError(
f"sign_l1_action: No encoding produced a verified signature. "
f"Expected address: {expected_address}. Last error: {last_error}"
)
# ── User-Signed Action (transfers, withdrawals, abstraction) ──────────────────────────
async def sign_user_set_abstraction(
user: str,
abstraction: str,
nonce: int,
) -> dict:
"""Sign a user set abstraction action.
Uses HyperliquidSignTransaction domain for user-signed actions.
Matches SDK's sign_user_set_abstraction_action exactly.
Args:
user: User address (lowercase)
abstraction: Abstraction mode ("unifiedAccount", "portfolioMargin", "disabled")
nonce: Timestamp in milliseconds
Returns: {"r": "0x...", "s": "0x...", "v": 27|28}
"""
types = {
"HyperliquidTransaction:UserSetAbstraction": [
{"name": "hyperliquidChain", "type": "string"},
{"name": "user", "type": "address"}, # ADDRESS, not string!
{"name": "abstraction", "type": "string"},
{"name": "nonce", "type": "uint64"},
]
}
action = {
"hyperliquidChain": "Mainnet",
"user": user.lower(),
"abstraction": abstraction,
"nonce": nonce,
}
return await sign_user_action(
action=action,
types=types,
primary_type="HyperliquidTransaction:UserSetAbstraction",
)
async def sign_user_action(
action: dict,
types: dict,
primary_type: str,
) -> dict:
"""
Sign a user-level action (USDC transfer, withdrawal) via wallet service.
Uses the HyperliquidSignTransaction domain (no msgpack/keccak).
Verifies the signature locally with ecrecover before returning.
Returns: {"r": "0x...", "s": "0x...", "v": 27|28}
"""
expected_address = await _get_wallet_address()
payload = {
"domain": USER_SIGNED_DOMAIN,
"types": types,
"primaryType": primary_type,
"message": action,
}
result = await _wallet_request("POST", "/agent/sign-typed-data", payload)
sig = _parse_signature(result)
# Verify locally
sig_hex = _signature_to_hex(sig)
match = _verify_signature_locally(
domain=USER_SIGNED_DOMAIN,
types=types,
primary_type=primary_type,
message=action,
signature_hex=sig_hex,
expected_address=expected_address,
)
if not match:
logger.error(
f"sign_user_action: signature mismatch! "
f"Expected address: {expected_address}, action type: {primary_type}"
)
return sig
# ── Signature parsing ─────────────────────────────────────────────────────
def _parse_signature(result: dict) -> dict:
"""
Parse signature from wallet service into {r, s, v} format.
The wallet service may return either:
- A flat hex signature string in result["signature"]
- Already-split {r, s, v} components
"""
sig = result.get("signature", result)
if isinstance(sig, str):
# Flat hex signature — split into r, s, v
sig_hex = sig.replace("0x", "")
if len(sig_hex) == 130:
r = "0x" + sig_hex[:64]
s = "0x" + sig_hex[64:128]
v = int(sig_hex[128:130], 16)
# Normalize v to 27/28
if v < 27:
v += 27
return {"r": r, "s": s, "v": v}
raise ValueError(f"Unexpected signature length: {len(sig_hex)}")
if isinstance(sig, dict):
# Already has components
r = sig.get("r", "")
s = sig.get("s", "")
v = sig.get("v", 0)
if isinstance(v, str):
v = int(v, 16) if v.startswith("0x") else int(v)
if v < 27:
v += 27
return {"r": r, "s": s, "v": v}
raise ValueError(f"Cannot parse signature from wallet response: {result}")
Related skills
How it compares
Pick hyperliquid when you need on-chain DEX agent tooling rather than centralized exchange REST integrations.
FAQ
What does hyperliquid do?
|
When should I use hyperliquid?
|
Is hyperliquid safe to install?
Review the Security Audits panel on this page before installing in production.