Skip to main content

bothan_bitfinex/api/msg/
ticker.rs

1//! Types for Bitfinex ticker data interaction.
2//!
3//! This module provides types for deserializing ticker data from the Bitfinex REST API,
4//! including both spot and funding ticker information. The module supports both array
5//! and object-based JSON responses from the Bitfinex API.
6
7pub mod funding;
8pub mod spot;
9
10use serde::{Deserialize, Serialize};
11
12/// Represents ticker data from the Bitfinex API.
13///
14/// The `Ticker` enum can represent different types of ticker data returned by the Bitfinex API,
15/// including spot trading tickers and funding tickers. Each variant corresponds to a specific
16/// type of market data, allowing for flexible handling of various ticker types.
17#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum Ticker {
20    /// Represents funding ticker data for leveraged trading markets.
21    Funding(funding::Ticker),
22    /// Represents spot ticker data for trading markets.
23    Spot(spot::Ticker),
24}
25
26impl Ticker {
27    /// Returns the symbol identifier for the ticker.
28    ///
29    /// This method extracts the symbol from either a funding or spot ticker,
30    /// providing a unified interface for accessing the trading pair identifier.
31    ///
32    /// # Returns
33    ///
34    /// A string slice containing the symbol (e.g., "tBTCUSD", "fUSD").
35    pub fn symbol(&self) -> &str {
36        match self {
37            Ticker::Funding(t) => &t.symbol,
38            Ticker::Spot(t) => &t.symbol,
39        }
40    }
41
42    /// Returns the last price from the ticker data.
43    ///
44    /// This method extracts the last price from either a funding or spot ticker,
45    /// providing a unified interface for accessing the current market price.
46    ///
47    /// # Returns
48    ///
49    /// A `f64` value representing the last traded price.
50    pub fn price(&self) -> f64 {
51        match self {
52            Ticker::Funding(t) => t.last_price,
53            Ticker::Spot(t) => t.last_price,
54        }
55    }
56}
57
58#[cfg(test)]
59mod test {
60    use super::*;
61
62    #[test]
63    fn test_parse_tickers_from_array() {
64        let json = r#"[["tBTCUSD",101530,39.76548266,101540,32.24226311,2680,0.0271063,101550,661.88869229,102760,98740],["fUSD",0.000180427397260274,0.0002,120,35441993.51575242,0.00008219,2,39208.22419296,-0.00005519,-0.5017,0.00005481,406448929.8255126,0.000137,0.000024,null,null,5863426.35928275]]"#;
65        let ticker: Vec<Ticker> = serde_json::from_str(json).unwrap();
66
67        let expected = vec![
68            Ticker::Spot(spot::Ticker {
69                symbol: "tBTCUSD".to_string(),
70                bid: 101530.0,
71                bid_size: 39.76548266,
72                ask: 101540.0,
73                ask_size: 32.24226311,
74                daily_change: 2680.0,
75                daily_change_relative: 0.0271063,
76                last_price: 101550.0,
77                volume: 661.88869229,
78                high: 102760.0,
79                low: 98740.0,
80            }),
81            Ticker::Funding(funding::Ticker {
82                symbol: "fUSD".to_string(),
83                frr: 0.000180427397260274,
84                bid: 0.0002,
85                bid_period: 120,
86                bid_size: 35441993.51575242,
87                ask: 0.00008219,
88                ask_period: 2,
89                ask_size: 39208.22419296,
90                daily_change: -0.00005519,
91                daily_change_relative: -0.5017,
92                last_price: 0.00005481,
93                volume: 406448929.8255126,
94                high: 0.000137,
95                low: 0.000024,
96                frr_amount_available: 5863426.35928275,
97            }),
98        ];
99        assert_eq!(ticker, expected);
100    }
101}