Skip to main content

tycho_simulation/
serde_helpers.rs

1/// serde functions for handling bytes as hex strings, such as `bytes::Bytes`
2pub mod hex_bytes {
3    use serde::{Deserialize, Deserializer, Serializer};
4
5    /// Serialize a byte vec as a hex string with 0x prefix
6    pub fn serialize<S, T>(x: T, s: S) -> Result<S::Ok, S::Error>
7    where
8        S: Serializer,
9        T: AsRef<[u8]>,
10    {
11        s.serialize_str(&format!("0x{encoded}", encoded = hex::encode(x.as_ref())))
12    }
13
14    /// Deserialize a hex string into a byte vec
15    /// Accepts a hex string with optional 0x prefix
16    pub fn deserialize<'de, T, D>(d: D) -> Result<T, D::Error>
17    where
18        D: Deserializer<'de>,
19        T: From<Vec<u8>>,
20    {
21        let value = String::deserialize(d)?;
22        if let Some(value) = value.strip_prefix("0x") {
23            hex::decode(value)
24        } else {
25            hex::decode(&value)
26        }
27        .map(Into::into)
28        .map_err(|e| serde::de::Error::custom(e.to_string()))
29    }
30}
31
32/// serde functions for handling Option of bytes
33pub mod hex_bytes_option {
34    use serde::{Deserialize, Deserializer, Serializer};
35
36    /// Serialize a byte vec as a Some hex string with 0x prefix
37    pub fn serialize<S, T>(x: &Option<T>, s: S) -> Result<S::Ok, S::Error>
38    where
39        S: Serializer,
40        T: AsRef<[u8]>,
41    {
42        if let Some(x) = x {
43            s.serialize_str(&format!("0x{encoded}", encoded = hex::encode(x.as_ref())))
44        } else {
45            s.serialize_none()
46        }
47    }
48
49    /// Deserialize a hex string into a byte vec or None
50    /// Accepts a hex string with optional 0x prefix
51    pub fn deserialize<'de, T, D>(d: D) -> Result<Option<T>, D::Error>
52    where
53        D: Deserializer<'de>,
54        T: From<Vec<u8>>,
55    {
56        let value: Option<String> = Option::deserialize(d)?;
57
58        match value {
59            Some(val) => {
60                let val = if let Some(stripped) = val.strip_prefix("0x") { stripped } else { &val };
61                hex::decode(val)
62                    .map(Into::into)
63                    .map(Some)
64                    .map_err(|e| serde::de::Error::custom(e.to_string()))
65            }
66            None => Ok(None),
67        }
68    }
69}
70
71/// Deserializes a hex string that must be a 20-byte EVM address into its bytes; any letter case
72/// is accepted, any other length rejected.
73#[cfg(feature = "book-feeds")]
74pub(crate) mod evm_address {
75    use alloy::primitives::Address;
76    use serde::{Deserialize, Deserializer};
77    use tycho_common::Bytes;
78
79    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Bytes, D::Error> {
80        Ok(Bytes::from(Address::deserialize(d)?.into_array()))
81    }
82}
83
84/// Serde helpers for `HashMap<String, Box<dyn ProtocolSim>>`.
85///
86/// Some `ProtocolSim` implementations (VM-backed states) return errors from
87/// their `Serialize` impl. This module provides a custom serializer that
88/// gracefully skips those entries instead of failing the entire map.
89pub mod protocol_states {
90    use std::collections::HashMap;
91
92    use serde::{ser::SerializeMap, Deserialize, Deserializer, Serializer};
93    use tracing::{debug, warn};
94    use tycho_common::simulation::protocol_sim::ProtocolSim;
95
96    /// Serializes a map of `ProtocolSim` trait objects, skipping entries
97    /// whose `Serialize` impl returns an error (e.g., VM-backed states).
98    pub fn serialize<S>(
99        states: &HashMap<String, Box<dyn ProtocolSim>>,
100        serializer: S,
101    ) -> Result<S::Ok, S::Error>
102    where
103        S: Serializer,
104    {
105        let mut map = serializer.serialize_map(None)?;
106        let mut skipped = 0u32;
107        for (key, value) in states {
108            match serde_json::to_value(value.as_ref()) {
109                Ok(json_val) => map.serialize_entry(key, &json_val)?,
110                Err(err) => {
111                    debug!(key, %err, "skipping non-serializable ProtocolSim entry");
112                    skipped += 1;
113                }
114            }
115        }
116        if skipped > 0 {
117            warn!(skipped, total = states.len(), "skipped non-serializable ProtocolSim entries");
118        }
119        map.end()
120    }
121
122    /// Deserializes back into the map. Non-serializable states are simply
123    /// absent from the data, so default deserialization works.
124    pub fn deserialize<'de, D>(
125        deserializer: D,
126    ) -> Result<HashMap<String, Box<dyn ProtocolSim>>, D::Error>
127    where
128        D: Deserializer<'de>,
129    {
130        HashMap::<String, Box<dyn ProtocolSim>>::deserialize(deserializer)
131    }
132}
133
134/// Macro to implement error-returning Serialize/Deserialize for protocols
135/// that cannot be serialized (e.g., due to VM state or external SDK dependencies).
136///
137/// # Examples
138/// ```ignore
139/// impl_non_serializable_protocol!(MyProtocolState, "error message");
140/// ```
141#[macro_export]
142macro_rules! impl_non_serializable_protocol {
143    ($type:ty, $msg:expr) => {
144        impl serde::Serialize for $type {
145            fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
146            where
147                S: serde::Serializer,
148            {
149                Err(serde::ser::Error::custom($msg))
150            }
151        }
152
153        impl<'de> serde::Deserialize<'de> for $type {
154            fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
155            where
156                D: serde::Deserializer<'de>,
157            {
158                Err(serde::de::Error::custom($msg))
159            }
160        }
161    };
162}
163
164#[cfg(test)]
165mod tests {
166    use std::{collections::HashMap, str::FromStr};
167
168    use serde::{Deserialize, Serialize};
169    use serde_json;
170    use tycho_common::{simulation::protocol_sim::ProtocolSim, Bytes};
171
172    use super::*;
173    use crate::protocol::models::Update;
174
175    #[derive(Debug, Serialize, Deserialize)]
176    struct TestStruct {
177        #[serde(with = "hex_bytes")]
178        bytes: Vec<u8>,
179
180        #[serde(with = "hex_bytes_option")]
181        bytes_option: Option<Vec<u8>>,
182    }
183
184    #[test]
185    fn hex_bytes_serialize_deserialize() {
186        let test_struct = TestStruct { bytes: vec![0u8; 10], bytes_option: Some(vec![0u8; 10]) };
187
188        // Serialize to JSON
189        let serialized = serde_json::to_string(&test_struct).unwrap();
190        assert_eq!(
191            serialized,
192            "{\"bytes\":\"0x00000000000000000000\",\"bytes_option\":\"0x00000000000000000000\"}"
193        );
194
195        // Deserialize from JSON
196        let deserialized: TestStruct = serde_json::from_str(&serialized).unwrap();
197        assert_eq!(deserialized.bytes, vec![0u8; 10]);
198        assert_eq!(deserialized.bytes_option, Some(vec![0u8; 10]));
199    }
200
201    #[test]
202    fn evm_address_accepts_any_case_and_rejects_other_lengths() {
203        #[derive(Deserialize)]
204        struct Holder(#[serde(deserialize_with = "evm_address::deserialize")] Bytes);
205
206        let lower = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
207        let mixed = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
208        let from_lower: Holder = serde_json::from_str(&format!("\"{lower}\"")).unwrap();
209        let from_mixed: Holder = serde_json::from_str(&format!("\"{mixed}\"")).unwrap();
210        assert_eq!(from_lower.0, from_mixed.0);
211        assert_eq!(from_lower.0, Bytes::from_str(lower).unwrap());
212
213        for not_an_address in ["0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb", "0x", "a0b8"] {
214            assert!(serde_json::from_str::<Holder>(&format!("\"{not_an_address}\"")).is_err());
215        }
216    }
217
218    #[test]
219    fn hex_bytes_option_none() {
220        let test_struct = TestStruct { bytes: vec![0u8; 10], bytes_option: None };
221
222        // Serialize to JSON
223        let serialized = serde_json::to_string(&test_struct).unwrap();
224        assert_eq!(serialized, "{\"bytes\":\"0x00000000000000000000\",\"bytes_option\":null}");
225
226        // Deserialize from JSON
227        let deserialized: TestStruct = serde_json::from_str(&serialized).unwrap();
228        assert_eq!(deserialized.bytes, vec![0u8; 10]);
229        assert_eq!(deserialized.bytes_option, None);
230    }
231
232    #[cfg(feature = "evm")]
233    #[test]
234    fn update_roundtrip_with_serializable_state() {
235        use alloy::primitives::U256;
236
237        use crate::evm::protocol::uniswap_v2::state::UniswapV2State;
238
239        let mut states: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
240        states.insert(
241            "pool_a".to_string(),
242            Box::new(UniswapV2State::new(U256::from(1000), U256::from(2000))),
243        );
244
245        let update = Update::new(12345, states, HashMap::new());
246        let json = serde_json::to_string(&update).unwrap();
247
248        assert!(json.contains("pool_a"));
249
250        let roundtripped: Update = serde_json::from_str(&json).unwrap();
251        assert_eq!(roundtripped.block_number_or_timestamp, 12345);
252        assert_eq!(roundtripped.states.len(), 1);
253        assert!(roundtripped
254            .states
255            .contains_key("pool_a"));
256    }
257
258    #[cfg(feature = "evm")]
259    #[test]
260    fn protocol_states_skips_non_serializable_entries() {
261        use alloy::primitives::U256;
262
263        use crate::evm::{decoder::MockProtocolSim, protocol::uniswap_v2::state::UniswapV2State};
264
265        let mut states: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
266        states.insert(
267            "serializable".to_string(),
268            Box::new(UniswapV2State::new(U256::from(1000), U256::from(2000))),
269        );
270        states.insert("non_serializable".to_string(), Box::new(MockProtocolSim::new()));
271
272        let update = Update::new(42, states, HashMap::new());
273        let json = serde_json::to_string(&update).expect("serialization should succeed");
274        let parsed: serde_json::Value = serde_json::from_str(&json).expect("valid JSON");
275
276        let states_map = parsed["states"]
277            .as_object()
278            .expect("states is a map");
279        assert_eq!(states_map.len(), 1, "only the serializable entry should survive");
280        assert!(states_map.contains_key("serializable"));
281        assert!(!states_map.contains_key("non_serializable"));
282    }
283
284    #[cfg(feature = "evm")]
285    #[test]
286    fn protocol_states_serialize_produces_valid_json() {
287        use alloy::primitives::U256;
288
289        use crate::evm::protocol::uniswap_v2::state::UniswapV2State;
290
291        let mut states: HashMap<String, Box<dyn ProtocolSim>> = HashMap::new();
292        states.insert(
293            "pool_x".to_string(),
294            Box::new(UniswapV2State::new(U256::from(100), U256::from(200))),
295        );
296        states.insert(
297            "pool_y".to_string(),
298            Box::new(UniswapV2State::new(U256::from(300), U256::from(400))),
299        );
300
301        #[derive(Serialize)]
302        struct Wrapper {
303            #[serde(with = "protocol_states")]
304            states: HashMap<String, Box<dyn ProtocolSim>>,
305        }
306
307        let wrapper = Wrapper { states };
308        let json = serde_json::to_value(&wrapper).unwrap();
309        let map = json["states"].as_object().unwrap();
310        assert_eq!(map.len(), 2);
311        assert!(map.contains_key("pool_x"));
312        assert!(map.contains_key("pool_y"));
313    }
314
315    #[test]
316    fn update_roundtrip_empty() {
317        let update = Update::new(99999, HashMap::new(), HashMap::new());
318        let json = serde_json::to_string(&update).unwrap();
319        let roundtripped: Update = serde_json::from_str(&json).unwrap();
320        assert_eq!(roundtripped.block_number_or_timestamp, 99999);
321        assert!(roundtripped.states.is_empty());
322        assert!(roundtripped.new_pairs.is_empty());
323    }
324}