Skip to main content

tycho_common/
dto.rs

1//! Data Transfer Objects (or structs)
2//!
3//! These structs serve to serialise and deserialize messages between server and client, they should
4//! be very simple and ideally not contain any business logic.
5//!
6//! Structs in here implement utoipa traits so they can be used to derive an OpenAPI schema.
7#![expect(
8    deprecated,
9    reason = "DTOs that are deprecated for consumers are still defined and used within this module"
10)]
11use std::{
12    collections::{BTreeMap, HashMap, HashSet},
13    fmt,
14    hash::{Hash, Hasher},
15    str::FromStr,
16};
17
18use arrayvec::ArrayString;
19use chrono::{NaiveDateTime, Utc};
20use deepsize::{Context, DeepSizeOf};
21use serde::{de, Deserialize, Deserializer, Serialize};
22use strum_macros::{Display, EnumString};
23use thiserror::Error;
24use utoipa::{IntoParams, ToSchema};
25use uuid::Uuid;
26
27use crate::{
28    models::{
29        self, chain_config::ChainConfigError, Address, Balance, Code, ComponentId, StoreKey,
30        StoreVal,
31    },
32    serde_primitives::{
33        hex_bytes, hex_bytes_option, hex_hashmap_key, hex_hashmap_key_value, hex_hashmap_value,
34    },
35    Bytes,
36};
37
38/// Currently supported Blockchains
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default, ToSchema)]
40#[serde(rename_all = "lowercase")]
41pub enum Chain {
42    #[default]
43    Ethereum,
44    Starknet,
45    ZkSync,
46    Arbitrum,
47    Base,
48    Bsc,
49    Unichain,
50    Polygon,
51    Plasma,
52    Robinhood,
53    Arc,
54    Elysium,
55    #[schema(value_type = String)]
56    Custom(ArrayString<32>),
57}
58
59impl DeepSizeOf for Chain {
60    fn deep_size_of_children(&self, _context: &mut Context) -> usize {
61        0
62    }
63}
64
65pub use models::chain_config::TvlThresholdTier;
66
67impl Chain {
68    /// Returns a default TVL threshold in native token units for the given tier.
69    pub fn default_tvl_threshold(&self, tier: TvlThresholdTier) -> f64 {
70        models::Chain::from(*self).default_tvl_threshold(tier)
71    }
72}
73
74impl fmt::Display for Chain {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        models::Chain::from(*self).fmt(f)
77    }
78}
79
80impl FromStr for Chain {
81    type Err = ChainConfigError;
82
83    fn from_str(s: &str) -> Result<Self, Self::Err> {
84        models::Chain::from_str(s).map(Self::from)
85    }
86}
87
88impl From<models::contract::Account> for ResponseAccount {
89    fn from(value: models::contract::Account) -> Self {
90        ResponseAccount::new(
91            value.chain.into(),
92            value.address,
93            value.title,
94            value.slots,
95            value.native_balance,
96            value
97                .token_balances
98                .into_iter()
99                .map(|(k, v)| (k, v.balance))
100                .collect(),
101            value.code,
102            value.code_hash,
103            value.balance_modify_tx,
104            value.code_modify_tx,
105            value.creation_tx,
106        )
107    }
108}
109
110impl From<models::Chain> for Chain {
111    fn from(value: models::Chain) -> Self {
112        match value {
113            models::Chain::Ethereum => Chain::Ethereum,
114            models::Chain::Starknet => Chain::Starknet,
115            models::Chain::ZkSync => Chain::ZkSync,
116            models::Chain::Arbitrum => Chain::Arbitrum,
117            models::Chain::Base => Chain::Base,
118            models::Chain::Bsc => Chain::Bsc,
119            models::Chain::Unichain => Chain::Unichain,
120            models::Chain::Polygon => Chain::Polygon,
121            models::Chain::Plasma => Chain::Plasma,
122            models::Chain::Robinhood => Chain::Robinhood,
123            models::Chain::Arc => Chain::Arc,
124            models::Chain::Elysium => Chain::Elysium,
125            models::Chain::Custom(id) => Chain::Custom(
126                ArrayString::from(id.as_str())
127                    .expect("custom chain name is already within 32 bytes"),
128            ),
129        }
130    }
131}
132
133#[derive(
134    Debug,
135    PartialEq,
136    Default,
137    Copy,
138    Clone,
139    Deserialize,
140    Serialize,
141    ToSchema,
142    EnumString,
143    Display,
144    DeepSizeOf,
145)]
146pub enum ChangeType {
147    #[default]
148    Update,
149    Deletion,
150    Creation,
151    Unspecified,
152}
153
154impl From<models::ChangeType> for ChangeType {
155    fn from(value: models::ChangeType) -> Self {
156        match value {
157            models::ChangeType::Update => ChangeType::Update,
158            models::ChangeType::Creation => ChangeType::Creation,
159            models::ChangeType::Deletion => ChangeType::Deletion,
160        }
161    }
162}
163
164impl ChangeType {
165    pub fn merge(&self, other: &Self) -> Self {
166        if matches!(self, Self::Creation) {
167            Self::Creation
168        } else {
169            *other
170        }
171    }
172}
173
174#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, Default)]
175pub struct ExtractorIdentity {
176    pub chain: Chain,
177    pub name: String,
178}
179
180impl ExtractorIdentity {
181    pub fn new(chain: Chain, name: &str) -> Self {
182        Self { chain, name: name.to_owned() }
183    }
184}
185
186impl fmt::Display for ExtractorIdentity {
187    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188        write!(f, "{}:{}", self.chain, self.name)
189    }
190}
191
192/// A command sent from the client to the server
193#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
194#[serde(tag = "method", rename_all = "lowercase")]
195pub enum Command {
196    Subscribe {
197        extractor_id: ExtractorIdentity,
198        include_state: bool,
199        /// Enable zstd compression for messages in this subscription.
200        /// Defaults to false for backward compatibility.
201        #[serde(default)]
202        compression: bool,
203        /// Enables receiving partial block update messages in this subscription.
204        /// Defaults to false for backward compatibility.
205        #[serde(default)]
206        partial_blocks: bool,
207    },
208    Unsubscribe {
209        subscription_id: Uuid,
210    },
211}
212
213/// A easy serializable version of `models::error::WebsocketError`
214///
215/// This serves purely to transfer errors via websocket. It is meant to render
216/// similarly to the original struct but does not have server side debug information
217/// attached.
218///
219/// It should contain information needed to handle errors correctly on the client side.
220#[derive(Error, Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
221pub enum WebsocketError {
222    #[error("Extractor not found: {0}")]
223    ExtractorNotFound(ExtractorIdentity),
224
225    #[error("Subscription not found: {0}")]
226    SubscriptionNotFound(Uuid),
227
228    #[error("Failed to parse JSON: {1}, msg: {0}")]
229    ParseError(String, String),
230
231    #[error("Failed to subscribe to extractor: {0}")]
232    SubscribeError(ExtractorIdentity),
233
234    #[error("Failed to compress message for subscription: {0}, error: {1}")]
235    CompressionError(Uuid, String),
236}
237
238impl From<crate::models::error::WebsocketError> for WebsocketError {
239    fn from(value: crate::models::error::WebsocketError) -> Self {
240        match value {
241            crate::models::error::WebsocketError::ExtractorNotFound(eid) => {
242                Self::ExtractorNotFound(eid.into())
243            }
244            crate::models::error::WebsocketError::SubscriptionNotFound(sid) => {
245                Self::SubscriptionNotFound(sid)
246            }
247            crate::models::error::WebsocketError::ParseError(raw, error) => {
248                Self::ParseError(error.to_string(), raw)
249            }
250            crate::models::error::WebsocketError::SubscribeError(eid) => {
251                Self::SubscribeError(eid.into())
252            }
253            crate::models::error::WebsocketError::CompressionError(sid, error) => {
254                Self::CompressionError(sid, error.to_string())
255            }
256        }
257    }
258}
259
260/// A response sent from the server to the client
261#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
262#[serde(tag = "method", rename_all = "lowercase")]
263pub enum Response {
264    NewSubscription { extractor_id: ExtractorIdentity, subscription_id: Uuid },
265    SubscriptionEnded { subscription_id: Uuid },
266    Error(WebsocketError),
267}
268
269/// A message sent from the server to the client
270#[expect(
271    clippy::large_enum_variant,
272    reason = "public API; boxing the large variant is a breaking change"
273)]
274#[derive(Serialize, Deserialize, Debug, Display, Clone)]
275#[serde(untagged)]
276pub enum WebSocketMessage {
277    BlockAggregatedChanges { subscription_id: Uuid, deltas: BlockAggregatedChanges },
278    Response(Response),
279}
280
281#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default, ToSchema)]
282pub struct Block {
283    pub number: u64,
284    #[serde(with = "hex_bytes")]
285    pub hash: Bytes,
286    #[serde(with = "hex_bytes")]
287    pub parent_hash: Bytes,
288    pub chain: Chain,
289    pub ts: NaiveDateTime,
290}
291
292#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema, Eq, Hash, DeepSizeOf)]
293#[serde(deny_unknown_fields)]
294pub struct BlockParam {
295    #[schema(value_type=Option<String>)]
296    #[serde(with = "hex_bytes_option", default)]
297    pub hash: Option<Bytes>,
298    #[deprecated(
299        note = "The `chain` field is deprecated and will be removed in a future version."
300    )]
301    #[serde(default)]
302    pub chain: Option<Chain>,
303    #[serde(default)]
304    pub number: Option<i64>,
305}
306
307impl From<&Block> for BlockParam {
308    fn from(value: &Block) -> Self {
309        // The hash should uniquely identify a block across chains
310        BlockParam { hash: Some(value.hash.clone()), chain: None, number: None }
311    }
312}
313
314#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
315pub struct TokenBalances(#[serde(with = "hex_hashmap_key")] pub HashMap<Bytes, ComponentBalance>);
316
317impl From<HashMap<Bytes, ComponentBalance>> for TokenBalances {
318    fn from(value: HashMap<Bytes, ComponentBalance>) -> Self {
319        TokenBalances(value)
320    }
321}
322
323#[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
324pub struct Transaction {
325    #[serde(with = "hex_bytes")]
326    pub hash: Bytes,
327    #[serde(with = "hex_bytes")]
328    pub block_hash: Bytes,
329    #[serde(with = "hex_bytes")]
330    pub from: Bytes,
331    #[serde(with = "hex_bytes_option")]
332    pub to: Option<Bytes>,
333    pub index: u64,
334}
335
336impl Transaction {
337    pub fn new(hash: Bytes, block_hash: Bytes, from: Bytes, to: Option<Bytes>, index: u64) -> Self {
338        Self { hash, block_hash, from, to, index }
339    }
340}
341
342/// A container for updates grouped by account/component.
343#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
344pub struct BlockAggregatedChanges {
345    pub extractor: String,
346    pub chain: Chain,
347    pub block: Block,
348    pub finalized_block_height: u64,
349    pub revert: bool,
350    #[serde(with = "hex_hashmap_key", default)]
351    pub new_tokens: HashMap<Bytes, ResponseToken>,
352    #[serde(alias = "account_deltas", with = "hex_hashmap_key")]
353    pub account_updates: HashMap<Bytes, AccountUpdate>,
354    #[serde(alias = "state_deltas")]
355    pub state_updates: HashMap<String, ProtocolStateDelta>,
356    pub new_protocol_components: HashMap<String, ProtocolComponent>,
357    pub deleted_protocol_components: HashMap<String, ProtocolComponent>,
358    pub component_balances: HashMap<String, TokenBalances>,
359    pub account_balances: HashMap<Bytes, HashMap<Bytes, AccountBalance>>,
360    pub component_tvl: HashMap<String, f64>,
361    pub dci_update: DCIUpdate,
362    /// The index of the partial block. None if it's a full block.
363    #[serde(default, skip_serializing_if = "Option::is_none")]
364    pub partial_block_index: Option<u32>,
365    /// Components paused in this block. Clients should stop tracking them.
366    #[serde(default)]
367    pub paused_components: HashSet<ComponentId>,
368    /// Components unpaused in this block. Clients may track them again.
369    #[serde(default)]
370    pub unpaused_components: HashSet<ComponentId>,
371}
372
373impl BlockAggregatedChanges {
374    #[expect(
375        clippy::too_many_arguments,
376        reason = "public API; grouping the arguments into a params struct or builder is a breaking change"
377    )]
378    pub fn new(
379        extractor: &str,
380        chain: Chain,
381        block: Block,
382        finalized_block_height: u64,
383        revert: bool,
384        account_updates: HashMap<Bytes, AccountUpdate>,
385        state_updates: HashMap<String, ProtocolStateDelta>,
386        new_protocol_components: HashMap<String, ProtocolComponent>,
387        deleted_protocol_components: HashMap<String, ProtocolComponent>,
388        component_balances: HashMap<String, HashMap<Bytes, ComponentBalance>>,
389        account_balances: HashMap<Bytes, HashMap<Bytes, AccountBalance>>,
390        dci_update: DCIUpdate,
391    ) -> Self {
392        BlockAggregatedChanges {
393            extractor: extractor.to_owned(),
394            chain,
395            block,
396            finalized_block_height,
397            revert,
398            new_tokens: HashMap::new(),
399            account_updates,
400            state_updates,
401            new_protocol_components,
402            deleted_protocol_components,
403            component_balances: component_balances
404                .into_iter()
405                .map(|(k, v)| (k, v.into()))
406                .collect(),
407            account_balances,
408            component_tvl: HashMap::new(),
409            dci_update,
410            partial_block_index: None,
411            paused_components: HashSet::new(),
412            unpaused_components: HashSet::new(),
413        }
414    }
415
416    pub fn merge(mut self, other: Self) -> Self {
417        other
418            .account_updates
419            .into_iter()
420            .for_each(|(k, v)| {
421                self.account_updates
422                    .entry(k)
423                    .and_modify(|e| {
424                        e.merge(&v);
425                    })
426                    .or_insert(v);
427            });
428
429        other
430            .state_updates
431            .into_iter()
432            .for_each(|(k, v)| {
433                self.state_updates
434                    .entry(k)
435                    .and_modify(|e| {
436                        e.merge(&v);
437                    })
438                    .or_insert(v);
439            });
440
441        other
442            .component_balances
443            .into_iter()
444            .for_each(|(k, v)| {
445                self.component_balances
446                    .entry(k)
447                    .and_modify(|e| e.0.extend(v.0.clone()))
448                    .or_insert_with(|| v);
449            });
450
451        other
452            .account_balances
453            .into_iter()
454            .for_each(|(k, v)| {
455                self.account_balances
456                    .entry(k)
457                    .and_modify(|e| e.extend(v.clone()))
458                    .or_insert(v);
459            });
460
461        self.component_tvl
462            .extend(other.component_tvl);
463        self.new_protocol_components
464            .extend(other.new_protocol_components);
465        self.deleted_protocol_components
466            .extend(other.deleted_protocol_components);
467        // A later pause or unpause of the same component overrides the earlier one.
468        for id in other.paused_components {
469            self.unpaused_components.remove(&id);
470            self.paused_components.insert(id);
471        }
472        for id in other.unpaused_components {
473            self.paused_components.remove(&id);
474            self.unpaused_components.insert(id);
475        }
476        self.revert = other.revert;
477        self.block = other.block;
478
479        self
480    }
481
482    pub fn get_block(&self) -> &Block {
483        &self.block
484    }
485
486    pub fn is_revert(&self) -> bool {
487        self.revert
488    }
489
490    pub fn filter_by_component<F: Fn(&str) -> bool>(&mut self, keep: F) {
491        self.state_updates
492            .retain(|k, _| keep(k));
493        self.component_balances
494            .retain(|k, _| keep(k));
495        self.component_tvl
496            .retain(|k, _| keep(k));
497    }
498
499    pub fn filter_by_contract<F: Fn(&Bytes) -> bool>(&mut self, keep: F) {
500        self.account_updates
501            .retain(|k, _| keep(k));
502        self.account_balances
503            .retain(|k, _| keep(k));
504    }
505
506    pub fn n_changes(&self) -> usize {
507        self.account_updates.len() + self.state_updates.len()
508    }
509
510    pub fn drop_state(&self) -> Self {
511        Self {
512            extractor: self.extractor.clone(),
513            chain: self.chain,
514            block: self.block.clone(),
515            finalized_block_height: self.finalized_block_height,
516            revert: self.revert,
517            new_tokens: self.new_tokens.clone(),
518            account_updates: HashMap::new(),
519            state_updates: HashMap::new(),
520            new_protocol_components: self.new_protocol_components.clone(),
521            deleted_protocol_components: self.deleted_protocol_components.clone(),
522            component_balances: self.component_balances.clone(),
523            account_balances: self.account_balances.clone(),
524            component_tvl: self.component_tvl.clone(),
525            dci_update: self.dci_update.clone(),
526            partial_block_index: self.partial_block_index,
527            paused_components: self.paused_components.clone(),
528            unpaused_components: self.unpaused_components.clone(),
529        }
530    }
531
532    pub fn is_partial_block(&self) -> bool {
533        self.partial_block_index.is_some()
534    }
535}
536
537impl From<models::blockchain::Block> for Block {
538    fn from(value: models::blockchain::Block) -> Self {
539        Self {
540            number: value.number,
541            hash: value.hash,
542            parent_hash: value.parent_hash,
543            chain: value.chain.into(),
544            ts: value.ts,
545        }
546    }
547}
548
549impl From<models::protocol::ComponentBalance> for ComponentBalance {
550    fn from(value: models::protocol::ComponentBalance) -> Self {
551        Self {
552            token: value.token,
553            balance: value.balance,
554            balance_float: value.balance_float,
555            modify_tx: value.modify_tx,
556            component_id: value.component_id,
557        }
558    }
559}
560
561impl From<models::contract::AccountBalance> for AccountBalance {
562    fn from(value: models::contract::AccountBalance) -> Self {
563        Self {
564            account: value.account,
565            token: value.token,
566            balance: value.balance,
567            modify_tx: value.modify_tx,
568        }
569    }
570}
571
572impl From<models::blockchain::BlockAggregatedChanges> for BlockAggregatedChanges {
573    fn from(value: models::blockchain::BlockAggregatedChanges) -> Self {
574        Self {
575            extractor: value.extractor,
576            chain: value.chain.into(),
577            block: value.block.into(),
578            finalized_block_height: value.finalized_block_height,
579            revert: value.revert,
580            account_updates: value
581                .account_deltas
582                .into_iter()
583                .map(|(k, v)| (k, v.into()))
584                .collect(),
585            state_updates: value
586                .state_deltas
587                .into_iter()
588                .map(|(k, v)| (k, v.into()))
589                .collect(),
590            new_protocol_components: value
591                .new_protocol_components
592                .into_iter()
593                .map(|(k, v)| (k, v.into()))
594                .collect(),
595            deleted_protocol_components: value
596                .deleted_protocol_components
597                .into_iter()
598                .map(|(k, v)| (k, v.into()))
599                .collect(),
600            component_balances: value
601                .component_balances
602                .into_iter()
603                .map(|(component_id, v)| {
604                    let balances: HashMap<Bytes, ComponentBalance> = v
605                        .into_iter()
606                        .map(|(k, v)| (k, ComponentBalance::from(v)))
607                        .collect();
608                    (component_id, balances.into())
609                })
610                .collect(),
611            account_balances: value
612                .account_balances
613                .into_iter()
614                .map(|(k, v)| {
615                    (
616                        k,
617                        v.into_iter()
618                            .map(|(k, v)| (k, v.into()))
619                            .collect(),
620                    )
621                })
622                .collect(),
623            dci_update: value.dci_update.into(),
624            new_tokens: value
625                .new_tokens
626                .into_iter()
627                .map(|(k, v)| (k, v.into()))
628                .collect(),
629            component_tvl: value.component_tvl,
630            partial_block_index: value.partial_block_index,
631            paused_components: value.paused_components,
632            unpaused_components: value.unpaused_components,
633        }
634    }
635}
636
637#[derive(PartialEq, Serialize, Deserialize, Clone, Debug, ToSchema)]
638pub struct AccountUpdate {
639    #[serde(with = "hex_bytes")]
640    #[schema(value_type=String)]
641    pub address: Bytes,
642    pub chain: Chain,
643    #[serde(with = "hex_hashmap_key_value")]
644    #[schema(value_type=HashMap<String, String>)]
645    pub slots: HashMap<Bytes, Bytes>,
646    #[serde(with = "hex_bytes_option")]
647    #[schema(value_type=Option<String>)]
648    pub balance: Option<Bytes>,
649    #[serde(with = "hex_bytes_option")]
650    #[schema(value_type=Option<String>)]
651    pub code: Option<Bytes>,
652    pub change: ChangeType,
653}
654
655impl AccountUpdate {
656    pub fn new(
657        address: Bytes,
658        chain: Chain,
659        slots: HashMap<Bytes, Bytes>,
660        balance: Option<Bytes>,
661        code: Option<Bytes>,
662        change: ChangeType,
663    ) -> Self {
664        Self { address, chain, slots, balance, code, change }
665    }
666
667    /// Merges a newer update for the same account into this one.
668    ///
669    /// Fields absent in `other` keep their current values, matching
670    /// [`models::contract::AccountDelta::merge`].
671    pub fn merge(&mut self, other: &Self) {
672        self.slots.extend(
673            other
674                .slots
675                .iter()
676                .map(|(k, v)| (k.clone(), v.clone())),
677        );
678        if other.balance.is_some() {
679            self.balance.clone_from(&other.balance);
680        }
681        if other.code.is_some() {
682            self.code.clone_from(&other.code);
683        }
684        self.change = self.change.merge(&other.change);
685    }
686}
687
688impl From<models::contract::AccountDelta> for AccountUpdate {
689    fn from(value: models::contract::AccountDelta) -> Self {
690        let code = value.code().clone();
691        let change_type = value.change_type().into();
692        AccountUpdate::new(
693            value.address,
694            value.chain.into(),
695            value
696                .slots
697                .into_iter()
698                .map(|(k, v)| (k, v.unwrap_or_default()))
699                .collect(),
700            value.balance,
701            code,
702            change_type,
703        )
704    }
705}
706
707/// Represents the static parts of a protocol component.
708#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize, ToSchema)]
709pub struct ProtocolComponent {
710    /// Unique identifier for this component
711    pub id: String,
712    /// Protocol system this component is part of
713    pub protocol_system: String,
714    /// Type of the protocol system
715    pub protocol_type_name: String,
716    pub chain: Chain,
717    /// Token addresses the component operates on
718    #[schema(value_type=Vec<String>)]
719    pub tokens: Vec<Bytes>,
720    /// Contract addresses involved in the components operations (may be empty for
721    /// native implementations)
722    #[serde(alias = "contract_addresses")]
723    #[schema(value_type=Vec<String>)]
724    pub contract_ids: Vec<Bytes>,
725    /// Constant attributes of the component
726    #[serde(with = "hex_hashmap_value")]
727    #[schema(value_type=HashMap<String, String>)]
728    pub static_attributes: HashMap<String, Bytes>,
729    /// Indicates if last change was update, create or delete (for internal use only).
730    #[serde(default)]
731    pub change: ChangeType,
732    /// Transaction hash which created this component
733    #[serde(with = "hex_bytes")]
734    #[schema(value_type=String)]
735    pub creation_tx: Bytes,
736    /// Date time of creation in UTC time
737    pub created_at: NaiveDateTime,
738}
739
740// Manual impl as `NaiveDateTime` structure referenced in `created_at` does not implement DeepSizeOf
741impl DeepSizeOf for ProtocolComponent {
742    fn deep_size_of_children(&self, context: &mut Context) -> usize {
743        self.id.deep_size_of_children(context) +
744            self.protocol_system
745                .deep_size_of_children(context) +
746            self.protocol_type_name
747                .deep_size_of_children(context) +
748            self.chain
749                .deep_size_of_children(context) +
750            self.tokens
751                .deep_size_of_children(context) +
752            self.contract_ids
753                .deep_size_of_children(context) +
754            self.static_attributes
755                .deep_size_of_children(context) +
756            self.change
757                .deep_size_of_children(context) +
758            self.creation_tx
759                .deep_size_of_children(context)
760    }
761}
762
763impl<T> From<models::protocol::ProtocolComponent<T>> for ProtocolComponent
764where
765    T: Into<Address> + Clone,
766{
767    fn from(value: models::protocol::ProtocolComponent<T>) -> Self {
768        Self {
769            id: value.id,
770            protocol_system: value.protocol_system,
771            protocol_type_name: value.protocol_type_name,
772            chain: value.chain.into(),
773            tokens: value
774                .tokens
775                .into_iter()
776                .map(|t| t.into())
777                .collect(),
778            contract_ids: value.contract_addresses,
779            static_attributes: value.static_attributes,
780            change: value.change.into(),
781            creation_tx: value.creation_tx,
782            created_at: value.created_at,
783        }
784    }
785}
786
787#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
788pub struct ComponentBalance {
789    #[serde(with = "hex_bytes")]
790    pub token: Bytes,
791    pub balance: Bytes,
792    pub balance_float: f64,
793    #[serde(with = "hex_bytes")]
794    pub modify_tx: Bytes,
795    pub component_id: String,
796}
797
798#[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize, ToSchema)]
799/// Represents a change in protocol state.
800pub struct ProtocolStateDelta {
801    pub component_id: String,
802    #[schema(value_type=HashMap<String, String>)]
803    pub updated_attributes: HashMap<String, Bytes>,
804    pub deleted_attributes: HashSet<String>,
805}
806
807impl From<models::protocol::ProtocolComponentStateDelta> for ProtocolStateDelta {
808    fn from(value: models::protocol::ProtocolComponentStateDelta) -> Self {
809        Self {
810            component_id: value.component_id,
811            updated_attributes: value.updated_attributes,
812            deleted_attributes: value.deleted_attributes,
813        }
814    }
815}
816
817impl ProtocolStateDelta {
818    /// Merges 'other' into 'self'.
819    ///
820    ///
821    /// During merge of these deltas a special situation can arise when an attribute is present in
822    /// `self.deleted_attributes` and `other.update_attributes``. If we would just merge the sets
823    /// of deleted attributes or vice versa, it would be ambiguous and potential lead to a
824    /// deletion of an attribute that should actually be present, or retention of an actually
825    /// deleted attribute.
826    ///
827    /// This situation is handled the following way:
828    ///
829    ///     - If an attribute is deleted and in the next message recreated, it is removed from the
830    ///       set of deleted attributes and kept in updated_attributes. This way it's temporary
831    ///       deletion is never communicated to the final receiver.
832    ///     - If an attribute was updated and is deleted in the next message, it is removed from
833    ///       updated attributes and kept in deleted. This way the attributes temporary update (or
834    ///       potentially short-lived existence) before its deletion is never communicated to the
835    ///       final receiver.
836    pub fn merge(&mut self, other: &Self) {
837        // either updated and then deleted -> keep in deleted, remove from updated
838        self.updated_attributes
839            .retain(|k, _| !other.deleted_attributes.contains(k));
840
841        // or deleted and then updated/recreated -> remove from deleted and keep in updated
842        self.deleted_attributes.retain(|attr| {
843            !other
844                .updated_attributes
845                .contains_key(attr)
846        });
847
848        // simply merge updates
849        self.updated_attributes.extend(
850            other
851                .updated_attributes
852                .iter()
853                .map(|(k, v)| (k.clone(), v.clone())),
854        );
855
856        // simply merge deletions
857        self.deleted_attributes
858            .extend(other.deleted_attributes.iter().cloned());
859    }
860}
861
862/// Pagination parameter
863#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema, Eq, Hash, DeepSizeOf)]
864#[serde(deny_unknown_fields)]
865pub struct PaginationParams {
866    /// What page to retrieve
867    #[serde(default)]
868    pub page: i64,
869    /// How many results to return per page
870    #[serde(default)]
871    #[schema(default = 100)]
872    pub page_size: i64,
873}
874
875impl PaginationParams {
876    pub fn new(page: i64, page_size: i64) -> Self {
877        Self { page, page_size }
878    }
879}
880
881impl Default for PaginationParams {
882    fn default() -> Self {
883        PaginationParams { page: 0, page_size: 100 }
884    }
885}
886
887/// Defines pagination size limits for request types.
888///
889/// Different limits apply based on whether compression is enabled,
890/// as compressed responses can safely transfer more data.
891pub trait PaginationLimits {
892    /// Maximum page size when compression is enabled (e.g., zstd)
893    const MAX_PAGE_SIZE_COMPRESSED: i64;
894
895    /// Maximum page size when compression is disabled
896    const MAX_PAGE_SIZE_UNCOMPRESSED: i64;
897
898    /// Returns the effective maximum page size based on compression setting
899    fn effective_max_page_size(compression: bool) -> i64 {
900        if compression {
901            Self::MAX_PAGE_SIZE_COMPRESSED
902        } else {
903            Self::MAX_PAGE_SIZE_UNCOMPRESSED
904        }
905    }
906
907    /// Returns a reference to the pagination parameters
908    fn pagination(&self) -> &PaginationParams;
909}
910
911/// Macro to implement PaginationLimits for request types
912///
913/// When INCREASING these limits, ensure to immediately redeploy the servers.
914///
915/// Why: pagination limits are shared. Clients use these constants to set their max page size.
916/// When clients upgrade before servers, they request more than old servers allow and get errors.
917macro_rules! impl_pagination_limits {
918    ($type:ty, compressed = $comp:expr, uncompressed = $uncomp:expr) => {
919        impl $crate::dto::PaginationLimits for $type {
920            const MAX_PAGE_SIZE_COMPRESSED: i64 = $comp;
921            const MAX_PAGE_SIZE_UNCOMPRESSED: i64 = $uncomp;
922
923            fn pagination(&self) -> &$crate::dto::PaginationParams {
924                &self.pagination
925            }
926        }
927    };
928}
929
930#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema, Eq, Hash, DeepSizeOf)]
931#[serde(deny_unknown_fields)]
932pub struct PaginationResponse {
933    pub page: i64,
934    pub page_size: i64,
935    /// The total number of items available across all pages of results
936    pub total: i64,
937}
938
939/// Current pagination information
940impl PaginationResponse {
941    pub fn new(page: i64, page_size: i64, total: i64) -> Self {
942        Self { page, page_size, total }
943    }
944
945    pub fn total_pages(&self) -> i64 {
946        // ceil(total / page_size)
947        (self.total + self.page_size - 1) / self.page_size
948    }
949}
950
951#[derive(
952    Clone, Serialize, Debug, Default, Deserialize, PartialEq, ToSchema, Eq, Hash, DeepSizeOf,
953)]
954#[serde(deny_unknown_fields)]
955pub struct StateRequestBody {
956    /// Filters response by contract addresses
957    #[serde(alias = "contractIds")]
958    #[schema(value_type=Option<Vec<String>>)]
959    pub contract_ids: Option<Vec<Bytes>>,
960    /// Does not filter response, only required to correctly apply unconfirmed state
961    /// from ReorgBuffers
962    #[serde(alias = "protocolSystem", default)]
963    pub protocol_system: String,
964    #[serde(default = "VersionParam::default")]
965    pub version: VersionParam,
966    #[serde(default)]
967    pub chain: Chain,
968    #[serde(default)]
969    pub pagination: PaginationParams,
970}
971
972// When INCREASING these limits, please read the warning in the macro definition.
973impl_pagination_limits!(StateRequestBody, compressed = 1200, uncompressed = 100);
974
975impl StateRequestBody {
976    pub fn new(
977        contract_ids: Option<Vec<Bytes>>,
978        protocol_system: String,
979        version: VersionParam,
980        chain: Chain,
981        pagination: PaginationParams,
982    ) -> Self {
983        Self { contract_ids, protocol_system, version, chain, pagination }
984    }
985
986    #[expect(
987        clippy::needless_pass_by_value,
988        reason = "public API; taking `&BlockParam` is a breaking change"
989    )]
990    pub fn from_block(protocol_system: &str, block: BlockParam) -> Self {
991        Self {
992            contract_ids: None,
993            protocol_system: protocol_system.to_string(),
994            version: VersionParam { timestamp: None, block: Some(block.clone()) },
995            chain: block.chain.unwrap_or_default(),
996            pagination: PaginationParams::default(),
997        }
998    }
999
1000    pub fn from_timestamp(protocol_system: &str, timestamp: NaiveDateTime, chain: Chain) -> Self {
1001        Self {
1002            contract_ids: None,
1003            protocol_system: protocol_system.to_string(),
1004            version: VersionParam { timestamp: Some(timestamp), block: None },
1005            chain,
1006            pagination: PaginationParams::default(),
1007        }
1008    }
1009}
1010
1011/// Response from Tycho server for a contract state request.
1012#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema, DeepSizeOf)]
1013pub struct StateRequestResponse {
1014    pub accounts: Vec<ResponseAccount>,
1015    pub pagination: PaginationResponse,
1016}
1017
1018impl StateRequestResponse {
1019    pub fn new(accounts: Vec<ResponseAccount>, pagination: PaginationResponse) -> Self {
1020        Self { accounts, pagination }
1021    }
1022}
1023
1024#[derive(PartialEq, Clone, Serialize, Deserialize, Default, ToSchema, DeepSizeOf)]
1025#[serde(rename = "Account")]
1026/// Account struct for the response from Tycho server for a contract state request.
1027///
1028/// Code is serialized as a hex string instead of a list of bytes.
1029pub struct ResponseAccount {
1030    pub chain: Chain,
1031    /// The address of the account as hex encoded string
1032    #[schema(value_type=String, example="0xc9f2e6ea1637E499406986ac50ddC92401ce1f58")]
1033    #[serde(with = "hex_bytes")]
1034    pub address: Bytes,
1035    /// The title of the account usualy specifying its function within the protocol
1036    #[schema(value_type=String, example="Protocol Vault")]
1037    pub title: String,
1038    /// Contract storage map of hex encoded string values
1039    #[schema(value_type=HashMap<String, String>, example=json!({"0x....": "0x...."}))]
1040    #[serde(with = "hex_hashmap_key_value")]
1041    pub slots: HashMap<Bytes, Bytes>,
1042    /// The balance of the account in the native token
1043    #[schema(value_type=String, example="0x00")]
1044    #[serde(with = "hex_bytes")]
1045    pub native_balance: Bytes,
1046    /// Balances of this account in other tokens (only tokens balance that are
1047    /// relevant to the protocol are returned here)
1048    #[schema(value_type=HashMap<String, String>, example=json!({"0x....": "0x...."}))]
1049    #[serde(with = "hex_hashmap_key_value")]
1050    pub token_balances: HashMap<Bytes, Bytes>,
1051    /// The accounts code as hex encoded string
1052    #[schema(value_type=String, example="0xBADBABE")]
1053    #[serde(with = "hex_bytes")]
1054    pub code: Bytes,
1055    /// The hash of above code
1056    #[schema(value_type=String, example="0x123456789")]
1057    #[serde(with = "hex_bytes")]
1058    pub code_hash: Bytes,
1059    /// Transaction hash which last modified native balance
1060    #[schema(value_type=String, example="0x8f1133bfb054a23aedfe5d25b1d81b96195396d8b88bd5d4bcf865fc1ae2c3f4")]
1061    #[serde(with = "hex_bytes")]
1062    pub balance_modify_tx: Bytes,
1063    /// Transaction hash which last modified code
1064    #[schema(value_type=String, example="0x8f1133bfb054a23aedfe5d25b1d81b96195396d8b88bd5d4bcf865fc1ae2c3f4")]
1065    #[serde(with = "hex_bytes")]
1066    pub code_modify_tx: Bytes,
1067    /// Transaction hash which created the account
1068    #[deprecated(note = "The `creation_tx` field is deprecated.")]
1069    #[schema(value_type=Option<String>, example="0x8f1133bfb054a23aedfe5d25b1d81b96195396d8b88bd5d4bcf865fc1ae2c3f4")]
1070    #[serde(with = "hex_bytes_option")]
1071    pub creation_tx: Option<Bytes>,
1072}
1073
1074impl ResponseAccount {
1075    #[expect(
1076        clippy::too_many_arguments,
1077        reason = "public API; grouping the arguments into a params struct or builder is a breaking change"
1078    )]
1079    pub fn new(
1080        chain: Chain,
1081        address: Bytes,
1082        title: String,
1083        slots: HashMap<Bytes, Bytes>,
1084        native_balance: Bytes,
1085        token_balances: HashMap<Bytes, Bytes>,
1086        code: Bytes,
1087        code_hash: Bytes,
1088        balance_modify_tx: Bytes,
1089        code_modify_tx: Bytes,
1090        creation_tx: Option<Bytes>,
1091    ) -> Self {
1092        Self {
1093            chain,
1094            address,
1095            title,
1096            slots,
1097            native_balance,
1098            token_balances,
1099            code,
1100            code_hash,
1101            balance_modify_tx,
1102            code_modify_tx,
1103            creation_tx,
1104        }
1105    }
1106}
1107
1108/// Implement Debug for ResponseAccount manually to avoid printing the code field.
1109impl fmt::Debug for ResponseAccount {
1110    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1111        f.debug_struct("ResponseAccount")
1112            .field("chain", &self.chain)
1113            .field("address", &self.address)
1114            .field("title", &self.title)
1115            .field("slots", &self.slots)
1116            .field("native_balance", &self.native_balance)
1117            .field("token_balances", &self.token_balances)
1118            .field("code", &format!("[{} bytes]", self.code.len()))
1119            .field("code_hash", &self.code_hash)
1120            .field("balance_modify_tx", &self.balance_modify_tx)
1121            .field("code_modify_tx", &self.code_modify_tx)
1122            .field("creation_tx", &self.creation_tx)
1123            .finish()
1124    }
1125}
1126
1127#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
1128pub struct AccountBalance {
1129    #[serde(with = "hex_bytes")]
1130    pub account: Bytes,
1131    #[serde(with = "hex_bytes")]
1132    pub token: Bytes,
1133    #[serde(with = "hex_bytes")]
1134    pub balance: Bytes,
1135    #[serde(with = "hex_bytes")]
1136    pub modify_tx: Bytes,
1137}
1138
1139#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema)]
1140#[serde(deny_unknown_fields)]
1141pub struct ContractId {
1142    #[serde(with = "hex_bytes")]
1143    #[schema(value_type=String)]
1144    pub address: Bytes,
1145    pub chain: Chain,
1146}
1147
1148/// Uniquely identifies a contract on a specific chain.
1149impl ContractId {
1150    pub fn new(chain: Chain, address: Bytes) -> Self {
1151        Self { address, chain }
1152    }
1153
1154    pub fn address(&self) -> &Bytes {
1155        &self.address
1156    }
1157}
1158
1159impl fmt::Display for ContractId {
1160    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1161        write!(f, "{:?}: 0x{}", self.chain, hex::encode(&self.address))
1162    }
1163}
1164
1165/// The version of the requested state, given as either a timestamp or a block.
1166///
1167/// If block is provided, the state at that exact block is returned. Will error if the block
1168/// has not been processed yet. If timestamp is provided, the state at the latest block before
1169/// that timestamp is returned.
1170/// Defaults to the current time.
1171#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema, Eq, Hash)]
1172#[serde(deny_unknown_fields)]
1173pub struct VersionParam {
1174    pub timestamp: Option<NaiveDateTime>,
1175    pub block: Option<BlockParam>,
1176}
1177
1178impl DeepSizeOf for VersionParam {
1179    fn deep_size_of_children(&self, context: &mut Context) -> usize {
1180        if let Some(block) = &self.block {
1181            return block.deep_size_of_children(context);
1182        }
1183
1184        0
1185    }
1186}
1187
1188impl VersionParam {
1189    pub fn new(timestamp: Option<NaiveDateTime>, block: Option<BlockParam>) -> Self {
1190        Self { timestamp, block }
1191    }
1192
1193    pub fn at_block(chain: Chain, block_number: u64) -> Self {
1194        Self::new(
1195            None,
1196            Some({
1197                #[expect(
1198                    deprecated,
1199                    reason = "BlockParam is deprecated for consumers but still constructed by this crate"
1200                )]
1201                #[expect(
1202                    clippy::cast_possible_wrap,
1203                    reason = "public API; returning a Result for block numbers above i64::MAX is a breaking change"
1204                )]
1205                BlockParam { hash: None, chain: Some(chain), number: Some(block_number as i64) }
1206            }),
1207        )
1208    }
1209}
1210
1211impl Default for VersionParam {
1212    fn default() -> Self {
1213        VersionParam { timestamp: Some(Utc::now().naive_utc()), block: None }
1214    }
1215}
1216
1217#[deprecated(note = "Use StateRequestBody instead")]
1218#[derive(Serialize, Deserialize, Default, Debug, IntoParams)]
1219pub struct StateRequestParameters {
1220    /// The minimum TVL of the protocol components to return, denoted in the chain's native token.
1221    #[param(default = 0)]
1222    pub tvl_gt: Option<u64>,
1223    /// The minimum inertia of the protocol components to return.
1224    #[param(default = 0)]
1225    pub inertia_min_gt: Option<u64>,
1226    /// Whether to include ERC20 balances in the response.
1227    #[serde(default = "default_include_balances_flag")]
1228    pub include_balances: bool,
1229    #[serde(default)]
1230    pub pagination: PaginationParams,
1231}
1232
1233impl StateRequestParameters {
1234    pub fn new(include_balances: bool) -> Self {
1235        Self {
1236            tvl_gt: None,
1237            inertia_min_gt: None,
1238            include_balances,
1239            pagination: PaginationParams::default(),
1240        }
1241    }
1242
1243    pub fn to_query_string(&self) -> String {
1244        let mut parts = vec![format!("include_balances={}", self.include_balances)];
1245
1246        if let Some(tvl_gt) = self.tvl_gt {
1247            parts.push(format!("tvl_gt={tvl_gt}"));
1248        }
1249
1250        if let Some(inertia) = self.inertia_min_gt {
1251            parts.push(format!("inertia_min_gt={inertia}"));
1252        }
1253
1254        let mut res = parts.join("&");
1255        if !res.is_empty() {
1256            res = format!("?{res}");
1257        }
1258        res
1259    }
1260}
1261
1262#[derive(
1263    Serialize, Deserialize, Debug, Default, PartialEq, ToSchema, Eq, Hash, Clone, DeepSizeOf,
1264)]
1265#[serde(deny_unknown_fields)]
1266pub struct TokensRequestBody {
1267    /// Filters tokens by addresses
1268    #[serde(alias = "tokenAddresses")]
1269    #[schema(value_type=Option<Vec<String>>)]
1270    pub token_addresses: Option<Vec<Bytes>>,
1271    /// Quality is between 0-100, where:
1272    ///  - 100: Normal ERC-20 Token behavior
1273    ///  - 75: Rebasing token
1274    ///  - 50: Fee-on-transfer token
1275    ///  - 10: Token analysis failed at first detection
1276    ///  - 5: Token analysis failed multiple times (after creation)
1277    ///  - 0: Failed to extract attributes, like Decimal or Symbol
1278    #[serde(default)]
1279    pub min_quality: Option<i32>,
1280    /// Filters tokens by recent trade activity
1281    #[serde(default)]
1282    pub traded_n_days_ago: Option<u64>,
1283    /// Max page size supported is 3000
1284    #[serde(default)]
1285    pub pagination: PaginationParams,
1286    /// Filter tokens by blockchain, default 'ethereum'
1287    #[serde(default)]
1288    pub chain: Chain,
1289}
1290
1291// When INCREASING these limits, please read the warning in the macro definition.
1292impl_pagination_limits!(TokensRequestBody, compressed = 12900, uncompressed = 3000);
1293
1294/// Response from Tycho server for a tokens request.
1295#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema, Eq, Hash, DeepSizeOf)]
1296pub struct TokensRequestResponse {
1297    pub tokens: Vec<ResponseToken>,
1298    pub pagination: PaginationResponse,
1299}
1300
1301impl TokensRequestResponse {
1302    pub fn new(tokens: Vec<ResponseToken>, pagination_request: &PaginationResponse) -> Self {
1303        Self { tokens, pagination: pagination_request.clone() }
1304    }
1305}
1306
1307#[derive(
1308    PartialEq, Debug, Clone, Serialize, Deserialize, Default, ToSchema, Eq, Hash, DeepSizeOf,
1309)]
1310#[serde(rename = "Token")]
1311/// Token struct for the response from Tycho server for a tokens request.
1312pub struct ResponseToken {
1313    pub chain: Chain,
1314    /// The address of this token as hex encoded string
1315    #[schema(value_type=String, example="0xc9f2e6ea1637E499406986ac50ddC92401ce1f58")]
1316    #[serde(with = "hex_bytes")]
1317    pub address: Bytes,
1318    /// A shorthand symbol for this token (not unique)
1319    #[schema(value_type=String, example="WETH")]
1320    pub symbol: String,
1321    /// The number of decimals used to represent token values
1322    pub decimals: u32,
1323    /// The tax this token charges on transfers in basis points
1324    pub tax: u64,
1325    /// Gas usage of the token, currently is always a single averaged value
1326    pub gas: Vec<Option<u64>>,
1327    /// Quality is between 0-100, where:
1328    ///  - 100: Normal ERC-20 Token behavior
1329    ///  - 75: Rebasing token
1330    ///  - 50: Fee-on-transfer token
1331    ///  - 10: Token analysis failed at first detection
1332    ///  - 5: Token analysis failed multiple times (after creation)
1333    ///  - 0: Failed to extract attributes, like Decimal or Symbol
1334    pub quality: u32,
1335}
1336
1337impl From<models::token::Token> for ResponseToken {
1338    fn from(value: models::token::Token) -> Self {
1339        Self {
1340            chain: value.chain.into(),
1341            address: value.address,
1342            symbol: value.symbol,
1343            decimals: value.decimals,
1344            tax: value.tax,
1345            gas: value.gas,
1346            quality: value.quality,
1347        }
1348    }
1349}
1350
1351#[derive(Serialize, Deserialize, Debug, Default, ToSchema, Clone, DeepSizeOf)]
1352#[serde(deny_unknown_fields)]
1353pub struct ProtocolComponentsRequestBody {
1354    /// Filters by protocol, required to correctly apply unconfirmed state from
1355    /// ReorgBuffers
1356    pub protocol_system: String,
1357    /// Filter by component ids
1358    #[schema(value_type=Option<Vec<String>>)]
1359    #[serde(alias = "componentAddresses")]
1360    pub component_ids: Option<Vec<ComponentId>>,
1361    /// The minimum TVL of the protocol components to return, denoted in the chain's
1362    /// native token.
1363    #[serde(default)]
1364    pub tvl_gt: Option<f64>,
1365    #[serde(default)]
1366    pub chain: Chain,
1367    /// Max page size supported is 500
1368    #[serde(default)]
1369    pub pagination: PaginationParams,
1370}
1371
1372// When INCREASING these limits, please read the warning in the macro definition.
1373impl_pagination_limits!(ProtocolComponentsRequestBody, compressed = 2550, uncompressed = 500);
1374
1375// Implement PartialEq where tvl is considered equal if the difference is less than 1e-6
1376impl PartialEq for ProtocolComponentsRequestBody {
1377    fn eq(&self, other: &Self) -> bool {
1378        let tvl_close_enough = match (self.tvl_gt, other.tvl_gt) {
1379            (Some(a), Some(b)) => (a - b).abs() < 1e-6,
1380            (None, None) => true,
1381            _ => false,
1382        };
1383
1384        self.protocol_system == other.protocol_system &&
1385            self.component_ids == other.component_ids &&
1386            tvl_close_enough &&
1387            self.chain == other.chain &&
1388            self.pagination == other.pagination
1389    }
1390}
1391
1392// Implement Eq without any new logic
1393impl Eq for ProtocolComponentsRequestBody {}
1394
1395impl Hash for ProtocolComponentsRequestBody {
1396    fn hash<H: Hasher>(&self, state: &mut H) {
1397        self.protocol_system.hash(state);
1398        self.component_ids.hash(state);
1399
1400        // Handle the f64 `tvl_gt` field by converting it into a hashable integer
1401        if let Some(tvl) = self.tvl_gt {
1402            // Convert f64 to bits and hash those bits
1403            tvl.to_bits().hash(state);
1404        } else {
1405            // Use a constant value to represent None
1406            state.write_u8(0);
1407        }
1408
1409        self.chain.hash(state);
1410        self.pagination.hash(state);
1411    }
1412}
1413
1414impl ProtocolComponentsRequestBody {
1415    pub fn system_filtered(system: &str, tvl_gt: Option<f64>, chain: Chain) -> Self {
1416        Self {
1417            protocol_system: system.to_string(),
1418            component_ids: None,
1419            tvl_gt,
1420            chain,
1421            pagination: Default::default(),
1422        }
1423    }
1424
1425    pub fn id_filtered(system: &str, ids: Vec<String>, chain: Chain) -> Self {
1426        Self {
1427            protocol_system: system.to_string(),
1428            component_ids: Some(ids),
1429            tvl_gt: None,
1430            chain,
1431            pagination: Default::default(),
1432        }
1433    }
1434}
1435
1436impl ProtocolComponentsRequestBody {
1437    pub fn new(
1438        protocol_system: String,
1439        component_ids: Option<Vec<String>>,
1440        tvl_gt: Option<f64>,
1441        chain: Chain,
1442        pagination: PaginationParams,
1443    ) -> Self {
1444        Self { protocol_system, component_ids, tvl_gt, chain, pagination }
1445    }
1446}
1447
1448#[deprecated(note = "Use ProtocolComponentsRequestBody instead")]
1449#[derive(Serialize, Deserialize, Default, Debug, IntoParams)]
1450pub struct ProtocolComponentRequestParameters {
1451    /// The minimum TVL of the protocol components to return, denoted in the chain's native token.
1452    #[param(default = 0)]
1453    pub tvl_gt: Option<f64>,
1454}
1455
1456impl ProtocolComponentRequestParameters {
1457    pub fn tvl_filtered(min_tvl: f64) -> Self {
1458        Self { tvl_gt: Some(min_tvl) }
1459    }
1460}
1461
1462impl ProtocolComponentRequestParameters {
1463    pub fn to_query_string(&self) -> String {
1464        if let Some(tvl_gt) = self.tvl_gt {
1465            return format!("?tvl_gt={tvl_gt}");
1466        }
1467        String::new()
1468    }
1469}
1470
1471/// Response from Tycho server for a protocol components request.
1472#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema, DeepSizeOf)]
1473pub struct ProtocolComponentRequestResponse {
1474    pub protocol_components: Vec<ProtocolComponent>,
1475    pub pagination: PaginationResponse,
1476    /// Ids of the components in `protocol_components` that are paused. Clients should not track
1477    /// them.
1478    #[serde(default)]
1479    #[schema(value_type = Vec<String>)]
1480    pub paused_component_ids: Vec<ComponentId>,
1481}
1482
1483impl ProtocolComponentRequestResponse {
1484    pub fn new(
1485        protocol_components: Vec<ProtocolComponent>,
1486        pagination: PaginationResponse,
1487    ) -> Self {
1488        Self { protocol_components, pagination, paused_component_ids: Vec::new() }
1489    }
1490}
1491
1492#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema, Eq, Hash)]
1493#[serde(deny_unknown_fields)]
1494#[deprecated]
1495pub struct ProtocolId {
1496    pub id: String,
1497    pub chain: Chain,
1498}
1499
1500impl From<ProtocolId> for String {
1501    fn from(protocol_id: ProtocolId) -> Self {
1502        protocol_id.id
1503    }
1504}
1505
1506impl AsRef<str> for ProtocolId {
1507    fn as_ref(&self) -> &str {
1508        &self.id
1509    }
1510}
1511
1512/// Protocol State struct for the response from Tycho server for a protocol state request.
1513#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize, ToSchema, DeepSizeOf)]
1514pub struct ResponseProtocolState {
1515    /// Component id this state belongs to
1516    pub component_id: String,
1517    /// Attributes of the component. If an attribute's value is a `bigint`,
1518    /// it will be encoded as a big endian signed hex string.
1519    #[schema(value_type=HashMap<String, String>)]
1520    #[serde(with = "hex_hashmap_value")]
1521    pub attributes: HashMap<String, Bytes>,
1522    /// Sum aggregated balances of the component
1523    #[schema(value_type=HashMap<String, String>)]
1524    #[serde(with = "hex_hashmap_key_value")]
1525    pub balances: HashMap<Bytes, Bytes>,
1526}
1527
1528impl From<models::protocol::ProtocolComponentState> for ResponseProtocolState {
1529    fn from(value: models::protocol::ProtocolComponentState) -> Self {
1530        Self {
1531            component_id: value.component_id,
1532            attributes: value.attributes,
1533            balances: value.balances,
1534        }
1535    }
1536}
1537
1538fn default_include_balances_flag() -> bool {
1539    true
1540}
1541
1542/// Max page size supported is 100
1543#[derive(Clone, Debug, Serialize, PartialEq, ToSchema, Default, Eq, Hash, DeepSizeOf)]
1544#[serde(deny_unknown_fields)]
1545pub struct ProtocolStateRequestBody {
1546    /// Filters response by protocol components ids
1547    #[serde(alias = "protocolIds")]
1548    pub protocol_ids: Option<Vec<String>>,
1549    /// Filters by protocol, required to correctly apply unconfirmed state from
1550    /// ReorgBuffers
1551    #[serde(alias = "protocolSystem")]
1552    pub protocol_system: String,
1553    #[serde(default)]
1554    pub chain: Chain,
1555    /// Whether to include account balances in the response. Defaults to true.
1556    #[serde(default = "default_include_balances_flag")]
1557    pub include_balances: bool,
1558    #[serde(default = "VersionParam::default")]
1559    pub version: VersionParam,
1560    #[serde(default)]
1561    pub pagination: PaginationParams,
1562}
1563
1564// When INCREASING these limits, please read the warning in the macro definition.
1565impl_pagination_limits!(ProtocolStateRequestBody, compressed = 360, uncompressed = 100);
1566
1567impl ProtocolStateRequestBody {
1568    pub fn id_filtered<I, T>(ids: I) -> Self
1569    where
1570        I: IntoIterator<Item = T>,
1571        T: Into<String>,
1572    {
1573        Self {
1574            protocol_ids: Some(
1575                ids.into_iter()
1576                    .map(Into::into)
1577                    .collect(),
1578            ),
1579            ..Default::default()
1580        }
1581    }
1582}
1583
1584/// Custom deserializer for ProtocolStateRequestBody to support backwards compatibility with the old
1585/// ProtocolIds format.
1586/// To be removed when the old format is no longer supported.
1587impl<'de> Deserialize<'de> for ProtocolStateRequestBody {
1588    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1589    where
1590        D: Deserializer<'de>,
1591    {
1592        #[derive(Deserialize)]
1593        #[serde(untagged)]
1594        enum ProtocolIdOrString {
1595            Old(Vec<ProtocolId>),
1596            New(Vec<String>),
1597        }
1598
1599        struct ProtocolStateRequestBodyVisitor;
1600
1601        impl<'de> de::Visitor<'de> for ProtocolStateRequestBodyVisitor {
1602            type Value = ProtocolStateRequestBody;
1603
1604            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1605                formatter.write_str("struct ProtocolStateRequestBody")
1606            }
1607
1608            fn visit_map<V>(self, mut map: V) -> Result<ProtocolStateRequestBody, V::Error>
1609            where
1610                V: de::MapAccess<'de>,
1611            {
1612                let mut protocol_ids = None;
1613                let mut protocol_system = None;
1614                let mut version = None;
1615                let mut chain = None;
1616                let mut include_balances = None;
1617                let mut pagination = None;
1618
1619                while let Some(key) = map.next_key::<String>()? {
1620                    match key.as_str() {
1621                        "protocol_ids" | "protocolIds" => {
1622                            let value: ProtocolIdOrString = map.next_value()?;
1623                            protocol_ids = match value {
1624                                ProtocolIdOrString::Old(ids) => {
1625                                    Some(ids.into_iter().map(|p| p.id).collect())
1626                                }
1627                                ProtocolIdOrString::New(ids_str) => Some(ids_str),
1628                            };
1629                        }
1630                        "protocol_system" | "protocolSystem" => {
1631                            protocol_system = Some(map.next_value()?);
1632                        }
1633                        "version" => {
1634                            version = Some(map.next_value()?);
1635                        }
1636                        "chain" => {
1637                            chain = Some(map.next_value()?);
1638                        }
1639                        "include_balances" => {
1640                            include_balances = Some(map.next_value()?);
1641                        }
1642                        "pagination" => {
1643                            pagination = Some(map.next_value()?);
1644                        }
1645                        _ => {
1646                            return Err(de::Error::unknown_field(
1647                                &key,
1648                                &[
1649                                    "contract_ids",
1650                                    "protocol_system",
1651                                    "version",
1652                                    "chain",
1653                                    "include_balances",
1654                                    "pagination",
1655                                ],
1656                            ))
1657                        }
1658                    }
1659                }
1660
1661                Ok(ProtocolStateRequestBody {
1662                    protocol_ids,
1663                    protocol_system: protocol_system.unwrap_or_default(),
1664                    version: version.unwrap_or_else(VersionParam::default),
1665                    chain: chain.unwrap_or_else(Chain::default),
1666                    include_balances: include_balances.unwrap_or(true),
1667                    pagination: pagination.unwrap_or_else(PaginationParams::default),
1668                })
1669            }
1670        }
1671
1672        deserializer.deserialize_struct(
1673            "ProtocolStateRequestBody",
1674            &[
1675                "contract_ids",
1676                "protocol_system",
1677                "version",
1678                "chain",
1679                "include_balances",
1680                "pagination",
1681            ],
1682            ProtocolStateRequestBodyVisitor,
1683        )
1684    }
1685}
1686
1687#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema, DeepSizeOf)]
1688pub struct ProtocolStateRequestResponse {
1689    pub states: Vec<ResponseProtocolState>,
1690    pub pagination: PaginationResponse,
1691}
1692
1693impl ProtocolStateRequestResponse {
1694    pub fn new(states: Vec<ResponseProtocolState>, pagination: PaginationResponse) -> Self {
1695        Self { states, pagination }
1696    }
1697}
1698
1699#[derive(Serialize, Clone, PartialEq, Hash, Eq)]
1700pub struct ProtocolComponentId {
1701    pub chain: Chain,
1702    pub system: String,
1703    pub id: String,
1704}
1705
1706#[derive(Debug, Serialize, ToSchema)]
1707#[serde(tag = "status", content = "message")]
1708#[schema(example = json!({"status": "NotReady", "message": "No db connection"}))]
1709pub enum Health {
1710    Ready,
1711    Starting(String),
1712    NotReady(String),
1713}
1714
1715#[derive(Serialize, Deserialize, Debug, Default, PartialEq, ToSchema, Eq, Hash, Clone)]
1716#[serde(deny_unknown_fields)]
1717pub struct ProtocolSystemsRequestBody {
1718    #[serde(default)]
1719    pub chain: Chain,
1720    #[serde(default)]
1721    pub pagination: PaginationParams,
1722}
1723
1724// When INCREASING these limits, please read the warning in the macro definition.
1725impl_pagination_limits!(ProtocolSystemsRequestBody, compressed = 100, uncompressed = 100);
1726
1727#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema, Eq, Hash)]
1728pub struct ProtocolSystemsRequestResponse {
1729    /// List of currently supported protocol systems
1730    pub protocol_systems: Vec<String>,
1731    /// Protocol systems that use Dynamic Contract Indexing (DCI).
1732    /// Clients should only fetch entrypoints for these protocols.
1733    #[serde(default)]
1734    pub dci_protocols: Vec<String>,
1735    pub pagination: PaginationResponse,
1736}
1737
1738impl ProtocolSystemsRequestResponse {
1739    pub fn new(
1740        protocol_systems: Vec<String>,
1741        dci_protocols: Vec<String>,
1742        pagination: PaginationResponse,
1743    ) -> Self {
1744        Self { protocol_systems, dci_protocols, pagination }
1745    }
1746}
1747
1748#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
1749pub struct DCIUpdate {
1750    /// Map of component id to the new entrypoints associated with the component
1751    pub new_entrypoints: HashMap<ComponentId, HashSet<EntryPoint>>,
1752    /// Map of entrypoint id to the new entrypoint params associtated with it (and optionally the
1753    /// component linked to those params)
1754    pub new_entrypoint_params: HashMap<String, HashSet<(TracingParams, String)>>,
1755    /// Map of entrypoint id to its trace result
1756    pub trace_results: HashMap<String, TracingResult>,
1757}
1758
1759impl From<models::blockchain::DCIUpdate> for DCIUpdate {
1760    fn from(value: models::blockchain::DCIUpdate) -> Self {
1761        Self {
1762            new_entrypoints: value
1763                .new_entrypoints
1764                .into_iter()
1765                .map(|(k, v)| {
1766                    (
1767                        k,
1768                        v.into_iter()
1769                            .map(|v| v.into())
1770                            .collect(),
1771                    )
1772                })
1773                .collect(),
1774            new_entrypoint_params: value
1775                .new_entrypoint_params
1776                .into_iter()
1777                .map(|(k, v)| {
1778                    (
1779                        k,
1780                        v.into_iter()
1781                            .map(|(params, i)| (params.into(), i))
1782                            .collect(),
1783                    )
1784                })
1785                .collect(),
1786            trace_results: value
1787                .trace_results
1788                .into_iter()
1789                .map(|(k, v)| (k, v.into()))
1790                .collect(),
1791        }
1792    }
1793}
1794
1795#[derive(Serialize, Deserialize, Debug, Default, PartialEq, ToSchema, Eq, Hash, Clone)]
1796#[serde(deny_unknown_fields)]
1797pub struct ComponentTvlRequestBody {
1798    #[serde(default)]
1799    pub chain: Chain,
1800    /// Filters protocol components by protocol system
1801    /// Useful when `component_ids` is omitted to fetch all components under a specific system.
1802    #[serde(alias = "protocolSystem")]
1803    pub protocol_system: Option<String>,
1804    #[serde(default)]
1805    pub component_ids: Option<Vec<String>>,
1806    #[serde(default)]
1807    pub pagination: PaginationParams,
1808}
1809
1810// When INCREASING these limits, please read the warning in the macro definition.
1811impl_pagination_limits!(ComponentTvlRequestBody, compressed = 100, uncompressed = 100);
1812
1813impl ComponentTvlRequestBody {
1814    pub fn system_filtered(system: &str, chain: Chain) -> Self {
1815        Self {
1816            chain,
1817            protocol_system: Some(system.to_string()),
1818            component_ids: None,
1819            pagination: Default::default(),
1820        }
1821    }
1822
1823    pub fn id_filtered(ids: Vec<String>, chain: Chain) -> Self {
1824        Self {
1825            chain,
1826            protocol_system: None,
1827            component_ids: Some(ids),
1828            pagination: Default::default(),
1829        }
1830    }
1831}
1832// #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema, Eq, Hash)]
1833#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema)]
1834pub struct ComponentTvlRequestResponse {
1835    pub tvl: HashMap<String, f64>,
1836    pub pagination: PaginationResponse,
1837}
1838
1839impl ComponentTvlRequestResponse {
1840    pub fn new(tvl: HashMap<String, f64>, pagination: PaginationResponse) -> Self {
1841        Self { tvl, pagination }
1842    }
1843}
1844
1845#[derive(
1846    Serialize, Deserialize, Debug, Default, PartialEq, ToSchema, Eq, Hash, Clone, DeepSizeOf,
1847)]
1848pub struct TracedEntryPointRequestBody {
1849    #[serde(default)]
1850    pub chain: Chain,
1851    /// Filters by protocol, required to correctly apply unconfirmed state from
1852    /// ReorgBuffers
1853    pub protocol_system: String,
1854    /// Filter by component ids
1855    #[schema(value_type = Option<Vec<String>>)]
1856    pub component_ids: Option<Vec<ComponentId>>,
1857    /// Max page size supported is 100
1858    #[serde(default)]
1859    pub pagination: PaginationParams,
1860}
1861
1862// When INCREASING these limits, please read the warning in the macro definition.
1863impl_pagination_limits!(TracedEntryPointRequestBody, compressed = 100, uncompressed = 100);
1864
1865#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema, Eq, Hash, DeepSizeOf)]
1866pub struct EntryPoint {
1867    #[schema(example = "0xEdf63cce4bA70cbE74064b7687882E71ebB0e988:getRate()")]
1868    /// Entry point id.
1869    pub external_id: String,
1870    #[schema(value_type=String, example="0x8f4E8439b970363648421C692dd897Fb9c0Bd1D9")]
1871    #[serde(with = "hex_bytes")]
1872    /// The address of the contract to trace.
1873    pub target: Bytes,
1874    #[schema(example = "getRate()")]
1875    /// The signature of the function to trace.
1876    pub signature: String,
1877}
1878
1879#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Eq, Hash, DeepSizeOf)]
1880pub enum StorageOverride {
1881    /// Applies changes incrementally to the existing account storage.
1882    /// Only modifies the specific storage slots provided in the map while
1883    /// preserving all other storage slots.
1884    #[schema(value_type=HashMap<String, String>)]
1885    Diff(BTreeMap<StoreKey, StoreVal>),
1886
1887    /// Completely replaces the account's storage state.
1888    /// Only the storage slots provided in the map will exist after the operation,
1889    /// and any existing storage slots not included will be cleared/zeroed.
1890    #[schema(value_type=HashMap<String, String>)]
1891    Replace(BTreeMap<StoreKey, StoreVal>),
1892}
1893
1894impl From<models::blockchain::StorageOverride> for StorageOverride {
1895    fn from(value: models::blockchain::StorageOverride) -> Self {
1896        match value {
1897            models::blockchain::StorageOverride::Diff(diff) => StorageOverride::Diff(diff),
1898            models::blockchain::StorageOverride::Replace(replace) => {
1899                StorageOverride::Replace(replace)
1900            }
1901        }
1902    }
1903}
1904
1905/// State overrides for an account.
1906///
1907/// Used to modify account state. Commonly used for testing contract interactions with specific
1908/// state conditions or simulating transactions with modified balances/code.
1909#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Eq, Hash, DeepSizeOf)]
1910pub struct AccountOverrides {
1911    /// Storage slots to override
1912    pub slots: Option<StorageOverride>,
1913    #[schema(value_type=Option<String>)]
1914    /// Native token balance override
1915    pub native_balance: Option<Balance>,
1916    #[schema(value_type=Option<String>)]
1917    /// Contract code override
1918    pub code: Option<Code>,
1919}
1920
1921impl From<models::blockchain::AccountOverrides> for AccountOverrides {
1922    fn from(value: models::blockchain::AccountOverrides) -> Self {
1923        AccountOverrides {
1924            slots: value.slots.map(|s| s.into()),
1925            native_balance: value.native_balance,
1926            code: value.code,
1927        }
1928    }
1929}
1930
1931#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema, Eq, Hash, DeepSizeOf)]
1932pub struct RPCTracerParams {
1933    /// The caller address of the transaction, if not provided tracing uses the default value
1934    /// for an address defined by the VM.
1935    #[schema(value_type=Option<String>)]
1936    #[serde(with = "hex_bytes_option", default)]
1937    pub caller: Option<Bytes>,
1938    /// The call data used for the tracing call, this needs to include the function selector
1939    #[schema(value_type=String, example="0x679aefce")]
1940    #[serde(with = "hex_bytes")]
1941    pub calldata: Bytes,
1942    /// Optionally allow for state overrides so that the call works as expected
1943    pub state_overrides: Option<BTreeMap<Address, AccountOverrides>>,
1944    /// Addresses to prune from trace results. Useful for hooks that use mock
1945    /// accounts/routers that shouldn't be tracked in the final DCI results.
1946    #[schema(value_type=Option<Vec<String>>)]
1947    #[serde(default)]
1948    pub prune_addresses: Option<Vec<Address>>,
1949}
1950
1951impl From<models::blockchain::RPCTracerParams> for RPCTracerParams {
1952    fn from(value: models::blockchain::RPCTracerParams) -> Self {
1953        RPCTracerParams {
1954            caller: value.caller,
1955            calldata: value.calldata,
1956            state_overrides: value.state_overrides.map(|overrides| {
1957                overrides
1958                    .into_iter()
1959                    .map(|(address, account_overrides)| (address, account_overrides.into()))
1960                    .collect()
1961            }),
1962            prune_addresses: value.prune_addresses,
1963        }
1964    }
1965}
1966
1967#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, Hash, DeepSizeOf, ToSchema)]
1968#[serde(tag = "method", rename_all = "lowercase")]
1969pub enum TracingParams {
1970    /// Uses RPC calls to retrieve the called addresses and retriggers
1971    RPCTracer(RPCTracerParams),
1972}
1973
1974impl From<models::blockchain::TracingParams> for TracingParams {
1975    fn from(value: models::blockchain::TracingParams) -> Self {
1976        match value {
1977            models::blockchain::TracingParams::RPCTracer(params) => {
1978                TracingParams::RPCTracer(params.into())
1979            }
1980        }
1981    }
1982}
1983
1984impl From<models::blockchain::EntryPoint> for EntryPoint {
1985    fn from(value: models::blockchain::EntryPoint) -> Self {
1986        Self { external_id: value.external_id, target: value.target, signature: value.signature }
1987    }
1988}
1989
1990#[derive(Serialize, Deserialize, Debug, PartialEq, ToSchema, Eq, Clone, DeepSizeOf)]
1991pub struct EntryPointWithTracingParams {
1992    /// The entry point object
1993    pub entry_point: EntryPoint,
1994    /// The parameters used
1995    pub params: TracingParams,
1996}
1997
1998impl From<models::blockchain::EntryPointWithTracingParams> for EntryPointWithTracingParams {
1999    fn from(value: models::blockchain::EntryPointWithTracingParams) -> Self {
2000        Self { entry_point: value.entry_point.into(), params: value.params.into() }
2001    }
2002}
2003
2004#[derive(
2005    Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize, DeepSizeOf,
2006)]
2007pub struct AddressStorageLocation {
2008    pub key: StoreKey,
2009    pub offset: u8,
2010}
2011
2012impl AddressStorageLocation {
2013    pub fn new(key: StoreKey, offset: u8) -> Self {
2014        Self { key, offset }
2015    }
2016}
2017
2018impl From<models::blockchain::AddressStorageLocation> for AddressStorageLocation {
2019    fn from(value: models::blockchain::AddressStorageLocation) -> Self {
2020        Self { key: value.key, offset: value.offset }
2021    }
2022}
2023
2024fn deserialize_retriggers_from_value(
2025    value: &serde_json::Value,
2026) -> Result<HashSet<(StoreKey, AddressStorageLocation)>, String> {
2027    use serde::Deserialize;
2028    use serde_json::Value;
2029
2030    let mut result = HashSet::new();
2031
2032    if let Value::Array(items) = value {
2033        for item in items {
2034            if let Value::Array(pair) = item {
2035                if pair.len() == 2 {
2036                    let key = StoreKey::deserialize(&pair[0])
2037                        .map_err(|e| format!("Failed to deserialize key: {}", e))?;
2038
2039                    // Handle both old format (string) and new format (AddressStorageLocation)
2040                    let addr_storage = match &pair[1] {
2041                        Value::String(_) => {
2042                            // Old format: just a string key with offset defaulted to 0
2043                            let storage_key = StoreKey::deserialize(&pair[1]).map_err(|e| {
2044                                format!("Failed to deserialize old format storage key: {}", e)
2045                            })?;
2046                            AddressStorageLocation::new(storage_key, 12)
2047                        }
2048                        Value::Object(_) => {
2049                            // New format: AddressStorageLocation struct
2050                            AddressStorageLocation::deserialize(&pair[1]).map_err(|e| {
2051                                format!("Failed to deserialize AddressStorageLocation: {}", e)
2052                            })?
2053                        }
2054                        Value::Null | Value::Bool(_) | Value::Number(_) | Value::Array(_) => {
2055                            return Err("Invalid retrigger format".to_string())
2056                        }
2057                    };
2058
2059                    result.insert((key, addr_storage));
2060                }
2061            }
2062        }
2063    }
2064
2065    Ok(result)
2066}
2067
2068#[derive(Serialize, Debug, Default, PartialEq, ToSchema, Eq, Clone, DeepSizeOf)]
2069pub struct TracingResult {
2070    #[schema(value_type=HashSet<(String, String)>)]
2071    pub retriggers: HashSet<(StoreKey, AddressStorageLocation)>,
2072    #[schema(value_type=HashMap<String,HashSet<String>>)]
2073    pub accessed_slots: HashMap<Address, HashSet<StoreKey>>,
2074}
2075
2076/// Deserialize TracingResult with backward compatibility for retriggers
2077/// TODO: remove this after offset detection is deployed in production
2078impl<'de> Deserialize<'de> for TracingResult {
2079    fn deserialize<D>(deserializer: D) -> Result<TracingResult, D::Error>
2080    where
2081        D: Deserializer<'de>,
2082    {
2083        use serde::de::Error;
2084        use serde_json::Value;
2085
2086        let value = Value::deserialize(deserializer)?;
2087        let mut result = TracingResult::default();
2088
2089        if let Value::Object(map) = value {
2090            // Deserialize retriggers using our custom deserializer
2091            if let Some(retriggers_value) = map.get("retriggers") {
2092                result.retriggers =
2093                    deserialize_retriggers_from_value(retriggers_value).map_err(|e| {
2094                        D::Error::custom(format!("Failed to deserialize retriggers: {}", e))
2095                    })?;
2096            }
2097
2098            // Deserialize accessed_slots normally
2099            if let Some(accessed_slots_value) = map.get("accessed_slots") {
2100                result.accessed_slots = serde_json::from_value(accessed_slots_value.clone())
2101                    .map_err(|e| {
2102                        D::Error::custom(format!("Failed to deserialize accessed_slots: {}", e))
2103                    })?;
2104            }
2105        }
2106
2107        Ok(result)
2108    }
2109}
2110
2111impl From<models::blockchain::TracingResult> for TracingResult {
2112    fn from(value: models::blockchain::TracingResult) -> Self {
2113        TracingResult {
2114            retriggers: value
2115                .retriggers
2116                .into_iter()
2117                .map(|(k, v)| (k, v.into()))
2118                .collect(),
2119            accessed_slots: value.accessed_slots,
2120        }
2121    }
2122}
2123
2124#[derive(Serialize, PartialEq, ToSchema, Eq, Clone, Debug, Deserialize, DeepSizeOf)]
2125pub struct TracedEntryPointRequestResponse {
2126    /// Map of protocol component id to a list of a tuple containing each entry point with its
2127    /// tracing parameters and its corresponding tracing results.
2128    #[schema(value_type = HashMap<String, Vec<(EntryPointWithTracingParams, TracingResult)>>)]
2129    pub traced_entry_points:
2130        HashMap<ComponentId, Vec<(EntryPointWithTracingParams, TracingResult)>>,
2131    pub pagination: PaginationResponse,
2132}
2133
2134#[derive(Serialize, Deserialize, Debug, Default, PartialEq, ToSchema, Eq, Clone)]
2135pub struct AddEntryPointRequestBody {
2136    #[serde(default)]
2137    pub chain: Chain,
2138    #[schema(value_type=String)]
2139    #[serde(default)]
2140    pub block_hash: Bytes,
2141    /// The map of component ids to their tracing params to insert
2142    #[schema(value_type = Vec<(String, Vec<EntryPointWithTracingParams>)>)]
2143    pub entry_points_with_tracing_data: Vec<(ComponentId, Vec<EntryPointWithTracingParams>)>,
2144}
2145
2146#[derive(Serialize, PartialEq, ToSchema, Eq, Clone, Debug, Deserialize)]
2147pub struct AddEntryPointRequestResponse {
2148    /// Map of protocol component id to a list of a tuple containing each entry point with its
2149    /// tracing parameters and its corresponding tracing results.
2150    #[schema(value_type = HashMap<String, Vec<(EntryPointWithTracingParams, TracingResult)>>)]
2151    pub traced_entry_points:
2152        HashMap<ComponentId, Vec<(EntryPointWithTracingParams, TracingResult)>>,
2153}
2154
2155#[cfg(test)]
2156mod test {
2157    use std::str::FromStr;
2158
2159    use maplit::hashmap;
2160    use rstest::rstest;
2161
2162    use super::*;
2163
2164    /// Test backward compatibility for Command::Subscribe compression field.
2165    /// Should default to false when not specified.
2166    #[rstest]
2167    #[case::legacy_format(None, false)]
2168    #[case::explicit_true(Some(true), true)]
2169    #[case::explicit_false(Some(false), false)]
2170    fn test_subscribe_compression_backward_compatibility(
2171        #[case] compression: Option<bool>,
2172        #[case] expected: bool,
2173    ) {
2174        use serde_json::json;
2175
2176        let mut json_value = json!({
2177            "method": "subscribe",
2178            "extractor_id": {
2179                "chain": "ethereum",
2180                "name": "test"
2181            },
2182            "include_state": true
2183        });
2184
2185        if let Some(value) = compression {
2186            json_value["compression"] = json!(value);
2187        }
2188
2189        let command: Command =
2190            serde_json::from_value(json_value).expect("Failed to deserialize Subscribe command");
2191
2192        if let Command::Subscribe { compression, .. } = command {
2193            assert_eq!(compression, expected);
2194        } else {
2195            panic!("Expected Subscribe command");
2196        }
2197    }
2198
2199    /// Test backward compatibility for Command::Subscribe partial_blocks field.
2200    /// Should default to false when not specified.
2201    #[rstest]
2202    #[case::legacy_format(None, false)]
2203    #[case::explicit_true(Some(true), true)]
2204    #[case::explicit_false(Some(false), false)]
2205    fn test_subscribe_partial_blocks_backward_compatibility(
2206        #[case] partial_blocks: Option<bool>,
2207        #[case] expected: bool,
2208    ) {
2209        use serde_json::json;
2210
2211        let mut json_value = json!({
2212            "method": "subscribe",
2213            "extractor_id": {
2214                "chain": "ethereum",
2215                "name": "test"
2216            },
2217            "include_state": true
2218        });
2219
2220        if let Some(value) = partial_blocks {
2221            json_value["partial_blocks"] = json!(value);
2222        }
2223
2224        let command: Command =
2225            serde_json::from_value(json_value).expect("Failed to deserialize Subscribe command");
2226
2227        if let Command::Subscribe { partial_blocks, .. } = command {
2228            assert_eq!(partial_blocks, expected);
2229        } else {
2230            panic!("Expected Subscribe command");
2231        }
2232    }
2233
2234    /// Test backward compatibility for ProtocolSystemsRequestResponse dci_protocols field.
2235    /// Should default to empty vec when not specified (old server responses).
2236    #[rstest]
2237    #[case::legacy_format(None, vec![])]
2238    #[case::with_dci(Some(vec!["vm:curve"]), vec!["vm:curve"])]
2239    #[case::empty_dci(Some(vec![]), vec![])]
2240    fn test_protocol_systems_dci_backward_compatibility(
2241        #[case] dci_protocols: Option<Vec<&str>>,
2242        #[case] expected: Vec<&str>,
2243    ) {
2244        use serde_json::json;
2245
2246        let mut json_value = json!({
2247            "protocol_systems": ["uniswap_v2", "vm:curve"],
2248            "pagination": { "page": 0, "page_size": 20, "total": 2 }
2249        });
2250
2251        if let Some(dci) = dci_protocols {
2252            json_value["dci_protocols"] = json!(dci);
2253        }
2254
2255        let resp: ProtocolSystemsRequestResponse =
2256            serde_json::from_value(json_value).expect("Failed to deserialize response");
2257
2258        assert_eq!(resp.dci_protocols, expected);
2259
2260        // Verify round-trip
2261        let serialized = serde_json::to_string(&resp).unwrap();
2262        let round_tripped: ProtocolSystemsRequestResponse =
2263            serde_json::from_str(&serialized).unwrap();
2264        assert_eq!(resp, round_tripped);
2265    }
2266
2267    #[test]
2268    fn test_tracing_result_backward_compatibility() {
2269        use serde_json::json;
2270
2271        // Test old format (string storage locations)
2272        let old_format_json = json!({
2273            "retriggers": [
2274                ["0x01", "0x02"],
2275                ["0x03", "0x04"]
2276            ],
2277            "accessed_slots": {
2278                "0x05": ["0x06", "0x07"]
2279            }
2280        });
2281
2282        let result: TracingResult = serde_json::from_value(old_format_json).unwrap();
2283
2284        // Check that retriggers were deserialized correctly with offset 0
2285        assert_eq!(result.retriggers.len(), 2);
2286        let retriggers_vec: Vec<_> = result.retriggers.iter().collect();
2287        assert!(retriggers_vec.iter().any(|(k, v)| {
2288            k == &Bytes::from("0x01") && v.key == Bytes::from("0x02") && v.offset == 12
2289        }));
2290        assert!(retriggers_vec.iter().any(|(k, v)| {
2291            k == &Bytes::from("0x03") && v.key == Bytes::from("0x04") && v.offset == 12
2292        }));
2293
2294        // Test new format (AddressStorageLocation objects)
2295        let new_format_json = json!({
2296            "retriggers": [
2297                ["0x01", {"key": "0x02", "offset": 12}],
2298                ["0x03", {"key": "0x04", "offset": 5}]
2299            ],
2300            "accessed_slots": {
2301                "0x05": ["0x06", "0x07"]
2302            }
2303        });
2304
2305        let result2: TracingResult = serde_json::from_value(new_format_json).unwrap();
2306
2307        // Check that new format retriggers were deserialized correctly with proper offsets
2308        assert_eq!(result2.retriggers.len(), 2);
2309        let retriggers_vec2: Vec<_> = result2.retriggers.iter().collect();
2310        assert!(retriggers_vec2.iter().any(|(k, v)| {
2311            k == &Bytes::from("0x01") && v.key == Bytes::from("0x02") && v.offset == 12
2312        }));
2313        assert!(retriggers_vec2.iter().any(|(k, v)| {
2314            k == &Bytes::from("0x03") && v.key == Bytes::from("0x04") && v.offset == 5
2315        }));
2316    }
2317
2318    #[rstest]
2319    #[case::legacy_format(None, None)]
2320    #[case::full_block(Some(serde_json::Value::Null), None)]
2321    #[case::partial_block(Some(serde_json::json!(1)), Some(1))]
2322    fn test_block_changes_is_partial_backward_compatibility(
2323        #[case] has_partial_value: Option<serde_json::Value>,
2324        #[case] expected: Option<u32>,
2325    ) {
2326        use serde_json::json;
2327
2328        let mut json_value = json!({
2329            "extractor": "test_extractor",
2330            "chain": "ethereum",
2331            "block": {
2332                "number": 100,
2333                "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
2334                "parent_hash": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
2335                "chain": "ethereum",
2336                "ts": "2024-01-01T00:00:00"
2337            },
2338            "finalized_block_height": 99,
2339            "revert": false,
2340            "new_tokens": {},
2341            "account_updates": {},
2342            "state_updates": {},
2343            "new_protocol_components": {},
2344            "deleted_protocol_components": {},
2345            "component_balances": {},
2346            "account_balances": {},
2347            "component_tvl": {},
2348            "dci_update": {
2349                "new_entrypoints": {},
2350                "new_entrypoint_params": {},
2351                "trace_results": {}
2352            }
2353        });
2354
2355        // Add is_partial field only if specified
2356        if let Some(partial_value) = has_partial_value {
2357            json_value["partial_block_index"] = partial_value;
2358        }
2359
2360        let block_changes: BlockAggregatedChanges = serde_json::from_value(json_value)
2361            .expect("Failed to deserialize BlockAggregatedChanges");
2362
2363        assert_eq!(block_changes.partial_block_index, expected);
2364    }
2365
2366    #[test]
2367    fn test_protocol_components_equality() {
2368        let body1 = ProtocolComponentsRequestBody {
2369            protocol_system: "protocol1".to_string(),
2370            component_ids: Some(vec!["component1".to_string(), "component2".to_string()]),
2371            tvl_gt: Some(1000.0),
2372            chain: Chain::Ethereum,
2373            pagination: PaginationParams::default(),
2374        };
2375
2376        let body2 = ProtocolComponentsRequestBody {
2377            protocol_system: "protocol1".to_string(),
2378            component_ids: Some(vec!["component1".to_string(), "component2".to_string()]),
2379            tvl_gt: Some(1000.0 + 1e-7), // Within the tolerance ±1e-6
2380            chain: Chain::Ethereum,
2381            pagination: PaginationParams::default(),
2382        };
2383
2384        // These should be considered equal due to the tolerance in tvl_gt
2385        assert_eq!(body1, body2);
2386    }
2387
2388    #[test]
2389    fn test_protocol_components_inequality() {
2390        let body1 = ProtocolComponentsRequestBody {
2391            protocol_system: "protocol1".to_string(),
2392            component_ids: Some(vec!["component1".to_string(), "component2".to_string()]),
2393            tvl_gt: Some(1000.0),
2394            chain: Chain::Ethereum,
2395            pagination: PaginationParams::default(),
2396        };
2397
2398        let body2 = ProtocolComponentsRequestBody {
2399            protocol_system: "protocol1".to_string(),
2400            component_ids: Some(vec!["component1".to_string(), "component2".to_string()]),
2401            tvl_gt: Some(1000.0 + 1e-5), // Outside the tolerance ±1e-6
2402            chain: Chain::Ethereum,
2403            pagination: PaginationParams::default(),
2404        };
2405
2406        // These should not be equal due to the difference in tvl_gt
2407        assert_ne!(body1, body2);
2408    }
2409
2410    #[test]
2411    fn test_parse_state_request() {
2412        let json_str = r#"
2413    {
2414        "contractIds": [
2415            "0xb4eccE46b8D4e4abFd03C9B806276A6735C9c092"
2416        ],
2417        "protocol_system": "uniswap_v2",
2418        "version": {
2419            "timestamp": "2069-01-01T04:20:00",
2420            "block": {
2421                "hash": "0x24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4",
2422                "number": 213,
2423                "chain": "ethereum"
2424            }
2425        }
2426    }
2427    "#;
2428
2429        let result: StateRequestBody = serde_json::from_str(json_str).unwrap();
2430
2431        let contract0 = "b4eccE46b8D4e4abFd03C9B806276A6735C9c092"
2432            .parse()
2433            .unwrap();
2434        let block_hash = "24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4"
2435            .parse()
2436            .unwrap();
2437        let block_number = 213;
2438
2439        let expected_timestamp =
2440            NaiveDateTime::parse_from_str("2069-01-01T04:20:00", "%Y-%m-%dT%H:%M:%S").unwrap();
2441
2442        let expected = StateRequestBody {
2443            contract_ids: Some(vec![contract0]),
2444            protocol_system: "uniswap_v2".to_string(),
2445            version: VersionParam {
2446                timestamp: Some(expected_timestamp),
2447                block: Some(BlockParam {
2448                    hash: Some(block_hash),
2449                    chain: Some(Chain::Ethereum),
2450                    number: Some(block_number),
2451                }),
2452            },
2453            chain: Chain::Ethereum,
2454            pagination: PaginationParams::default(),
2455        };
2456
2457        assert_eq!(result, expected);
2458    }
2459
2460    #[test]
2461    fn test_parse_state_request_dual_interface() {
2462        let json_common = r#"
2463    {
2464        "__CONTRACT_IDS__": [
2465            "0xb4eccE46b8D4e4abFd03C9B806276A6735C9c092"
2466        ],
2467        "version": {
2468            "timestamp": "2069-01-01T04:20:00",
2469            "block": {
2470                "hash": "0x24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4",
2471                "number": 213,
2472                "chain": "ethereum"
2473            }
2474        }
2475    }
2476    "#;
2477
2478        let json_str_snake = json_common.replace("\"__CONTRACT_IDS__\"", "\"contract_ids\"");
2479        let json_str_camel = json_common.replace("\"__CONTRACT_IDS__\"", "\"contractIds\"");
2480
2481        let snake: StateRequestBody = serde_json::from_str(&json_str_snake).unwrap();
2482        let camel: StateRequestBody = serde_json::from_str(&json_str_camel).unwrap();
2483
2484        assert_eq!(snake, camel);
2485    }
2486
2487    #[test]
2488    fn test_parse_state_request_unknown_field() {
2489        let body = r#"
2490    {
2491        "contract_ids_with_typo_error": [
2492            {
2493                "address": "0xb4eccE46b8D4e4abFd03C9B806276A6735C9c092",
2494                "chain": "ethereum"
2495            }
2496        ],
2497        "version": {
2498            "timestamp": "2069-01-01T04:20:00",
2499            "block": {
2500                "hash": "0x24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4",
2501                "parentHash": "0x8d75152454e60413efe758cc424bfd339897062d7e658f302765eb7b50971815",
2502                "number": 213,
2503                "chain": "ethereum"
2504            }
2505        }
2506    }
2507    "#;
2508
2509        let decoded = serde_json::from_str::<StateRequestBody>(body);
2510
2511        assert!(decoded.is_err(), "Expected an error due to unknown field");
2512
2513        if let Err(e) = decoded {
2514            assert!(
2515                e.to_string()
2516                    .contains("unknown field `contract_ids_with_typo_error`"),
2517                "Error message does not contain expected unknown field information"
2518            );
2519        }
2520    }
2521
2522    #[test]
2523    fn test_parse_state_request_no_contract_specified() {
2524        let json_str = r#"
2525    {
2526        "protocol_system": "uniswap_v2",
2527        "version": {
2528            "timestamp": "2069-01-01T04:20:00",
2529            "block": {
2530                "hash": "0x24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4",
2531                "number": 213,
2532                "chain": "ethereum"
2533            }
2534        }
2535    }
2536    "#;
2537
2538        let result: StateRequestBody = serde_json::from_str(json_str).unwrap();
2539
2540        let block_hash = "24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4".into();
2541        let block_number = 213;
2542        let expected_timestamp =
2543            NaiveDateTime::parse_from_str("2069-01-01T04:20:00", "%Y-%m-%dT%H:%M:%S").unwrap();
2544
2545        let expected = StateRequestBody {
2546            contract_ids: None,
2547            protocol_system: "uniswap_v2".to_string(),
2548            version: VersionParam {
2549                timestamp: Some(expected_timestamp),
2550                block: Some(BlockParam {
2551                    hash: Some(block_hash),
2552                    chain: Some(Chain::Ethereum),
2553                    number: Some(block_number),
2554                }),
2555            },
2556            chain: Chain::Ethereum,
2557            pagination: PaginationParams { page: 0, page_size: 100 },
2558        };
2559
2560        assert_eq!(result, expected);
2561    }
2562
2563    #[rstest]
2564    #[case::deprecated_ids(
2565        r#"
2566    {
2567        "protocol_ids": [
2568            {
2569                "id": "0xb4eccE46b8D4e4abFd03C9B806276A6735C9c092",
2570                "chain": "ethereum"
2571            }
2572        ],
2573        "protocol_system": "uniswap_v2",
2574        "include_balances": false,
2575        "version": {
2576            "timestamp": "2069-01-01T04:20:00",
2577            "block": {
2578                "hash": "0x24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4",
2579                "number": 213,
2580                "chain": "ethereum"
2581            }
2582        }
2583    }
2584    "#
2585    )]
2586    #[case(
2587        r#"
2588    {
2589        "protocolIds": [
2590            "0xb4eccE46b8D4e4abFd03C9B806276A6735C9c092"
2591        ],
2592        "protocol_system": "uniswap_v2",
2593        "include_balances": false,
2594        "version": {
2595            "timestamp": "2069-01-01T04:20:00",
2596            "block": {
2597                "hash": "0x24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4",
2598                "number": 213,
2599                "chain": "ethereum"
2600            }
2601        }
2602    }
2603    "#
2604    )]
2605    fn test_parse_protocol_state_request(#[case] json_str: &str) {
2606        let result: ProtocolStateRequestBody = serde_json::from_str(json_str).unwrap();
2607
2608        let block_hash = "24101f9cb26cd09425b52da10e8c2f56ede94089a8bbe0f31f1cda5f4daa52c4"
2609            .parse()
2610            .unwrap();
2611        let block_number = 213;
2612
2613        let expected_timestamp =
2614            NaiveDateTime::parse_from_str("2069-01-01T04:20:00", "%Y-%m-%dT%H:%M:%S").unwrap();
2615
2616        let expected = ProtocolStateRequestBody {
2617            protocol_ids: Some(vec!["0xb4eccE46b8D4e4abFd03C9B806276A6735C9c092".to_string()]),
2618            protocol_system: "uniswap_v2".to_string(),
2619            version: VersionParam {
2620                timestamp: Some(expected_timestamp),
2621                block: Some(BlockParam {
2622                    hash: Some(block_hash),
2623                    chain: Some(Chain::Ethereum),
2624                    number: Some(block_number),
2625                }),
2626            },
2627            chain: Chain::Ethereum,
2628            include_balances: false,
2629            pagination: PaginationParams::default(),
2630        };
2631
2632        assert_eq!(result, expected);
2633    }
2634
2635    #[rstest]
2636    #[case::with_protocol_ids(vec![ProtocolId { id: "id1".to_string(), chain: Chain::Ethereum }, ProtocolId { id: "id2".to_string(), chain: Chain::Ethereum }], vec!["id1".to_string(), "id2".to_string()])]
2637    #[case::with_strings(vec!["id1".to_string(), "id2".to_string()], vec!["id1".to_string(), "id2".to_string()])]
2638    fn test_id_filtered<T>(#[case] input_ids: Vec<T>, #[case] expected_ids: Vec<String>)
2639    where
2640        T: Into<String> + Clone,
2641    {
2642        let request_body = ProtocolStateRequestBody::id_filtered(input_ids);
2643
2644        assert_eq!(request_body.protocol_ids, Some(expected_ids));
2645    }
2646
2647    fn create_models_block_changes() -> crate::models::blockchain::BlockAggregatedChanges {
2648        let base_ts = 1694534400; // Example base timestamp for 2023-09-14T00:00:00
2649
2650        crate::models::blockchain::BlockAggregatedChanges {
2651            extractor: "native_name".to_string(),
2652            block: models::blockchain::Block::new(
2653                3,
2654                models::Chain::Ethereum,
2655                Bytes::from_str("0x0000000000000000000000000000000000000000000000000000000000000003").unwrap(),
2656                Bytes::from_str("0x0000000000000000000000000000000000000000000000000000000000000002").unwrap(),
2657                chrono::DateTime::from_timestamp(base_ts + 3000, 0).unwrap().naive_utc(),
2658            ),
2659            db_committed_block_height: Some(1),
2660            finalized_block_height: 1,
2661            revert: true,
2662            state_deltas: HashMap::from([
2663                ("pc_1".to_string(), models::protocol::ProtocolComponentStateDelta {
2664                    component_id: "pc_1".to_string(),
2665                    updated_attributes: HashMap::from([
2666                        ("attr_2".to_string(), Bytes::from("0x0000000000000002")),
2667                        ("attr_1".to_string(), Bytes::from("0x00000000000003e8")),
2668                    ]),
2669                    deleted_attributes: HashSet::new(),
2670                    ..Default::default()
2671                }),
2672            ]),
2673            new_protocol_components: HashMap::from([
2674                ("pc_2".to_string(), crate::models::protocol::ProtocolComponent {
2675                    id: "pc_2".to_string(),
2676                    protocol_system: "native_protocol_system".to_string(),
2677                    protocol_type_name: "pt_1".to_string(),
2678                    chain: models::Chain::Ethereum,
2679                    tokens: vec![
2680                        Bytes::from_str("0xdac17f958d2ee523a2206206994597c13d831ec7").unwrap(),
2681                        Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
2682                    ],
2683                    contract_addresses: vec![],
2684                    static_attributes: HashMap::new(),
2685                    change: models::ChangeType::Creation,
2686                    creation_tx: Bytes::from_str("0x000000000000000000000000000000000000000000000000000000000000c351").unwrap(),
2687                    created_at: chrono::DateTime::from_timestamp(base_ts + 5000, 0).unwrap().naive_utc(),
2688                }),
2689            ]),
2690            deleted_protocol_components: HashMap::from([
2691                ("pc_3".to_string(), crate::models::protocol::ProtocolComponent {
2692                    id: "pc_3".to_string(),
2693                    protocol_system: "native_protocol_system".to_string(),
2694                    protocol_type_name: "pt_2".to_string(),
2695                    chain: models::Chain::Ethereum,
2696                    tokens: vec![
2697                        Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap(),
2698                        Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(),
2699                    ],
2700                    contract_addresses: vec![],
2701                    static_attributes: HashMap::new(),
2702                    change: models::ChangeType::Deletion,
2703                    creation_tx: Bytes::from_str("0x0000000000000000000000000000000000000000000000000000000000009c41").unwrap(),
2704                    created_at: chrono::DateTime::from_timestamp(base_ts + 4000, 0).unwrap().naive_utc(),
2705                }),
2706            ]),
2707            component_balances: HashMap::from([
2708                ("pc_1".to_string(), HashMap::from([
2709                    (Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(), models::protocol::ComponentBalance {
2710                        token: Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
2711                        balance: Bytes::from("0x00000001"),
2712                        balance_float: 1.0,
2713                        modify_tx: Bytes::from_str("0x0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
2714                        component_id: "pc_1".to_string(),
2715                    }),
2716                    (Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(), models::protocol::ComponentBalance {
2717                        token: Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(),
2718                        balance: Bytes::from("0x000003e8"),
2719                        balance_float: 1000.0,
2720                        modify_tx: Bytes::from_str("0x0000000000000000000000000000000000000000000000000000000000007531").unwrap(),
2721                        component_id: "pc_1".to_string(),
2722                    }),
2723                ])),
2724            ]),
2725            account_balances: HashMap::from([
2726                (Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(), HashMap::from([
2727                    (Bytes::from_str("0x7a250d5630b4cf539739df2c5dacb4c659f2488d").unwrap(), models::contract::AccountBalance {
2728                        account: Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(),
2729                        token: Bytes::from_str("0x7a250d5630b4cf539739df2c5dacb4c659f2488d").unwrap(),
2730                        balance: Bytes::from("0x000003e8"),
2731                        modify_tx: Bytes::from_str("0x0000000000000000000000000000000000000000000000000000000000007531").unwrap(),
2732                    }),
2733                    ])),
2734            ]),
2735            ..Default::default()
2736        }
2737    }
2738
2739    #[test]
2740    fn test_serialize_deserialize_block_changes() {
2741        // Test that models::BlockAggregatedChanges serialized as json can be deserialized as
2742        // dto::BlockAggregatedChanges.
2743
2744        // Create a models::BlockAggregatedChanges instance
2745        let block_entity_changes = create_models_block_changes();
2746
2747        // Serialize the struct into JSON
2748        let json_data = serde_json::to_string(&block_entity_changes).expect("Failed to serialize");
2749
2750        // Deserialize the JSON back into a dto::BlockAggregatedChanges struct
2751        serde_json::from_str::<BlockAggregatedChanges>(&json_data).expect("parsing failed");
2752    }
2753
2754    fn ids(ids: &[&str]) -> HashSet<ComponentId> {
2755        ids.iter()
2756            .map(|id| id.to_string())
2757            .collect()
2758    }
2759
2760    fn pause_changes(paused: &[&str], unpaused: &[&str]) -> BlockAggregatedChanges {
2761        BlockAggregatedChanges {
2762            paused_components: ids(paused),
2763            unpaused_components: ids(unpaused),
2764            ..Default::default()
2765        }
2766    }
2767
2768    #[test]
2769    fn test_block_changes_pause_events_round_trip() {
2770        let mut model = create_models_block_changes();
2771        model.paused_components = ids(&["a"]);
2772        model.unpaused_components = ids(&["b"]);
2773        let json = serde_json::to_string(&model).expect("serialize model");
2774
2775        let dto: BlockAggregatedChanges = serde_json::from_str(&json).expect("deserialize dto");
2776        let back = models::blockchain::BlockAggregatedChanges::from(dto.clone());
2777        let dto_json = serde_json::to_string(&dto).expect("serialize dto");
2778        let dto_again: BlockAggregatedChanges =
2779            serde_json::from_str(&dto_json).expect("deserialize dto again");
2780
2781        assert_eq!(dto.paused_components, ids(&["a"]));
2782        assert_eq!(dto.unpaused_components, ids(&["b"]));
2783        assert_eq!(BlockAggregatedChanges::from(model), dto);
2784        assert_eq!(back.paused_components, ids(&["a"]));
2785        assert_eq!(back.unpaused_components, ids(&["b"]));
2786        assert_eq!(dto_again, dto);
2787    }
2788
2789    #[test]
2790    fn test_block_changes_without_pause_events() {
2791        let mut json = serde_json::to_value(create_models_block_changes()).expect("serialize");
2792        let fields = json
2793            .as_object_mut()
2794            .expect("json object");
2795        fields.remove("paused_components");
2796        fields.remove("unpaused_components");
2797
2798        let dto: BlockAggregatedChanges = serde_json::from_value(json.clone()).expect("dto");
2799        let model: models::blockchain::BlockAggregatedChanges =
2800            serde_json::from_value(json).expect("model");
2801
2802        assert!(dto.paused_components.is_empty());
2803        assert!(dto.unpaused_components.is_empty());
2804        assert!(model.paused_components.is_empty());
2805        assert!(model.unpaused_components.is_empty());
2806    }
2807
2808    #[rstest]
2809    #[case::pause_then_unpause(pause_changes(&["a"], &[]), pause_changes(&[], &["a"]), &[], &["a"])]
2810    #[case::unpause_then_pause(pause_changes(&[], &["a"]), pause_changes(&["a"], &[]), &["a"], &[])]
2811    #[case::disjoint_ids(pause_changes(&["a"], &["b"]), pause_changes(&["c"], &["d"]), &["a", "c"], &["b", "d"])]
2812    fn test_block_changes_merge_pause_events(
2813        #[case] earlier: BlockAggregatedChanges,
2814        #[case] later: BlockAggregatedChanges,
2815        #[case] expected_paused: &[&str],
2816        #[case] expected_unpaused: &[&str],
2817    ) {
2818        let merged = earlier.merge(later);
2819
2820        assert_eq!(merged.paused_components, ids(expected_paused));
2821        assert_eq!(merged.unpaused_components, ids(expected_unpaused));
2822    }
2823
2824    #[test]
2825    fn test_block_changes_drop_state_keeps_pause_events() {
2826        let dropped = pause_changes(&["a"], &["b"]).drop_state();
2827
2828        assert_eq!(dropped.paused_components, ids(&["a"]));
2829        assert_eq!(dropped.unpaused_components, ids(&["b"]));
2830    }
2831
2832    #[rstest]
2833    #[case::with_paused_ids(r#","paused_component_ids":["a"]"#, vec!["a".to_string()])]
2834    #[case::without_paused_ids("", vec![])]
2835    fn test_protocol_components_response_paused_ids(
2836        #[case] paused_field: &str,
2837        #[case] expected: Vec<ComponentId>,
2838    ) {
2839        let json = format!(
2840            r#"{{"protocol_components":[],"pagination":{{"page":0,"page_size":20,"total":0}}{paused_field}}}"#
2841        );
2842
2843        let response: ProtocolComponentRequestResponse =
2844            serde_json::from_str(&json).expect("deserialize");
2845
2846        assert_eq!(response.paused_component_ids, expected);
2847    }
2848
2849    #[test]
2850    fn test_parse_block_changes() {
2851        let json_data = r#"
2852        {
2853            "extractor": "vm:ambient",
2854            "chain": "ethereum",
2855            "block": {
2856                "number": 123,
2857                "hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2858                "parent_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2859                "chain": "ethereum",
2860                "ts": "2023-09-14T00:00:00"
2861            },
2862            "finalized_block_height": 0,
2863            "revert": false,
2864            "new_tokens": {},
2865            "account_updates": {
2866                "0x7a250d5630b4cf539739df2c5dacb4c659f2488d": {
2867                    "address": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
2868                    "chain": "ethereum",
2869                    "slots": {},
2870                    "balance": "0x01f4",
2871                    "code": "",
2872                    "change": "Update"
2873                }
2874            },
2875            "state_updates": {
2876                "component_1": {
2877                    "component_id": "component_1",
2878                    "updated_attributes": {"attr1": "0x01"},
2879                    "deleted_attributes": ["attr2"]
2880                }
2881            },
2882            "new_protocol_components":
2883                { "protocol_1": {
2884                        "id": "protocol_1",
2885                        "protocol_system": "system_1",
2886                        "protocol_type_name": "type_1",
2887                        "chain": "ethereum",
2888                        "tokens": ["0x01", "0x02"],
2889                        "contract_ids": ["0x01", "0x02"],
2890                        "static_attributes": {"attr1": "0x01f4"},
2891                        "change": "Update",
2892                        "creation_tx": "0x01",
2893                        "created_at": "2023-09-14T00:00:00"
2894                    }
2895                },
2896            "deleted_protocol_components": {},
2897            "component_balances": {
2898                "protocol_1":
2899                    {
2900                        "0x01": {
2901                            "token": "0x01",
2902                            "balance": "0xb77831d23691653a01",
2903                            "balance_float": 3.3844151001790677e21,
2904                            "modify_tx": "0x01",
2905                            "component_id": "protocol_1"
2906                        }
2907                    }
2908            },
2909            "account_balances": {
2910                "0x7a250d5630b4cf539739df2c5dacb4c659f2488d": {
2911                    "0x7a250d5630b4cf539739df2c5dacb4c659f2488d": {
2912                        "account": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
2913                        "token": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
2914                        "balance": "0x01f4",
2915                        "modify_tx": "0x01"
2916                    }
2917                }
2918            },
2919            "component_tvl": {
2920                "protocol_1": 1000.0
2921            },
2922            "dci_update": {
2923                "new_entrypoints": {
2924                    "component_1": [
2925                        {
2926                            "external_id": "0x01:sig()",
2927                            "target": "0x01",
2928                            "signature": "sig()"
2929                        }
2930                    ]
2931                },
2932                "new_entrypoint_params": {
2933                    "0x01:sig()": [
2934                        [
2935                            {
2936                                "method": "rpctracer",
2937                                "caller": "0x01",
2938                                "calldata": "0x02"
2939                            },
2940                            "component_1"
2941                        ]
2942                    ]
2943                },
2944                "trace_results": {
2945                    "0x01:sig()": {
2946                        "retriggers": [
2947                            ["0x01", {"key": "0x02", "offset": 12}]
2948                        ],
2949                        "accessed_slots": {
2950                            "0x03": ["0x03", "0x04"]
2951                        }
2952                    }
2953                }
2954            }
2955        }
2956        "#;
2957
2958        serde_json::from_str::<BlockAggregatedChanges>(json_data).expect("parsing failed");
2959    }
2960
2961    #[test]
2962    fn test_parse_websocket_message() {
2963        let json_data = r#"
2964        {
2965            "subscription_id": "5d23bfbe-89ad-4ea3-8672-dc9e973ac9dc",
2966            "deltas": {
2967                "type": "BlockAggregatedChanges",
2968                "extractor": "uniswap_v2",
2969                "chain": "ethereum",
2970                "block": {
2971                    "number": 19291517,
2972                    "hash": "0xbc3ea4896c0be8da6229387a8571b72818aa258daf4fab46471003ad74c4ee83",
2973                    "parent_hash": "0x89ca5b8d593574cf6c886f41ef8208bf6bdc1a90ef36046cb8c84bc880b9af8f",
2974                    "chain": "ethereum",
2975                    "ts": "2024-02-23T16:35:35"
2976                },
2977                "finalized_block_height": 0,
2978                "revert": false,
2979                "new_tokens": {},
2980                "account_updates": {
2981                    "0x7a250d5630b4cf539739df2c5dacb4c659f2488d": {
2982                        "address": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
2983                        "chain": "ethereum",
2984                        "slots": {},
2985                        "balance": "0x01f4",
2986                        "code": "",
2987                        "change": "Update"
2988                    }
2989                },
2990                "state_updates": {
2991                    "0xde6faedbcae38eec6d33ad61473a04a6dd7f6e28": {
2992                        "component_id": "0xde6faedbcae38eec6d33ad61473a04a6dd7f6e28",
2993                        "updated_attributes": {
2994                            "reserve0": "0x87f7b5973a7f28a8b32404",
2995                            "reserve1": "0x09e9564b11"
2996                        },
2997                        "deleted_attributes": []
2998                    },
2999                    "0x99c59000f5a76c54c4fd7d82720c045bdcf1450d": {
3000                        "component_id": "0x99c59000f5a76c54c4fd7d82720c045bdcf1450d",
3001                        "updated_attributes": {
3002                            "reserve1": "0x44d9a8fd662c2f4d03",
3003                            "reserve0": "0x500b1261f811d5bf423e"
3004                        },
3005                        "deleted_attributes": []
3006                    }
3007                },
3008                "new_protocol_components": {},
3009                "deleted_protocol_components": {},
3010                "component_balances": {
3011                    "0x99c59000f5a76c54c4fd7d82720c045bdcf1450d": {
3012                        "0x9012744b7a564623b6c3e40b144fc196bdedf1a9": {
3013                            "token": "0x9012744b7a564623b6c3e40b144fc196bdedf1a9",
3014                            "balance": "0x500b1261f811d5bf423e",
3015                            "balance_float": 3.779935574269033E23,
3016                            "modify_tx": "0xe46c4db085fb6c6f3408a65524555797adb264e1d5cf3b66ad154598f85ac4bf",
3017                            "component_id": "0x99c59000f5a76c54c4fd7d82720c045bdcf1450d"
3018                        },
3019                        "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2": {
3020                            "token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
3021                            "balance": "0x44d9a8fd662c2f4d03",
3022                            "balance_float": 1.270062661329837E21,
3023                            "modify_tx": "0xe46c4db085fb6c6f3408a65524555797adb264e1d5cf3b66ad154598f85ac4bf",
3024                            "component_id": "0x99c59000f5a76c54c4fd7d82720c045bdcf1450d"
3025                        }
3026                    }
3027                },
3028                "account_balances": {
3029                    "0x7a250d5630b4cf539739df2c5dacb4c659f2488d": {
3030                        "0x7a250d5630b4cf539739df2c5dacb4c659f2488d": {
3031                            "account": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
3032                            "token": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
3033                            "balance": "0x01f4",
3034                            "modify_tx": "0x01"
3035                        }
3036                    }
3037                },
3038                "component_tvl": {},
3039                "dci_update": {
3040                    "new_entrypoints": {
3041                        "0xde6faedbcae38eec6d33ad61473a04a6dd7f6e28": [
3042                            {
3043                                "external_id": "0x01:sig()",
3044                                "target": "0x01",
3045                                "signature": "sig()"
3046                            }
3047                        ]
3048                    },
3049                    "new_entrypoint_params": {
3050                        "0x01:sig()": [
3051                            [
3052                                {
3053                                    "method": "rpctracer",
3054                                    "caller": "0x01",
3055                                    "calldata": "0x02"
3056                                },
3057                                "0xde6faedbcae38eec6d33ad61473a04a6dd7f6e28"
3058                            ]
3059                        ]
3060                    },
3061                    "trace_results": {
3062                        "0x01:sig()": {
3063                            "retriggers": [
3064                                ["0x01", {"key": "0x02", "offset": 12}]
3065                            ],
3066                            "accessed_slots": {
3067                                "0x03": ["0x03", "0x04"]
3068                            }
3069                        }
3070                    }
3071                }
3072            }
3073        }
3074        "#;
3075        serde_json::from_str::<WebSocketMessage>(json_data).expect("parsing failed");
3076    }
3077
3078    #[test]
3079    fn test_protocol_state_delta_merge_update_delete() {
3080        // Initialize ProtocolStateDelta instances
3081        let mut delta1 = ProtocolStateDelta {
3082            component_id: "Component1".to_string(),
3083            updated_attributes: HashMap::from([(
3084                "Attribute1".to_string(),
3085                Bytes::from("0xbadbabe420"),
3086            )]),
3087            deleted_attributes: HashSet::new(),
3088        };
3089        let delta2 = ProtocolStateDelta {
3090            component_id: "Component1".to_string(),
3091            updated_attributes: HashMap::from([(
3092                "Attribute2".to_string(),
3093                Bytes::from("0x0badbabe"),
3094            )]),
3095            deleted_attributes: HashSet::from(["Attribute1".to_string()]),
3096        };
3097        let exp = ProtocolStateDelta {
3098            component_id: "Component1".to_string(),
3099            updated_attributes: HashMap::from([(
3100                "Attribute2".to_string(),
3101                Bytes::from("0x0badbabe"),
3102            )]),
3103            deleted_attributes: HashSet::from(["Attribute1".to_string()]),
3104        };
3105
3106        delta1.merge(&delta2);
3107
3108        assert_eq!(delta1, exp);
3109    }
3110
3111    #[test]
3112    fn test_protocol_state_delta_merge_delete_update() {
3113        // Initialize ProtocolStateDelta instances
3114        let mut delta1 = ProtocolStateDelta {
3115            component_id: "Component1".to_string(),
3116            updated_attributes: HashMap::new(),
3117            deleted_attributes: HashSet::from(["Attribute1".to_string()]),
3118        };
3119        let delta2 = ProtocolStateDelta {
3120            component_id: "Component1".to_string(),
3121            updated_attributes: HashMap::from([(
3122                "Attribute1".to_string(),
3123                Bytes::from("0x0badbabe"),
3124            )]),
3125            deleted_attributes: HashSet::new(),
3126        };
3127        let exp = ProtocolStateDelta {
3128            component_id: "Component1".to_string(),
3129            updated_attributes: HashMap::from([(
3130                "Attribute1".to_string(),
3131                Bytes::from("0x0badbabe"),
3132            )]),
3133            deleted_attributes: HashSet::new(),
3134        };
3135
3136        delta1.merge(&delta2);
3137
3138        assert_eq!(delta1, exp);
3139    }
3140
3141    #[test]
3142    fn test_account_update_merge() {
3143        // Initialize AccountUpdate instances with same address and valid hex strings for Bytes
3144        let mut account1 = AccountUpdate::new(
3145            Bytes::from(b"0x1234"),
3146            Chain::Ethereum,
3147            HashMap::from([(Bytes::from("0xaabb"), Bytes::from("0xccdd"))]),
3148            Some(Bytes::from("0x1000")),
3149            Some(Bytes::from("0xdeadbeaf")),
3150            ChangeType::Creation,
3151        );
3152
3153        let account2 = AccountUpdate::new(
3154            Bytes::from(b"0x1234"), // Same id as account1
3155            Chain::Ethereum,
3156            HashMap::from([(Bytes::from("0xeeff"), Bytes::from("0x11223344"))]),
3157            Some(Bytes::from("0x2000")),
3158            Some(Bytes::from("0xcafebabe")),
3159            ChangeType::Update,
3160        );
3161
3162        // Merge account2 into account1
3163        account1.merge(&account2);
3164
3165        // Define the expected state after merge
3166        let expected = AccountUpdate::new(
3167            Bytes::from(b"0x1234"), // Same id as before the merge
3168            Chain::Ethereum,
3169            HashMap::from([
3170                (Bytes::from("0xaabb"), Bytes::from("0xccdd")), // Original slot from account1
3171                (Bytes::from("0xeeff"), Bytes::from("0x11223344")), // New slot from account2
3172            ]),
3173            Some(Bytes::from("0x2000")),     // Updated balance
3174            Some(Bytes::from("0xcafebabe")), // Updated code
3175            ChangeType::Creation,            // Updated change type
3176        );
3177
3178        // Assert the new account1 equals to the expected state
3179        assert_eq!(account1, expected);
3180    }
3181
3182    #[test]
3183    fn test_account_update_merge_keeps_code_and_balance_when_other_carries_none() {
3184        let mut creation = AccountUpdate::new(
3185            Bytes::from(b"0x1234"),
3186            Chain::Ethereum,
3187            HashMap::from([(Bytes::from("0xaabb"), Bytes::from("0xccdd"))]),
3188            Some(Bytes::from("0x1000")),
3189            Some(Bytes::from("0xdeadbeaf")),
3190            ChangeType::Creation,
3191        );
3192
3193        // Storage-only delta for the same account, as produced during catch-up reduce(merge).
3194        let storage_only_update = AccountUpdate::new(
3195            Bytes::from(b"0x1234"),
3196            Chain::Ethereum,
3197            HashMap::from([(Bytes::from("0xeeff"), Bytes::from("0x11223344"))]),
3198            None,
3199            None,
3200            ChangeType::Update,
3201        );
3202
3203        creation.merge(&storage_only_update);
3204
3205        assert_eq!(creation.change, ChangeType::Creation);
3206        assert_eq!(creation.code, Some(Bytes::from("0xdeadbeaf")));
3207        assert_eq!(creation.balance, Some(Bytes::from("0x1000")));
3208        assert_eq!(
3209            creation.slots,
3210            HashMap::from([
3211                (Bytes::from("0xaabb"), Bytes::from("0xccdd")),
3212                (Bytes::from("0xeeff"), Bytes::from("0x11223344")),
3213            ])
3214        );
3215    }
3216
3217    #[test]
3218    fn test_block_account_changes_merge() {
3219        // Prepare account updates
3220        let old_account_updates: HashMap<Bytes, AccountUpdate> = [(
3221            Bytes::from("0x0011"),
3222            AccountUpdate {
3223                address: Bytes::from("0x00"),
3224                chain: Chain::Ethereum,
3225                slots: HashMap::from([(Bytes::from("0x0022"), Bytes::from("0x0033"))]),
3226                balance: Some(Bytes::from("0x01")),
3227                code: Some(Bytes::from("0x02")),
3228                change: ChangeType::Creation,
3229            },
3230        )]
3231        .into_iter()
3232        .collect();
3233        let new_account_updates: HashMap<Bytes, AccountUpdate> = [(
3234            Bytes::from("0x0011"),
3235            AccountUpdate {
3236                address: Bytes::from("0x00"),
3237                chain: Chain::Ethereum,
3238                slots: HashMap::from([(Bytes::from("0x0044"), Bytes::from("0x0055"))]),
3239                balance: Some(Bytes::from("0x03")),
3240                code: Some(Bytes::from("0x04")),
3241                change: ChangeType::Update,
3242            },
3243        )]
3244        .into_iter()
3245        .collect();
3246        // Create initial and new BlockAccountChanges instances
3247        let block_account_changes_initial = BlockAggregatedChanges {
3248            extractor: "extractor1".to_string(),
3249            revert: false,
3250            account_updates: old_account_updates,
3251            ..Default::default()
3252        };
3253
3254        let block_account_changes_new = BlockAggregatedChanges {
3255            extractor: "extractor2".to_string(),
3256            revert: true,
3257            account_updates: new_account_updates,
3258            ..Default::default()
3259        };
3260
3261        // Merge the new BlockAggregatedChanges into the initial one
3262        let res = block_account_changes_initial.merge(block_account_changes_new);
3263
3264        // Create the expected result of the merge operation
3265        let expected_account_updates: HashMap<Bytes, AccountUpdate> = [(
3266            Bytes::from("0x0011"),
3267            AccountUpdate {
3268                address: Bytes::from("0x00"),
3269                chain: Chain::Ethereum,
3270                slots: HashMap::from([
3271                    (Bytes::from("0x0044"), Bytes::from("0x0055")),
3272                    (Bytes::from("0x0022"), Bytes::from("0x0033")),
3273                ]),
3274                balance: Some(Bytes::from("0x03")),
3275                code: Some(Bytes::from("0x04")),
3276                change: ChangeType::Creation,
3277            },
3278        )]
3279        .into_iter()
3280        .collect();
3281        let block_account_changes_expected = BlockAggregatedChanges {
3282            extractor: "extractor1".to_string(),
3283            revert: true,
3284            account_updates: expected_account_updates,
3285            ..Default::default()
3286        };
3287        assert_eq!(res, block_account_changes_expected);
3288    }
3289
3290    #[test]
3291    fn test_block_entity_changes_merge() {
3292        // Initialize two BlockAggregatedChanges instances with different details
3293        let block_entity_changes_result1 = BlockAggregatedChanges {
3294            extractor: String::from("extractor1"),
3295            revert: false,
3296            state_updates: hashmap! { "state1".to_string() => ProtocolStateDelta::default() },
3297            new_protocol_components: hashmap! { "component1".to_string() => ProtocolComponent::default() },
3298            deleted_protocol_components: HashMap::new(),
3299            component_balances: hashmap! {
3300                "component1".to_string() => TokenBalances(hashmap! {
3301                    Bytes::from("0x01") => ComponentBalance {
3302                            token: Bytes::from("0x01"),
3303                            balance: Bytes::from("0x01"),
3304                            balance_float: 1.0,
3305                            modify_tx: Bytes::from("0x00"),
3306                            component_id: "component1".to_string()
3307                        },
3308                    Bytes::from("0x02") => ComponentBalance {
3309                        token: Bytes::from("0x02"),
3310                        balance: Bytes::from("0x02"),
3311                        balance_float: 2.0,
3312                        modify_tx: Bytes::from("0x00"),
3313                        component_id: "component1".to_string()
3314                    },
3315                })
3316
3317            },
3318            component_tvl: hashmap! { "tvl1".to_string() => 1000.0 },
3319            ..Default::default()
3320        };
3321        let block_entity_changes_result2 = BlockAggregatedChanges {
3322            extractor: String::from("extractor2"),
3323            revert: true,
3324            state_updates: hashmap! { "state2".to_string() => ProtocolStateDelta::default() },
3325            new_protocol_components: hashmap! { "component2".to_string() => ProtocolComponent::default() },
3326            deleted_protocol_components: hashmap! { "component3".to_string() => ProtocolComponent::default() },
3327            component_balances: hashmap! {
3328                "component1".to_string() => TokenBalances::default(),
3329                "component2".to_string() => TokenBalances::default()
3330            },
3331            component_tvl: hashmap! { "tvl2".to_string() => 2000.0 },
3332            ..Default::default()
3333        };
3334
3335        let res = block_entity_changes_result1.merge(block_entity_changes_result2);
3336
3337        let expected_block_entity_changes_result = BlockAggregatedChanges {
3338            extractor: String::from("extractor1"),
3339            revert: true,
3340            state_updates: hashmap! {
3341                "state1".to_string() => ProtocolStateDelta::default(),
3342                "state2".to_string() => ProtocolStateDelta::default(),
3343            },
3344            new_protocol_components: hashmap! {
3345                "component1".to_string() => ProtocolComponent::default(),
3346                "component2".to_string() => ProtocolComponent::default(),
3347            },
3348            deleted_protocol_components: hashmap! {
3349                "component3".to_string() => ProtocolComponent::default(),
3350            },
3351            component_balances: hashmap! {
3352                "component1".to_string() => TokenBalances(hashmap! {
3353                    Bytes::from("0x01") => ComponentBalance {
3354                            token: Bytes::from("0x01"),
3355                            balance: Bytes::from("0x01"),
3356                            balance_float: 1.0,
3357                            modify_tx: Bytes::from("0x00"),
3358                            component_id: "component1".to_string()
3359                        },
3360                    Bytes::from("0x02") => ComponentBalance {
3361                        token: Bytes::from("0x02"),
3362                        balance: Bytes::from("0x02"),
3363                        balance_float: 2.0,
3364                        modify_tx: Bytes::from("0x00"),
3365                        component_id: "component1".to_string()
3366                        },
3367                    }),
3368                "component2".to_string() => TokenBalances::default(),
3369            },
3370            component_tvl: hashmap! {
3371                "tvl1".to_string() => 1000.0,
3372                "tvl2".to_string() => 2000.0
3373            },
3374            ..Default::default()
3375        };
3376
3377        assert_eq!(res, expected_block_entity_changes_result);
3378    }
3379
3380    #[test]
3381    fn test_websocket_error_serialization() {
3382        let extractor_id = ExtractorIdentity::new(Chain::Ethereum, "test_extractor");
3383        let subscription_id = Uuid::new_v4();
3384
3385        // Test ExtractorNotFound serialization
3386        let error = WebsocketError::ExtractorNotFound(extractor_id.clone());
3387        let json = serde_json::to_string(&error).unwrap();
3388        let deserialized: WebsocketError = serde_json::from_str(&json).unwrap();
3389        assert_eq!(error, deserialized);
3390
3391        // Test SubscriptionNotFound serialization
3392        let error = WebsocketError::SubscriptionNotFound(subscription_id);
3393        let json = serde_json::to_string(&error).unwrap();
3394        let deserialized: WebsocketError = serde_json::from_str(&json).unwrap();
3395        assert_eq!(error, deserialized);
3396
3397        // Test ParseError serialization
3398        let error = WebsocketError::ParseError("{asd".to_string(), "invalid json".to_string());
3399        let json = serde_json::to_string(&error).unwrap();
3400        let deserialized: WebsocketError = serde_json::from_str(&json).unwrap();
3401        assert_eq!(error, deserialized);
3402
3403        // Test SubscribeError serialization
3404        let error = WebsocketError::SubscribeError(extractor_id);
3405        let json = serde_json::to_string(&error).unwrap();
3406        let deserialized: WebsocketError = serde_json::from_str(&json).unwrap();
3407        assert_eq!(error, deserialized);
3408
3409        // Test CompressionError serialization
3410        let error =
3411            WebsocketError::CompressionError(subscription_id, "Compression failed".to_string());
3412        let json = serde_json::to_string(&error).unwrap();
3413        let deserialized: WebsocketError = serde_json::from_str(&json).unwrap();
3414        assert_eq!(error, deserialized);
3415    }
3416
3417    #[test]
3418    fn test_websocket_message_with_error_response() {
3419        let error =
3420            WebsocketError::ParseError("}asdfas".to_string(), "malformed request".to_string());
3421        let response = Response::Error(error.clone());
3422        let message = WebSocketMessage::Response(response);
3423
3424        let json = serde_json::to_string(&message).unwrap();
3425        let deserialized: WebSocketMessage = serde_json::from_str(&json).unwrap();
3426
3427        if let WebSocketMessage::Response(Response::Error(deserialized_error)) = deserialized {
3428            assert_eq!(error, deserialized_error);
3429        } else {
3430            panic!("Expected WebSocketMessage::Response(Response::Error)");
3431        }
3432    }
3433
3434    #[test]
3435    fn test_websocket_error_conversion_from_models() {
3436        use crate::models::error;
3437
3438        let extractor_id =
3439            crate::models::ExtractorIdentity::new(crate::models::Chain::Ethereum, "test");
3440        let subscription_id = Uuid::new_v4();
3441
3442        // Test ExtractorNotFound conversion
3443        let models_error = error::WebsocketError::ExtractorNotFound(extractor_id.clone());
3444        let dto_error: WebsocketError = models_error.into();
3445        assert_eq!(dto_error, WebsocketError::ExtractorNotFound(extractor_id.clone().into()));
3446
3447        // Test SubscriptionNotFound conversion
3448        let models_error = error::WebsocketError::SubscriptionNotFound(subscription_id);
3449        let dto_error: WebsocketError = models_error.into();
3450        assert_eq!(dto_error, WebsocketError::SubscriptionNotFound(subscription_id));
3451
3452        // Test ParseError conversion - create a real JSON parse error
3453        let json_result: Result<serde_json::Value, _> = serde_json::from_str("{invalid json");
3454        let json_error = json_result.unwrap_err();
3455        let models_error =
3456            error::WebsocketError::ParseError("{invalid json".to_string(), json_error);
3457        let dto_error: WebsocketError = models_error.into();
3458        if let WebsocketError::ParseError(msg, error_msg) = dto_error {
3459            // Just check that we have a non-empty error message
3460            assert!(!error_msg.is_empty(), "Error message should not be empty, got: '{}'", msg);
3461        } else {
3462            panic!("Expected ParseError variant");
3463        }
3464
3465        // Test SubscribeError conversion
3466        let models_error = error::WebsocketError::SubscribeError(extractor_id.clone());
3467        let dto_error: WebsocketError = models_error.into();
3468        assert_eq!(dto_error, WebsocketError::SubscribeError(extractor_id.into()));
3469
3470        // Test CompressionError conversion
3471        let io_error = std::io::Error::other("Compression failed");
3472        let models_error = error::WebsocketError::CompressionError(subscription_id, io_error);
3473        let dto_error: WebsocketError = models_error.into();
3474        if let WebsocketError::CompressionError(sub_id, msg) = &dto_error {
3475            assert_eq!(*sub_id, subscription_id);
3476            assert!(msg.contains("Compression failed"));
3477        } else {
3478            panic!("Expected CompressionError variant");
3479        }
3480    }
3481}
3482
3483#[cfg(test)]
3484mod memory_size_tests {
3485    use std::collections::HashMap;
3486
3487    use super::*;
3488
3489    #[test]
3490    fn test_state_request_response_memory_size_empty() {
3491        let response = StateRequestResponse {
3492            accounts: vec![],
3493            pagination: PaginationResponse::new(1, 10, 0),
3494        };
3495
3496        let size = response.deep_size_of();
3497
3498        // Should at least include base struct sizes
3499        assert!(size >= 48, "Empty response should have minimum size of 48 bytes, got {}", size);
3500        assert!(size < 200, "Empty response should not be too large, got {}", size);
3501    }
3502
3503    #[test]
3504    fn test_state_request_response_memory_size_scales_with_slots() {
3505        let create_response_with_slots = |slot_count: usize| {
3506            let mut slots = HashMap::new();
3507            for i in 0..slot_count {
3508                let key = vec![u8::try_from(i).unwrap(); 32]; // 32-byte key
3509                let value = vec![u8::try_from(i + 100).unwrap(); 32]; // 32-byte value
3510                slots.insert(key.into(), value.into());
3511            }
3512
3513            let account = ResponseAccount::new(
3514                Chain::Ethereum,
3515                vec![1; 20].into(),
3516                "Pool".to_string(),
3517                slots,
3518                vec![1; 32].into(),
3519                HashMap::new(),
3520                vec![].into(), // empty code
3521                vec![1; 32].into(),
3522                vec![1; 32].into(),
3523                vec![1; 32].into(),
3524                None,
3525            );
3526
3527            StateRequestResponse {
3528                accounts: vec![account],
3529                pagination: PaginationResponse::new(1, 10, 1),
3530            }
3531        };
3532
3533        let small_response = create_response_with_slots(10);
3534        let large_response = create_response_with_slots(100);
3535
3536        let small_size = small_response.deep_size_of();
3537        let large_size = large_response.deep_size_of();
3538
3539        // Large response should be significantly bigger
3540        assert!(
3541            large_size > small_size * 5,
3542            "Large response ({} bytes) should be much larger than small response ({} bytes)",
3543            large_size,
3544            small_size
3545        );
3546
3547        // Each slot should contribute at least 64 bytes (32 + 32 + overhead)
3548        let size_diff = large_size - small_size;
3549        let expected_min_diff = 90 * 64; // 90 additional slots * 64 bytes each
3550        assert!(
3551            size_diff > expected_min_diff,
3552            "Size difference ({} bytes) should reflect the additional slot data",
3553            size_diff
3554        );
3555    }
3556}
3557
3558#[cfg(test)]
3559mod pagination_limits_tests {
3560    use super::*;
3561
3562    // Test struct for pagination limits
3563    #[derive(Clone, Debug)]
3564    struct TestRequestBody {
3565        pagination: PaginationParams,
3566    }
3567
3568    // Implement pagination limits for test struct
3569    impl_pagination_limits!(TestRequestBody, compressed = 500, uncompressed = 50);
3570
3571    #[test]
3572    fn test_effective_max_page_size() {
3573        // Test effective max with compression enabled
3574        let max_size = TestRequestBody::effective_max_page_size(true);
3575        assert_eq!(max_size, 500, "Should return compressed limit when compression is enabled");
3576
3577        // Test effective max with compression disabled
3578        let max_size = TestRequestBody::effective_max_page_size(false);
3579        assert_eq!(max_size, 50, "Should return uncompressed limit when compression is disabled");
3580    }
3581}