Skip to main content

bulk_client/api/parts/
events.rs

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