Skip to main content

Web3 client for Hyperliquid

Project description

hl-web3-py

A Python Web3 client library for interacting with the Hyperliquid DEX on both mainnet and testnet.

Features

  • Trading Operations: Place and cancel orders on Hyperliquid perpetual markets
  • Account Management: Query positions, balances, and account information
  • Vault Operations: Deposit and withdraw from HLP vaults
  • Staking & Delegation: Stake HYPE tokens and delegate to validators
  • Spot Trading: Transfer spot tokens and manage spot balances
  • Market Data: Access real-time pricing, order book, and market information
  • Multi-Network Support: Works with both Hyperliquid mainnet and testnet

Installation

pip install hl-web3

For development:

git clone https://github.com/oneforalone/hl-web3-py
cd hl-web3-py
uv sync

Quick Start

Environment Setup

Create a .env file with your private key:

PRIVATE_KEY=your_private_key_here

Basic Usage

import asyncio
from hl_web3.info import Info
from hl_web3.exchange import Exchange
from hl_web3.utils.constants import HL_TESTNET_RPC_URL, SCALE_FACTOR
from hl_web3.utils.types import Tif

async def main():
    # Initialize info client (read-only)
    info = Info(HL_TESTNET_RPC_URL)

    # Initialize exchange client (requires private key)
    exchange = Exchange(HL_TESTNET_RPC_URL, "your_private_key")

    # Get market data
    btc_price = await info.get_mark_px(3)  # BTC perp asset ID
    print(f"BTC Mark Price: ${btc_price / SCALE_FACTOR}")

    # Place a limit order
    asset = 3  # BTC perp
    is_buy = True
    price = int(50000 * SCALE_FACTOR)  # $50,000
    size = int(0.001 * SCALE_FACTOR)   # 0.001 BTC
    reduce_only = False
    time_in_force = Tif.Gtc
    client_order_id = 12345

    tx = await exchange.place_order(
        asset, is_buy, price, size, reduce_only, time_in_force, client_order_id
    )
    print(f"Order placed: {tx}")

if __name__ == "__main__":
    asyncio.run(main())

Core Components

Info Client

The Info class provides read-only access to market data and account information:

from hl_web3.info import Info

info = Info("https://rpc.hyperliquid-testnet.xyz/evm")

# Market data
mark_price = await info.get_mark_px(asset_id)
oracle_price = await info.get_oracle_px(asset_id)
spot_price = await info.get_spot_px(spot_id)
best_bid_offer = await info.get_bbo(asset_id)

# Account information
position = await info.get_user_position(user_address, asset_id)
spot_balance = await info.get_user_spot_balance(user_address, spot_id)
vault_equity = await info.get_user_vault_equity(user_address, vault_address)
withdrawable = await info.get_user_withdrawable(user_address)

# Asset information
perp_info = await info.get_perp_asset_info(asset_id)
spot_info = await info.get_spot_info(spot_id)
token_info = await info.get_token_info(token_id)

Exchange Client

The Exchange class enables trading and account management operations:

from hl_web3.exchange import Exchange

exchange = Exchange("https://rpc.hyperliquid-testnet.xyz/evm", private_key)

# Trading
tx = await exchange.place_order(asset, is_buy, price, size, reduce_only, tif, cloid)
tx = await exchange.cancel_order_by_oid(asset, order_id)
tx = await exchange.cancel_order_by_cloid(asset, client_order_id)

# Vault operations
tx = await exchange.vault_transfer(vault_address, is_deposit, usd_amount)

# Staking
tx = await exchange.staking_deposit(hype_amount)
tx = await exchange.staking_withdraw(hype_amount)
tx = await exchange.token_delegate(validator_address, hype_amount, is_undelegate)

# Spot transfers
tx = await exchange.spot_send(destination_address, token_id, amount)
tx = await exchange.send_usd_class_transfer(amount, to_perp)

# API wallet management
tx = await exchange.add_api_wallet(api_address, name)

Supported Networks

Mainnet

  • RPC URL: https://rpc.hyperliquid.xyz/evm
  • Use HL_RPC_URL constant

Testnet

  • RPC URL: https://rpc.hyperliquid-testnet.xyz/evm
  • Use HL_TESTNET_RPC_URL constant

Connection Types

  • HTTPS (recommended)
  • WebSocket
  • IPC (limited support)

Data Types

The library includes comprehensive type definitions for all Hyperliquid data structures:

Trading Types

  • ActionType: Enum for different action types (LimitOrder, VaultTransfer, etc.)
  • Tif: Time in Force options (Alo, Gtc, Ioc)
  • AssetType: Perp or Spot assets

Account Data

  • Position: Perpetual position information
  • SpotBalance: Spot token balances
  • UserVaultEquity: Vault equity and lock information
  • Delegation: Staking delegation details

Market Data

  • PerpAssetInfo: Perpetual asset metadata
  • SpotInfo: Spot market information
  • TokenInfo: Token contract details
  • Bbo: Best bid/offer prices

Constants and Scaling

Price and Size Scaling

  • SCALE_FACTOR = 10**8: Used for perpetual prices and sizes
  • USD_SCALE_FACTOR = 10**6: Used for USD amounts

Example Asset IDs (Testnet)

  • BTC Perp: 3
  • BTC Spot: 50
  • BTC Token: 69

Testing

The library includes comprehensive tests covering all functionality:

# Run all tests
pytest

# Run specific test categories
pytest tests/test_info.py      # Market data tests
pytest tests/test_exchange.py  # Trading tests
pytest tests/test_endpoint.py  # Connection tests
pytest tests/test_utils.py     # Utility tests

Test Environment

Tests require a .env file with:

PRIVATE_KEY=your_testnet_private_key

Some tests are skipped by default due to balance requirements:

  • Token delegation tests (require HYPE tokens)
  • Staking tests (require HYPE tokens)

Error Handling

The library provides proper error handling for common scenarios:

  • Invalid asset IDs
  • Insufficient balances
  • Network connectivity issues
  • Invalid action types

Development

Requirements

  • Python 3.10+
  • uv (for dependency management)
  • web3.py 7.13.0+
  • eth-abi 5.2.0+

Setup

git clone https://github.com/oneforalone/hl-web3-py
cd hl-web3-py
uv sync

Code Quality

The project uses:

  • pre-commit hooks
  • pytest for testing
  • ruff for linting
  • pytest-cov for coverage

License

MIT License

Support

For issues and questions:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hl_web3-0.1.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hl_web3-0.1.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file hl_web3-0.1.1.tar.gz.

File metadata

  • Download URL: hl_web3-0.1.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.6

File hashes

Hashes for hl_web3-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b20e91d52fd4f88a4de8ad2978f943510569473422c5ac48386c0173f7536260
MD5 0f6cb27b3c8ad8669928a5eb42620ab5
BLAKE2b-256 0023ae30eb77ff76fc6216baf47ca0f5936055ace03555463f189c1b27f07b58

See more details on using hashes here.

File details

Details for the file hl_web3-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hl_web3-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.6

File hashes

Hashes for hl_web3-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 898c653ff2dde69ebc1ef93d61a7d763f562d56c9d5d7eb131a233b0e5baae9d
MD5 4a86e5c21ee664b06b7b8cf246895a63
BLAKE2b-256 e08fa170b5504897eb589798303c4baf77600945f6d82644a4f9db6760d41823

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page