Hyperliquid Integration
How Atract integrates with Hyperliquid for trading execution.
Overview
Hyperliquid is a high-performance L1 blockchain optimized for perpetual futures trading. Atract uses Hyperliquid for all trade execution and risk management through its native smart contracts.
Why Hyperliquid?
Advantages:
Fast execution (~100ms)
Low fees (~0.025% taker, rebates for makers)
Deep liquidity
Non-custodial
Native USDC
Integration Points
1. Trading Execution
Orders submitted via API
Agent wallet signs orders
Execution on Hyperliquid L1
2. Balance Queries
Real-time account state
Position tracking
Margin calculations
3. Market Data
Price feeds
Order book depth
Trade history
HLClient Library
Location: web/src/lib/hlClient.ts
Methods:
// Account queries
getAccountState(address): Promise<AccountState>
getOpenOrders(address): Promise<Order[]>
// Trading
postToExchange(action, signature): Promise<Response>
// Market data
getAssetId(symbol): Promise<number>
getOrderBook(symbol): Promise<OrderBook>Agent Wallet Flow
1. User clicks "Buy BTC"
2. Backend creates order action
3. Agent wallet signs (server-side)
4. Signed order sent to Hyperliquid
5. HL verifies agent is authorized
6. Order executes
7. Response returned to userOrder Types Supported
Market Orders:
{
asset: 0, // BTC
isBuy: true,
size: 0.1,
limitPx: null
}Limit Orders:
{
asset: 0,
isBuy: true,
size: 0.1,
limitPx: 50000,
tif: 'GTC' // or 'IOC', 'ALO'
}Balance Monitoring
Oracle queries every 60 seconds:
const state = await HLClient.getAccountState(vaultAddress);
const balance = state.marginSummary.accountValue;
// Update on-chain vault
await vaultContract.updateBalance(balance);Error Handling
Common errors:
Insufficient margin
Invalid signature
Order rejected
Network timeout
Retry logic:
async function executeWithRetry(fn, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
if (i === maxRetries - 1) throw error;
await sleep(Math.pow(2, i) * 1000);
}
}
}Rate Limits
100 requests/second per IP
No per-user limits
WebSocket recommended for real-time data
Related Pages
Last updated