DeribitEx.SessionContext (deribit_ex v0.1.0)

View Source

Manages session context and order state preservation during Deribit token operations.

This module tracks the state of sessions across token changes and provides functionality for preserving order state during session transitions. It handles:

  • Session tracking during token exchange (switching subaccounts)
  • Session tracking during token forking (creating named sessions)
  • Order state preservation across session transitions
  • Automatic channel resubscription after token changes

Integrates with both token management and order management to ensure continuity of operations during authentication changes.

Summary

Types

t()

Represents a Deribit session with tracking information.

The session transition type

Functions

Generates a unique session ID.

Invalidates a session during logout operations.

Creates a new SessionContext from authentication response data.

Creates a new SessionContext from a token exchange response.

Creates a new SessionContext from a token fork response.

Updates an existing SessionContext with refreshed token data.

Types

t()

@type t() :: %DeribitEx.SessionContext{
  access_token: String.t(),
  active: boolean(),
  created_at: integer(),
  expires_at: integer(),
  id: String.t(),
  prev_id: String.t() | nil,
  refresh_token: String.t(),
  scope: String.t() | nil,
  session_name: String.t() | nil,
  subject_id: integer() | nil,
  transition: transition_type()
}

Represents a Deribit session with tracking information.

  • id: Unique ID for this session
  • prev_id: ID of the previous session (for tracking transitions)
  • created_at: When this session was created
  • access_token: Current access token for this session
  • refresh_token: Current refresh token for this session
  • expires_at: When the current token will expire
  • transition: How this session was created
  • subject_id: Account ID for this session (from exchange_token)
  • session_name: Session name (from fork_token)
  • scope: Authentication permissions for this session
  • active: Whether this session is currently active

transition_type()

@type transition_type() :: :exchange | :fork | :refresh | :initial

The session transition type:

  • :exchange - Token exchange for switching subaccounts
  • :fork - Token fork for creating named sessions
  • :refresh - Normal token refresh within same session
  • :initial - Initial session creation

Functions

generate_session_id()

@spec generate_session_id() :: String.t()

Generates a unique session ID.

Uses a combination of timestamp and random bytes for uniqueness.

invalidate(current_session)

@spec invalidate(t()) :: {:ok, t()}

Invalidates a session during logout operations.

Parameters

  • current_session - The session to invalidate

Returns

  • {:ok, session} - Invalidated session

new_from_auth(auth_data, opts \\ [])

@spec new_from_auth(
  map(),
  keyword()
) :: {:ok, t()}

Creates a new SessionContext from authentication response data.

Used when first authenticating to create the initial session context.

Parameters

  • auth_data - Authentication response from Deribit containing access_token, etc.
  • opts - Additional options for session creation

Returns

  • {:ok, session} - A new session context

new_from_exchange(current_session, exchange_data, subject_id)

@spec new_from_exchange(t(), map(), integer()) :: {:ok, t()}

Creates a new SessionContext from a token exchange response.

Used when switching between subaccounts to track the session transition.

Parameters

  • current_session - The current active session
  • exchange_data - Response from exchange_token operation
  • subject_id - The ID of the subaccount being switched to

Returns

  • {:ok, session} - A new session context with transition tracking

new_from_fork(current_session, fork_data, session_name)

@spec new_from_fork(t(), map(), String.t()) :: {:ok, t()}

Creates a new SessionContext from a token fork response.

Used when creating a named session to track the session transition.

Parameters

  • current_session - The current active session
  • fork_data - Response from fork_token operation
  • session_name - The name of the new session being created

Returns

  • {:ok, session} - A new session context with transition tracking

update_from_refresh(current_session, refresh_data)

@spec update_from_refresh(t(), map()) :: {:ok, t()}

Updates an existing SessionContext with refreshed token data.

Used during normal token refresh operations to maintain session continuity.

Parameters

  • current_session - The current active session
  • refresh_data - Response from token refresh operation

Returns

  • {:ok, session} - Updated session context with new token information