Skip to main content

nautilus_model/accounts/
stubs.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//! Lightweight stub implementations useful in unit tests where a full account object is
17//! unnecessary.
18
19use rstest::fixture;
20
21use crate::{
22    accounts::{Account, AccountAny, BettingAccount, CashAccount, MarginAccount},
23    enums::{AccountType, LiquiditySide},
24    events::account::{
25        state::AccountState,
26        stubs::{
27            betting_account_state, cash_account_state, cash_account_state_million_usd,
28            cash_account_state_million_usdt, cash_account_state_multi, margin_account_state,
29        },
30    },
31    identifiers::stubs::{account_id, uuid4},
32    instruments::InstrumentAny,
33    types::{AccountBalance, Currency, Money, Price, Quantity},
34};
35
36impl Default for CashAccount {
37    /// Creates a new default [`CashAccount`] instance.
38    fn default() -> Self {
39        // million dollar account
40        let init_event = AccountState::new(
41            account_id(),
42            AccountType::Cash,
43            vec![AccountBalance::new(
44                Money::from("1000000 USD"),
45                Money::from("0 USD"),
46                Money::from("1000000 USD"),
47            )],
48            vec![],
49            true,
50            uuid4(),
51            0.into(),
52            0.into(),
53            Some(Currency::USD()),
54        );
55        Self::new(init_event, false, false)
56    }
57}
58
59impl Default for AccountAny {
60    /// Creates a new default [`AccountAny`] instance.
61    fn default() -> Self {
62        Self::Cash(CashAccount::default())
63    }
64}
65
66#[fixture]
67pub fn margin_account(margin_account_state: AccountState) -> MarginAccount {
68    MarginAccount::new(margin_account_state, true)
69}
70
71#[fixture]
72pub fn cash_account(cash_account_state: AccountState) -> CashAccount {
73    CashAccount::new(cash_account_state, true, false)
74}
75
76#[fixture]
77pub fn betting_account(betting_account_state: AccountState) -> BettingAccount {
78    BettingAccount::new(betting_account_state, true)
79}
80
81#[fixture]
82pub fn cash_account_million_usd(cash_account_state_million_usd: AccountState) -> CashAccount {
83    CashAccount::new(cash_account_state_million_usd, true, false)
84}
85
86#[fixture]
87pub fn cash_account_multi(cash_account_state_multi: AccountState) -> CashAccount {
88    CashAccount::new(cash_account_state_multi, true, false)
89}
90
91#[fixture]
92pub fn cash_account_borrowing(cash_account_state: AccountState) -> CashAccount {
93    CashAccount::new(cash_account_state, true, true)
94}
95
96#[fixture]
97pub fn cash_account_borrowing_million_usd(
98    cash_account_state_million_usd: AccountState,
99) -> CashAccount {
100    CashAccount::new(cash_account_state_million_usd, true, true)
101}
102
103/// Helper to calculate commission in test fixtures.
104///
105/// # Panics
106///
107/// Panics if the underlying `calculate_commission` returns an error.
108#[must_use]
109pub fn calculate_commission(
110    instrument: &InstrumentAny,
111    quantity: Quantity,
112    price: Price,
113    currency: Option<Currency>,
114) -> Money {
115    let account_state = if Some(Currency::USDT()) == currency {
116        cash_account_state_million_usdt()
117    } else {
118        cash_account_state_million_usd("1000000 USD", "0 USD", "1000000 USD")
119    };
120    let account = cash_account_million_usd(account_state);
121    account
122        .calculate_commission(instrument, quantity, price, LiquiditySide::Taker, None)
123        .unwrap()
124}