View Source Hyperliquid.Transport.Rpc (hyperliquid v0.2.2)
JSON-RPC transport for Hyperliquid EVM.
Provides low-level JSON-RPC communication with the Hyperliquid EVM RPC endpoint. Supports all standard Ethereum JSON-RPC methods plus Hyperliquid-specific methods.
Usage
# Use default RPC endpoint from config
{:ok, block_number} = Rpc.call("eth_blockNumber", [])
# Use a named RPC endpoint from the registry
{:ok, block_number} = Rpc.call("eth_blockNumber", [], rpc_name: :alchemy)
# Use custom RPC endpoint (direct URL override)
{:ok, block_number} = Rpc.call("eth_blockNumber", [], rpc_url: "https://custom-rpc.xyz")
# Batch requests
{:ok, results} = Rpc.batch([
{"eth_blockNumber", []},
{"eth_chainId", []}
])Configuration
The RPC transport uses Hyperliquid.Config for default URL configuration:
Config.rpc_base/0- Base URL for RPC requests (defaults to official Hyperliquid RPC)
You can override the RPC URL per-request using the :rpc_url option.
Summary
Functions
Make a batch of JSON-RPC calls.
Make a batch of JSON-RPC calls. Raises on error.
Make a JSON-RPC call.
Make a JSON-RPC call. Raises on error.
Get the current RPC URL being used.
Check if the RPC endpoint is reachable.
Check if the RPC endpoint is reachable. Raises on error.
Subscribe to events via WebSocket (if supported).
Subscribe to events via WebSocket. Raises on error.
Unsubscribe from events.
Unsubscribe from events. Raises on error.
Types
@type batch_result() :: {:ok, [any()]} | {:error, Hyperliquid.Error.t()}
@type rpc_opts() :: [ rpc_url: String.t(), rpc_name: atom() | String.t(), timeout: non_neg_integer(), recv_timeout: non_neg_integer() ]
@type rpc_result() :: {:ok, any()} | {:error, Hyperliquid.Error.t()}
Functions
@spec batch([{String.t(), list()}], rpc_opts()) :: batch_result()
Make a batch of JSON-RPC calls.
Parameters
requests: List of {method, params} tuplesopts: Optional configuration (same ascall/3)
Returns
{:ok, results}- List of results in same order as requests{:error, %Error{}}- Error with details
Examples
{:ok, [block_number, chain_id]} = Rpc.batch([
{"eth_blockNumber", []},
{"eth_chainId", []}
])
Make a batch of JSON-RPC calls. Raises on error.
@spec call(String.t(), list(), rpc_opts()) :: rpc_result()
Make a JSON-RPC call.
Parameters
method: RPC method name (e.g., "eth_blockNumber")params: List of parameters for the methodopts: Optional configuration:rpc_url- Override default RPC URL (highest priority):rpc_name- Use a named RPC from the registry (e.g., :alchemy, :quicknode):timeout- Request timeout in ms:recv_timeout- Receive timeout in ms
Returns
{:ok, result}- RPC result{:error, %Error{}}- Error with details
Examples
{:ok, "0x1234"} = Rpc.call("eth_blockNumber", [])
{:ok, balance} = Rpc.call("eth_getBalance", ["0x...", "latest"])
{:ok, block} = Rpc.call("eth_getBlockByNumber", ["0x1", true],
rpc_name: :alchemy
)
{:ok, block} = Rpc.call("eth_getBlockByNumber", ["0x1", true],
rpc_url: "https://custom-rpc.xyz"
)
Make a JSON-RPC call. Raises on error.
Get the current RPC URL being used.
Parameters
opts: Optional configuration with:rpc_urloverride
Returns
- String with the RPC URL
Check if the RPC endpoint is reachable.
Parameters
opts: Optional configuration
Returns
{:ok, true}- Endpoint is reachable{:error, %Error{}}- Error with details
Check if the RPC endpoint is reachable. Raises on error.
@spec subscribe(String.t(), list(), rpc_opts()) :: rpc_result()
Subscribe to events via WebSocket (if supported).
Note: This requires a WebSocket connection to the RPC endpoint. Not all RPC endpoints support subscriptions.
Parameters
subscription_type: Type of subscription (e.g., "newHeads", "logs")params: Optional parameters for the subscriptionopts: Optional configuration
Returns
{:ok, subscription_id}- Subscription ID{:error, %Error{}}- Error with details
Subscribe to events via WebSocket. Raises on error.
@spec unsubscribe(String.t(), rpc_opts()) :: rpc_result()
Unsubscribe from events.
Parameters
subscription_id: ID returned from subscribeopts: Optional configuration
Returns
{:ok, true}- Successfully unsubscribed{:error, %Error{}}- Error with details
Unsubscribe from events. Raises on error.