DeribitEx.Adapter (deribit_ex v0.2.0)
View SourceWebsockexNova adapter for the Deribit WebSocket API.
Handles connection, authentication, and message processing specifically for the Deribit JSON-RPC WebSocket protocol.
Summary
Functions
Provides connection configuration for the WebsockexNova client.
Generates authentication data for Deribit API.
Generates data for the disable_cancel_on_disconnect operation.
Generates data for the disable_heartbeat operation.
Generates data for the enable_cancel_on_disconnect operation.
Generates data for token exchange operation.
Generates data for token fork operation.
Generates data for the get_cancel_on_disconnect operation.
Generates data for logout operation.
Generates data for the set_heartbeat operation.
Generates data for the unsubscribe_all operation.
Generates data for the unsubscribe operation.
Handles authentication response from Deribit.
Handles the connection established event.
Handles disable_cancel_on_disconnect response from Deribit.
Handles disable_heartbeat response from Deribit.
Handles enable_cancel_on_disconnect response from Deribit.
Handles token exchange response from Deribit.
Handles token fork response from Deribit.
Handles raw WebSocket frames from the Deribit API.
Handles get_cancel_on_disconnect response from Deribit.
Handles various info messages sent to the process.
Handles logout response from Deribit.
Handles messages from Deribit WebSocket API.
Handles set_heartbeat response from Deribit.
Handles subscription response from Deribit.
Handles unsubscribe_all response from Deribit.
Handles unsubscribe response from Deribit.
Initializes the adapter's state.
Sends a JSON-RPC request to the Deribit API.
Creates a subscription request for a specific channel.
Handles termination of the adapter process.
Functions
Provides connection configuration for the WebsockexNova client.
Merges provided options with sensible defaults for Deribit connection.
Configures optimal authentication refresh thresholds based on Deribit's token expiration patterns. Tokens typically last 900 seconds, and refresh begins at the configured threshold before expiration.
Generates authentication data for Deribit API.
Uses client credentials from the connection state to create a proper Deribit authentication request.
Generates data for the disable_cancel_on_disconnect operation.
This function prepares the JSON-RPC request for the private/disable_cancel_on_disconnect endpoint, which disables automatic cancellation of orders on disconnection.
Parameters
params
- Parameters for disabling COD (optional scope)state
- The current adapter state
Returns
{:ok, encoded_request, updated_state}
- The JSON request and updated state
Generates data for the disable_heartbeat operation.
This function prepares the JSON-RPC request for the public/disable_heartbeat endpoint, which disables server heartbeat messages for the connection.
Parameters
params
- Parameters (unused but kept for consistency)state
- The current adapter state
Returns
{:ok, encoded_request, updated_state}
- The JSON request and updated state
Generates data for the enable_cancel_on_disconnect operation.
This function prepares the JSON-RPC request for the private/enable_cancel_on_disconnect endpoint, which enables automatic cancellation of orders on disconnection.
Parameters
params
- Parameters for COD, includes the scope (connection or account)state
- The current adapter state
Returns
{:ok, encoded_request, updated_state}
- The JSON request and updated state
Generates data for token exchange operation.
This function creates the payload for the public/exchange_token RPC method used to generate a new access token for switching subaccounts.
Generates data for token fork operation.
This function creates the payload for the public/fork_token RPC method used to create a new named session via refresh token.
Generates data for the get_cancel_on_disconnect operation.
This function prepares the JSON-RPC request for the private/get_cancel_on_disconnect endpoint, which gets the status of automatic order cancellation on disconnection.
Parameters
params
- Parameters for getting COD (no additional parameters needed)state
- The current adapter state
Returns
{:ok, encoded_request, updated_state}
- The JSON request and updated state
Generates data for logout operation.
This function creates the payload for the private/logout RPC method used to gracefully close the session and optionally invalidate tokens.
Parameters
params
- Parameters for the logout request:"invalidate_token"
- Whether to invalidate all tokens (defaults to true)
state
- Current adapter state containing the access token
Returns
{:ok, payload, updated_state}
- The JSON-RPC request as a string and updated state- Raises an error if no access token is found in the state
Examples
# Generate payload to logout and invalidate tokens
params = %{"invalidate_token" => true}
{:ok, payload, updated_state} = generate_logout_data(params, state)
# Generate payload to logout without invalidating tokens
params = %{"invalidate_token" => false}
{:ok, payload, updated_state} = generate_logout_data(params, state)
Generates data for the set_heartbeat operation.
This function prepares the JSON-RPC request for the public/set_heartbeat endpoint, which enables server heartbeat messages for the connection.
Parameters
params
- Parameters containing at least the heartbeat interval in seconds (minimum 10s)state
- The current adapter state
Returns
{:ok, encoded_request, updated_state}
- The JSON request and updated state
Generates data for the unsubscribe_all operation.
This function prepares the JSON-RPC request for the public/unsubscribe_all endpoint, which stops all active subscriptions.
Parameters
params
- Parameters (unused but kept for consistency)state
- The current adapter state
Returns
{:ok, encoded_request, updated_state}
- The JSON request and updated state
Generates data for the unsubscribe operation.
This function prepares the JSON-RPC request for the public/unsubscribe or private/unsubscribe endpoint, which stops the stream for specified channels.
Parameters
params
- Parameters for unsubscribe, includes the channels to unsubscribe fromstate
- The current adapter state
Returns
{:ok, encoded_request, updated_state}
- The JSON request and updated state
Handles authentication response from Deribit.
Processes different types of responses:
- Success: Stores token and expiration time
- Error: Stores error details
- Other: Passes through
Uses WebsockexNova's built-in authentication refresh mechanism to handle token renewal before expiration.
Handles the connection established event.
Executes telemetry events for connection opening, updates the state, and handles reconnection logic by resetting reconnect_attempts counter.
Leverages WebsockexNova's connection event callbacks to automatically trigger authentication and resubscription after reconnections.
Handles disable_cancel_on_disconnect response from Deribit.
Updates the state to reflect the COD status after disabling it.
Handles disable_heartbeat response from Deribit.
Updates the state to reflect the heartbeat status after disabling it.
Handles enable_cancel_on_disconnect response from Deribit.
Updates the state to reflect the COD status after enabling it.
Handles token exchange response from Deribit.
Processes response from the public/exchange_token endpoint and updates state with new tokens and expiration information.
Handles token fork response from Deribit.
Processes response from the public/fork_token endpoint and updates state with new tokens and expiration information.
Handles raw WebSocket frames from the Deribit API.
This function is called for each incoming WebSocket frame before JSON parsing. Handles heartbeat test_request messages directly at the frame level.
Handles get_cancel_on_disconnect response from Deribit.
Updates the state with the current COD status from the API.
Handles various info messages sent to the process.
This used to handle manual refresh_auth messages, but now we use WebsockexNova's built-in auth refresh mechanism. This handler remains for backward compatibility with existing tests and other potential custom messages.
Handles logout response from Deribit.
Updates the state to reflect the user is no longer authenticated by:
- Setting auth_status to :unauthenticated
- Removing access_token, refresh_token, and related fields
This ensures that after logout, any subsequent operations requiring authentication will require a new authentication process.
Parameters
response
- The response from Deribit for the logout requeststate
- Current adapter state
Returns
{:ok, updated_state}
- On successful logout with state cleared of auth data{:error, error, updated_state}
- On error response, with error recorded in state{:ok, state}
- For unrecognized responses (fallthrough case)
Examples
# Successful logout
response = %{"result" => "ok"}
{:ok, updated_state} = handle_logout_response(response, state)
# updated_state.auth_status == :unauthenticated
# Error response
response = %{"error" => %{"code" => 10011, "message" => "Error message"}}
{:error, error, updated_state} = handle_logout_response(response, state)
@spec handle_message(map(), map()) :: {:needs_auth, map(), map()} | {:ok, map(), map()} | {:error, any(), map()} | {:reply, String.t(), map()}
Handles messages from Deribit WebSocket API.
Processes different message types:
- Auth errors: Signals need for authentication
- JSON-RPC responses: Matches with tracked requests and processes appropriately
- Other messages: Delegates to DefaultMessageHandler
Handles set_heartbeat response from Deribit.
Updates the state to reflect the heartbeat status after enabling it.
Handles subscription response from Deribit.
Handles unsubscribe_all response from Deribit.
Updates the state to clear all subscriptions.
Handles unsubscribe response from Deribit.
Updates the state to remove the unsubscribed channels.
Initializes the adapter's state.
Sends a JSON-RPC request to the Deribit API.
This is a general-purpose RPC handler for sending any Deribit API request. It handles request generation, authentication, timeout tracking, and more.
Parameters
method
- The JSON-RPC method to call (e.g., "public/get_time")params
- The parameters to pass to the method (map)state
- The current adapter stateoptions
- Optional request options
Returns
{:ok, encoded_request, updated_state}
- The JSON request and updated state
Creates a subscription request for a specific channel.
Handles different subscription types (public vs. private) based on channel name and authentication status.
Handles termination of the adapter process.
Executes telemetry events for connection closing and updates reconnection attempts. Handles graceful shutdowns vs. unexpected disconnections.
Enhanced to better integrate with WebsockexNova's connection lifecycle management for intelligent reconnections with authentication preservation.