WebsockexAdapter.Examples.DeribitGenServerAdapter (WebsockexAdapter v0.1.1)

View Source

Production-ready supervised Deribit adapter with automatic reconnection.

Features: Monitor WebSocket client, auto-reconnect, restore auth/subscriptions. Only 5 public functions for clean API. See DeribitRpc for available methods.

Summary

Functions

Authenticates with Deribit using configured credentials.

Returns a specification to start this module under a supervisor.

Returns the current adapter state.

Sends a JSON-RPC request to Deribit.

Starts the Deribit adapter GenServer.

Subscribes to Deribit channels for real-time data.

Types

state()

@type state() :: %{
  client: WebsockexAdapter.Client.t() | nil,
  monitor_ref: reference() | nil,
  authenticated: boolean(),
  was_authenticated: boolean(),
  subscriptions: MapSet.t(String.t()),
  client_id: String.t() | nil,
  client_secret: String.t() | nil,
  url: String.t(),
  opts: keyword()
}

Functions

authenticate(adapter)

@spec authenticate(GenServer.server()) :: :ok | {:error, atom()}

Authenticates with Deribit using configured credentials.

Returns

  • :ok on successful authentication
  • {:error, :not_connected} if not connected
  • {:error, :missing_credentials} if credentials not configured

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_state(adapter)

@spec get_state(GenServer.server()) :: {:ok, state()}

Returns the current adapter state.

Returns

{:ok, state} with the internal state map.

send_request(adapter, method, params \\ %{})

@spec send_request(GenServer.server(), String.t(), map()) ::
  {:ok, map()} | {:error, atom()}

Sends a JSON-RPC request to Deribit.

Parameters

  • adapter - The adapter GenServer
  • method - RPC method name
  • params - Method parameters (default: %{})

Returns

The response from Deribit or {:error, :not_connected}.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the Deribit adapter GenServer.

Options

  • :name - GenServer name (required)
  • :client_id - Deribit API client ID
  • :client_secret - Deribit API client secret
  • :url - WebSocket URL (defaults to test.deribit.com)
  • :heartbeat_interval - Heartbeat interval in seconds
  • :handler - Optional message handler module

Returns

{:ok, pid} on success.

subscribe(adapter, channels)

@spec subscribe(GenServer.server(), [String.t()]) :: :ok | {:error, atom()}

Subscribes to Deribit channels for real-time data.

Parameters

  • adapter - The adapter GenServer
  • channels - List of channel names

Returns

  • :ok on successful subscription
  • {:error, :not_connected} if not connected