pragma_common/entries/
open_interest.rs

1#[cfg(feature = "proto")]
2use prost::Message;
3
4use crate::Pair;
5#[cfg(feature = "proto")]
6use crate::{ProtoDeserialize, ProtoSerialize};
7
8#[derive(Debug, Clone, PartialEq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(
11    feature = "borsh",
12    derive(borsh::BorshSerialize, borsh::BorshDeserialize)
13)]
14#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
15pub struct OpenInterestEntry {
16    pub source: String,
17    pub pair: Pair,
18    pub open_interest: f64,
19    pub timestamp_ms: i64,
20}
21
22#[cfg(feature = "proto")]
23impl OpenInterestEntry {
24    fn to_proto(&self) -> crate::schema::OpenInterestEntry {
25        crate::schema::OpenInterestEntry {
26            source: self.source.clone(),
27            pair: Some(crate::schema::Pair {
28                base: self.pair.base.clone(),
29                quote: self.pair.quote.clone(),
30            }),
31            open_interest: self.open_interest,
32            timestamp_ms: self.timestamp_ms,
33        }
34    }
35
36    fn from_proto(proto: crate::schema::OpenInterestEntry) -> Result<Self, prost::DecodeError> {
37        let pair = proto
38            .pair
39            .ok_or_else(|| prost::DecodeError::new("Missing pair field in OpenInterestEntry"))?;
40
41        Ok(OpenInterestEntry {
42            source: proto.source,
43            pair: Pair {
44                base: pair.base,
45                quote: pair.quote,
46            },
47            open_interest: proto.open_interest,
48            timestamp_ms: proto.timestamp_ms,
49        })
50    }
51}
52
53#[cfg(feature = "proto")]
54impl ProtoSerialize for OpenInterestEntry {
55    fn to_proto_bytes(&self) -> Vec<u8> {
56        let proto = self.to_proto();
57        let mut buf = Vec::new();
58        proto
59            .encode(&mut buf)
60            .expect("Failed to encode OpenInterestEntry to protobuf");
61        buf
62    }
63}
64
65#[cfg(feature = "proto")]
66impl ProtoDeserialize for OpenInterestEntry {
67    fn from_proto_bytes(bytes: &[u8]) -> Result<Self, prost::DecodeError> {
68        let proto = crate::schema::OpenInterestEntry::decode(bytes)?;
69        Self::from_proto(proto)
70    }
71}