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
place_order/1
,place_order/2
,place_order/3
- Place one or multiple orderscancel_orders/1
,cancel_orders/2
- Cancel multiple orderscancel_order/2
,cancel_order/3
- Cancel a single ordercancel_order_by_cloid/2
,cancel_order_by_cloid/3
- Cancel an order by client order IDcancel_orders_by_cloid/1
,cancel_orders_by_cloid/2
- Cancel multiple orders by client order IDsmodify_order/2
,modify_order/3
- Modify an existing ordermodify_multiple_orders/1
,modify_multiple_orders/2
- Modify multiple orders
Account Management
update_leverage/3
- Update leverage for an assetupdate_isolated_margin/3
- Update isolated margin for an assetspot_perp_transfer/2
- Transfer between spot and perpetual accountsvault_transfer/3
- Transfer to/from a vaultcreate_sub_account/1
- Create a sub-accountsub_account_transfer/3
- Transfer funds to/from a sub-accountsub_account_spot_transfer/4
- Transfer spot tokens to/from a sub-account
Withdrawal and Transfers
usd_send/3
- Send USD to another addressspot_send/4
- Send spot tokens to another addresswithdraw_from_bridge/3
- Withdraw funds from the bridge
All functions return a tuple {:ok, result}
on success, or {:error, details}
on failure.
Summary
Functions
Cancels a single order.
Cancels order by cloid.
Cancels multiple orders.
Creates a new sub-account.
Modifies an existing order.
Places one or multiple orders.
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
Cancels a single order.
Parameters
asset
: Integer representing the asset's index in the coin listoid
: The order ID to cancelvault_address
: Optional vault address
Example
iex> Hyperliquid.Api.Exchange.cancel_order(5, 123)
{:ok, %{...}}
Cancels order by cloid.
Parameters
asset
: Integer representing the asset's index in the coin listcloid
: The cloid to cancelvault_address
: Optional vault address
Example
iex> Hyperliquid.Api.Exchange.cancel_order_by_cloid(5, "0x123")
{:ok, %{...}}
Cancels multiple orders.
Parameters
cancels
: List of orders to cancelvault_address
: Optional vault address
Example
iex> Hyperliquid.Api.Exchange.cancel_orders([%{a: 5, o: 123}, %{a: 5, o: 456}])
{:ok, %{...}}
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, %{...}}
Modifies an existing order.
Parameters
oid
: The order ID to modifyorder
: A map containing the new order detailsvault_address
: Optional vault address
Example
iex> Hyperliquid.Api.Exchange.modify_order(123, order)
{:ok, %{...}}
Places one or multiple orders.
Parameters
order
: A single order or a list of ordersgrouping
: 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"
}}
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, %{...}}
Transfers spot tokens to or from a sub-account.
Parameters
user
: The address or identifier of the sub-accountis_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, %{...}}
Transfers funds to or from a sub-account.
Parameters
user
: The address or identifier of the sub-accountis_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, %{...}}
Updates the isolated margin for a specific asset.
Parameters
asset
: Integer representing the asset's index in the coin listis_buy
: Boolean indicating whether it's a buy positionntli
: The new notional total liability increase
Example
iex> Hyperliquid.Api.Exchange.update_isolated_margin(1, true, 1000)
{:ok, %{...}}
Updates the leverage for a specific asset.
Parameters
asset
: Integer representing the asset's index in the coin listis_cross
: Boolean indicating whether to use cross marginleverage
: The new leverage value
Example
iex> Hyperliquid.Api.Exchange.update_leverage(1, true, 10)
{:ok, %{...}}
Transfers funds to or from a vault.
Parameters
vault_address
: The address of the vaultis_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, %{...}}
Withdraws funds from the bridge.
Parameters
destination
: The destination addressamount
: The amount to withdrawtime
: 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, %{...}}