Skip to main content

maxt_bindings_common/
contract.rs

1use maxt::{
2    AccountStream, Balance, BoxFuture, Candle, CandleRequest, FundingPayment, FundingRate,
3    HistoryRequest, MarginRequest, MarginSummary, Market, MarketInfo, MarketKind, MarketStream,
4    Order, OrderBook, OrderRequest, Page, Position, Result, StreamConfig, Subscription, Ticker,
5    Trade,
6};
7
8/// An owned call across a language binding boundary.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum AdapterCall {
12    /// Lists markets of one kind.
13    Markets {
14        /// The requested instrument kind.
15        kind: MarketKind,
16    },
17    /// Reads recent trades.
18    Trades {
19        /// The market to read.
20        market: Market,
21        /// The maximum requested row count.
22        limit: Option<u32>,
23    },
24    /// Reads an order book snapshot.
25    OrderBook {
26        /// The market to read.
27        market: Market,
28        /// The requested levels per side.
29        depth: Option<u32>,
30    },
31    /// Reads a ticker.
32    Ticker {
33        /// The market to read.
34        market: Market,
35    },
36    /// Reads historical candles.
37    Candles {
38        /// The complete candle request.
39        request: CandleRequest,
40    },
41    /// Opens a market-data stream.
42    Subscribe {
43        /// The requested markets and feeds.
44        subscription: Subscription,
45        /// Connection and buffering settings.
46        config: StreamConfig,
47    },
48    /// Reads account balances.
49    Balances,
50    /// Reads open orders, optionally for one market.
51    OpenOrders {
52        /// The optional market filter.
53        market: Option<Market>,
54    },
55    /// Opens an account stream.
56    SubscribeAccount {
57        /// Connection and buffering settings.
58        config: StreamConfig,
59    },
60    /// Places an order.
61    PlaceOrder {
62        /// The complete order request.
63        request: OrderRequest,
64    },
65    /// Cancels an order.
66    CancelOrder {
67        /// The order's market.
68        market: Market,
69        /// The exchange's order identifier.
70        order_id: String,
71    },
72    /// Reads open positions, optionally for one market.
73    Positions {
74        /// The optional market filter.
75        market: Option<Market>,
76    },
77    /// Reads account-wide margin state.
78    MarginSummary,
79    /// Reads funding-rate history.
80    FundingRates {
81        /// The complete history request.
82        request: HistoryRequest,
83    },
84    /// Reads funding-payment history.
85    FundingPayments {
86        /// The complete history request.
87        request: HistoryRequest,
88    },
89    /// Changes leverage or margin mode.
90    SetMargin {
91        /// The complete margin request.
92        request: MarginRequest,
93    },
94}
95
96/// An owned reply returned by a foreign dispatcher.
97#[derive(Debug)]
98#[non_exhaustive]
99pub enum AdapterReply {
100    /// Result of [`AdapterCall::Markets`].
101    Markets(Vec<MarketInfo>),
102    /// Result of [`AdapterCall::Trades`].
103    Trades(Vec<Trade>),
104    /// Result of [`AdapterCall::OrderBook`].
105    OrderBook(OrderBook),
106    /// Result of [`AdapterCall::Ticker`].
107    Ticker(Ticker),
108    /// Result of [`AdapterCall::Candles`].
109    Candles(Vec<Candle>),
110    /// Result of [`AdapterCall::Subscribe`].
111    MarketStream(MarketStream),
112    /// Result of [`AdapterCall::Balances`].
113    Balances(Vec<Balance>),
114    /// Result of [`AdapterCall::OpenOrders`].
115    OpenOrders(Vec<Order>),
116    /// Result of [`AdapterCall::SubscribeAccount`].
117    AccountStream(AccountStream),
118    /// Result of [`AdapterCall::PlaceOrder`].
119    PlaceOrder(Order),
120    /// Result of [`AdapterCall::CancelOrder`].
121    CancelOrder(Order),
122    /// Result of [`AdapterCall::Positions`].
123    Positions(Vec<Position>),
124    /// Result of [`AdapterCall::MarginSummary`].
125    MarginSummary(MarginSummary),
126    /// Result of [`AdapterCall::FundingRates`].
127    FundingRates(Page<FundingRate>),
128    /// Result of [`AdapterCall::FundingPayments`].
129    FundingPayments(Page<FundingPayment>),
130    /// Result of [`AdapterCall::SetMargin`].
131    Unit,
132}
133
134/// Executes owned adapter calls in a foreign runtime.
135pub trait ForeignDispatcher: Send + Sync + 'static {
136    /// Dispatches one call and returns its typed reply.
137    fn dispatch(&self, call: AdapterCall) -> BoxFuture<'_, Result<AdapterReply>>;
138}
139
140impl AdapterReply {
141    pub(crate) const fn kind(&self) -> &'static str {
142        match self {
143            Self::Markets(_) => "Markets",
144            Self::Trades(_) => "Trades",
145            Self::OrderBook(_) => "OrderBook",
146            Self::Ticker(_) => "Ticker",
147            Self::Candles(_) => "Candles",
148            Self::MarketStream(_) => "MarketStream",
149            Self::Balances(_) => "Balances",
150            Self::OpenOrders(_) => "OpenOrders",
151            Self::AccountStream(_) => "AccountStream",
152            Self::PlaceOrder(_) => "PlaceOrder",
153            Self::CancelOrder(_) => "CancelOrder",
154            Self::Positions(_) => "Positions",
155            Self::MarginSummary(_) => "MarginSummary",
156            Self::FundingRates(_) => "FundingRates",
157            Self::FundingPayments(_) => "FundingPayments",
158            Self::Unit => "Unit",
159        }
160    }
161}