DeribitEx.OrderContext (deribit_ex v0.1.0)
View SourceManages 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.
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
@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 Deribitsession_id
: The ID of the session that created the orderinstrument_name
: The instrument for this order (e.g., "BTC-PERPETUAL")direction
: Whether this is a "buy" or "sell" orderstatus
: Current order status (e.g., "open", "filled", "cancelled")created_at
: When this order was createdupdated_at
: When this order was last updatedmetadata
: Additional order details that should be preserved
@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 informationorders_by_session
: Map of session_id to list of order_idsactive_session_id
: ID of the currently active session
Functions
@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 OrderContextsession_id
: The session ID to get active orders for
Returns
{:ok, orders}
: List of active orders for the session
@spec get_orders_for_session(t(), String.t()) :: {:ok, [order_entry()]}
Gets all orders from the specified session.
Parameters
context
: The current OrderContextsession_id
: The session ID to get orders for
Returns
{:ok, orders}
: List of orders for the 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 OrderContextprev_session
: The previous session that is being transitioned fromnew_session
: The new session being transitioned to
Returns
{:ok, updated_context}
: Updated OrderContext with the session transition
@spec new() :: t()
Creates a new OrderContext instance.
Returns
- A new empty OrderContext
Registers a new order with the order context.
Parameters
context
: The current OrderContextorder
: The order information to registersession_id
: The ID of the session creating this order
Returns
{:ok, updated_context}
: Updated OrderContext with the new order
Updates an existing order in the context.
Parameters
context
: The current OrderContextorder
: 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