Hyperliquid Prime
A TypeScript SDK that acts as a prime broker layer on top of Hyperliquid's perp markets — both native (ETH, BTC) and HIP-3 deployer markets. Automatically discovers all markets for an asset, compares liquidity/funding/cost, and routes to the best execution — or splits across multiple venues for optimal fills with automatic collateral swaps.
When to Use This Skill
- - Trading crypto, stocks (AAPL, NVDA, TSLA), indexes, or commodities (GOLD, SILVER) on Hyperliquid
- Need best execution across multiple perp markets (native + HIP-3) for the same asset
- Splitting large orders across venues for better fills and lower price impact
- Comparing funding rates across different collateral types
- Aggregated orderbook view across fragmented markets
- Managing positions that may be spread across multiple collateral types
- Automatic collateral swaps (USDC → USDH, USDT0) when non-USDC markets offer better prices
Quick Start
Installation
CODEBLOCK0
Read-Only Usage (no wallet needed)
CODEBLOCK1
Trading (wallet required)
CODEBLOCK2
CLI
CODEBLOCK3
Important: Fees & Automatic Actions
Builder Fee: A 1 basis point (0.01%) builder fee is charged by default on all SDK-executed orders via Hyperliquid's native builder fee mechanism. On the first trading order from a wallet, the SDK sends an on-chain approval transaction to authorize this fee. To disable entirely, set builder: null in the config.
Collateral Swaps (Split Orders Only): When executeSplit() routes orders to non-USDC collateral markets, the SDK automatically:
- 1. Enables DEX abstraction on the user's account
- Transfers USDC from the perp account to the spot account
- Places a spot order to swap USDC into the required collateral token (e.g., USDH, USDT0)
- A 1% buffer is added to swap amounts to account for slippage
These actions only occur during split order execution and only when the best liquidity requires non-USDC collateral.
Read-Only Operations: Quotes, orderbooks, funding comparisons, and market discovery require no wallet, no fees, and perform no on-chain actions.
Credentials: Trading operations require a private key via HP_PRIVATE_KEY environment variable or the privateKey config option. The key is used to sign transactions sent to the Hyperliquid API. Source code is available for audit at .
User Confirmation Flow: The SDK uses a quote-then-execute pattern as the confirmation mechanism:
- 1.
quote() / quoteSplit() are read-only — they return an execution plan with estimated prices, markets, and costs. No on-chain actions are taken. - The caller reviews the plan (programmatically or via CLI output).
- INLINECODE6 /
executeSplit() must be explicitly called to perform on-chain actions (place orders, approve fees, swap collateral). - One-step convenience methods (
long(), short(), longSplit(), shortSplit()) combine both steps — use quote-then-execute for explicit control.
Implementation Note: This skill bundle contains instructions only (SKILL.md). The SDK implementation must be installed separately via npm install hyperliquid-prime. The source code is open-source and available for audit at the GitHub repository before installation.
How Routing Works
When you call hp.quote("TSLA", "buy", 50), the router:
- 1. Fetches the orderbook for every TSLA market
- Simulates walking each book to estimate average fill price and price impact
- Scores each market using:
-
Price impact (dominant) — cost in basis points to fill
-
Funding rate (secondary) — prefers favorable funding direction
-
Collateral swap cost (penalty) — estimated cost to swap into the required collateral
- 4. Selects the lowest-score market and builds an execution plan
For split orders (quoteSplit), the router merges all orderbooks, walks the combined book greedily to consume the cheapest liquidity first across all venues, and builds split execution legs. Collateral requirements and swaps are estimated and executed at executeSplit(...) time using live balances. If leverage is included in the quote options, execution applies that leverage per market leg before order placement.
For single-market orders, leverage included in quote(...) is carried into the execution plan and applied before the order is sent.
Configuration
CODEBLOCK4
Builder Fee
A 1 basis point (0.01%) builder fee is included by default on all SDK-executed orders via Hyperliquid's native builder fee mechanism. The fee is auto-approved on the trader's first order. Set builder: null to disable, or provide a custom { address, feeBps } to override.
Key Methods
Read-Only
- -
getMarkets(asset) — All perp markets for an asset (native + HIP-3) - INLINECODE20 — Asset groups with multiple markets
- INLINECODE21 — Merged orderbook across all markets
- INLINECODE22 — Funding rates compared across markets
- INLINECODE23 — Routing quote for single best market
- INLINECODE24 — Split quote across multiple markets
Trading (wallet required)
- -
execute(plan) — Execute a single-market quote - INLINECODE26 — Execute a split quote (handles collateral swaps)
- INLINECODE27 — Quote + execute a long on best market
- INLINECODE28 — Quote + execute a short on best market
- INLINECODE29 — Split quote + execute a long across markets
- INLINECODE30 — Split quote + execute a short across markets
- INLINECODE31 — Close all positions for an asset
Trade Options
- -
leverage?: number — Positive number, e.g. 5 for 5x. - INLINECODE34 — Default
true (cross); set false for isolated. - INLINECODE37 requires
leverage. If leverage is omitted, no leverage-setting API call is made.
Position & Balance
- -
getPositions() — All positions with market metadata - INLINECODE40 — Positions grouped by base asset
- INLINECODE41 — Account margin summary
Repository
License
MIT
Hyperliquid Prime
一个TypeScript SDK,作为Hyperliquid永续合约市场(包括原生ETH、BTC市场和HIP-3部署者市场)之上的主经纪商层。自动发现某个资产的所有市场,比较流动性/资金费率/成本,路由到最佳执行路径——或在多个交易场所之间拆分订单以实现最优成交,并自动进行抵押品互换。
何时使用此技能
- - 在Hyperliquid上交易加密货币、股票(AAPL、NVDA、TSLA)、指数或大宗商品(GOLD、SILVER)
- 需要针对同一资产在多个永续合约市场(原生+HIP-3)之间获得最佳执行
- 跨交易场所拆分大额订单以获得更好成交和更低价格冲击
- 比较不同抵押品类型的资金费率
- 查看跨碎片化市场的聚合订单簿
- 管理可能分布在多种抵押品类型中的头寸
- 当非USDC市场提供更优价格时,自动进行抵押品互换(USDC → USDH、USDT0)
快速开始
安装
bash
npm install hyperliquid-prime
只读使用(无需钱包)
typescript
import { HyperliquidPrime } from hyperliquid-prime
const hp = new HyperliquidPrime({ testnet: true })
await hp.connect()
// 获取某个资产的所有永续合约市场(原生+HIP-3)
const markets = hp.getMarkets(ETH) // 或 TSLA、BTC 等
// 获取最佳执行的路由报价
const quote = await hp.quote(TSLA, buy, 50)
const quoteWithLev = await hp.quote(TSLA, buy, 50, { leverage: 5, isCross: true })
// 聚合订单簿
const book = await hp.getAggregatedBook(TSLA)
// 资金费率比较
const funding = await hp.getFundingComparison(TSLA)
await hp.disconnect()
交易(需要钱包)
typescript
const hp = new HyperliquidPrime({
privateKey: 0x...,
testnet: true,
})
await hp.connect()
// 先报价再执行(推荐)
const quote = await hp.quote(TSLA, buy, 50, { leverage: 5, isCross: true })
const receipt = await hp.execute(quote.plan)
// 一步式便捷方法
const receipt2 = await hp.long(TSLA, 50, { leverage: 5 })
const receipt3 = await hp.short(TSLA, 25, { leverage: 3, isCross: false })
// 跨多个市场拆分以获得更好成交
const splitQuote = await hp.quoteSplit(TSLA, buy, 200, { leverage: 4 })
const splitReceipt = await hp.executeSplit(splitQuote.splitPlan)
// 或一步式:await hp.longSplit(TSLA, 200)
// 统一头寸视图
const positions = await hp.getGroupedPositions()
await hp.disconnect()
CLI
bash
显示某个资产的所有永续合约市场(原生+HIP-3)
hp markets ETH
hp markets TSLA
聚合订单簿
hp book TSLA
比较资金费率
hp funding TSLA
获取路由报价
hp quote TSLA buy 50
hp quote TSLA buy 50 --leverage 5
hp quote TSLA buy 50 --leverage 3 --isolated
执行交易
HP
PRIVATEKEY=0x... hp long TSLA 50
HP
PRIVATEKEY=0x... hp short TSLA 25
HP
PRIVATEKEY=0x... hp long TSLA 50 --leverage 5
HP
PRIVATEKEY=0x... hp short TSLA 25 --leverage 3 --isolated
查看头寸和余额
HP
PRIVATEKEY=0x... hp positions
HP
PRIVATEKEY=0x... hp balance
使用测试网
hp markets TSLA --testnet
重要:费用与自动操作
构建者费用:通过Hyperliquid原生构建者费用机制,所有SDK执行的订单默认收取1个基点(0.01%)的构建者费用。在钱包首次交易订单时,SDK会发送一笔链上批准交易以授权此费用。要完全禁用,请在配置中设置builder: null。
抵押品互换(仅限拆分订单):当executeSplit()将订单路由到非USDC抵押品市场时,SDK会自动:
- 1. 在用户账户上启用DEX抽象
- 将USDC从永续合约账户转移到现货账户
- 下达现货订单将USDC兑换为所需的抵押品代币(例如USDH、USDT0)
- 兑换金额增加1%的缓冲以应对滑点
这些操作仅在拆分订单执行期间发生,且仅在最佳流动性需要非USDC抵押品时触发。
只读操作:报价、订单簿、资金费率比较和市场发现无需钱包、无需费用,也不执行任何链上操作。
凭证:交易操作需要通过HPPRIVATEKEY环境变量或privateKey配置选项提供私钥。该密钥用于签署发送到Hyperliquid API的交易。源代码可在进行审计。
用户确认流程:SDK采用先报价后执行的模式作为确认机制:
- 1. quote() / quoteSplit()是只读的——它们返回包含预估价格、市场和成本的执行计划。不执行任何链上操作。
- 调用者审查该计划(通过编程方式或CLI输出)。
- 必须显式调用execute() / executeSplit()来执行链上操作(下达订单、批准费用、互换抵押品)。
- 一步式便捷方法(long()、short()、longSplit()、shortSplit())将两个步骤合并——如需显式控制,请使用先报价后执行模式。
实现说明:此技能包仅包含指令(SKILL.md)。SDK实现必须通过npm install hyperliquid-prime单独安装。源代码是开源的,可在安装前在GitHub仓库进行审计。
路由工作原理
当您调用hp.quote(TSLA, buy, 50)时,路由器会:
- 1. 获取每个TSLA市场的订单簿
- 模拟逐层扫描每个订单簿以估算平均成交价格和价格冲击
- 评分每个市场,依据:
-
价格冲击(主导因素)——以基点计算的成交成本
-
资金费率(次要因素)——偏好有利的资金方向
-
抵押品互换成本(惩罚项)——兑换为所需抵押品的预估成本
- 4. 选择评分最低的市场并构建执行计划
对于拆分订单(quoteSplit),路由器合并所有订单簿,贪婪地扫描合并后的订单簿以优先消耗所有交易场所中最便宜的流动性,并构建拆分执行腿。抵押品需求和互换在executeSplit(...)时使用实时余额进行估算和执行。如果报价选项中包含杠杆,执行时会在下单前对每个市场腿应用该杠杆。
对于单一市场订单,quote(...)中包含的杠杆会带入执行计划,并在发送订单前应用。
配置
typescript
interface HyperliquidPrimeConfig {
privateKey?: 0x${string} // 交易所需
walletAddress?: string // 如未提供,从privateKey派生
testnet?: boolean // 默认:false
defaultSlippage?: number // 默认:0.01(1%)
logLevel?: debug | info | warn | error | silent
prettyLogs?: boolean // 默认:false
builder?: BuilderConfig | null // 构建者费用(默认:1个基点,设为null禁用)
}
构建者费用
通过Hyperliquid原生构建者费用机制,所有SDK执行的订单默认包含1个基点(0.01%)的构建者费用。该费用在交易者的首次订单时自动批准。设置builder: null以禁用,或提供自定义的{ address, feeBps }进行覆盖。
主要方法
只读
- - getMarkets(asset) — 某个资产的所有永续合约市场(原生+HIP-3)
- getAggregatedMarkets() — 具有多个市场的资产组
- getAggregatedBook(asset) — 跨所有市场的合并订单簿
- getFundingComparison(asset) — 跨市场比较资金费率
- quote(asset, side, size, options?) — 单一最佳市场的路由报价
- quoteSplit(asset, side, size, options?) — 跨多个市场的拆分报价
交易(需要钱包)
- - execute(plan) — 执行单一市场报价
- executeSplit(plan) — 执行拆分报价(处理抵押品互换)
- long(asset, size, options?) — 在最佳市场报价+执行做多
- short(asset,