Skip to main content

bulk_client/api/parts/
events.rs

1use serde_json::Value;
2use crate::msgs::account::{Fill, LeverageSetting, Margin, OrderState, PositionInfo};
3use crate::msgs::md::{Candle, L2Snapshot, Ticker};
4
5
6// ─────────────────────────────────────────────────────────────────────────────
7// Event topics
8// ─────────────────────────────────────────────────────────────────────────────
9
10
11/// Topics to subscribe to
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13#[allow(unused)]
14pub enum Topic {
15    Ticker,
16    Trades,
17    L2Snapshot,
18    L2Delta,
19    Candle,
20    Account,
21    Margin,
22    Position,
23    Fill,
24    Leverage,
25    Order,
26    Error,
27    /// Connection lifecycle events (connected, disconnected).
28    Status
29}
30
31// ─────────────────────────────────────────────────────────────────────────────
32// Event return types
33// ─────────────────────────────────────────────────────────────────────────────
34
35/// Typed event payload emitted to handlers.
36#[derive(Debug, Clone)]
37#[allow(unused)]
38pub enum Event {
39    Ticker(Ticker),
40    Trades(Vec<Fill>),
41    L2Snapshot(L2Snapshot),
42    L2Delta(L2Snapshot),
43    Candle(Candle),
44    Margin(Margin),
45    Position(PositionInfo),
46    Order(OrderState),
47    Fill(Fill),
48    Leverage(Vec<LeverageSetting>),
49    Error(Value),
50    /// Emitted on the [`Topic::Status`] channel when the WebSocket connection
51    /// is cleanly established (after the actor sends initial subscriptions).
52    Connected,
53    /// Emitted on the [`Topic::Status`] channel when the WebSocket connection
54    /// is lost for any reason.  The string contains a human-readable cause.
55    Disconnected(String),
56}
57
58#[allow(unused)]
59impl Event {
60    pub fn topic(&self) -> Topic {
61        match self {
62            Event::Ticker(_) => Topic::Ticker,
63            Event::Trades(_) => Topic::Trades,
64            Event::L2Snapshot(_) => Topic::L2Snapshot,
65            Event::L2Delta(_) => Topic::L2Delta,
66            Event::Candle(_) => Topic::Candle,
67            Event::Margin(_) => Topic::Margin,
68            Event::Position(_) => Topic::Position,
69            Event::Order(_) => Topic::Order,
70            Event::Fill(_) => Topic::Fill,
71            Event::Leverage(_) => Topic::Leverage,
72            Event::Error(_) => Topic::Error,
73            Event::Connected | Event::Disconnected(_) => Topic::Status,
74        }
75    }
76}