Skip to main content

nautilus_coinbase/common/
consts.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 std::{sync::LazyLock, time::Duration};
17
18use nautilus_model::identifiers::Venue;
19use ustr::Ustr;
20
21pub const COINBASE: &str = "COINBASE";
22
23pub static COINBASE_VENUE: LazyLock<Venue> = LazyLock::new(|| Venue::new(Ustr::from(COINBASE)));
24
25pub const REST_URL: &str = "https://api.coinbase.com";
26pub const REST_API_PATH: &str = "/api/v3/brokerage";
27pub const WS_URL: &str = "wss://advanced-trade-ws.coinbase.com";
28pub const WS_USER_URL: &str = "wss://advanced-trade-ws-user.coinbase.com";
29
30pub const REST_URL_SANDBOX: &str = "https://api-sandbox.coinbase.com";
31pub const WS_URL_SANDBOX: &str = "wss://advanced-trade-ws-sandbox.coinbase.com";
32pub const WS_USER_URL_SANDBOX: &str = "wss://advanced-trade-ws-user-sandbox.coinbase.com";
33
34pub const JWT_ISSUER: &str = "cdp";
35
36/// Coinbase requires JWT regeneration within 2 minutes
37pub const JWT_EXPIRY_SECS: u64 = 120;
38
39pub const ORDER_CONFIG_MARKET_IOC: &str = "market_market_ioc";
40pub const ORDER_CONFIG_LIMIT_GTC: &str = "limit_limit_gtc";
41pub const ORDER_CONFIG_LIMIT_GTD: &str = "limit_limit_gtd";
42pub const ORDER_CONFIG_LIMIT_FOK: &str = "limit_limit_fok";
43pub const ORDER_CONFIG_STOP_LIMIT_GTC: &str = "stop_limit_stop_limit_gtc";
44pub const ORDER_CONFIG_STOP_LIMIT_GTD: &str = "stop_limit_stop_limit_gtd";
45pub const ORDER_CONFIG_BASE_SIZE: &str = "base_size";
46pub const ORDER_CONFIG_QUOTE_SIZE: &str = "quote_size";
47pub const ORDER_CONFIG_LIMIT_PRICE: &str = "limit_price";
48pub const ORDER_CONFIG_STOP_PRICE: &str = "stop_price";
49pub const ORDER_CONFIG_POST_ONLY: &str = "post_only";
50pub const ORDER_CONFIG_END_TIME: &str = "end_time";
51
52/// Maximum page size accepted by Coinbase's `/accounts` endpoint.
53pub const ACCOUNTS_PAGE_LIMIT: &str = "250";
54
55/// `order_status` filter value for Coinbase's `/orders/historical/batch`
56/// endpoint; selects orders the venue considers `OPEN`.
57pub const ORDER_STATUS_OPEN: &str = "OPEN";
58
59pub const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
60
61/// WebSocket control-frame ping interval, in seconds.
62pub const WS_HEARTBEAT_SECS: u64 = 30;
63
64pub const RECONNECT_BASE_BACKOFF: Duration = Duration::from_millis(250);
65pub const RECONNECT_MAX_BACKOFF: Duration = Duration::from_secs(30);
66pub const RECONNECT_JITTER_MS: u64 = 200;
67pub const RECONNECT_BACKOFF_FACTOR: f64 = 2.0;
68pub const RECONNECT_TIMEOUT: Duration = Duration::from_secs(15);
69
70/// Maximum time the client waits for the feed handler task to drain on
71/// disconnect before forcibly aborting.
72pub const WS_DISCONNECT_TIMEOUT: Duration = Duration::from_secs(5);
73
74/// Coinbase disconnects if no subscription within 5 seconds
75pub const WS_SUBSCRIBE_DEADLINE: Duration = Duration::from_secs(5);
76
77#[cfg(test)]
78mod tests {
79    use rstest::rstest;
80
81    use super::*;
82
83    #[rstest]
84    fn test_venue_constant() {
85        assert_eq!(COINBASE_VENUE.as_str(), COINBASE);
86    }
87
88    #[rstest]
89    fn test_url_constants() {
90        assert!(REST_URL.starts_with("https://"));
91        assert!(WS_URL.starts_with("wss://"));
92        assert!(WS_USER_URL.starts_with("wss://"));
93    }
94
95    #[rstest]
96    fn test_timeout_constants() {
97        assert_eq!(HTTP_TIMEOUT, Duration::from_secs(10));
98        assert_eq!(WS_HEARTBEAT_SECS, 30);
99        assert_eq!(WS_SUBSCRIBE_DEADLINE, Duration::from_secs(5));
100    }
101
102    #[rstest]
103    fn test_order_configuration_constants() {
104        assert_eq!(ORDER_CONFIG_MARKET_IOC, "market_market_ioc");
105        assert_eq!(ORDER_CONFIG_LIMIT_GTC, "limit_limit_gtc");
106        assert_eq!(ORDER_CONFIG_LIMIT_GTD, "limit_limit_gtd");
107        assert_eq!(ORDER_CONFIG_LIMIT_FOK, "limit_limit_fok");
108        assert_eq!(ORDER_CONFIG_STOP_LIMIT_GTC, "stop_limit_stop_limit_gtc");
109        assert_eq!(ORDER_CONFIG_STOP_LIMIT_GTD, "stop_limit_stop_limit_gtd");
110        assert_eq!(ORDER_CONFIG_BASE_SIZE, "base_size");
111        assert_eq!(ORDER_CONFIG_QUOTE_SIZE, "quote_size");
112        assert_eq!(ORDER_CONFIG_LIMIT_PRICE, "limit_price");
113        assert_eq!(ORDER_CONFIG_STOP_PRICE, "stop_price");
114        assert_eq!(ORDER_CONFIG_POST_ONLY, "post_only");
115        assert_eq!(ORDER_CONFIG_END_TIME, "end_time");
116    }
117}