coinbase-advanced 1.0.0

A Rust client library for the Coinbase Advanced Trade API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! WebSocket message types.

use serde::{Deserialize, Serialize};

use super::channels::ChannelName;

/// A message received from the WebSocket.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// The channel the message is from.
    pub channel: ChannelName,
    /// The client ID for the message.
    pub client_id: String,
    /// The timestamp for the message.
    pub timestamp: String,
    /// The sequence number for the message.
    pub sequence_num: u64,
    /// The events in the message.
    pub events: Events,
}

/// Events that can be received in a message.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Events {
    /// Status events.
    Status(Vec<StatusEvent>),
    /// Candle events.
    Candles(Vec<CandlesEvent>),
    /// Ticker events.
    Ticker(Vec<TickerEvent>),
    /// Level 2 order book events.
    Level2(Vec<Level2Event>),
    /// User events.
    User(Vec<UserEvent>),
    /// Market trade events.
    MarketTrades(Vec<MarketTradesEvent>),
    /// Heartbeat events.
    Heartbeats(Vec<HeartbeatsEvent>),
    /// Subscription confirmation events.
    Subscriptions(Vec<SubscriptionsEvent>),
    /// Futures balance summary events.
    FuturesBalanceSummary(Vec<FuturesBalanceSummaryEvent>),
}

/// Event type (snapshot or update).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum EventType {
    /// Initial snapshot of data.
    Snapshot,
    /// Incremental update.
    Update,
}

/// Status event containing product status updates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatusEvent {
    /// Event type.
    pub r#type: EventType,
    /// Product updates.
    pub products: Vec<ProductStatus>,
}

/// Product status information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProductStatus {
    /// Product type.
    pub product_type: String,
    /// Product ID.
    pub id: String,
    /// Base currency symbol.
    pub base_currency: String,
    /// Quote currency symbol.
    pub quote_currency: String,
    /// Minimum base increment.
    pub base_increment: String,
    /// Minimum quote increment.
    pub quote_increment: String,
    /// Display name.
    pub display_name: String,
    /// Product status.
    pub status: String,
    /// Additional status message.
    #[serde(default)]
    pub status_message: String,
    /// Minimum market funds.
    pub min_market_funds: String,
}

/// Candles event containing candle updates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CandlesEvent {
    /// Event type.
    pub r#type: EventType,
    /// Candle updates.
    pub candles: Vec<CandleUpdate>,
}

/// A candle (OHLCV) update.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CandleUpdate {
    /// Product ID.
    pub product_id: String,
    /// Start time.
    pub start: String,
    /// Open price.
    pub open: String,
    /// High price.
    pub high: String,
    /// Low price.
    pub low: String,
    /// Close price.
    pub close: String,
    /// Volume.
    pub volume: String,
}

/// Ticker event containing ticker updates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TickerEvent {
    /// Event type.
    pub r#type: EventType,
    /// Ticker updates.
    pub tickers: Vec<TickerUpdate>,
}

/// A ticker update.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TickerUpdate {
    /// Ticker type.
    pub r#type: String,
    /// Product ID.
    pub product_id: String,
    /// Current price.
    pub price: String,
    /// 24-hour volume.
    pub volume_24_h: String,
    /// 24-hour low.
    pub low_24_h: String,
    /// 24-hour high.
    pub high_24_h: String,
    /// 52-week low.
    pub low_52_w: String,
    /// 52-week high.
    pub high_52_w: String,
    /// 24-hour price percentage change.
    pub price_percent_chg_24_h: String,
}

/// Level 2 order book event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Level2Event {
    /// Event type.
    pub r#type: EventType,
    /// Product ID.
    pub product_id: String,
    /// Order book updates.
    pub updates: Vec<Level2Update>,
}

/// A Level 2 order book update.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Level2Update {
    /// Side (bid or ask).
    pub side: Level2Side,
    /// Event time.
    pub event_time: String,
    /// Price level.
    pub price_level: String,
    /// New quantity at this level.
    pub new_quantity: String,
}

/// Side of a Level 2 order book entry.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Level2Side {
    /// Bid (buy) side.
    Bid,
    /// Ask (sell) side.
    #[serde(alias = "offer")]
    Ask,
}

/// User event containing order updates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserEvent {
    /// Event type.
    pub r#type: EventType,
    /// Order updates.
    pub orders: Vec<OrderUpdate>,
}

/// An order update for the user channel.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderUpdate {
    /// Average fill price.
    #[serde(default)]
    pub avg_price: String,
    /// Cancel reason if cancelled.
    #[serde(default)]
    pub cancel_reason: String,
    /// Client-provided order ID.
    #[serde(default)]
    pub client_order_id: String,
    /// Completion percentage.
    #[serde(default)]
    pub completion_percentage: String,
    /// Contract expiry type.
    #[serde(default)]
    pub contract_expiry_type: String,
    /// Cumulative filled quantity.
    #[serde(default)]
    pub cumulative_quantity: String,
    /// Total filled value.
    #[serde(default)]
    pub filled_value: String,
    /// Remaining quantity.
    #[serde(default)]
    pub leaves_quantity: String,
    /// Limit price.
    #[serde(default)]
    pub limit_price: String,
    /// Number of fills.
    #[serde(default)]
    pub number_of_fills: String,
    /// Order ID.
    pub order_id: String,
    /// Order side (BUY or SELL).
    pub order_side: String,
    /// Order type.
    pub order_type: String,
    /// Outstanding hold amount.
    #[serde(default)]
    pub outstanding_hold_amount: String,
    /// Post-only flag.
    #[serde(default)]
    pub post_only: bool,
    /// Product ID.
    pub product_id: String,
    /// Product type.
    #[serde(default)]
    pub product_type: String,
    /// Reject reason if rejected.
    #[serde(default)]
    pub reject_reason: Option<String>,
    /// Retail portfolio ID.
    #[serde(default)]
    pub retail_portfolio_id: String,
    /// Risk managed by.
    #[serde(default)]
    pub risk_managed_by: String,
    /// Order status.
    pub status: String,
    /// Stop price.
    #[serde(default)]
    pub stop_price: Option<String>,
    /// Time in force.
    #[serde(default)]
    pub time_in_force: String,
    /// Total fees.
    #[serde(default)]
    pub total_fees: String,
    /// Total value after fees.
    #[serde(default)]
    pub total_value_after_fees: String,
    /// Trigger status.
    #[serde(default)]
    pub trigger_status: String,
    /// Creation time.
    #[serde(default)]
    pub creation_time: String,
    /// End time.
    #[serde(default)]
    pub end_time: String,
    /// Start time.
    #[serde(default)]
    pub start_time: String,
}

/// Market trades event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketTradesEvent {
    /// Event type.
    pub r#type: EventType,
    /// Trade updates.
    pub trades: Vec<TradeUpdate>,
}

/// A trade update.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TradeUpdate {
    /// Trade ID.
    pub trade_id: String,
    /// Product ID.
    pub product_id: String,
    /// Trade price.
    pub price: String,
    /// Trade size.
    pub size: String,
    /// Trade side (BUY or SELL).
    pub side: String,
    /// Trade time.
    pub time: String,
}

/// Heartbeat event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeartbeatsEvent {
    /// Current server time.
    pub current_time: String,
    /// Heartbeat counter.
    pub heartbeat_counter: u64,
}

/// Subscriptions confirmation event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionsEvent {
    /// Current subscriptions.
    pub subscriptions: SubscriptionStatus,
}

/// Current subscription status.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SubscriptionStatus {
    /// Status channel subscriptions.
    #[serde(default)]
    pub status: Vec<String>,
    /// Ticker channel subscriptions.
    #[serde(default)]
    pub ticker: Vec<String>,
    /// Ticker batch channel subscriptions.
    #[serde(default)]
    pub ticker_batch: Vec<String>,
    /// Level 2 channel subscriptions.
    #[serde(default)]
    pub level2: Option<Vec<String>>,
    /// User channel subscriptions.
    #[serde(default)]
    pub user: Option<Vec<String>>,
    /// Market trades channel subscriptions.
    #[serde(default)]
    pub market_trades: Option<Vec<String>>,
    /// Heartbeats channel subscriptions.
    #[serde(default)]
    pub heartbeats: Option<Vec<String>>,
}

/// Futures balance summary event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FuturesBalanceSummaryEvent {
    /// Event type.
    pub r#type: EventType,
    /// Balance summary data.
    pub fcm_balance_summary: FuturesBalanceSummary,
}

/// Futures balance summary data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FuturesBalanceSummary {
    /// Futures buying power.
    #[serde(default)]
    pub futures_buying_power: String,
    /// Total USD balance.
    #[serde(default)]
    pub total_usd_balance: String,
    /// CBI USD balance.
    #[serde(default)]
    pub cbi_usd_balance: String,
    /// CFM USD balance.
    #[serde(default)]
    pub cfm_usd_balance: String,
    /// Total open orders hold amount.
    #[serde(default)]
    pub total_open_orders_hold_amount: String,
    /// Unrealized PnL.
    #[serde(default)]
    pub unrealized_pnl: String,
    /// Daily realized PnL.
    #[serde(default)]
    pub daily_realized_pnl: String,
    /// Initial margin.
    #[serde(default)]
    pub initial_margin: String,
    /// Available margin.
    #[serde(default)]
    pub available_margin: String,
    /// Liquidation threshold.
    #[serde(default)]
    pub liquidation_threshold: String,
    /// Liquidation buffer amount.
    #[serde(default)]
    pub liquidation_buffer_amount: String,
    /// Liquidation buffer percentage.
    #[serde(default)]
    pub liquidation_buffer_percentage: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_heartbeat_deserialize() {
        let data = r#"
            {
                "channel":"heartbeats",
                "client_id":"",
                "timestamp":"2025-01-14T22:11:18.791273556Z",
                "sequence_num":17,
                "events":
                [
                    {
                        "current_time":"2025-01-14 22:11:18.787177997 +0000 UTC m=+25541.571430466",
                        "heartbeat_counter":25539
                    }
                ]
            }
        "#;

        let msg: Result<Message, _> = serde_json::from_str(data);
        assert!(msg.is_ok());
    }

    #[test]
    fn test_level2_side_deserialize() {
        // Test normal cases
        assert_eq!(
            serde_json::from_str::<Level2Side>(r#""bid""#).unwrap(),
            Level2Side::Bid
        );
        assert_eq!(
            serde_json::from_str::<Level2Side>(r#""ask""#).unwrap(),
            Level2Side::Ask
        );
        // Test "offer" alias
        assert_eq!(
            serde_json::from_str::<Level2Side>(r#""offer""#).unwrap(),
            Level2Side::Ask
        );
    }
}