barter_execution/order/
state.rs

1use crate::{error::OrderError, order::id::OrderId};
2use barter_instrument::{
3    asset::{AssetIndex, name::AssetNameExchange},
4    instrument::{InstrumentIndex, name::InstrumentNameExchange},
5};
6use chrono::{DateTime, Utc};
7use derive_more::{Constructor, From};
8use rust_decimal::Decimal;
9use serde::{Deserialize, Serialize};
10
11/// Convenient type alias for an [`OrderState`] keyed with [`AssetNameExchange`]
12/// and [`InstrumentNameExchange`].
13pub type UnindexedOrderState = OrderState<AssetNameExchange, InstrumentNameExchange>;
14
15#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, From)]
16pub enum OrderState<AssetKey = AssetIndex, InstrumentKey = InstrumentIndex> {
17    Active(ActiveOrderState),
18    Inactive(InactiveOrderState<AssetKey, InstrumentKey>),
19}
20
21impl<AssetKey, InstrumentKey> OrderState<AssetKey, InstrumentKey> {
22    pub fn active<S>(state: S) -> Self
23    where
24        S: Into<ActiveOrderState>,
25    {
26        OrderState::Active(state.into())
27    }
28
29    pub fn inactive<S>(state: S) -> Self
30    where
31        S: Into<InactiveOrderState<AssetKey, InstrumentKey>>,
32    {
33        OrderState::Inactive(state.into())
34    }
35
36    pub fn fully_filled() -> Self {
37        Self::Inactive(InactiveOrderState::FullyFilled)
38    }
39
40    pub fn expired() -> Self {
41        Self::Inactive(InactiveOrderState::Expired)
42    }
43}
44
45#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, From)]
46pub enum ActiveOrderState {
47    OpenInFlight(OpenInFlight),
48    Open(Open),
49    CancelInFlight(CancelInFlight),
50}
51
52impl ActiveOrderState {
53    pub fn open_meta(&self) -> Option<&Open> {
54        match self {
55            Self::OpenInFlight(_) => None,
56            Self::Open(open) => Some(open),
57            Self::CancelInFlight(cancel) => cancel.order.as_ref(),
58        }
59    }
60}
61
62#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
63pub struct OpenInFlight;
64
65#[derive(
66    Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Constructor,
67)]
68pub struct Open {
69    pub id: OrderId,
70    pub time_exchange: DateTime<Utc>,
71    pub filled_quantity: Decimal,
72}
73
74impl Open {
75    pub fn quantity_remaining(&self, initial_quantity: Decimal) -> Decimal {
76        initial_quantity - self.filled_quantity
77    }
78}
79
80#[derive(
81    Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default, Deserialize, Serialize, Constructor,
82)]
83pub struct CancelInFlight {
84    pub order: Option<Open>,
85}
86
87#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, From)]
88pub enum InactiveOrderState<AssetKey, InstrumentKey> {
89    Cancelled(Cancelled),
90    FullyFilled,
91    OpenFailed(OrderError<AssetKey, InstrumentKey>),
92    Expired,
93}
94
95#[derive(
96    Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Constructor,
97)]
98pub struct Cancelled {
99    pub id: OrderId,
100    pub time_exchange: DateTime<Utc>,
101}