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