df_hyperliquid_core/trade/manage/
modify.rs1use crate::account::FrontendOrder;
4use crate::trade::orders::Tif;
5use crate::trade::orders::limit::limit_on;
6use crate::trade::orders::tpsl::Covers;
7use crate::trade::orders::trigger::{ExitPrice, fill_price, trigger_on};
8use crate::trade::orders::{Cloid, Order};
9use crate::trade::ticket::{Amount, Market, Refusal, at_least};
10use crate::utils::Dec;
11use crate::utils::rounding::MINIMUM_ORDER_VALUE;
12use crate::utils::sign::{L1Action, Map, Value};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum OrderRef {
17 Oid(u64),
19 Cloid(Cloid),
21}
22
23impl OrderRef {
24 #[must_use]
26 pub fn wire(self) -> Value {
27 match self {
28 OrderRef::Oid(oid) => Value::Uint(oid),
29 OrderRef::Cloid(cloid) => Value::from(cloid.to_hex()),
30 }
31 }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct Modify {
39 target: OrderRef,
40 order: Order,
41 always_place: bool,
42}
43
44impl Modify {
45 #[must_use]
47 pub const fn new(target: OrderRef, order: Order) -> Modify {
48 Modify { target, order, always_place: order.is_trigger() }
49 }
50
51 #[must_use]
53 pub const fn always_place(mut self) -> Modify {
54 self.always_place = true;
55 self
56 }
57
58 #[must_use]
60 pub const fn is_always_place(&self) -> bool {
61 self.always_place
62 }
63
64 #[must_use]
66 pub const fn target(&self) -> OrderRef {
67 self.target
68 }
69
70 #[must_use]
72 pub const fn order(&self) -> &Order {
73 &self.order
74 }
75
76 #[must_use]
78 pub fn entry(&self) -> Map {
79 Map::new().put("oid", self.target.wire()).put("order", self.order.wire())
80 }
81}
82
83impl L1Action for Modify {
84 fn action(&self) -> Map {
85 Map::new()
86 .put("type", "modify")
87 .put("oid", self.target.wire())
88 .put("order", self.order.wire())
89 .put_some("a", self.always_place.then_some(true))
90 }
91}
92
93#[derive(Debug, Clone, PartialEq, Eq)]
97pub struct BatchModify {
98 modifies: Vec<Modify>,
99 always_place: bool,
100}
101
102impl BatchModify {
103 #[must_use]
105 pub fn new(modifies: Vec<Modify>) -> BatchModify {
106 let always_place = modifies.iter().any(|modify| modify.order.is_trigger());
107 BatchModify { modifies, always_place }
108 }
109
110 #[must_use]
112 pub fn always_place(mut self) -> BatchModify {
113 self.always_place = true;
114 self
115 }
116
117 #[must_use]
119 pub fn modifies(&self) -> &[Modify] {
120 &self.modifies
121 }
122
123 #[must_use]
125 pub const fn is_always_place(&self) -> bool {
126 self.always_place
127 }
128}
129
130impl L1Action for BatchModify {
131 fn action(&self) -> Map {
132 Map::new()
133 .put("type", "batchModify")
134 .put("modifies", Value::array(self.modifies.iter().map(Modify::entry)))
135 .put_some("a", self.always_place.then_some(true))
136 }
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
141pub enum Change {
142 Limit {
144 price: Dec,
146 size: Amount,
148 tif: Tif,
150 },
151 Trigger {
153 trigger: Dec,
155 price: ExitPrice,
157 size: Covers,
159 },
160}
161
162pub fn modify(market: &Market, order: &FrontendOrder, change: &Change) -> Result<BatchModify, Refusal> {
166 let grid = market.grid();
167 let side = order.side;
168 let new = match *change {
169 Change::Limit { .. } if order.is_trigger => return Err(Refusal::NotALimit(order.oid)),
170 Change::Limit { tif, .. } if !matches!(tif, Tif::Alo | Tif::Gtc) => return Err(Refusal::ModifyTif(tif)),
171 Change::Limit { price, size, tif } => {
172 let price = limit_on(grid, price, side, "a new price")?;
173 let size = size.size_on(grid, price)?;
174 at_least(size, price, MINIMUM_ORDER_VALUE)?;
175 Order::limit(market.asset(), side, price, size, tif)
176 }
177 Change::Trigger { .. } if !order.is_trigger => return Err(Refusal::NotATrigger(order.oid)),
178 Change::Trigger { trigger, price, size } => {
179 let Some(tpsl) = order.tpsl() else {
180 return Err(Refusal::UnknownTrigger { oid: order.oid, kind: order.order_type.word().to_lowercase() });
181 };
182 let trigger = trigger_on(grid, trigger)?;
183 let size = match size {
184 Covers::Position if order.sz.is_zero() => Dec::ZERO,
185 Covers::Position => return Err(Refusal::NotWholePosition(order.oid)),
186 Covers::Size(amount) => {
187 let size = amount.size_on(grid, trigger)?;
188 at_least(size, trigger, MINIMUM_ORDER_VALUE)?;
189 size
190 }
191 };
192 let (price, is_market) = fill_price(grid, trigger, price, side)?;
193 Order::trigger(market.asset(), side, price, size, trigger, tpsl, is_market)
194 }
195 };
196 let new = match order.reduce_only {
197 true => new.reduce_only(),
198 false => new,
199 };
200 let new = match order.cloid {
201 Some(cloid) => new.with_cloid(cloid),
202 None => new,
203 };
204 Ok(BatchModify::new(vec![Modify::new(OrderRef::Oid(order.oid), new)]))
205}
206
207#[cfg(test)]
208#[path = "tests/modify.rs"]
209mod tests;