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