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