Skip to main content

nautilus_hyperliquid/
data_types.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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//! Hyperliquid-specific custom data types.
17//!
18//! These types carry Hyperliquid domain data through the Nautilus data engine as
19//! [`CustomData`](nautilus_model::data::CustomData).
20
21use std::collections::HashMap;
22
23use nautilus_core::UnixNanos;
24use nautilus_model::{identifiers::InstrumentId, types::Price};
25use nautilus_persistence_macros::custom_data;
26
27/// Hyperliquid all mid prices snapshot from the `allMids` WebSocket channel.
28#[cfg_attr(
29    feature = "arrow",
30    custom_data(pyo3, stub_module = "nautilus_trader.hyperliquid")
31)]
32#[cfg_attr(
33    not(feature = "arrow"),
34    custom_data(pyo3, no_arrow, stub_module = "nautilus_trader.hyperliquid")
35)]
36pub struct HyperliquidAllMids {
37    /// Mapping of instrument ID to mid price for all tradable coins.
38    #[custom_data_field(json)]
39    pub mids: HashMap<InstrumentId, Price>,
40    /// UNIX timestamp (nanoseconds) when the data event occurred.
41    pub ts_event: UnixNanos,
42    /// UNIX timestamp (nanoseconds) when the instance was initialized.
43    pub ts_init: UnixNanos,
44}
45
46/// Registers Hyperliquid custom data types.
47///
48/// Safe to call multiple times (idempotent via internal `Once` guards).
49pub fn register_hyperliquid_custom_data() {
50    #[cfg(feature = "arrow")]
51    nautilus_serialization::ensure_custom_data_registered::<HyperliquidAllMids>();
52
53    #[cfg(not(feature = "arrow"))]
54    let _ = nautilus_model::data::ensure_custom_data_json_registered::<HyperliquidAllMids>();
55}
56
57#[cfg(test)]
58mod tests {
59    use rstest::rstest;
60
61    use super::*;
62
63    #[rstest]
64    fn test_register_hyperliquid_custom_data_is_idempotent() {
65        register_hyperliquid_custom_data();
66        register_hyperliquid_custom_data();
67    }
68
69    #[cfg(feature = "arrow")]
70    #[rstest]
71    fn test_hyperliquid_all_mids_arrow_schema() {
72        use arrow::datatypes::DataType;
73        use nautilus_serialization::arrow::ArrowSchemaProvider;
74
75        let schema = HyperliquidAllMids::get_schema(None);
76
77        assert_eq!(schema.fields().len(), 3);
78        assert_eq!(schema.field(0).name(), "mids");
79        assert_eq!(schema.field(0).data_type(), &DataType::Utf8);
80        assert_eq!(schema.field(1).name(), "ts_event");
81        assert_eq!(schema.field(1).data_type(), &DataType::UInt64);
82        assert_eq!(schema.field(2).name(), "ts_init");
83        assert_eq!(schema.field(2).data_type(), &DataType::UInt64);
84    }
85}