Skip to main content

nautilus_kraken/execution/
mod.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//! Live execution client implementations for the Kraken adapter.
17//!
18//! This module provides separate execution clients for Kraken Spot and Futures markets:
19//!
20//! - [`KrakenSpotExecutionClient`] - For Spot markets using WebSocket v2
21//! - [`KrakenFuturesExecutionClient`] - For Futures markets
22//!
23//! # Supported Operations
24//!
25//! ## Common
26//! - Order submission (market, limit, stop)
27//! - Order modification
28//! - Order cancellation (single, batch, cancel-all)
29//! - Account state and balance queries
30//!
31//! ## Futures Only
32//! - Position management
33
34mod futures;
35mod spot;
36
37pub use futures::KrakenFuturesExecutionClient;
38pub use spot::KrakenSpotExecutionClient;
39
40use crate::http::error::KrakenHttpError;
41
42#[derive(Debug)]
43enum CancelCommandFailure {
44    LocalValidation(String),
45    Ambiguous(String),
46    VenueReject(String),
47}
48
49impl CancelCommandFailure {
50    fn local(message: impl Into<String>) -> Self {
51        Self::LocalValidation(message.into())
52    }
53
54    fn ambiguous(message: impl Into<String>) -> Self {
55        Self::Ambiguous(message.into())
56    }
57
58    fn venue_reject(message: impl Into<String>) -> Self {
59        Self::VenueReject(message.into())
60    }
61}
62
63fn classify_cancel_http_failure(error: KrakenHttpError) -> CancelCommandFailure {
64    match error {
65        KrakenHttpError::AuthenticationError(message) => CancelCommandFailure::local(message),
66        KrakenHttpError::MissingCredentials => CancelCommandFailure::local("Missing credentials"),
67        KrakenHttpError::NetworkError(message) | KrakenHttpError::ParseError(message) => {
68            CancelCommandFailure::ambiguous(message)
69        }
70        KrakenHttpError::ApiError(message) => {
71            CancelCommandFailure::ambiguous(format_cancel_api_errors(&message))
72        }
73    }
74}
75
76fn classify_spot_single_cancel_http_failure(error: KrakenHttpError) -> CancelCommandFailure {
77    match error {
78        KrakenHttpError::ApiError(message) if contains_spot_cancel_rejection(&message) => {
79            CancelCommandFailure::venue_reject(format_cancel_api_errors(&message))
80        }
81        KrakenHttpError::ApiError(message) => {
82            CancelCommandFailure::ambiguous(format_cancel_api_errors(&message))
83        }
84        other => classify_cancel_http_failure(other),
85    }
86}
87
88fn contains_spot_cancel_rejection(errors: &[String]) -> bool {
89    errors.iter().any(|e| e.trim_start().starts_with("EOrder:"))
90}
91
92fn format_cancel_api_errors(errors: &[String]) -> String {
93    if errors.is_empty() {
94        "unknown error (empty error list)".to_string()
95    } else {
96        errors.join(", ")
97    }
98}