Skip to main content

nautilus_hyperliquid/python/
arrow.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
10//! Arrow helpers for Python-defined Hyperliquid custom data streams.
11
12use nautilus_core::python::to_pyvalue_err;
13use nautilus_serialization::{
14    arrow::EncodeToRecordBatch, python::arrow::arrow_record_batch_to_pybytes,
15};
16use pyo3::{prelude::*, types::PyBytes};
17
18use crate::data_types::HyperliquidPublicTrade;
19
20#[pymethods]
21#[pyo3_stub_gen::derive::gen_stub_pymethods]
22impl HyperliquidPublicTrade {
23    /// Encodes public Hyperliquid trades into Arrow IPC bytes for streaming persistence.
24    ///
25    /// # Errors
26    ///
27    /// Returns an error if no data is provided or Arrow encoding fails.
28    #[staticmethod]
29    #[expect(clippy::needless_pass_by_value)]
30    fn to_arrow_record_batch_bytes(py: Python<'_>, data: Vec<Self>) -> PyResult<Py<PyBytes>> {
31        let first = data
32            .first()
33            .ok_or_else(|| to_pyvalue_err("Cannot encode an empty HyperliquidPublicTrade batch"))?;
34        let metadata = Self::metadata(first);
35        let batch = Self::encode_batch(&metadata, &data).map_err(to_pyvalue_err)?;
36        arrow_record_batch_to_pybytes(py, &batch)
37    }
38}