Skip to main content

nautilus_serialization/arrow/
funding.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
16use std::collections::HashMap;
17
18use arrow::{datatypes::Schema, error::ArrowError, record_batch::RecordBatch};
19use nautilus_model::data::{Data, FundingRateUpdate};
20
21use super::{
22    ArrowSchemaProvider, DecodeDataFromRecordBatch, DecodeFromRecordBatch, EncodeToRecordBatch,
23    EncodingError, KEY_INSTRUMENT_ID,
24    json::{
25        JsonFieldSpec, decode_batch, encode_batch_with_identifier, metadata_for_type,
26        schema_for_type_with_identifier,
27    },
28};
29
30const FUNDING_RATE_UPDATE_FIELDS: &[JsonFieldSpec] = &[
31    JsonFieldSpec::utf8("instrument_id", false),
32    JsonFieldSpec::utf8("rate", false),
33    JsonFieldSpec::u64("interval", true),
34    JsonFieldSpec::timestamp("next_funding_ns", true),
35    JsonFieldSpec::timestamp("ts_event", false),
36    JsonFieldSpec::timestamp("ts_init", false),
37];
38
39impl ArrowSchemaProvider for FundingRateUpdate {
40    fn get_schema(metadata: Option<HashMap<String, String>>) -> Schema {
41        schema_for_type_with_identifier("FundingRateUpdate", metadata, FUNDING_RATE_UPDATE_FIELDS)
42    }
43}
44
45impl EncodeToRecordBatch for FundingRateUpdate {
46    fn encode_batch<T>(
47        metadata: &HashMap<String, String>,
48        data: &[T],
49    ) -> Result<RecordBatch, ArrowError>
50    where
51        T: std::borrow::Borrow<Self>,
52    {
53        encode_batch_with_identifier(
54            "FundingRateUpdate",
55            metadata,
56            data.iter().map(std::borrow::Borrow::borrow),
57            FUNDING_RATE_UPDATE_FIELDS,
58            data.iter()
59                .map(std::borrow::Borrow::borrow)
60                .map(|update| update.instrument_id),
61        )
62    }
63
64    fn metadata(&self) -> HashMap<String, String> {
65        let mut metadata = metadata_for_type("FundingRateUpdate");
66        metadata.insert(
67            KEY_INSTRUMENT_ID.to_string(),
68            self.instrument_id.to_string(),
69        );
70        metadata
71    }
72}
73
74impl DecodeFromRecordBatch for FundingRateUpdate {
75    fn decode_batch(
76        metadata: &HashMap<String, String>,
77        record_batch: RecordBatch,
78    ) -> Result<Vec<Self>, EncodingError> {
79        decode_batch(
80            metadata,
81            &record_batch,
82            FUNDING_RATE_UPDATE_FIELDS,
83            Some("FundingRateUpdate"),
84        )
85    }
86}
87
88impl DecodeDataFromRecordBatch for FundingRateUpdate {
89    fn decode_data_batch(
90        metadata: &HashMap<String, String>,
91        record_batch: RecordBatch,
92    ) -> Result<Vec<Data>, EncodingError> {
93        let updates = Self::decode_batch(metadata, record_batch)?;
94        Ok(updates.into_iter().map(Data::from).collect())
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use std::str::FromStr;
101
102    use arrow::array::StringArray;
103    use nautilus_core::UnixNanos;
104    use nautilus_model::identifiers::InstrumentId;
105    use rstest::rstest;
106    use rust_decimal::Decimal;
107
108    use super::*;
109
110    #[rstest]
111    fn test_funding_rate_update_round_trip_preserves_decimal_precision() {
112        let update = FundingRateUpdate::new(
113            InstrumentId::from("BTCUSDT-PERP.BINANCE"),
114            Decimal::from_str("0.000123456789123456789").unwrap(),
115            Some(480),
116            Some(UnixNanos::from(9_000_000_000)),
117            UnixNanos::from(1_000_000_000),
118            UnixNanos::from(2_000_000_000),
119        );
120        let metadata = update.metadata();
121        let batch = FundingRateUpdate::encode_batch(&metadata, &[update]).unwrap();
122        let identifiers = batch
123            .column_by_name("identifier")
124            .unwrap()
125            .as_any()
126            .downcast_ref::<StringArray>()
127            .unwrap();
128        assert_eq!(FundingRateUpdate::get_fields()["rate"], "Utf8");
129        assert_eq!(
130            batch.schema().field_with_name("rate").unwrap().data_type(),
131            &arrow::datatypes::DataType::Utf8
132        );
133        assert_eq!(identifiers.value(0), "BTCUSDT-PERP.BINANCE");
134        let decoded = FundingRateUpdate::decode_batch(batch.schema().metadata(), batch).unwrap();
135
136        assert_eq!(decoded, vec![update]);
137    }
138
139    #[rstest]
140    fn test_funding_rate_update_round_trip_null_optionals() {
141        let update = FundingRateUpdate::new(
142            InstrumentId::from("BTCUSDT-PERP.BINANCE"),
143            Decimal::from_str("0.0001").unwrap(),
144            None,
145            None,
146            UnixNanos::from(1_000_000_000),
147            UnixNanos::from(2_000_000_000),
148        );
149        let metadata = update.metadata();
150        let batch = FundingRateUpdate::encode_batch(&metadata, &[update]).unwrap();
151        let decoded = FundingRateUpdate::decode_batch(batch.schema().metadata(), batch).unwrap();
152
153        assert_eq!(decoded, vec![update]);
154        assert!(decoded[0].interval.is_none());
155        assert!(decoded[0].next_funding_ns.is_none());
156    }
157}