Skip to main content

nautilus_kraken/
config.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//! Configuration types for Kraken data and execution clients.
17
18use nautilus_model::identifiers::{AccountId, TraderId};
19use nautilus_network::websocket::TransportBackend;
20
21use crate::common::{
22    enums::{KrakenEnvironment, KrakenProductType},
23    urls::{get_kraken_http_base_url, get_kraken_ws_private_url, get_kraken_ws_public_url},
24};
25
26/// Configuration for the Kraken data client.
27#[derive(Debug, Clone, bon::Builder)]
28#[cfg_attr(
29    feature = "python",
30    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.kraken", from_py_object)
31)]
32#[cfg_attr(
33    feature = "python",
34    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.kraken")
35)]
36pub struct KrakenDataClientConfig {
37    pub api_key: Option<String>,
38    pub api_secret: Option<String>,
39    #[builder(default = KrakenProductType::Spot)]
40    pub product_type: KrakenProductType,
41    #[builder(default = KrakenEnvironment::Mainnet)]
42    pub environment: KrakenEnvironment,
43    pub base_url: Option<String>,
44    pub ws_public_url: Option<String>,
45    pub ws_private_url: Option<String>,
46    /// Optional proxy URL for HTTP and WebSocket transports.
47    pub proxy_url: Option<String>,
48    #[builder(default = 30)]
49    pub timeout_secs: u64,
50    #[builder(default = 30)]
51    pub heartbeat_interval_secs: u64,
52    pub max_requests_per_second: Option<u32>,
53    #[builder(default)]
54    pub transport_backend: TransportBackend,
55}
56
57impl Default for KrakenDataClientConfig {
58    fn default() -> Self {
59        Self::builder().build()
60    }
61}
62
63impl KrakenDataClientConfig {
64    /// Returns true if both API key and secret are set.
65    pub fn has_api_credentials(&self) -> bool {
66        self.api_key.is_some() && self.api_secret.is_some()
67    }
68
69    /// Returns the HTTP base URL for the configured product type and environment.
70    pub fn http_base_url(&self) -> String {
71        self.base_url.clone().unwrap_or_else(|| {
72            get_kraken_http_base_url(self.product_type, self.environment).to_string()
73        })
74    }
75
76    /// Returns the public WebSocket URL for the configured product type and environment.
77    pub fn ws_public_url(&self) -> String {
78        self.ws_public_url.clone().unwrap_or_else(|| {
79            get_kraken_ws_public_url(self.product_type, self.environment).to_string()
80        })
81    }
82
83    /// Returns the private WebSocket URL for the configured product type and environment.
84    pub fn ws_private_url(&self) -> String {
85        self.ws_private_url.clone().unwrap_or_else(|| {
86            get_kraken_ws_private_url(self.product_type, self.environment).to_string()
87        })
88    }
89}
90
91/// Configuration for the Kraken execution client.
92#[derive(Debug, Clone, bon::Builder)]
93#[cfg_attr(
94    feature = "python",
95    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.kraken", from_py_object)
96)]
97#[cfg_attr(
98    feature = "python",
99    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.kraken")
100)]
101pub struct KrakenExecClientConfig {
102    #[builder(default)]
103    pub trader_id: TraderId,
104    #[builder(default = AccountId::from("KRAKEN-001"))]
105    pub account_id: AccountId,
106    #[builder(default)]
107    pub api_key: String,
108    #[builder(default)]
109    pub api_secret: String,
110    #[builder(default = KrakenProductType::Spot)]
111    pub product_type: KrakenProductType,
112    #[builder(default = KrakenEnvironment::Mainnet)]
113    pub environment: KrakenEnvironment,
114    pub base_url: Option<String>,
115    pub ws_url: Option<String>,
116    /// Optional proxy URL for HTTP and WebSocket transports.
117    pub proxy_url: Option<String>,
118    #[builder(default = 30)]
119    pub timeout_secs: u64,
120    #[builder(default = 30)]
121    pub heartbeat_interval_secs: u64,
122    pub max_requests_per_second: Option<u32>,
123    #[builder(default)]
124    pub transport_backend: TransportBackend,
125}
126
127impl Default for KrakenExecClientConfig {
128    fn default() -> Self {
129        Self::builder().build()
130    }
131}
132
133impl KrakenExecClientConfig {
134    /// Returns the HTTP base URL for the configured product type and environment.
135    pub fn http_base_url(&self) -> String {
136        self.base_url.clone().unwrap_or_else(|| {
137            get_kraken_http_base_url(self.product_type, self.environment).to_string()
138        })
139    }
140
141    /// Returns the WebSocket URL for the configured product type and environment.
142    pub fn ws_url(&self) -> String {
143        self.ws_url.clone().unwrap_or_else(|| {
144            get_kraken_ws_private_url(self.product_type, self.environment).to_string()
145        })
146    }
147}