Skip to main content

df_hyperliquid_core/trade/
action.rs

1//! Every trading action as one value, so what a confirmation shows and what is signed are the
2//! same value.
3
4use crate::trade::manage::{BatchModify, Modify};
5use crate::trade::manage::{Cancel, CancelByCloid};
6use crate::trade::manage::{Noop, ScheduleCancel};
7use crate::trade::margin::{UpdateIsolatedMargin, UpdateLeverage};
8use crate::trade::orders::{PlaceOrder, TrailingStop};
9use crate::trade::orders::{Twap, TwapCancel};
10use crate::utils::sign::{L1Action, Map};
11
12/// One trading action of any kind, signed as the action it holds.
13#[derive(Debug, Clone, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum TradeAction {
16    /// `order`: one or more orders.
17    Place(PlaceOrder),
18    /// `cancel`: orders by their oid.
19    Cancel(Cancel),
20    /// `cancelByCloid`: orders by their client order id.
21    CancelByCloid(CancelByCloid),
22    /// `modify`: one resting order replaced in place.
23    Modify(Modify),
24    /// `batchModify`: several resting orders replaced in place.
25    BatchModify(BatchModify),
26    /// `twapOrder`: a TWAP.
27    Twap(Twap),
28    /// `twapCancel`: a running TWAP stopped.
29    TwapCancel(TwapCancel),
30    /// `updateLeverage`: a perp's leverage and margin mode.
31    Leverage(UpdateLeverage),
32    /// `updateIsolatedMargin`: USDC moved into or out of an isolated position.
33    Margin(UpdateIsolatedMargin),
34    /// `scheduleCancel`: the dead man's switch armed or disarmed.
35    ScheduleCancel(ScheduleCancel),
36    /// `noop`: a nonce spent so an order signed with it never lands.
37    Noop(Noop),
38    /// `trailingStop`: a stop that trails the best price.
39    TrailingStop(TrailingStop),
40}
41
42impl TradeAction {
43    /// The action's `type`, which is also what an answer about it names.
44    #[must_use]
45    pub const fn kind(&self) -> &'static str {
46        match self {
47            TradeAction::Place(_) => "order",
48            TradeAction::Cancel(_) => "cancel",
49            TradeAction::CancelByCloid(_) => "cancelByCloid",
50            TradeAction::Modify(_) => "modify",
51            TradeAction::BatchModify(_) => "batchModify",
52            TradeAction::Twap(_) => "twapOrder",
53            TradeAction::TwapCancel(_) => "twapCancel",
54            TradeAction::Leverage(_) => "updateLeverage",
55            TradeAction::Margin(_) => "updateIsolatedMargin",
56            TradeAction::ScheduleCancel(_) => "scheduleCancel",
57            TradeAction::Noop(_) => "noop",
58            TradeAction::TrailingStop(_) => "trailingStop",
59        }
60    }
61
62    /// How many statuses the venue answers it with, one an order, cancel or
63    /// modify; `None` for an action answered by one status or none.
64    #[must_use]
65    pub fn items(&self) -> Option<usize> {
66        match self {
67            TradeAction::Place(place) => Some(place.orders().len()),
68            TradeAction::Cancel(cancel) => Some(cancel.cancels().len()),
69            TradeAction::CancelByCloid(cancel) => Some(cancel.cancels().len()),
70            TradeAction::BatchModify(batch) => Some(batch.modifies().len()),
71            _ => None,
72        }
73    }
74}
75
76impl L1Action for TradeAction {
77    fn action(&self) -> Map {
78        match self {
79            TradeAction::Place(action) => action.action(),
80            TradeAction::Cancel(action) => action.action(),
81            TradeAction::CancelByCloid(action) => action.action(),
82            TradeAction::Modify(action) => action.action(),
83            TradeAction::BatchModify(action) => action.action(),
84            TradeAction::Twap(action) => action.action(),
85            TradeAction::TwapCancel(action) => action.action(),
86            TradeAction::Leverage(action) => action.action(),
87            TradeAction::Margin(action) => action.action(),
88            TradeAction::ScheduleCancel(action) => action.action(),
89            TradeAction::Noop(action) => action.action(),
90            TradeAction::TrailingStop(action) => action.action(),
91        }
92    }
93}
94
95macro_rules! from {
96    ($($action:ident => $variant:ident),* $(,)?) => {
97        $(impl From<$action> for TradeAction {
98            fn from(action: $action) -> TradeAction {
99                TradeAction::$variant(action)
100            }
101        })*
102    };
103}
104
105from! {
106    PlaceOrder => Place,
107    Cancel => Cancel,
108    CancelByCloid => CancelByCloid,
109    Modify => Modify,
110    BatchModify => BatchModify,
111    Twap => Twap,
112    TwapCancel => TwapCancel,
113    UpdateLeverage => Leverage,
114    UpdateIsolatedMargin => Margin,
115    ScheduleCancel => ScheduleCancel,
116    Noop => Noop,
117    TrailingStop => TrailingStop,
118}
119
120#[cfg(test)]
121#[path = "tests/action.rs"]
122mod tests;