View Source Hyperliquid.Api.Exchange (hyperliquid v0.1.6)

Module for interacting with the Hyperliquid Exchange API endpoints.

This module provides functions to perform various operations on the Hyperliquid exchange, including placing and canceling orders, modifying orders, updating leverage, transferring funds, and managing sub-accounts.

It uses the Hyperliquid.Api macro to set up the basic API interaction functionality.

Functions

Order Management

Account Management

Withdrawal and Transfers

All functions return a tuple {:ok, result} on success, or {:error, details} on failure.

Summary

Functions

Creates a new sub-account.

Transfers funds between spot and perpetual accounts.

Transfers spot tokens to or from a sub-account.

Transfers funds to or from a sub-account.

Updates the isolated margin for a specific asset.

Updates the leverage for a specific asset.

Transfers funds to or from a vault.

Withdraws funds from the bridge.

Functions

Link to this function

cancel_order(asset, oid, vault_address \\ nil)

View Source

Cancels a single order.

Parameters

  • asset: Integer representing the asset's index in the coin list
  • oid: The order ID to cancel
  • vault_address: Optional vault address

Example

iex> Hyperliquid.Api.Exchange.cancel_order(5, 123)
{:ok, %{...}}
Link to this function

cancel_order_by_cloid(asset, cloid, vault_address \\ nil)

View Source

Cancels order by cloid.

Parameters

  • asset: Integer representing the asset's index in the coin list
  • cloid: The cloid to cancel
  • vault_address: Optional vault address

Example

iex> Hyperliquid.Api.Exchange.cancel_order_by_cloid(5, "0x123")
{:ok, %{...}}
Link to this function

cancel_orders(cancels, vault_address \\ nil)

View Source

Cancels multiple orders.

Parameters

  • cancels: List of orders to cancel
  • vault_address: Optional vault address

Example

iex> Hyperliquid.Api.Exchange.cancel_orders([%{a: 5, o: 123}, %{a: 5, o: 456}])
{:ok, %{...}}
Link to this function

cancel_orders_by_cloid(cancels, vault_address \\ nil)

View Source
Link to this function

create_sub_account(name)

View Source

Creates a new sub-account.

Parameters

  • name: The name for the new sub-account

Example

iex> Hyperliquid.Api.Exchange.create_sub_account("trading_bot_1")
{:ok, %{...}}
Link to this function

modify_multiple_orders(modifies, vault_address \\ nil)

View Source
Link to this function

modify_order(oid, order, vault_address \\ nil)

View Source

Modifies an existing order.

Parameters

  • oid: The order ID to modify
  • order: A map containing the new order details
  • vault_address: Optional vault address

Example

iex> Hyperliquid.Api.Exchange.modify_order(123, order)
{:ok, %{...}}
Link to this function

place_order(order, grouping \\ "na", vault_address \\ nil)

View Source

Places one or multiple orders.

Parameters

  • order: A single order or a list of orders
  • grouping: Order grouping (default: "na")
  • vault_address: Optional vault address

Examples

iex> Hyperliquid.Api.Exchange.place_order(order)
{:ok,
  %{
    "response" => %{
      "data" => %{
        "statuses" => [
          %{"filled" => %{"avgPx" => "115.17", "oid" => 18422439200, "totalSz" => "1.0"}}
        ]
      },
      "type" => "order"
    },
    "status" => "ok"
  }}
Link to this function

post_action(action, vault_address)

View Source
Link to this function

post_action(action, vault_address, nonce)

View Source
Link to this function

post_action(action, vault_address, nonce, secret)

View Source
Link to this function

spot_perp_transfer(amount, to_perp)

View Source

Transfers funds between spot and perpetual accounts.

Parameters

  • amount: The amount to transfer (in USDC)
  • to_perp: Boolean indicating the direction of transfer (true for spot to perp, false for perp to spot)

Example

iex> Hyperliquid.Api.Exchange.spot_perp_transfer(1000, true)
{:ok, %{...}}
Link to this function

spot_send(destination, token, amount, time)

View Source
Link to this function

sub_account_spot_transfer(user, is_deposit, token, amount)

View Source

Transfers spot tokens to or from a sub-account.

Parameters

  • user: The address or identifier of the sub-account
  • is_deposit: Boolean indicating whether it's a deposit (true) or withdrawal (false)
  • token: The token to transfer (e.g., "BTC", "ETH")
  • amount: The amount of the token to transfer

Example

iex> Hyperliquid.Api.Exchange.sub_account_spot_transfer("0x9876...", true, "BTC", 0.1)
{:ok, %{...}}
Link to this function

sub_account_transfer(user, is_deposit, amount_usd)

View Source

Transfers funds to or from a sub-account.

Parameters

  • user: The address or identifier of the sub-account
  • is_deposit: Boolean indicating whether it's a deposit (true) or withdrawal (false)
  • amount_usd: The amount to transfer in USD cents (e.g., 1_000_000 = $1)

Example

iex> Hyperliquid.Api.Exchange.sub_account_transfer("0x5678...", true, 1_000_000)
{:ok, %{...}}
Link to this function

update_isolated_margin(asset, is_buy, ntli)

View Source

Updates the isolated margin for a specific asset.

Parameters

  • asset: Integer representing the asset's index in the coin list
  • is_buy: Boolean indicating whether it's a buy position
  • ntli: The new notional total liability increase

Example

iex> Hyperliquid.Api.Exchange.update_isolated_margin(1, true, 1000)
{:ok, %{...}}
Link to this function

update_leverage(asset, is_cross, leverage)

View Source

Updates the leverage for a specific asset.

Parameters

  • asset: Integer representing the asset's index in the coin list
  • is_cross: Boolean indicating whether to use cross margin
  • leverage: The new leverage value

Example

iex> Hyperliquid.Api.Exchange.update_leverage(1, true, 10)
{:ok, %{...}}
Link to this function

usd_send(destination, amount, time)

View Source
Link to this function

vault_transfer(vault_address, is_deposit, amount_usd)

View Source

Transfers funds to or from a vault.

Parameters

  • vault_address: The address of the vault
  • is_deposit: Boolean indicating whether it's a deposit (true) or withdrawal (false)
  • amount_usd: The amount to transfer in USD (positive for transfer, negative for withdraw)

Example

iex> Hyperliquid.Api.Exchange.vault_transfer("0x1234...", true, 1000)
{:ok, %{...}}
Link to this function

withdraw_from_bridge(destination, amount, time)

View Source

Withdraws funds from the bridge.

Parameters

  • destination: The destination address
  • amount: The amount to withdraw
  • time: The timestamp for the withdrawal

Returns

{:ok, result} on success, where result contains the response from the API. {:error, details} on failure.

Example

iex> Hyperliquid.Api.Exchange.withdraw_from_bridge("0x1234...", 1000000, 1625097600)
{:ok, %{...}}