nautilus_hyperliquid/common/
parse.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Parsing utilities that convert Hyperliquid payloads into Nautilus domain models.
17//!
18//! # Conditional Order Support
19//!
20//! This module implements comprehensive conditional order support for Hyperliquid,
21//! following patterns established in the OKX, Bybit, and BitMEX adapters.
22//!
23//! ## Supported Order Types
24//!
25//! ### Standard Orders
26//! - **Market**: Implemented as IOC (Immediate-or-Cancel) limit orders
27//! - **Limit**: Standard limit orders with GTC/IOC/ALO time-in-force
28//!
29//! ### Conditional/Trigger Orders
30//! - **StopMarket**: Protective stop that triggers at specified price and executes at market
31//! - **StopLimit**: Protective stop that triggers at specified price and executes at limit
32//! - **MarketIfTouched**: Profit-taking/entry order that triggers and executes at market
33//! - **LimitIfTouched**: Profit-taking/entry order that triggers and executes at limit
34//!
35//! ## Order Semantics
36//!
37//! ### Stop Orders (StopMarket/StopLimit)
38//! - Used for protective stops and risk management
39//! - Mapped to Hyperliquid's trigger orders with `tpsl: Sl`
40//! - Trigger when price reaches the stop level
41//! - Execute immediately (market) or at limit price
42//!
43//! ### If Touched Orders (MarketIfTouched/LimitIfTouched)
44//! - Used for profit-taking or entry orders
45//! - Mapped to Hyperliquid's trigger orders with `tpsl: Tp`
46//! - Trigger when price reaches the target level
47//! - Execute immediately (market) or at limit price
48//!
49//! ## Trigger Price Logic
50//!
51//! The `tpsl` field (Take Profit / Stop Loss) is determined by:
52//! 1. **Order Type**: Stop orders → SL, If Touched orders → TP
53//! 2. **Price Relationship** (if available):
54//!    - For BUY orders: trigger above market → SL, below → TP
55//!    - For SELL orders: trigger below market → SL, above → TP
56//!
57//! ## Trigger Type Support
58//!
59//! Currently, Hyperliquid uses **last traded price** for all trigger evaluations.
60//!
61//! Future enhancement: Add support for mark/index price triggers if Hyperliquid API adds this feature.
62//! See OKX's `OKXTriggerType` and Bybit's `BybitTriggerType` for reference implementations.
63//!
64//! ## Examples
65//!
66//! ### Stop Loss Order
67//! ```ignore
68//! // Long position at $100, stop loss at $95
69//! let order = StopMarket {
70//!     side: Sell,
71//!     trigger_price: $95,
72//!     // ... other fields
73//! };
74//! // Maps to: Trigger { is_market: true, trigger_px: $95, tpsl: Sl }
75//! ```
76//!
77//! ### Take Profit Order
78//! ```ignore
79//! // Long position at $100, take profit at $110
80//! let order = MarketIfTouched {
81//!     side: Sell,
82//!     trigger_price: $110,
83//!     // ... other fields
84//! };
85//! // Maps to: Trigger { is_market: true, trigger_px: $110, tpsl: Tp }
86//! ```
87//!
88//! ## Integration with Other Adapters
89//!
90//! This implementation reuses patterns from:
91//! - **OKX**: Conditional order types and algo order API structure
92//! - **Bybit**: TP/SL mode detection and trigger direction logic
93//! - **BitMEX**: Stop order handling and trigger price validation
94//!
95//! See:
96//! - `crates/adapters/okx/src/common/consts.rs` - OKX_CONDITIONAL_ORDER_TYPES
97//! - `crates/adapters/bybit/src/common/enums.rs` - BybitStopOrderType, BybitTriggerType
98//! - `crates/adapters/bitmex/src/execution/mod.rs` - trigger_price handling
99
100use std::str::FromStr;
101
102use anyhow::Context;
103use nautilus_model::{
104    enums::{OrderSide, OrderStatus, OrderType, TimeInForce},
105    identifiers::{InstrumentId, Symbol, Venue},
106    orders::{Order, any::OrderAny},
107    types::{AccountBalance, Currency, MarginBalance, Money},
108};
109use rust_decimal::Decimal;
110use serde::{Deserialize, Deserializer, Serializer};
111use serde_json::Value;
112
113use crate::http::models::{
114    AssetId, Cloid, CrossMarginSummary, HyperliquidExchangeResponse,
115    HyperliquidExecCancelByCloidRequest, HyperliquidExecLimitParams, HyperliquidExecOrderKind,
116    HyperliquidExecPlaceOrderRequest, HyperliquidExecTif, HyperliquidExecTpSl,
117    HyperliquidExecTriggerParams,
118};
119
120/// Serializes decimal as string (lossless, no scientific notation).
121pub fn serialize_decimal_as_str<S>(decimal: &Decimal, serializer: S) -> Result<S::Ok, S::Error>
122where
123    S: Serializer,
124{
125    serializer.serialize_str(&decimal.normalize().to_string())
126}
127
128/// Deserializes decimal from string only (reject numbers to avoid precision loss).
129pub fn deserialize_decimal_from_str<'de, D>(deserializer: D) -> Result<Decimal, D::Error>
130where
131    D: Deserializer<'de>,
132{
133    let s = String::deserialize(deserializer)?;
134    Decimal::from_str(&s).map_err(serde::de::Error::custom)
135}
136
137/// Serialize optional decimal as string
138pub fn serialize_optional_decimal_as_str<S>(
139    decimal: &Option<Decimal>,
140    serializer: S,
141) -> Result<S::Ok, S::Error>
142where
143    S: Serializer,
144{
145    match decimal {
146        Some(d) => serializer.serialize_str(&d.normalize().to_string()),
147        None => serializer.serialize_none(),
148    }
149}
150
151/// Deserialize optional decimal from string
152pub fn deserialize_optional_decimal_from_str<'de, D>(
153    deserializer: D,
154) -> Result<Option<Decimal>, D::Error>
155where
156    D: Deserializer<'de>,
157{
158    let opt = Option::<String>::deserialize(deserializer)?;
159    match opt {
160        Some(s) => {
161            let decimal = Decimal::from_str(&s).map_err(serde::de::Error::custom)?;
162            Ok(Some(decimal))
163        }
164        None => Ok(None),
165    }
166}
167
168/// Serialize vector of decimals as strings
169pub fn serialize_vec_decimal_as_str<S>(
170    decimals: &Vec<Decimal>,
171    serializer: S,
172) -> Result<S::Ok, S::Error>
173where
174    S: Serializer,
175{
176    use serde::ser::SerializeSeq;
177    let mut seq = serializer.serialize_seq(Some(decimals.len()))?;
178    for decimal in decimals {
179        seq.serialize_element(&decimal.normalize().to_string())?;
180    }
181    seq.end()
182}
183
184/// Deserialize vector of decimals from strings
185pub fn deserialize_vec_decimal_from_str<'de, D>(deserializer: D) -> Result<Vec<Decimal>, D::Error>
186where
187    D: Deserializer<'de>,
188{
189    let strings = Vec::<String>::deserialize(deserializer)?;
190    strings
191        .into_iter()
192        .map(|s| Decimal::from_str(&s).map_err(serde::de::Error::custom))
193        .collect()
194}
195
196////////////////////////////////////////////////////////////////////////////////
197// Normalization and Validation Functions
198////////////////////////////////////////////////////////////////////////////////
199
200/// Round price down to the nearest valid tick size
201#[inline]
202pub fn round_down_to_tick(price: Decimal, tick_size: Decimal) -> Decimal {
203    if tick_size.is_zero() {
204        return price;
205    }
206    (price / tick_size).floor() * tick_size
207}
208
209/// Round quantity down to the nearest valid step size
210#[inline]
211pub fn round_down_to_step(qty: Decimal, step_size: Decimal) -> Decimal {
212    if step_size.is_zero() {
213        return qty;
214    }
215    (qty / step_size).floor() * step_size
216}
217
218/// Ensure the notional value meets minimum requirements
219#[inline]
220pub fn ensure_min_notional(
221    price: Decimal,
222    qty: Decimal,
223    min_notional: Decimal,
224) -> Result<(), String> {
225    let notional = price * qty;
226    if notional < min_notional {
227        Err(format!(
228            "Notional value {} is less than minimum required {}",
229            notional, min_notional
230        ))
231    } else {
232        Ok(())
233    }
234}
235
236/// Normalize price to the specified number of decimal places
237pub fn normalize_price(price: Decimal, decimals: u8) -> Decimal {
238    let scale = Decimal::from(10_u64.pow(decimals as u32));
239    (price * scale).floor() / scale
240}
241
242/// Normalize quantity to the specified number of decimal places
243pub fn normalize_quantity(qty: Decimal, decimals: u8) -> Decimal {
244    let scale = Decimal::from(10_u64.pow(decimals as u32));
245    (qty * scale).floor() / scale
246}
247
248/// Complete normalization for an order including price, quantity, and notional validation
249pub fn normalize_order(
250    price: Decimal,
251    qty: Decimal,
252    tick_size: Decimal,
253    step_size: Decimal,
254    min_notional: Decimal,
255    price_decimals: u8,
256    size_decimals: u8,
257) -> Result<(Decimal, Decimal), String> {
258    // Normalize to decimal places first
259    let normalized_price = normalize_price(price, price_decimals);
260    let normalized_qty = normalize_quantity(qty, size_decimals);
261
262    // Round down to tick/step sizes
263    let final_price = round_down_to_tick(normalized_price, tick_size);
264    let final_qty = round_down_to_step(normalized_qty, step_size);
265
266    // Validate minimum notional
267    ensure_min_notional(final_price, final_qty, min_notional)?;
268
269    Ok((final_price, final_qty))
270}
271
272// ================================================================================================
273// Order Conversion Functions
274// ================================================================================================
275
276/// Converts a Nautilus `TimeInForce` to Hyperliquid TIF.
277///
278/// # Errors
279///
280/// Returns an error if the time in force is not supported.
281pub fn time_in_force_to_hyperliquid_tif(
282    tif: TimeInForce,
283    is_post_only: bool,
284) -> anyhow::Result<HyperliquidExecTif> {
285    match (tif, is_post_only) {
286        (_, true) => Ok(HyperliquidExecTif::Alo), // Always use ALO for post-only orders
287        (TimeInForce::Gtc, false) => Ok(HyperliquidExecTif::Gtc),
288        (TimeInForce::Ioc, false) => Ok(HyperliquidExecTif::Ioc),
289        (TimeInForce::Fok, false) => Ok(HyperliquidExecTif::Ioc), // FOK maps to IOC in Hyperliquid
290        _ => anyhow::bail!("Unsupported time in force for Hyperliquid: {tif:?}"),
291    }
292}
293
294/// Extracts asset ID from instrument symbol.
295///
296/// For Hyperliquid, this typically involves parsing the symbol to get the underlying asset.
297/// Currently supports a hardcoded mapping for common assets.
298///
299/// # Errors
300///
301/// Returns an error if the symbol format is unsupported or the asset is not found.
302pub fn extract_asset_id_from_symbol(symbol: &str) -> anyhow::Result<AssetId> {
303    // For perpetuals, remove "-USD" suffix to get the base asset
304    if let Some(base) = symbol.strip_suffix("-USD") {
305        // Convert symbol like "BTC-USD" to asset index
306        // This is a simplified mapping - in practice you'd need to query the asset registry
307        Ok(match base {
308            "BTC" => 0,
309            "ETH" => 1,
310            "DOGE" => 3,
311            "SOL" => 4,
312            "WIF" => 8,
313            "SHIB" => 10,
314            "PEPE" => 11,
315            _ => {
316                // For unknown assets, we'll need to query the meta endpoint
317                // For now, return a placeholder that will need to be resolved
318                anyhow::bail!("Asset ID mapping not found for symbol: {symbol}")
319            }
320        })
321    } else {
322        anyhow::bail!("Cannot extract asset ID from symbol: {symbol}")
323    }
324}
325
326/// Determines if a trigger order should be TP (take profit) or SL (stop loss).
327///
328/// Logic follows exchange patterns from OKX/Bybit:
329/// - For BUY orders: trigger above current price = SL, below = TP
330/// - For SELL orders: trigger below current price = SL, above = TP
331/// - For Market/Limit If Touched orders: always TP (triggered when price reaches target)
332///
333/// # Note
334///
335/// Hyperliquid's trigger logic:
336/// - StopMarket/StopLimit: Protective stops (SL)
337/// - MarketIfTouched/LimitIfTouched: Profit taking or entry orders (TP)
338fn determine_tpsl_type(
339    order_type: OrderType,
340    order_side: OrderSide,
341    trigger_price: Decimal,
342    current_price: Option<Decimal>,
343) -> HyperliquidExecTpSl {
344    match order_type {
345        // Stop orders are protective - always SL
346        OrderType::StopMarket | OrderType::StopLimit => HyperliquidExecTpSl::Sl,
347
348        // If Touched orders are profit-taking or entry orders - always TP
349        OrderType::MarketIfTouched | OrderType::LimitIfTouched => HyperliquidExecTpSl::Tp,
350
351        // For other trigger types, try to infer from price relationship if available
352        _ => {
353            if let Some(current) = current_price {
354                match order_side {
355                    OrderSide::Buy => {
356                        // Buy order: trigger above market = stop loss, below = take profit
357                        if trigger_price > current {
358                            HyperliquidExecTpSl::Sl
359                        } else {
360                            HyperliquidExecTpSl::Tp
361                        }
362                    }
363                    OrderSide::Sell => {
364                        // Sell order: trigger below market = stop loss, above = take profit
365                        if trigger_price < current {
366                            HyperliquidExecTpSl::Sl
367                        } else {
368                            HyperliquidExecTpSl::Tp
369                        }
370                    }
371                    _ => HyperliquidExecTpSl::Sl, // Default to SL for safety
372                }
373            } else {
374                // No market price available, default to SL for safety
375                HyperliquidExecTpSl::Sl
376            }
377        }
378    }
379}
380
381/// Converts a Nautilus order into a Hyperliquid order request.
382///
383/// # Supported Order Types
384///
385/// - `Market`: Implemented as IOC limit order
386/// - `Limit`: Standard limit order with TIF (GTC/IOC/ALO)
387/// - `StopMarket`: Trigger order with market execution (protective stop)
388/// - `StopLimit`: Trigger order with limit price (protective stop)
389/// - `MarketIfTouched`: Trigger order with market execution (profit taking/entry)
390/// - `LimitIfTouched`: Trigger order with limit price (profit taking/entry)
391///
392/// # Conditional Order Patterns
393///
394/// Following patterns from OKX and Bybit adapters:
395/// - Stop orders (StopMarket/StopLimit) use `tpsl: Sl`
396/// - If Touched orders (MIT/LIT) use `tpsl: Tp`
397/// - Trigger price determines when order activates
398/// - Order side and trigger price relationship determines TP vs SL semantics
399///
400/// # Trigger Type Support
401///
402/// Hyperliquid currently uses last traded price for all triggers.
403/// Future enhancement: Add support for mark/index price triggers if Hyperliquid API supports it.
404pub fn order_to_hyperliquid_request(
405    order: &OrderAny,
406) -> anyhow::Result<HyperliquidExecPlaceOrderRequest> {
407    let instrument_id = order.instrument_id();
408    let symbol = instrument_id.symbol.as_str();
409    let asset = extract_asset_id_from_symbol(symbol)
410        .with_context(|| format!("Failed to extract asset ID from symbol: {}", symbol))?;
411
412    let is_buy = matches!(order.order_side(), OrderSide::Buy);
413    let reduce_only = order.is_reduce_only();
414    let order_side = order.order_side();
415    let order_type = order.order_type();
416
417    // Convert price to decimal
418    let price_decimal = match order.price() {
419        Some(price) => Decimal::from_str_exact(&price.to_string())
420            .with_context(|| format!("Failed to convert price to decimal: {}", price))?,
421        None => {
422            // For market orders without price, use 0 as placeholder
423            // The actual market price will be determined by the exchange
424            if matches!(
425                order_type,
426                OrderType::Market | OrderType::StopMarket | OrderType::MarketIfTouched
427            ) {
428                Decimal::ZERO
429            } else {
430                anyhow::bail!("Limit orders require a price")
431            }
432        }
433    };
434
435    // Convert size to decimal
436    let size_decimal =
437        Decimal::from_str_exact(&order.quantity().to_string()).with_context(|| {
438            format!(
439                "Failed to convert quantity to decimal: {}",
440                order.quantity()
441            )
442        })?;
443
444    // Determine order kind based on order type
445    let kind = match order_type {
446        OrderType::Market => {
447            // Market orders in Hyperliquid are implemented as limit orders with IOC time-in-force
448            HyperliquidExecOrderKind::Limit {
449                limit: HyperliquidExecLimitParams {
450                    tif: HyperliquidExecTif::Ioc,
451                },
452            }
453        }
454        OrderType::Limit => {
455            let tif =
456                time_in_force_to_hyperliquid_tif(order.time_in_force(), order.is_post_only())?;
457            HyperliquidExecOrderKind::Limit {
458                limit: HyperliquidExecLimitParams { tif },
459            }
460        }
461        OrderType::StopMarket => {
462            if let Some(trigger_price) = order.trigger_price() {
463                let trigger_price_decimal = Decimal::from_str_exact(&trigger_price.to_string())
464                    .with_context(|| {
465                        format!(
466                            "Failed to convert trigger price to decimal: {}",
467                            trigger_price
468                        )
469                    })?;
470
471                // Determine TP/SL based on order semantics
472                let tpsl = determine_tpsl_type(
473                    order_type,
474                    order_side,
475                    trigger_price_decimal,
476                    None, // Current market price not available here
477                );
478
479                HyperliquidExecOrderKind::Trigger {
480                    trigger: HyperliquidExecTriggerParams {
481                        is_market: true,
482                        trigger_px: trigger_price_decimal,
483                        tpsl,
484                    },
485                }
486            } else {
487                anyhow::bail!("Stop market orders require a trigger price")
488            }
489        }
490        OrderType::StopLimit => {
491            if let Some(trigger_price) = order.trigger_price() {
492                let trigger_price_decimal = Decimal::from_str_exact(&trigger_price.to_string())
493                    .with_context(|| {
494                        format!(
495                            "Failed to convert trigger price to decimal: {}",
496                            trigger_price
497                        )
498                    })?;
499
500                // Determine TP/SL based on order semantics
501                let tpsl = determine_tpsl_type(order_type, order_side, trigger_price_decimal, None);
502
503                HyperliquidExecOrderKind::Trigger {
504                    trigger: HyperliquidExecTriggerParams {
505                        is_market: false,
506                        trigger_px: trigger_price_decimal,
507                        tpsl,
508                    },
509                }
510            } else {
511                anyhow::bail!("Stop limit orders require a trigger price")
512            }
513        }
514        OrderType::MarketIfTouched => {
515            // MIT orders trigger when price is reached and execute at market
516            // These are typically used for profit taking or entry orders
517            if let Some(trigger_price) = order.trigger_price() {
518                let trigger_price_decimal = Decimal::from_str_exact(&trigger_price.to_string())
519                    .with_context(|| {
520                        format!(
521                            "Failed to convert trigger price to decimal: {}",
522                            trigger_price
523                        )
524                    })?;
525
526                HyperliquidExecOrderKind::Trigger {
527                    trigger: HyperliquidExecTriggerParams {
528                        is_market: true,
529                        trigger_px: trigger_price_decimal,
530                        tpsl: HyperliquidExecTpSl::Tp, // MIT is typically for profit taking
531                    },
532                }
533            } else {
534                anyhow::bail!("Market-if-touched orders require a trigger price")
535            }
536        }
537        OrderType::LimitIfTouched => {
538            // LIT orders trigger when price is reached and execute at limit price
539            // These are typically used for profit taking or entry orders with price control
540            if let Some(trigger_price) = order.trigger_price() {
541                let trigger_price_decimal = Decimal::from_str_exact(&trigger_price.to_string())
542                    .with_context(|| {
543                        format!(
544                            "Failed to convert trigger price to decimal: {}",
545                            trigger_price
546                        )
547                    })?;
548
549                HyperliquidExecOrderKind::Trigger {
550                    trigger: HyperliquidExecTriggerParams {
551                        is_market: false,
552                        trigger_px: trigger_price_decimal,
553                        tpsl: HyperliquidExecTpSl::Tp, // LIT is typically for profit taking
554                    },
555                }
556            } else {
557                anyhow::bail!("Limit-if-touched orders require a trigger price")
558            }
559        }
560        _ => anyhow::bail!("Unsupported order type for Hyperliquid: {:?}", order_type),
561    };
562
563    // Convert client order ID to CLOID
564    let cloid = match Cloid::from_hex(order.client_order_id()) {
565        Ok(cloid) => Some(cloid),
566        Err(err) => {
567            anyhow::bail!(
568                "Failed to convert client order ID '{}' to CLOID: {}",
569                order.client_order_id(),
570                err
571            )
572        }
573    };
574
575    Ok(HyperliquidExecPlaceOrderRequest {
576        asset,
577        is_buy,
578        price: price_decimal,
579        size: size_decimal,
580        reduce_only,
581        kind,
582        cloid,
583    })
584}
585
586/// Converts a list of Nautilus orders into Hyperliquid order requests.
587pub fn orders_to_hyperliquid_requests(
588    orders: &[&OrderAny],
589) -> anyhow::Result<Vec<HyperliquidExecPlaceOrderRequest>> {
590    orders
591        .iter()
592        .map(|order| order_to_hyperliquid_request(order))
593        .collect()
594}
595
596/// Creates a JSON value representing multiple orders for the Hyperliquid exchange action.
597pub fn orders_to_hyperliquid_action_value(orders: &[&OrderAny]) -> anyhow::Result<Value> {
598    let requests = orders_to_hyperliquid_requests(orders)?;
599    serde_json::to_value(requests).context("Failed to serialize orders to JSON")
600}
601
602/// Converts an OrderAny into a Hyperliquid order request.
603pub fn order_any_to_hyperliquid_request(
604    order: &OrderAny,
605) -> anyhow::Result<HyperliquidExecPlaceOrderRequest> {
606    order_to_hyperliquid_request(order)
607}
608
609/// Converts a client order ID to a Hyperliquid cancel request.
610///
611/// # Errors
612///
613/// Returns an error if the symbol cannot be parsed or the client order ID is invalid.
614pub fn client_order_id_to_cancel_request(
615    client_order_id: &str,
616    symbol: &str,
617) -> anyhow::Result<HyperliquidExecCancelByCloidRequest> {
618    let asset = extract_asset_id_from_symbol(symbol)
619        .with_context(|| format!("Failed to extract asset ID from symbol: {}", symbol))?;
620
621    let cloid = Cloid::from_hex(client_order_id).map_err(|e| {
622        anyhow::anyhow!(
623            "Failed to convert client order ID '{}' to CLOID: {}",
624            client_order_id,
625            e
626        )
627    })?;
628
629    Ok(HyperliquidExecCancelByCloidRequest { asset, cloid })
630}
631
632/// Creates a JSON value representing cancel requests for the Hyperliquid exchange action.
633pub fn cancel_requests_to_hyperliquid_action_value(
634    requests: &[HyperliquidExecCancelByCloidRequest],
635) -> anyhow::Result<Value> {
636    serde_json::to_value(requests).context("Failed to serialize cancel requests to JSON")
637}
638
639/// Checks if a Hyperliquid exchange response indicates success.
640pub fn is_response_successful(response: &HyperliquidExchangeResponse) -> bool {
641    matches!(response, HyperliquidExchangeResponse::Status { status, .. } if status == "ok")
642}
643
644/// Extracts error message from a Hyperliquid exchange response.
645pub fn extract_error_message(response: &HyperliquidExchangeResponse) -> String {
646    match response {
647        HyperliquidExchangeResponse::Status { status, response } => {
648            if status == "ok" {
649                "Operation successful".to_string()
650            } else {
651                // Try to extract error message from response data
652                if let Some(error_msg) = response.get("error").and_then(|v| v.as_str()) {
653                    error_msg.to_string()
654                } else {
655                    format!("Request failed with status: {}", status)
656                }
657            }
658        }
659        HyperliquidExchangeResponse::Error { error } => error.clone(),
660    }
661}
662
663/// Determines if an order is a conditional/trigger order based on order data.
664///
665/// # Arguments
666///
667/// * `trigger_px` - Optional trigger price
668/// * `tpsl` - Optional TP/SL indicator
669///
670/// # Returns
671///
672/// `true` if the order is a conditional order, `false` otherwise.
673pub fn is_conditional_order_data(trigger_px: Option<&str>, tpsl: Option<&str>) -> bool {
674    trigger_px.is_some() && tpsl.is_some()
675}
676
677/// Parses trigger order type from Hyperliquid order data.
678///
679/// # Arguments
680///
681/// * `is_market` - Whether this is a market trigger order
682/// * `tpsl` - TP/SL indicator ("tp" or "sl")
683///
684/// # Returns
685///
686/// The corresponding Nautilus `OrderType`.
687pub fn parse_trigger_order_type(is_market: bool, tpsl: &str) -> OrderType {
688    match (is_market, tpsl) {
689        (true, "sl") => OrderType::StopMarket,
690        (false, "sl") => OrderType::StopLimit,
691        (true, "tp") => OrderType::MarketIfTouched,
692        (false, "tp") => OrderType::LimitIfTouched,
693        _ => OrderType::StopMarket, // Default fallback
694    }
695}
696
697/// Extracts order status from WebSocket order data.
698///
699/// # Arguments
700///
701/// * `status` - Status string from WebSocket
702/// * `trigger_activated` - Whether trigger has been activated (for conditional orders)
703///
704/// # Returns
705///
706/// A tuple of (OrderStatus, optional trigger status string).
707pub fn parse_order_status_with_trigger(
708    status: &str,
709    trigger_activated: Option<bool>,
710) -> (OrderStatus, Option<String>) {
711    use crate::common::enums::hyperliquid_status_to_order_status;
712
713    let base_status = hyperliquid_status_to_order_status(status);
714
715    // For conditional orders, add trigger status information
716    if let Some(activated) = trigger_activated {
717        let trigger_status = if activated {
718            Some("activated".to_string())
719        } else {
720            Some("pending".to_string())
721        };
722        (base_status, trigger_status)
723    } else {
724        (base_status, None)
725    }
726}
727
728/// Converts WebSocket trailing stop data to description string.
729///
730/// # Arguments
731///
732/// * `offset` - Trailing offset value
733/// * `offset_type` - Type of offset ("price", "percentage", "basisPoints")
734/// * `callback_price` - Current callback price
735///
736/// # Returns
737///
738/// Human-readable description of trailing stop parameters.
739pub fn format_trailing_stop_info(
740    offset: &str,
741    offset_type: &str,
742    callback_price: Option<&str>,
743) -> String {
744    let offset_desc = match offset_type {
745        "percentage" => format!("{}%", offset),
746        "basisPoints" => format!("{} bps", offset),
747        "price" => offset.to_string(),
748        _ => offset.to_string(),
749    };
750
751    if let Some(callback) = callback_price {
752        format!(
753            "Trailing stop: {} offset, callback at {}",
754            offset_desc, callback
755        )
756    } else {
757        format!("Trailing stop: {} offset", offset_desc)
758    }
759}
760
761/// Validates conditional order parameters from WebSocket data.
762///
763/// # Arguments
764///
765/// * `trigger_px` - Trigger price
766/// * `tpsl` - TP/SL indicator
767/// * `is_market` - Market or limit flag
768///
769/// # Returns
770///
771/// `Ok(())` if parameters are valid, `Err` with description otherwise.
772///
773/// # Panics
774///
775/// This function does not panic - it returns errors instead of panicking.
776pub fn validate_conditional_order_params(
777    trigger_px: Option<&str>,
778    tpsl: Option<&str>,
779    is_market: Option<bool>,
780) -> anyhow::Result<()> {
781    if trigger_px.is_none() {
782        anyhow::bail!("Conditional order missing trigger price");
783    }
784
785    if tpsl.is_none() {
786        anyhow::bail!("Conditional order missing tpsl indicator");
787    }
788
789    let tpsl_value = tpsl.expect("tpsl should be Some at this point");
790    if tpsl_value != "tp" && tpsl_value != "sl" {
791        anyhow::bail!("Invalid tpsl value: {}", tpsl_value);
792    }
793
794    if is_market.is_none() {
795        anyhow::bail!("Conditional order missing is_market flag");
796    }
797
798    Ok(())
799}
800
801/// Parses trigger price from string to Decimal.
802///
803/// # Arguments
804///
805/// * `trigger_px` - Trigger price as string
806///
807/// # Returns
808///
809/// Parsed Decimal value or error.
810pub fn parse_trigger_price(trigger_px: &str) -> anyhow::Result<Decimal> {
811    Decimal::from_str_exact(trigger_px)
812        .with_context(|| format!("Failed to parse trigger price: {}", trigger_px))
813}
814
815/// Parses Hyperliquid clearinghouse state into Nautilus account balances and margins.
816///
817/// # Errors
818///
819/// Returns an error if the data cannot be parsed.
820pub fn parse_account_balances_and_margins(
821    cross_margin_summary: &CrossMarginSummary,
822) -> anyhow::Result<(Vec<AccountBalance>, Vec<MarginBalance>)> {
823    let mut balances = Vec::new();
824    let mut margins = Vec::new();
825
826    // Parse balance from cross margin summary
827    let currency = Currency::USD(); // Hyperliquid uses USDC/USD
828
829    // Account value represents total collateral
830    let total_value = cross_margin_summary
831        .account_value
832        .to_string()
833        .parse::<f64>()?;
834
835    // Withdrawable represents available balance
836    let withdrawable = cross_margin_summary
837        .withdrawable
838        .to_string()
839        .parse::<f64>()?;
840
841    // Total margin used is locked in positions
842    let margin_used = cross_margin_summary
843        .total_margin_used
844        .to_string()
845        .parse::<f64>()?;
846
847    // Calculate total, locked, and free
848    let total = Money::new(total_value, currency);
849    let locked = Money::new(margin_used, currency);
850    let free = Money::new(withdrawable, currency);
851
852    let balance = AccountBalance::new(total, locked, free);
853    balances.push(balance);
854
855    // Create margin balance for the account
856    // Initial margin = margin used (locked in positions)
857    // Maintenance margin can be approximated from leverage and position values
858    // For now, use margin_used as both initial and maintenance (conservative)
859    if margin_used > 0.0 {
860        let margin_instrument_id =
861            InstrumentId::new(Symbol::new("ACCOUNT"), Venue::new("HYPERLIQUID"));
862
863        let initial_margin = Money::new(margin_used, currency);
864        let maintenance_margin = Money::new(margin_used, currency);
865
866        let margin_balance =
867            MarginBalance::new(initial_margin, maintenance_margin, margin_instrument_id);
868
869        margins.push(margin_balance);
870    }
871
872    Ok((balances, margins))
873}
874
875////////////////////////////////////////////////////////////////////////////////
876// Tests
877////////////////////////////////////////////////////////////////////////////////
878
879#[cfg(test)]
880mod tests {
881    use rstest::rstest;
882    use serde::{Deserialize, Serialize};
883
884    use super::*;
885
886    #[derive(Serialize, Deserialize)]
887    struct TestStruct {
888        #[serde(
889            serialize_with = "serialize_decimal_as_str",
890            deserialize_with = "deserialize_decimal_from_str"
891        )]
892        value: Decimal,
893        #[serde(
894            serialize_with = "serialize_optional_decimal_as_str",
895            deserialize_with = "deserialize_optional_decimal_from_str"
896        )]
897        optional_value: Option<Decimal>,
898    }
899
900    #[rstest]
901    fn test_decimal_serialization_roundtrip() {
902        let original = TestStruct {
903            value: Decimal::from_str("123.456789012345678901234567890").unwrap(),
904            optional_value: Some(Decimal::from_str("0.000000001").unwrap()),
905        };
906
907        let json = serde_json::to_string(&original).unwrap();
908        println!("Serialized: {}", json);
909
910        // Check that it's serialized as strings (rust_decimal may normalize precision)
911        assert!(json.contains("\"123.45678901234567890123456789\""));
912        assert!(json.contains("\"0.000000001\""));
913
914        let deserialized: TestStruct = serde_json::from_str(&json).unwrap();
915        assert_eq!(original.value, deserialized.value);
916        assert_eq!(original.optional_value, deserialized.optional_value);
917    }
918
919    #[rstest]
920    fn test_decimal_precision_preservation() {
921        let test_cases = [
922            "0",
923            "1",
924            "0.1",
925            "0.01",
926            "0.001",
927            "123.456789012345678901234567890",
928            "999999999999999999.999999999999999999",
929        ];
930
931        for case in test_cases {
932            let decimal = Decimal::from_str(case).unwrap();
933            let test_struct = TestStruct {
934                value: decimal,
935                optional_value: Some(decimal),
936            };
937
938            let json = serde_json::to_string(&test_struct).unwrap();
939            let parsed: TestStruct = serde_json::from_str(&json).unwrap();
940
941            assert_eq!(decimal, parsed.value, "Failed for case: {}", case);
942            assert_eq!(
943                Some(decimal),
944                parsed.optional_value,
945                "Failed for case: {}",
946                case
947            );
948        }
949    }
950
951    #[rstest]
952    fn test_optional_none_handling() {
953        let test_struct = TestStruct {
954            value: Decimal::from_str("42.0").unwrap(),
955            optional_value: None,
956        };
957
958        let json = serde_json::to_string(&test_struct).unwrap();
959        assert!(json.contains("null"));
960
961        let parsed: TestStruct = serde_json::from_str(&json).unwrap();
962        assert_eq!(test_struct.value, parsed.value);
963        assert_eq!(None, parsed.optional_value);
964    }
965
966    #[rstest]
967    fn test_round_down_to_tick() {
968        use rust_decimal_macros::dec;
969
970        assert_eq!(round_down_to_tick(dec!(100.07), dec!(0.05)), dec!(100.05));
971        assert_eq!(round_down_to_tick(dec!(100.03), dec!(0.05)), dec!(100.00));
972        assert_eq!(round_down_to_tick(dec!(100.05), dec!(0.05)), dec!(100.05));
973
974        // Edge case: zero tick size
975        assert_eq!(round_down_to_tick(dec!(100.07), dec!(0)), dec!(100.07));
976    }
977
978    #[rstest]
979    fn test_round_down_to_step() {
980        use rust_decimal_macros::dec;
981
982        assert_eq!(
983            round_down_to_step(dec!(0.12349), dec!(0.0001)),
984            dec!(0.1234)
985        );
986        assert_eq!(round_down_to_step(dec!(1.5555), dec!(0.1)), dec!(1.5));
987        assert_eq!(round_down_to_step(dec!(1.0001), dec!(0.0001)), dec!(1.0001));
988
989        // Edge case: zero step size
990        assert_eq!(round_down_to_step(dec!(0.12349), dec!(0)), dec!(0.12349));
991    }
992
993    #[rstest]
994    fn test_min_notional_validation() {
995        use rust_decimal_macros::dec;
996
997        // Should pass
998        assert!(ensure_min_notional(dec!(100), dec!(0.1), dec!(10)).is_ok());
999        assert!(ensure_min_notional(dec!(100), dec!(0.11), dec!(10)).is_ok());
1000
1001        // Should fail
1002        assert!(ensure_min_notional(dec!(100), dec!(0.05), dec!(10)).is_err());
1003        assert!(ensure_min_notional(dec!(1), dec!(5), dec!(10)).is_err());
1004
1005        // Edge case: exactly at minimum
1006        assert!(ensure_min_notional(dec!(100), dec!(0.1), dec!(10)).is_ok());
1007    }
1008
1009    #[rstest]
1010    fn test_normalize_price() {
1011        use rust_decimal_macros::dec;
1012
1013        assert_eq!(normalize_price(dec!(100.12345), 2), dec!(100.12));
1014        assert_eq!(normalize_price(dec!(100.19999), 2), dec!(100.19));
1015        assert_eq!(normalize_price(dec!(100.999), 0), dec!(100));
1016        assert_eq!(normalize_price(dec!(100.12345), 4), dec!(100.1234));
1017    }
1018
1019    #[rstest]
1020    fn test_normalize_quantity() {
1021        use rust_decimal_macros::dec;
1022
1023        assert_eq!(normalize_quantity(dec!(1.12345), 3), dec!(1.123));
1024        assert_eq!(normalize_quantity(dec!(1.99999), 3), dec!(1.999));
1025        assert_eq!(normalize_quantity(dec!(1.999), 0), dec!(1));
1026        assert_eq!(normalize_quantity(dec!(1.12345), 5), dec!(1.12345));
1027    }
1028
1029    #[rstest]
1030    fn test_normalize_order_complete() {
1031        use rust_decimal_macros::dec;
1032
1033        let result = normalize_order(
1034            dec!(100.12345), // price
1035            dec!(0.123456),  // qty
1036            dec!(0.01),      // tick_size
1037            dec!(0.0001),    // step_size
1038            dec!(10),        // min_notional
1039            2,               // price_decimals
1040            4,               // size_decimals
1041        );
1042
1043        assert!(result.is_ok());
1044        let (price, qty) = result.unwrap();
1045        assert_eq!(price, dec!(100.12)); // normalized and rounded down
1046        assert_eq!(qty, dec!(0.1234)); // normalized and rounded down
1047    }
1048
1049    #[rstest]
1050    fn test_normalize_order_min_notional_fail() {
1051        use rust_decimal_macros::dec;
1052
1053        let result = normalize_order(
1054            dec!(100.12345), // price
1055            dec!(0.05),      // qty (too small for min notional)
1056            dec!(0.01),      // tick_size
1057            dec!(0.0001),    // step_size
1058            dec!(10),        // min_notional
1059            2,               // price_decimals
1060            4,               // size_decimals
1061        );
1062
1063        assert!(result.is_err());
1064        assert!(result.unwrap_err().contains("Notional value"));
1065    }
1066
1067    #[rstest]
1068    fn test_edge_cases() {
1069        use rust_decimal_macros::dec;
1070
1071        // Test with very small numbers
1072        assert_eq!(
1073            round_down_to_tick(dec!(0.000001), dec!(0.000001)),
1074            dec!(0.000001)
1075        );
1076
1077        // Test with large numbers
1078        assert_eq!(round_down_to_tick(dec!(999999.99), dec!(1.0)), dec!(999999));
1079
1080        // Test rounding edge case
1081        assert_eq!(
1082            round_down_to_tick(dec!(100.009999), dec!(0.01)),
1083            dec!(100.00)
1084        );
1085    }
1086
1087    // ========================================================================
1088    // Conditional Order Parsing Tests
1089    // ========================================================================
1090
1091    #[rstest]
1092    fn test_is_conditional_order_data() {
1093        // Test with trigger price and tpsl (conditional)
1094        assert!(is_conditional_order_data(Some("50000.0"), Some("sl")));
1095
1096        // Test with only trigger price (not conditional - needs both)
1097        assert!(!is_conditional_order_data(Some("50000.0"), None));
1098
1099        // Test with only tpsl (not conditional - needs both)
1100        assert!(!is_conditional_order_data(None, Some("tp")));
1101
1102        // Test with no conditional fields
1103        assert!(!is_conditional_order_data(None, None));
1104    }
1105
1106    #[rstest]
1107    fn test_parse_trigger_order_type() {
1108        // Stop Market
1109        assert_eq!(parse_trigger_order_type(true, "sl"), OrderType::StopMarket);
1110
1111        // Stop Limit
1112        assert_eq!(parse_trigger_order_type(false, "sl"), OrderType::StopLimit);
1113
1114        // Take Profit Market
1115        assert_eq!(
1116            parse_trigger_order_type(true, "tp"),
1117            OrderType::MarketIfTouched
1118        );
1119
1120        // Take Profit Limit
1121        assert_eq!(
1122            parse_trigger_order_type(false, "tp"),
1123            OrderType::LimitIfTouched
1124        );
1125    }
1126
1127    #[rstest]
1128    fn test_parse_order_status_with_trigger() {
1129        // Test with open status and activated trigger
1130        let (status, trigger_status) = parse_order_status_with_trigger("open", Some(true));
1131        assert_eq!(status, OrderStatus::Accepted);
1132        assert_eq!(trigger_status, Some("activated".to_string()));
1133
1134        // Test with open status and not activated
1135        let (status, trigger_status) = parse_order_status_with_trigger("open", Some(false));
1136        assert_eq!(status, OrderStatus::Accepted);
1137        assert_eq!(trigger_status, Some("pending".to_string()));
1138
1139        // Test without trigger info
1140        let (status, trigger_status) = parse_order_status_with_trigger("open", None);
1141        assert_eq!(status, OrderStatus::Accepted);
1142        assert_eq!(trigger_status, None);
1143    }
1144
1145    #[rstest]
1146    fn test_format_trailing_stop_info() {
1147        // Price offset
1148        let info = format_trailing_stop_info("100.0", "price", Some("50000.0"));
1149        assert!(info.contains("100.0"));
1150        assert!(info.contains("callback at 50000.0"));
1151
1152        // Percentage offset
1153        let info = format_trailing_stop_info("5.0", "percentage", None);
1154        assert!(info.contains("5.0%"));
1155        assert!(info.contains("Trailing stop"));
1156
1157        // Basis points offset
1158        let info = format_trailing_stop_info("250", "basisPoints", Some("49000.0"));
1159        assert!(info.contains("250 bps"));
1160        assert!(info.contains("49000.0"));
1161    }
1162
1163    #[rstest]
1164    fn test_parse_trigger_price() {
1165        use rust_decimal_macros::dec;
1166
1167        // Valid price
1168        let result = parse_trigger_price("50000.0");
1169        assert!(result.is_ok());
1170        assert_eq!(result.unwrap(), dec!(50000.0));
1171
1172        // Valid integer price
1173        let result = parse_trigger_price("49000");
1174        assert!(result.is_ok());
1175        assert_eq!(result.unwrap(), dec!(49000));
1176
1177        // Invalid price
1178        let result = parse_trigger_price("invalid");
1179        assert!(result.is_err());
1180
1181        // Empty string
1182        let result = parse_trigger_price("");
1183        assert!(result.is_err());
1184    }
1185}