Skip to content

dzmbs/hlz

Repository files navigation

hlz

Zig tooling for Hyperliquid — SDK, CLI, and trading terminal.

version license status platform zig

hlz trading terminal

Install · Quick Start · Docs · Credits


What is this

A Zig implementation of the Hyperliquid SDK with a CLI and trading terminal. Two binaries:

Binary Size What it does
hlz 891 KB 45-command CLI — market data, trading, transfers, streaming
hlz-terminal 1036 KB Trading terminal — candlestick chart, order book, trade tape

Both are static binaries. Pipe-aware — tables on TTY, JSON when piped.


Install

curl -fsSL https://raw.githubusercontent.com/dzmbs/hlz/main/install.sh | sh

Or download a binary manually from Releases.

From source (requires Zig 0.15.2):

git clone https://github.com/dzmbs/hlz
cd hlz
zig build -Doptimize=ReleaseSmall
# Binaries in zig-out/bin/hlz and zig-out/bin/hlz-terminal

As a Zig dependency:

.dependencies = .{
    .hlz = .{
        .url = "git+https://github.com/dzmbs/hlz#main",
    },
},

Quick Start

# No auth needed for market data
hlz price BTC
hlz book ETH --live
hlz funding --top 10

# Set up a key for trading
hlz keys new trading
export HL_KEY_NAME=trading

# Trade
hlz buy BTC 0.1 @50000
hlz sell ETH 1.0
hlz cancel BTC --all

# Check account
hlz portfolio
hlz orders

# Stream real-time data
hlz stream trades BTC

# Trading terminal
hlz trade BTC

SDK

const hlz = @import("hlz");
const Signer = hlz.crypto.signer.Signer;
const Decimal = hlz.math.decimal.Decimal;
const types = hlz.hypercore.types;
const signing = hlz.hypercore.signing;

const signer = try Signer.fromHex("your_64_char_hex_key");

const order = types.OrderRequest{
    .asset = 0,  // BTC
    .is_buy = true,
    .limit_px = try Decimal.fromString("50000"),
    .sz = try Decimal.fromString("0.1"),
    .reduce_only = false,
    .order_type = .{ .limit = .{ .tif = .Gtc } },
    .cloid = types.ZERO_CLOID,
};

const batch = types.BatchOrder{
    .orders = &[_]types.OrderRequest{order},
    .grouping = .na,
};

const nonce = @as(u64, @intCast(std.time.milliTimestamp()));
const sig = try signing.signOrder(signer, batch, nonce, .mainnet, null, null);

var client = hlz.hypercore.client.Client.mainnet(allocator);
defer client.deinit();
var result = try client.place(signer, batch, nonce, null, null);
defer result.deinit();

CLI Commands

Market Data (no auth)
Command Description
hlz price <COIN> Mid price + bid/ask spread
hlz mids [COIN] All mid prices
hlz funding [--top N] Funding rates
hlz book <COIN> [--live] L2 order book
hlz perps [--dex xyz] Perpetual markets
hlz spot Spot markets
hlz dexes HIP-3 DEXes
Trading
Command Description
hlz buy <COIN> <SZ> [@PX] Limit or market buy
hlz sell <COIN> <SZ> [@PX] Limit or market sell
hlz cancel <COIN> [OID] Cancel order(s)
hlz modify <COIN> <OID> <SZ> <PX> Modify order
hlz leverage <COIN> [N] Query or set leverage
hlz twap <COIN> buy|sell <SZ> --duration 1h --slices 10 TWAP execution
hlz batch "order1" "order2" Batch orders

Flags: --reduce-only, --tp <PX>, --sl <PX>, --tif gtc|ioc|alo, --dry-run

Account
Command Description
hlz portfolio [ADDR] Positions + spot balances
hlz positions [ADDR] Open positions
hlz orders [ADDR] Open orders
hlz fills [ADDR] Recent fills
hlz balance [ADDR] Balance + margin health
hlz status <OID> Order status
Transfers & Streaming
hlz send 100 USDC 0xAddress        # Send to address
hlz send 100 USDC --to spot        # Perp → spot

hlz stream trades BTC               # Real-time trades
hlz stream book ETH                 # Order book updates
Key Management
hlz keys new <NAME>                  # Generate encrypted key
hlz keys import <NAME>               # Import existing key
hlz keys ls                          # List keys
hlz keys default <NAME>              # Set default
hlz approve-agent <ADDR>             # Approve API wallet
Global Flags & Environment
--json              JSON output (auto when piped)
--quiet, -q         Minimal output
--chain testnet     Use testnet
--dry-run, -n       Preview without sending
--key-name <NAME>   Select keystore key
HL_KEY=...          # Private key
HL_ADDRESS=...      # Default address
HL_CHAIN=...        # mainnet or testnet
HL_OUTPUT=json      # Default format

Exit codes: 0 success, 1 error, 2 usage, 3 auth, 4 network


Agent Integration

Built for automation and AI agents:

  • JSON when piped, --json flag always available
  • Semantic exit codes (0/1/2/3/4)
  • --dry-run to preview any trade
  • --quiet for just the value
  • Stdin batch: echo "buy BTC 0.1" | hlz batch --stdin
  • No interactive prompts ever
PRICE=$(hlz price BTC -q)
hlz buy BTC 0.1 @${PRICE} --json
hlz orders --json | jq '.[] | select(.coin == "BTC")'

Architecture

src/
├── lib/          Primitives (crypto, encoding, math) — no Hyperliquid knowledge
├── sdk/          Hyperliquid SDK (client, ws, signing, types) — imports lib/
├── tui/          TUI framework (Buffer, Terminal, Layout, List, Chart) — standalone
├── cli/          CLI tool (45 commands) — imports sdk/ + tui/
└── terminal/     Trading terminal — imports sdk/ + tui/

Dependencies point down only. lib/ and tui/ depend on nothing.


Numbers

hlz binary 891 KB
hlz-terminal binary 1036 KB
Source ~18,800 lines
Commands 44
HTTP endpoints 60+ (info + exchange)
WS subscriptions 13 types
Response types 62
Unit tests 183
E2E tests 17

Building

zig build                              # Debug
zig build -Doptimize=ReleaseSmall      # Small binary (891KB)
zig build -Doptimize=ReleaseFast       # Fast binary
zig build -Dfast-crypto=true           # Custom GLV (~3.4x faster signing)
zig build test                         # Unit tests (183)
zig build bench                        # Signing benchmarks
zig build e2e                          # Live API tests

Credits

Inspired by hypersdk & hypecli (Rust SDK & CLI), hyperliquid-cli (TypeScript CLI), and zabi (Zig Ethereum tooling).

Zig   ×   Hyperliquid


MIT · Not affiliated with Hyperliquid.

About

A Zig implementation of the Hyperliquid SDK with a CLI and trading terminal.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors