Skip to main content

deribit_http/model/
index.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 15/9/25
5******************************************************************************/
6use pretty_simple_display::{DebugPretty, DisplaySimple};
7use serde::{Deserialize, Serialize};
8use serde_with::skip_serializing_none;
9
10/// Index data
11#[skip_serializing_none]
12#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
13pub struct IndexData {
14    /// BTC component (optional)
15    pub btc: Option<f64>,
16    /// ETH component (optional)
17    pub eth: Option<f64>,
18    /// USDC component (optional)
19    pub usdc: Option<f64>,
20    /// USDT component (optional)
21    pub usdt: Option<f64>,
22    /// EURR component (optional)
23    pub eurr: Option<f64>,
24    /// EDP (Estimated Delivery Price)
25    pub edp: f64,
26}
27
28/// Index price data
29#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
30pub struct IndexPriceData {
31    /// Current index price
32    pub index_price: f64,
33    /// Estimated delivery price
34    pub estimated_delivery_price: f64,
35}
36
37/// Index chart data point representing a single price observation.
38///
39/// The Deribit API returns chart data as arrays of `[timestamp, price]` tuples.
40/// This struct provides a typed representation with proper field names.
41#[derive(Debug, Clone, Copy, PartialEq)]
42pub struct IndexChartDataPoint {
43    /// Timestamp in milliseconds since Unix epoch
44    pub timestamp: u64,
45    /// Average index price at that timestamp
46    pub price: f64,
47}
48
49impl IndexChartDataPoint {
50    /// Creates a new index chart data point.
51    ///
52    /// # Arguments
53    ///
54    /// * `timestamp` - Timestamp in milliseconds since Unix epoch
55    /// * `price` - Average index price at that timestamp
56    #[must_use]
57    pub fn new(timestamp: u64, price: f64) -> Self {
58        Self { timestamp, price }
59    }
60}
61
62impl Serialize for IndexChartDataPoint {
63    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
64    where
65        S: serde::Serializer,
66    {
67        // Serialize as [timestamp, price] tuple to match API format
68        (self.timestamp, self.price).serialize(serializer)
69    }
70}
71
72impl<'de> Deserialize<'de> for IndexChartDataPoint {
73    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
74    where
75        D: serde::Deserializer<'de>,
76    {
77        // Deserialize from [timestamp, price] tuple
78        let (timestamp_f64, price): (f64, f64) = Deserialize::deserialize(deserializer)?;
79        // Convert timestamp from f64 to u64 (API returns it as a number)
80        let timestamp = timestamp_f64 as u64;
81        Ok(Self { timestamp, price })
82    }
83}