rusty_common/constants/
coinbase.rs

1//! Coinbase API endpoint constants
2//!
3//! Centralized URL and configuration constants for all Coinbase integrations
4//! across REST, WebSocket, and FIX protocols.
5
6/// Coinbase API endpoint URLs
7pub mod urls {
8    /// WebSocket feed URLs (Advanced Trading)
9    pub mod websocket {
10        /// Production WebSocket URL for Advanced Trading
11        pub const ADVANCED_PROD: &str = "wss://ws-feed.exchange.coinbase.com";
12
13        /// Sandbox WebSocket URL for Advanced Trading
14        pub const ADVANCED_SANDBOX: &str = "wss://ws-feed-public.sandbox.exchange.coinbase.com";
15
16        /// Legacy production WebSocket URL (for compatibility)
17        pub const LEGACY_PROD: &str = "wss://ws-feed.pro.coinbase.com";
18
19        /// Legacy sandbox WebSocket URL (for compatibility)
20        pub const LEGACY_SANDBOX: &str = "wss://ws-feed-public.sandbox.pro.coinbase.com";
21    }
22
23    /// REST API URLs
24    pub mod rest {
25        /// Production REST API URL for Advanced Trading
26        pub const ADVANCED_PROD: &str = "https://api.coinbase.com/api/v3";
27
28        /// Sandbox REST API URL for Advanced Trading
29        pub const ADVANCED_SANDBOX: &str = "https://api-public.sandbox.exchange.coinbase.com";
30
31        /// Legacy production REST API URL
32        pub const LEGACY_PROD: &str = "https://api.exchange.coinbase.com";
33
34        /// Legacy sandbox REST API URL
35        pub const LEGACY_SANDBOX: &str = "https://api-public.sandbox.exchange.coinbase.com";
36    }
37
38    /// FIX protocol URLs
39    pub mod fix {
40        /// Production FIX host for Order Entry
41        pub const PROD_HOST: &str = "fix-ord.exchange.coinbase.com";
42
43        /// Sandbox FIX host for Order Entry
44        pub const SANDBOX_HOST: &str = "fix-ord.sandbox.exchange.coinbase.com";
45
46        /// Standard FIX port for both production and sandbox
47        pub const PORT: u16 = 6121;
48
49        /// Production FIX host for Market Data
50        pub const MARKET_DATA_PROD_HOST: &str = "fix-md.exchange.coinbase.com";
51
52        /// Sandbox FIX host for Market Data
53        pub const MARKET_DATA_SANDBOX_HOST: &str = "fix-md.sandbox.exchange.coinbase.com";
54
55        /// Market Data FIX port
56        pub const MARKET_DATA_PORT: u16 = 6120;
57    }
58}
59
60/// Rate limiting constants
61pub mod rate_limits {
62    /// FIX 5.0 rate limits
63    pub mod fix50 {
64        /// Normal request rate limit (requests per second)
65        pub const NORMAL_RPS: u32 = 100;
66
67        /// Disconnect threshold (messages per second)
68        pub const DISCONNECT_THRESHOLD: u32 = 200;
69
70        /// Logon rate limit (logons per second per API key)
71        pub const LOGON_RPS: u32 = 2;
72
73        /// Maximum batch size for orders
74        pub const MAX_BATCH_SIZE: usize = 15;
75    }
76
77    /// FIX 4.2 rate limits (legacy)
78    pub mod fix42 {
79        /// Rolling second rate limit
80        pub const ROLLING_SECOND_LIMIT: u32 = 50;
81
82        /// Burst rate limit
83        pub const BURST_LIMIT: u32 = 100;
84    }
85
86    /// REST API rate limits
87    pub mod rest {
88        /// Standard rate limit for most endpoints
89        pub const STANDARD_RPS: u32 = 10;
90
91        /// Higher limit for market data endpoints
92        pub const MARKET_DATA_RPS: u32 = 100;
93    }
94
95    /// WebSocket rate limits
96    pub mod websocket {
97        /// Message rate limit (messages per second)
98        pub const MESSAGE_RPS: u32 = 100;
99
100        /// Subscription rate limit
101        pub const SUBSCRIPTION_RPS: u32 = 10;
102    }
103}
104
105/// Protocol version constants
106pub mod versions {
107    /// FIX protocol versions
108    pub mod fix {
109        /// Session layer version (FIXT.1.1)
110        pub const SESSION_VERSION: &[u8] = b"FIXT.1.1";
111
112        /// Application version for FIX 5.0 SP2
113        pub const APPLICATION_VERSION: &[u8] = b"9";
114
115        /// Target company ID for Coinbase
116        pub const TARGET_COMP_ID: &[u8] = b"Coinbase";
117
118        /// Heartbeat interval (seconds)
119        pub const HEARTBEAT_INTERVAL: u32 = 30;
120    }
121
122    /// REST API versions
123    pub mod rest {
124        /// Advanced Trade API version
125        pub const ADVANCED_TRADE: &str = "v3";
126
127        /// Legacy Exchange API version
128        pub const LEGACY_EXCHANGE: &str = "v1";
129    }
130}
131
132/// Message size limits
133pub mod limits {
134    /// FIX message limits
135    pub mod fix {
136        /// Maximum FIX message size (bytes)
137        pub const MAX_MESSAGE_SIZE: usize = 8192;
138
139        /// Maximum orders per session
140        pub const MAX_OPEN_ORDERS: usize = 500;
141    }
142
143    /// WebSocket message limits
144    pub mod websocket {
145        /// Maximum frame size (bytes)
146        pub const MAX_FRAME_SIZE: usize = 65536; // 64KB
147
148        /// Maximum message size (bytes)
149        pub const MAX_MESSAGE_SIZE: usize = 10 * 1024 * 1024; // 10MB
150    }
151}
152
153/// Environment detection helpers
154/// Represents the Coinbase API environment.
155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum Environment {
157    /// The production environment.
158    Production,
159    /// The sandbox environment.
160    Sandbox,
161}
162
163impl Environment {
164    /// Get WebSocket URL for the environment
165    pub const fn websocket_url(self) -> &'static str {
166        match self {
167            Environment::Production => urls::websocket::ADVANCED_PROD,
168            Environment::Sandbox => urls::websocket::ADVANCED_SANDBOX,
169        }
170    }
171
172    /// Get REST API URL for the environment
173    pub const fn rest_url(self) -> &'static str {
174        match self {
175            Environment::Production => urls::rest::ADVANCED_PROD,
176            Environment::Sandbox => urls::rest::ADVANCED_SANDBOX,
177        }
178    }
179
180    /// Get FIX host for the environment
181    pub const fn fix_host(self) -> &'static str {
182        match self {
183            Environment::Production => urls::fix::PROD_HOST,
184            Environment::Sandbox => urls::fix::SANDBOX_HOST,
185        }
186    }
187
188    /// Get FIX port (same for both environments)
189    pub const fn fix_port(self) -> u16 {
190        urls::fix::PORT
191    }
192}
193
194#[cfg(test)]
195mod tests {
196    use super::*;
197
198    #[test]
199    fn test_environment_urls() {
200        assert_eq!(
201            Environment::Production.websocket_url(),
202            "wss://ws-feed.exchange.coinbase.com"
203        );
204        assert_eq!(
205            Environment::Sandbox.websocket_url(),
206            "wss://ws-feed-public.sandbox.exchange.coinbase.com"
207        );
208    }
209
210    #[test]
211    fn test_rate_limits() {
212        assert_eq!(rate_limits::fix50::NORMAL_RPS, 100);
213        assert_eq!(rate_limits::fix50::DISCONNECT_THRESHOLD, 200);
214        assert_eq!(rate_limits::fix50::MAX_BATCH_SIZE, 15);
215    }
216
217    #[test]
218    fn test_fix_constants() {
219        assert_eq!(versions::fix::SESSION_VERSION, b"FIXT.1.1");
220        assert_eq!(versions::fix::APPLICATION_VERSION, b"9");
221        assert_eq!(versions::fix::TARGET_COMP_ID, b"Coinbase");
222    }
223}