Skip to main content

nautilus_model/events/funding/
settlement.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 nautilus_core::{UUID4, UnixNanos};
17use rust_decimal::Decimal;
18use serde::{Deserialize, Serialize};
19
20use crate::{
21    identifiers::{AccountId, InstrumentId, TraderId},
22    types::{Currency, Price},
23};
24
25/// Represents a funding settlement for a perpetual swap instrument.
26#[repr(C)]
27#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
28#[serde(tag = "type")]
29pub struct FundingSettlement {
30    /// The trader ID associated with the event.
31    pub trader_id: TraderId,
32    /// The instrument ID for the settlement.
33    pub instrument_id: InstrumentId,
34    /// The account ID receiving the settlement.
35    pub account_id: AccountId,
36    /// The funding rate applied for the settlement.
37    pub rate: Decimal,
38    /// The mark or settlement price used to value open positions.
39    pub settlement_price: Price,
40    /// The currency for resulting funding payments.
41    pub currency: Currency,
42    /// The unique identifier for the event.
43    pub event_id: UUID4,
44    /// UNIX timestamp (nanoseconds) when the event occurred.
45    pub ts_event: UnixNanos,
46    /// UNIX timestamp (nanoseconds) when the event was initialized.
47    pub ts_init: UnixNanos,
48}
49
50impl FundingSettlement {
51    /// Creates a new [`FundingSettlement`] instance.
52    #[must_use]
53    #[expect(clippy::too_many_arguments)]
54    pub const fn new(
55        trader_id: TraderId,
56        instrument_id: InstrumentId,
57        account_id: AccountId,
58        rate: Decimal,
59        settlement_price: Price,
60        currency: Currency,
61        event_id: UUID4,
62        ts_event: UnixNanos,
63        ts_init: UnixNanos,
64    ) -> Self {
65        Self {
66            trader_id,
67            instrument_id,
68            account_id,
69            rate,
70            settlement_price,
71            currency,
72            event_id,
73            ts_event,
74            ts_init,
75        }
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use std::str::FromStr;
82
83    use nautilus_core::{UUID4, UnixNanos};
84    use rstest::rstest;
85    use rust_decimal::Decimal;
86
87    use super::*;
88    use crate::{
89        identifiers::{AccountId, InstrumentId, TraderId},
90        types::{Currency, Price},
91    };
92
93    fn create_test_settlement() -> FundingSettlement {
94        FundingSettlement::new(
95            TraderId::from("TRADER-001"),
96            InstrumentId::from("BTCUSDT-PERP.BINANCE"),
97            AccountId::from("BINANCE-001"),
98            Decimal::from_str("0.0001").unwrap(),
99            Price::from("65000.00"),
100            Currency::USDT(),
101            UUID4::default(),
102            UnixNanos::from(1_000_000_000),
103            UnixNanos::from(2_000_000_000),
104        )
105    }
106
107    #[rstest]
108    fn test_funding_settlement_serialization() {
109        let original = create_test_settlement();
110
111        let json = serde_json::to_string(&original).unwrap();
112        let deserialized: FundingSettlement = serde_json::from_str(&json).unwrap();
113
114        assert_eq!(original, deserialized);
115    }
116}