DeribitEx.OrderContext (deribit_ex v0.1.0)

View Source

Manages order state preservation during token operations.

This module is responsible for:

  • Tracking orders associated with specific sessions
  • Preserving order information during session transitions
  • Providing a clean interface for order management integration
  • Supporting order state migration between sessions

Integrates with SessionContext to ensure order state continuity during authentication changes and token operations.

Summary

Types

Tracks an order's association with a session.

t()

Represents the order context containing all tracked orders.

Functions

Gets all active (open) orders from the specified session.

Gets all orders from the specified session.

Handles a session transition by migrating order tracking.

Creates a new OrderContext instance.

Registers a new order with the order context.

Updates an existing order in the context.

Types

order_entry()

@type order_entry() :: %{
  order_id: String.t(),
  session_id: String.t(),
  instrument_name: String.t(),
  direction: String.t(),
  status: String.t(),
  created_at: integer(),
  updated_at: integer(),
  metadata: map()
}

Tracks an order's association with a session.

  • order_id: The ID of the order from Deribit
  • session_id: The ID of the session that created the order
  • instrument_name: The instrument for this order (e.g., "BTC-PERPETUAL")
  • direction: Whether this is a "buy" or "sell" order
  • status: Current order status (e.g., "open", "filled", "cancelled")
  • created_at: When this order was created
  • updated_at: When this order was last updated
  • metadata: Additional order details that should be preserved

t()

@type t() :: %DeribitEx.OrderContext{
  active_session_id: String.t() | nil,
  orders: %{optional(String.t()) => order_entry()},
  orders_by_session: %{optional(String.t()) => [String.t()]}
}

Represents the order context containing all tracked orders.

  • orders: Map of order_id to order entry information
  • orders_by_session: Map of session_id to list of order_ids
  • active_session_id: ID of the currently active session

Functions

get_active_orders_for_session(context, session_id)

@spec get_active_orders_for_session(t(), String.t()) :: {:ok, [order_entry()]}

Gets all active (open) orders from the specified session.

Parameters

  • context: The current OrderContext
  • session_id: The session ID to get active orders for

Returns

  • {:ok, orders}: List of active orders for the session

get_orders_for_session(context, session_id)

@spec get_orders_for_session(t(), String.t()) :: {:ok, [order_entry()]}

Gets all orders from the specified session.

Parameters

  • context: The current OrderContext
  • session_id: The session ID to get orders for

Returns

  • {:ok, orders}: List of orders for the session

handle_session_transition(context, prev_session, new_session)

@spec handle_session_transition(
  t(),
  DeribitEx.SessionContext.t(),
  DeribitEx.SessionContext.t()
) ::
  {:ok, t()}

Handles a session transition by migrating order tracking.

This is called when tokens are exchanged or forked, to ensure order state is preserved across session changes.

Parameters

  • context: The current OrderContext
  • prev_session: The previous session that is being transitioned from
  • new_session: The new session being transitioned to

Returns

  • {:ok, updated_context}: Updated OrderContext with the session transition

new()

@spec new() :: t()

Creates a new OrderContext instance.

Returns

  • A new empty OrderContext

register_order(context, order, session_id)

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

Registers a new order with the order context.

Parameters

  • context: The current OrderContext
  • order: The order information to register
  • session_id: The ID of the session creating this order

Returns

  • {:ok, updated_context}: Updated OrderContext with the new order

update_order(context, order)

@spec update_order(t(), map()) :: {:ok, t()} | {:error, :not_found}

Updates an existing order in the context.

Parameters

  • context: The current OrderContext
  • order: The updated order information

Returns

  • {:ok, updated_context}: Updated OrderContext with the modified order
  • {:error, :not_found}: If the order doesn't exist in the context