pragma_common/entries/
depth.rs

1#[cfg(feature = "capnp")]
2use capnp::serialize;
3
4#[cfg(feature = "capnp")]
5use crate::schema_capnp::{self};
6use crate::{instrument_type::InstrumentType, web3::Chain, Pair};
7
8#[derive(Debug, Clone, PartialEq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct DepthEntry {
11    pub source: String,
12    pub chain: Option<Chain>,
13    pub instrument_type: InstrumentType,
14    pub pair: Pair,
15    pub depth: DepthLevel,
16}
17
18#[derive(Debug, Clone, PartialEq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20pub struct DepthLevel {
21    pub percentage: f64,
22    pub bid: f64,
23    pub ask: f64,
24}
25
26#[cfg(feature = "capnp")]
27impl crate::CapnpSerialize for DepthEntry {
28    fn to_capnp(&self) -> Vec<u8> {
29        let mut message = capnp::message::Builder::new_default();
30        let mut builder = message.init_root::<schema_capnp::depth_entry::Builder>();
31
32        builder.set_source(&self.source);
33        builder.set_instrument_type(match self.instrument_type {
34            InstrumentType::Spot => schema_capnp::InstrumentType::Spot,
35            InstrumentType::Perp => schema_capnp::InstrumentType::Perp,
36        });
37
38        let mut pair = builder.reborrow().init_pair();
39        pair.set_base(&self.pair.base);
40        pair.set_quote(&self.pair.quote);
41
42        let mut depth_level = builder.reborrow().init_depth();
43        depth_level.set_percentage(self.depth.percentage);
44        depth_level.set_bid(self.depth.bid);
45        depth_level.set_ask(self.depth.ask);
46
47        // Set the chain union
48        let mut chain = builder.reborrow().init_chain();
49        match &self.chain {
50            Some(serialized_chain) => {
51                chain.set_chain(match serialized_chain {
52                    Chain::Starknet => schema_capnp::Chain::Starknet,
53                    Chain::Solana => schema_capnp::Chain::Solana,
54                    Chain::Sui => schema_capnp::Chain::Sui,
55                    Chain::Aptos => schema_capnp::Chain::Aptos,
56                    Chain::Ethereum => schema_capnp::Chain::Ethereum,
57                    Chain::Base => schema_capnp::Chain::Base,
58                    Chain::Arbitrum => schema_capnp::Chain::Arbitrum,
59                    Chain::Optimism => schema_capnp::Chain::Optimism,
60                    Chain::ZkSync => schema_capnp::Chain::Zksync,
61                    Chain::Polygon => schema_capnp::Chain::Polygon,
62                    Chain::Bnb => schema_capnp::Chain::Bnb,
63                    Chain::Avalanche => schema_capnp::Chain::Avalanche,
64                    Chain::Gnosis => schema_capnp::Chain::Gnosis,
65                    Chain::Worldchain => schema_capnp::Chain::Worldchain,
66                });
67            }
68            None => {
69                chain.set_no_chain(());
70            }
71        };
72
73        let mut buffer = Vec::new();
74        serialize::write_message(&mut buffer, &message).unwrap();
75        buffer
76    }
77}
78
79#[cfg(feature = "capnp")]
80impl crate::CapnpDeserialize for DepthEntry {
81    fn from_capnp(bytes: &[u8]) -> Result<Self, capnp::Error>
82    where
83        Self: Sized,
84    {
85        let message_reader = serialize::read_message(bytes, capnp::message::ReaderOptions::new())?;
86        let reader = message_reader.get_root::<schema_capnp::depth_entry::Reader>()?;
87
88        let source = reader.get_source()?.to_string()?;
89        let instrument_type = match reader.get_instrument_type()? {
90            schema_capnp::InstrumentType::Spot => InstrumentType::Spot,
91            schema_capnp::InstrumentType::Perp => InstrumentType::Perp,
92        };
93
94        let pair_reader = reader.get_pair()?;
95        let pair = Pair {
96            base: pair_reader.get_base()?.to_string()?,
97            quote: pair_reader.get_quote()?.to_string()?,
98        };
99
100        let depth_reader = reader.get_depth()?;
101        let depth = DepthLevel {
102            percentage: depth_reader.get_percentage(),
103            bid: depth_reader.get_bid(),
104            ask: depth_reader.get_ask(),
105        };
106
107        // Extract chain from the union
108        let chain = match reader.get_chain().which()? {
109            schema_capnp::depth_entry::chain::NoChain(()) => None,
110            schema_capnp::depth_entry::chain::Chain(chain_reader) => Some(match chain_reader? {
111                schema_capnp::Chain::Starknet => Chain::Starknet,
112                schema_capnp::Chain::Solana => Chain::Solana,
113                schema_capnp::Chain::Sui => Chain::Sui,
114                schema_capnp::Chain::Aptos => Chain::Aptos,
115                schema_capnp::Chain::Ethereum => Chain::Ethereum,
116                schema_capnp::Chain::Base => Chain::Base,
117                schema_capnp::Chain::Arbitrum => Chain::Arbitrum,
118                schema_capnp::Chain::Optimism => Chain::Optimism,
119                schema_capnp::Chain::Zksync => Chain::ZkSync,
120                schema_capnp::Chain::Polygon => Chain::Polygon,
121                schema_capnp::Chain::Bnb => Chain::Bnb,
122                schema_capnp::Chain::Avalanche => Chain::Avalanche,
123                schema_capnp::Chain::Gnosis => Chain::Gnosis,
124                schema_capnp::Chain::Worldchain => Chain::Worldchain,
125            }),
126        };
127
128        Ok(DepthEntry {
129            source,
130            instrument_type,
131            pair,
132            depth,
133            chain,
134        })
135    }
136}