View Source Hyperliquid.Api.DelegationHelper (hyperliquid v0.2.2)

Helper module for generating delegated wrapper functions for API endpoints.

This module provides macros to automatically generate snake_case convenience functions that delegate to endpoint modules. It eliminates code duplication between Hyperliquid.Api.Info and Hyperliquid.Api.Exchange.

Usage

In a context module (Info, Exchange, etc.):

defmodule Hyperliquid.Api.Info do
  require Hyperliquid.Api.DelegationHelper
  alias Hyperliquid.Api.Registry

  DelegationHelper.generate_delegated_functions(:info)
end

Generated Functions

For each endpoint module with __endpoint_info__/0, the macro generates:

  • endpoint_name/N - Returns {:ok, result} or {:error, reason}
  • endpoint_name!/N - Returns result or raises on error

For endpoints with storage enabled:

  • fetch_endpoint_name/N - Fetch and persist to storage
  • fetch_endpoint_name!/N - Fetch, persist, and raise on error

Parameter Handling

Function signatures are generated based on endpoint metadata:

  • No params: endpoint_name() / endpoint_name!()
  • Optional only: endpoint_name(opts \\ []) / endpoint_name!(opts \\ [])
  • Required + optional: endpoint_name(param1, param2, opts \\ [])
  • Required only: endpoint_name(param1, param2)

Summary

Functions

Generates delegated functions for all endpoints in the given context.

Converts an endpoint module name to a snake_case function name atom.

Functions

Link to this macro

generate_delegated_functions(context)

View Source (macro)

Generates delegated functions for all endpoints in the given context.

Parameters

  • context - Atom: :info, :exchange, :explorer, or :stats

Generated Code

For each endpoint module in the context that exports __endpoint_info__/0, generates wrapper functions with proper signatures, docs, and specs.

Link to this function

module_to_function_name(endpoint_module)

View Source

Converts an endpoint module name to a snake_case function name atom.

Examples

iex> module_to_function_name(Hyperliquid.Api.Info.AllMids)
:all_mids

iex> module_to_function_name(Hyperliquid.Api.Info.L2Book)
:l2_book

iex> module_to_function_name(Hyperliquid.Api.Info.ClearinghouseState)
:clearinghouse_state