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