DeribitEx.TokenManager (deribit_ex v0.2.0)
View SourceIntegrates token management with order management.
This module provides the central integration point for token operations and order management, handling session transitions, order state preservation, and resubscription. It coordinates:
- Session tracking via SessionContext
- Order state preservation via OrderContext
- Channel resubscription via ResubscriptionHandler
Designed to be used by Adapter to perform token operations while preserving application state.
Summary
Functions
Gets all active orders for the current session.
Gets the current session ID if available.
Handles a token exchange operation for switching subaccounts.
Handles a token fork operation for creating a named session.
Handles logout and session invalidation.
Handles a token refresh operation.
Initializes the token manager from an authentication response.
Creates a new TokenManager instance.
Performs resubscription for all tracked channels.
Registers a new order with the token manager.
Registers a new subscription with the token manager.
Unregisters a subscription from the token manager.
Updates an existing order in the token manager.
Types
@type t() :: %DeribitEx.TokenManager{ orders: DeribitEx.OrderContext.t(), resubscription: DeribitEx.ResubscriptionHandler.t(), session: DeribitEx.SessionContext.t() | nil }
The complete token management state.
session
: Current session contextorders
: Order tracking contextresubscription
: Channel resubscription handler
Functions
@spec get_active_orders(t()) :: {:ok, [DeribitEx.OrderContext.order_entry()]} | {:error, :no_active_session}
Gets all active orders for the current session.
Parameters
manager
: Current token manager state
Returns
{:ok, orders}
: List of active orders{:error, :no_active_session}
: If no active session exists
Gets the current session ID if available.
Parameters
manager
: Current token manager state
Returns
{:ok, session_id}
: The current session ID{:error, :no_active_session}
: If no active session exists
Handles a token exchange operation for switching subaccounts.
Parameters
manager
: Current token manager stateexchange_data
: Response from exchange_token operationsubject_id
: The ID of the subaccount being switched to
Returns
{:ok, updated_manager}
: Updated token manager with new session{:error, reason}
: If the operation fails
Handles a token fork operation for creating a named session.
Parameters
manager
: Current token manager statefork_data
: Response from fork_token operationsession_name
: The name of the new session being created
Returns
{:ok, updated_manager}
: Updated token manager with new session{:error, reason}
: If the operation fails
Handles logout and session invalidation.
Parameters
manager
: Current token manager state
Returns
{:ok, updated_manager}
: Updated token manager with invalidated session
Handles a token refresh operation.
Parameters
manager
: Current token manager staterefresh_data
: Response from token refresh operation
Returns
{:ok, updated_manager}
: Updated token manager with refreshed session{:error, reason}
: If the operation fails
Initializes the token manager from an authentication response.
Parameters
manager
: Current token manager stateauth_data
: Authentication response from Deribitopts
: Additional options
Returns
{:ok, updated_manager}
: Updated token manager with session context
Creates a new TokenManager instance.
Parameters
opts
: Options for the token manager:max_retries
- Maximum resubscription retry attempts (default: 3)
Returns
- A new TokenManager instance
Performs resubscription for all tracked channels.
Parameters
manager
: Current token manager stateconn
: WebsockexNova client connection
Returns
{:ok, updated_manager, results}
: Updated manager and resubscription results{:error, reason, updated_manager}
: Error information if resubscription fails
Registers a new order with the token manager.
Parameters
manager
: Current token manager stateorder
: Order information
Returns
{:ok, updated_manager}
: Updated token manager with order registered{:error, reason}
: If the operation fails
Registers a new subscription with the token manager.
Parameters
manager
: Current token manager statechannel
: Channel name or topicparams
: Subscription parameters
Returns
{:ok, updated_manager}
: Updated token manager with subscription registered{:error, reason}
: If the operation fails
Unregisters a subscription from the token manager.
Parameters
manager
: Current token manager statechannel
: Channel to unregister
Returns
{:ok, updated_manager}
: Updated token manager with subscription removed
Updates an existing order in the token manager.
Parameters
manager
: Current token manager stateorder
: Updated order information
Returns
{:ok, updated_manager}
: Updated token manager with order updated{:error, reason}
: If the operation fails