Skip to main content

mkt_core/
stream.rs

1use std::time::Duration;
2
3use async_trait::async_trait;
4use mkt_types::{
5    AggTrade, AveragePrice, Balance, BlockTrade, BookTicker, ExchangeId, Fill, Kline, KlineRequest,
6    LastPrice, MiniTicker, Order, OrderBook, OrderBookDelta, Position, Symbol, Trade,
7};
8
9use crate::Result;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum Subscription {
14    LastPrice(Symbol),
15    OrderBook {
16        symbol: Symbol,
17        depth: Option<u16>,
18    },
19    OrderBookDeltas {
20        symbol: Symbol,
21        max_update_interval: Option<Duration>,
22    },
23    Trades(Symbol),
24    AggTrades(Symbol),
25    BlockTrades(Symbol),
26    BookTicker(Symbol),
27    AveragePrice(Symbol),
28    MiniTicker(Symbol),
29    Klines(KlineRequest),
30}
31
32#[derive(Debug, Clone, PartialEq, Eq)]
33#[non_exhaustive]
34pub enum PrivateSubscription {
35    Orders,
36    Fills,
37    Balances,
38    Positions,
39}
40
41#[derive(Debug, Clone, PartialEq)]
42#[non_exhaustive]
43pub enum MarketDataEvent {
44    LastPrice(LastPrice),
45    OrderBook(OrderBook),
46    OrderBookDelta(OrderBookDelta),
47    Trade(Trade),
48    AggTrade(AggTrade),
49    BlockTrade(BlockTrade),
50    BookTicker(BookTicker),
51    AveragePrice(AveragePrice),
52    MiniTicker(MiniTicker),
53    Kline(Kline),
54    Raw {
55        exchange_id: ExchangeId,
56        payload: RawPayload,
57    },
58}
59
60#[derive(Debug, Clone, PartialEq)]
61#[non_exhaustive]
62pub enum PrivateEvent {
63    Order(Order),
64    Fill(Fill),
65    Balance(Balance),
66    Position(Position),
67    Raw {
68        exchange_id: ExchangeId,
69        payload: RawPayload,
70    },
71}
72
73#[derive(Debug, Clone, PartialEq, Eq)]
74#[non_exhaustive]
75pub enum RawPayload {
76    Text(String),
77    Binary(Vec<u8>),
78}
79
80#[async_trait]
81pub trait EventStream: Send {
82    async fn next(&mut self) -> Result<Option<MarketDataEvent>>;
83
84    async fn close(&mut self) -> Result<()>;
85}
86
87#[async_trait]
88pub trait PrivateEventStream: Send {
89    async fn next(&mut self) -> Result<Option<PrivateEvent>>;
90
91    async fn close(&mut self) -> Result<()>;
92}