Skip to main content

tycho_client/
rpc.rs

1//! # Tycho RPC Client
2//!
3//! The objective of this module is to provide swift and simplified access to the Remote Procedure
4//! Call (RPC) endpoints of Tycho. These endpoints are chiefly responsible for facilitating data
5//! queries, especially querying snapshots of data.
6use std::{
7    collections::HashMap,
8    sync::Arc,
9    time::{Duration, SystemTime},
10};
11
12use async_trait::async_trait;
13use backoff::{exponential::ExponentialBackoffBuilder, ExponentialBackoff};
14use futures03::future::try_join_all;
15#[cfg(test)]
16use mockall::automock;
17use reqwest::{header, Client, ClientBuilder, Response, StatusCode, Url};
18use serde::Serialize;
19use thiserror::Error;
20use time::{format_description::well_known::Rfc2822, OffsetDateTime};
21use tokio::{
22    sync::{RwLock, Semaphore},
23    time::sleep,
24};
25use tracing::{debug, error, instrument, trace, warn};
26use tycho_common::{
27    dto::{
28        ComponentTvlRequestBody, ComponentTvlRequestResponse, PaginationLimits, PaginationParams,
29        ProtocolComponentRequestResponse, ProtocolComponentsRequestBody, ProtocolStateRequestBody,
30        ProtocolStateRequestResponse, ProtocolSystemsRequestBody, ProtocolSystemsRequestResponse,
31        StateRequestBody, StateRequestResponse, TokensRequestBody, TokensRequestResponse,
32        TracedEntryPointRequestBody, TracedEntryPointRequestResponse, VersionParam,
33    },
34    models::{
35        blockchain::{EntryPointWithTracingParams, TracedEntryPoints, TracingResult},
36        contract::Account,
37        protocol::{ProtocolComponent, ProtocolComponentState},
38        token::Token,
39        Chain, ComponentId,
40    },
41    Bytes,
42};
43
44/// Data payload returned by `RPCClient::get_protocol_systems`.
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct ProtocolSystems {
47    protocol_systems: Vec<String>,
48    dci_protocols: Vec<String>,
49}
50
51impl ProtocolSystems {
52    pub(crate) fn new(protocol_systems: Vec<String>, dci_protocols: Vec<String>) -> Self {
53        Self { protocol_systems, dci_protocols }
54    }
55
56    pub fn protocol_systems(&self) -> &[String] {
57        &self.protocol_systems
58    }
59
60    pub fn dci_protocols(&self) -> &[String] {
61        &self.dci_protocols
62    }
63}
64
65/// An RPC response page, bundling data with the pagination metadata the server returned.
66#[derive(Debug, Clone, PartialEq)]
67pub struct Page<T> {
68    data: T,
69    total: i64,
70    page: i64,
71    page_size: i64,
72}
73
74impl<T> Page<T> {
75    pub fn new(data: T, total: i64, page: i64, page_size: i64) -> Self {
76        Page { data, total, page, page_size }
77    }
78
79    pub fn data(&self) -> &T {
80        &self.data
81    }
82
83    pub fn into_data(self) -> T {
84        self.data
85    }
86
87    pub fn total(&self) -> i64 {
88        self.total
89    }
90
91    pub fn page(&self) -> i64 {
92        self.page
93    }
94
95    pub fn page_size(&self) -> i64 {
96        self.page_size
97    }
98}
99
100impl<T> Page<Vec<T>> {
101    pub fn len(&self) -> usize {
102        self.data.len()
103    }
104
105    pub fn is_empty(&self) -> bool {
106        self.data.is_empty()
107    }
108}
109
110impl<K, V, S: std::hash::BuildHasher> Page<HashMap<K, V, S>> {
111    pub fn len(&self) -> usize {
112        self.data.len()
113    }
114
115    pub fn is_empty(&self) -> bool {
116        self.data.is_empty()
117    }
118}
119
120impl<T: IntoIterator> IntoIterator for Page<T> {
121    type Item = T::Item;
122    type IntoIter = T::IntoIter;
123
124    fn into_iter(self) -> Self::IntoIter {
125        self.data.into_iter()
126    }
127}
128
129impl<'a, T> IntoIterator for &'a Page<T>
130where
131    &'a T: IntoIterator,
132{
133    type Item = <&'a T as IntoIterator>::Item;
134    type IntoIter = <&'a T as IntoIterator>::IntoIter;
135
136    fn into_iter(self) -> Self::IntoIter {
137        (&self.data).into_iter()
138    }
139}
140
141use crate::{
142    client_metadata::CLIENT_METADATA_HEADER,
143    feed::synchronizer::{ComponentWithState, Snapshot},
144    TYCHO_SERVER_VERSION,
145};
146
147/// Suggested concurrency level for RPC clients.
148pub const RPC_CLIENT_CONCURRENCY: usize = 4;
149
150/// Default number of contracts per `/contract_state` request. A few large contracts can hold most
151/// of a protocol's storage, so smaller requests let the server build them and the client decode
152/// them in parallel.
153pub const CONTRACT_STATE_CHUNK_SIZE: usize = 100;
154
155/// Parameters for [`RPCClient::get_contract_state`].
156#[derive(Clone, PartialEq, Debug)]
157pub struct ContractStateParams {
158    chain: Chain,
159    protocol_system: String,
160    contract_ids: Option<Vec<Bytes>>,
161    version: VersionParam,
162    page: i64,
163    page_size: i64,
164}
165
166impl ContractStateParams {
167    pub fn new(chain: Chain, protocol_system: impl Into<String>) -> Self {
168        Self {
169            chain,
170            protocol_system: protocol_system.into(),
171            contract_ids: None,
172            version: VersionParam::default(),
173            page: 0,
174            page_size: StateRequestBody::MAX_PAGE_SIZE_COMPRESSED,
175        }
176    }
177
178    pub fn with_contract_ids(mut self, ids: Vec<Bytes>) -> Self {
179        self.contract_ids = Some(ids);
180        self
181    }
182
183    pub fn with_version(mut self, version: VersionParam) -> Self {
184        self.version = version;
185        self
186    }
187
188    pub fn with_block_number(mut self, block_number: u64) -> Self {
189        self.version = VersionParam::at_block(self.chain.into(), block_number);
190        self
191    }
192
193    pub(crate) fn with_pagination(mut self, page: i64, page_size: i64) -> Self {
194        self.page = page;
195        self.page_size = page_size;
196        self
197    }
198}
199
200/// Parameters for [`RPCClient::get_protocol_components`].
201#[derive(Clone, PartialEq, Debug)]
202pub struct ProtocolComponentsParams {
203    chain: Chain,
204    protocol_system: String,
205    component_ids: Option<Vec<ComponentId>>,
206    tvl_gt: Option<f64>,
207    page: i64,
208    page_size: i64,
209}
210
211impl ProtocolComponentsParams {
212    pub fn new(chain: Chain, protocol_system: impl Into<String>) -> Self {
213        Self {
214            chain,
215            protocol_system: protocol_system.into(),
216            component_ids: None,
217            tvl_gt: None,
218            page: 0,
219            page_size: ProtocolComponentsRequestBody::MAX_PAGE_SIZE_COMPRESSED,
220        }
221    }
222
223    pub fn with_component_ids(mut self, ids: Vec<ComponentId>) -> Self {
224        self.component_ids = Some(ids);
225        self
226    }
227
228    pub fn with_tvl_gt(mut self, tvl_gt: f64) -> Self {
229        self.tvl_gt = Some(tvl_gt);
230        self
231    }
232
233    pub(crate) fn with_pagination(mut self, page: i64, page_size: i64) -> Self {
234        self.page = page;
235        self.page_size = page_size;
236        self
237    }
238
239    #[cfg(test)]
240    pub(crate) fn component_ids(&self) -> Option<&Vec<ComponentId>> {
241        self.component_ids.as_ref()
242    }
243}
244
245/// Parameters for [`RPCClient::get_protocol_states`].
246#[derive(Clone, PartialEq, Debug)]
247pub struct ProtocolStatesParams {
248    chain: Chain,
249    protocol_system: String,
250    protocol_ids: Option<Vec<String>>,
251    include_balances: bool,
252    version: VersionParam,
253    page: i64,
254    page_size: i64,
255}
256
257impl ProtocolStatesParams {
258    pub fn new(chain: Chain, protocol_system: impl Into<String>) -> Self {
259        Self {
260            chain,
261            protocol_system: protocol_system.into(),
262            protocol_ids: None,
263            include_balances: false,
264            version: VersionParam::default(),
265            page: 0,
266            page_size: ProtocolStateRequestBody::MAX_PAGE_SIZE_COMPRESSED,
267        }
268    }
269
270    pub fn with_protocol_ids(mut self, ids: Vec<String>) -> Self {
271        self.protocol_ids = Some(ids);
272        self
273    }
274
275    pub fn with_include_balances(mut self, include_balances: bool) -> Self {
276        self.include_balances = include_balances;
277        self
278    }
279
280    pub fn with_version(mut self, version: VersionParam) -> Self {
281        self.version = version;
282        self
283    }
284
285    pub fn with_block_number(mut self, block_number: u64) -> Self {
286        self.version = VersionParam::at_block(self.chain.into(), block_number);
287        self
288    }
289
290    pub(crate) fn with_pagination(mut self, page: i64, page_size: i64) -> Self {
291        self.page = page;
292        self.page_size = page_size;
293        self
294    }
295}
296
297/// Parameters for [`RPCClient::get_tokens`].
298#[derive(Clone, PartialEq, Debug)]
299pub struct TokensParams {
300    chain: Chain,
301    token_addresses: Option<Vec<Bytes>>,
302    min_quality: Option<i32>,
303    traded_n_days_ago: Option<u64>,
304    page: i64,
305    page_size: i64,
306}
307
308impl TokensParams {
309    pub fn new(chain: Chain) -> Self {
310        Self {
311            chain,
312            token_addresses: None,
313            min_quality: None,
314            traded_n_days_ago: None,
315            page: 0,
316            page_size: TokensRequestBody::MAX_PAGE_SIZE_COMPRESSED,
317        }
318    }
319
320    /// Returns only the tokens at `addresses`. An address Tycho does not know is left out of the
321    /// response; an empty list returns no tokens.
322    pub fn with_token_addresses(mut self, addresses: Vec<Bytes>) -> Self {
323        self.token_addresses = Some(addresses);
324        self
325    }
326
327    pub fn with_min_quality(mut self, min_quality: i32) -> Self {
328        self.min_quality = Some(min_quality);
329        self
330    }
331
332    pub fn with_traded_n_days_ago(mut self, days: u64) -> Self {
333        self.traded_n_days_ago = Some(days);
334        self
335    }
336
337    pub(crate) fn with_pagination(mut self, page: i64, page_size: i64) -> Self {
338        self.page = page;
339        self.page_size = page_size;
340        self
341    }
342}
343
344/// Parameters for [`RPCClient::get_protocol_systems`].
345#[derive(Clone, PartialEq, Debug)]
346pub struct ProtocolSystemsParams {
347    chain: Chain,
348    page: i64,
349    page_size: i64,
350}
351
352impl ProtocolSystemsParams {
353    pub fn new(chain: Chain) -> Self {
354        Self { chain, page: 0, page_size: ProtocolSystemsRequestBody::MAX_PAGE_SIZE_COMPRESSED }
355    }
356
357    pub(crate) fn with_pagination(mut self, page: i64, page_size: i64) -> Self {
358        self.page = page;
359        self.page_size = page_size;
360        self
361    }
362}
363
364/// Parameters for [`RPCClient::get_component_tvl`].
365#[derive(Clone, PartialEq, Debug)]
366pub struct ComponentTvlParams {
367    chain: Chain,
368    protocol_system: Option<String>,
369    component_ids: Option<Vec<String>>,
370    page: i64,
371    page_size: i64,
372}
373
374impl ComponentTvlParams {
375    pub fn new(chain: Chain) -> Self {
376        Self {
377            chain,
378            protocol_system: None,
379            component_ids: None,
380            page: 0,
381            page_size: ComponentTvlRequestBody::MAX_PAGE_SIZE_COMPRESSED,
382        }
383    }
384
385    pub fn with_protocol_system(mut self, protocol_system: impl Into<String>) -> Self {
386        self.protocol_system = Some(protocol_system.into());
387        self
388    }
389
390    pub fn with_component_ids(mut self, ids: Vec<String>) -> Self {
391        self.component_ids = Some(ids);
392        self
393    }
394
395    pub(crate) fn with_pagination(mut self, page: i64, page_size: i64) -> Self {
396        self.page = page;
397        self.page_size = page_size;
398        self
399    }
400}
401
402/// Parameters for [`RPCClient::get_traced_entry_points`].
403#[derive(Clone, PartialEq, Debug)]
404pub struct TracedEntryPointsParams {
405    chain: Chain,
406    protocol_system: String,
407    component_ids: Option<Vec<String>>,
408    page: i64,
409    page_size: i64,
410}
411
412impl TracedEntryPointsParams {
413    pub fn new(chain: Chain, protocol_system: impl Into<String>) -> Self {
414        Self {
415            chain,
416            protocol_system: protocol_system.into(),
417            component_ids: None,
418            page: 0,
419            page_size: TracedEntryPointRequestBody::MAX_PAGE_SIZE_COMPRESSED,
420        }
421    }
422
423    pub fn with_component_ids(mut self, ids: Vec<String>) -> Self {
424        self.component_ids = Some(ids);
425        self
426    }
427
428    pub(crate) fn with_pagination(mut self, page: i64, page_size: i64) -> Self {
429        self.page = page;
430        self.page_size = page_size;
431        self
432    }
433}
434
435/// Parameters for [`RPCClient::get_protocol_components_paginated`].
436#[derive(Clone, PartialEq, Debug)]
437pub struct ProtocolComponentsPaginatedParams {
438    chain: Chain,
439    protocol_system: String,
440    component_ids: Option<Vec<ComponentId>>,
441    tvl_gt: Option<f64>,
442    chunk_size: Option<usize>,
443    concurrency: usize,
444}
445
446impl ProtocolComponentsPaginatedParams {
447    pub fn new(chain: Chain, protocol_system: impl Into<String>, concurrency: usize) -> Self {
448        Self {
449            chain,
450            protocol_system: protocol_system.into(),
451            component_ids: None,
452            tvl_gt: None,
453            chunk_size: None,
454            concurrency,
455        }
456    }
457
458    pub fn with_component_ids(mut self, ids: Vec<ComponentId>) -> Self {
459        self.component_ids = Some(ids);
460        self
461    }
462
463    pub fn with_tvl_gt(mut self, tvl_gt: f64) -> Self {
464        self.tvl_gt = Some(tvl_gt);
465        self
466    }
467
468    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
469        self.chunk_size = Some(chunk_size);
470        self
471    }
472}
473
474/// Parameters for [`RPCClient::get_traced_entry_points_paginated`].
475#[derive(Clone, PartialEq, Debug)]
476pub struct TracedEntryPointsPaginatedParams {
477    chain: Chain,
478    protocol_system: String,
479    component_ids: Vec<String>,
480    chunk_size: Option<usize>,
481    concurrency: usize,
482}
483
484impl TracedEntryPointsPaginatedParams {
485    pub fn new(
486        chain: Chain,
487        protocol_system: impl Into<String>,
488        component_ids: Vec<String>,
489        concurrency: usize,
490    ) -> Self {
491        Self {
492            chain,
493            protocol_system: protocol_system.into(),
494            component_ids,
495            chunk_size: None,
496            concurrency,
497        }
498    }
499
500    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
501        self.chunk_size = Some(chunk_size);
502        self
503    }
504}
505
506/// Parameters for [`RPCClient::get_protocol_states_paginated`].
507#[derive(Clone, PartialEq, Debug)]
508pub struct ProtocolStatesPaginatedParams {
509    chain: Chain,
510    protocol_system: String,
511    protocol_ids: Vec<String>,
512    include_balances: bool,
513    version: VersionParam,
514    chunk_size: Option<usize>,
515    concurrency: usize,
516}
517
518impl ProtocolStatesPaginatedParams {
519    pub fn new(chain: Chain, protocol_system: impl Into<String>, concurrency: usize) -> Self {
520        Self {
521            chain,
522            protocol_system: protocol_system.into(),
523            protocol_ids: Vec::new(),
524            include_balances: true,
525            version: VersionParam::default(),
526            chunk_size: None,
527            concurrency,
528        }
529    }
530
531    pub fn with_protocol_ids(mut self, ids: Vec<String>) -> Self {
532        self.protocol_ids = ids;
533        self
534    }
535
536    pub fn with_include_balances(mut self, include_balances: bool) -> Self {
537        self.include_balances = include_balances;
538        self
539    }
540
541    pub fn with_version(mut self, version: VersionParam) -> Self {
542        self.version = version;
543        self
544    }
545
546    pub fn with_block_number(mut self, block_number: u64) -> Self {
547        self.version = VersionParam::at_block(self.chain.into(), block_number);
548        self
549    }
550
551    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
552        self.chunk_size = Some(chunk_size);
553        self
554    }
555}
556
557/// Parameters for [`RPCClient::get_all_tokens`].
558#[derive(Clone, PartialEq, Debug)]
559pub struct AllTokensParams {
560    chain: Chain,
561    min_quality: Option<i32>,
562    traded_n_days_ago: Option<u64>,
563    chunk_size: Option<usize>,
564    concurrency: usize,
565}
566
567impl AllTokensParams {
568    pub fn new(chain: Chain, concurrency: usize) -> Self {
569        Self { chain, min_quality: None, traded_n_days_ago: None, chunk_size: None, concurrency }
570    }
571
572    pub fn with_min_quality(mut self, min_quality: i32) -> Self {
573        self.min_quality = Some(min_quality);
574        self
575    }
576
577    pub fn with_traded_n_days_ago(mut self, days: u64) -> Self {
578        self.traded_n_days_ago = Some(days);
579        self
580    }
581
582    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
583        self.chunk_size = Some(chunk_size);
584        self
585    }
586}
587
588/// Parameters for [`RPCClient::get_component_tvl_paginated`].
589#[derive(Clone, PartialEq, Debug)]
590pub struct ComponentTvlPaginatedParams {
591    chain: Chain,
592    protocol_system: Option<String>,
593    component_ids: Option<Vec<String>>,
594    chunk_size: Option<usize>,
595    concurrency: usize,
596}
597
598impl ComponentTvlPaginatedParams {
599    pub fn new(chain: Chain, concurrency: usize) -> Self {
600        Self { chain, protocol_system: None, component_ids: None, chunk_size: None, concurrency }
601    }
602
603    pub fn with_protocol_system(mut self, protocol_system: impl Into<String>) -> Self {
604        self.protocol_system = Some(protocol_system.into());
605        self
606    }
607
608    pub fn with_component_ids(mut self, ids: Vec<String>) -> Self {
609        self.component_ids = Some(ids);
610        self
611    }
612
613    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
614        self.chunk_size = Some(chunk_size);
615        self
616    }
617}
618
619/// Parameters for [`RPCClient::get_contract_state_paginated`].
620#[derive(Clone, PartialEq, Debug)]
621pub struct ContractStatePaginatedParams {
622    chain: Chain,
623    protocol_system: String,
624    contract_ids: Vec<Bytes>,
625    version: VersionParam,
626    chunk_size: Option<usize>,
627    concurrency: usize,
628}
629
630impl ContractStatePaginatedParams {
631    pub fn new(chain: Chain, protocol_system: impl Into<String>, concurrency: usize) -> Self {
632        Self {
633            chain,
634            protocol_system: protocol_system.into(),
635            contract_ids: Vec::new(),
636            version: VersionParam::default(),
637            chunk_size: None,
638            concurrency,
639        }
640    }
641
642    pub fn with_contract_ids(mut self, ids: Vec<Bytes>) -> Self {
643        self.contract_ids = ids;
644        self
645    }
646
647    pub fn with_version(mut self, version: VersionParam) -> Self {
648        self.version = version;
649        self
650    }
651
652    pub fn with_block_number(mut self, block_number: u64) -> Self {
653        self.version = VersionParam::at_block(self.chain.into(), block_number);
654        self
655    }
656
657    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
658        self.chunk_size = Some(chunk_size);
659        self
660    }
661}
662
663/// Request body for fetching a snapshot of protocol states and VM storage.
664///
665/// This struct helps to coordinate fetching  multiple pieces of related data
666/// (protocol states, contract storage, TVL, entry points).
667#[derive(Clone, Debug, PartialEq)]
668pub struct SnapshotParameters<'a> {
669    /// Which chain to fetch snapshots for
670    pub chain: Chain,
671    /// Protocol system name, required for correct state resolution
672    pub protocol_system: &'a str,
673    /// Components to fetch protocol states for
674    pub components: &'a HashMap<ComponentId, ProtocolComponent>,
675    /// Traced entry points data mapped by component id (model types)
676    pub entrypoints: Option<&'a TracedEntryPoints>,
677    /// Contract addresses to fetch VM storage for
678    pub contract_ids: &'a [Bytes],
679    /// Block number for versioning
680    pub block_number: u64,
681    /// Whether to include balance information
682    pub include_balances: bool,
683    /// Whether to fetch TVL data
684    pub include_tvl: bool,
685}
686
687impl<'a> SnapshotParameters<'a> {
688    pub fn new(
689        chain: Chain,
690        protocol_system: &'a str,
691        components: &'a HashMap<ComponentId, ProtocolComponent>,
692        contract_ids: &'a [Bytes],
693        block_number: u64,
694    ) -> Self {
695        Self {
696            chain,
697            protocol_system,
698            components,
699            entrypoints: None,
700            contract_ids,
701            block_number,
702            include_balances: true,
703            include_tvl: true,
704        }
705    }
706
707    /// Set whether to include balance information (default: true)
708    pub fn include_balances(mut self, include_balances: bool) -> Self {
709        self.include_balances = include_balances;
710        self
711    }
712
713    /// Set whether to fetch TVL data (default: true)
714    pub fn include_tvl(mut self, include_tvl: bool) -> Self {
715        self.include_tvl = include_tvl;
716        self
717    }
718
719    pub fn entrypoints(mut self, entrypoints: &'a TracedEntryPoints) -> Self {
720        self.entrypoints = Some(entrypoints);
721        self
722    }
723}
724
725#[derive(Error, Debug)]
726pub enum RPCError {
727    /// The passed tycho url failed to parse.
728    #[error("Failed to parse URL: {0}. Error: {1}")]
729    UrlParsing(String, String),
730
731    /// The request data is not correctly formed.
732    #[error("Failed to format request: {0}")]
733    FormatRequest(String),
734
735    /// Errors forwarded from the HTTP protocol.
736    #[error("Unexpected HTTP client error: {0}")]
737    HttpClient(String, #[source] reqwest::Error),
738
739    /// The response from the server could not be parsed correctly.
740    #[error("Failed to parse response: {0}")]
741    ParseResponse(String),
742
743    /// The requested block is outside the server's retention window.
744    #[error("Snapshot block is stale: {0}")]
745    StaleBlock(String),
746
747    /// The requested extractor does not exist on the server.
748    #[error("Unknown extractor: {0}")]
749    UnknownExtractor(String),
750
751    /// Other fatal errors.
752    #[error("Fatal error: {0}")]
753    Fatal(String),
754
755    #[error("Rate limited until {0:?}")]
756    RateLimited(Option<SystemTime>),
757
758    #[error("Server unreachable: {0}")]
759    ServerUnreachable(String),
760}
761
762impl RPCError {
763    /// Converts an HTTP response body parse failure into the correct `RPCError`.
764    ///
765    /// The tycho server returns plain-text error messages (not JSON) when a requested block falls
766    /// outside its retention window. Detecting these here gives callers a typed signal to retry
767    /// with a more recent block rather than treating it as an unrecoverable parse failure.
768    ///
769    /// NOTE: The string matching below is coupled to the server's error message text. If those
770    /// messages change server-side this silently regresses to `ParseResponse`. Replace with a
771    /// structured error code if the server ever returns typed error responses.
772    fn from_parse_error(err: &serde_json::Error, body: &str) -> Self {
773        if body.contains("version is older than") || body.contains("Could not find Block") {
774            RPCError::StaleBlock(body.to_string())
775        } else if body.starts_with("Unknown extractor:") {
776            RPCError::UnknownExtractor(body.to_string())
777        } else {
778            RPCError::ParseResponse(format!("Error: {err}, Body: {body}"))
779        }
780    }
781}
782
783#[cfg_attr(test, automock)]
784#[async_trait]
785pub trait RPCClient: Send + Sync {
786    /// Returns whether compression is enabled for requests.
787    fn compression(&self) -> bool;
788
789    /// Retrieves a snapshot of contract state for the given contract addresses.
790    ///
791    /// `block_number` pins the query to a specific block; pass `None` to use the latest state.
792    async fn get_contract_state(
793        &self,
794        params: ContractStateParams,
795    ) -> Result<Page<Vec<Account>>, RPCError>;
796
797    /// Retrieves a snapshot of contract state for a set of contract IDs.
798    ///
799    /// If `chunk_size` is `None`, it defaults to [`CONTRACT_STATE_CHUNK_SIZE`], capped at the
800    /// maximum page size.
801    async fn get_contract_state_paginated(
802        &self,
803        params: ContractStatePaginatedParams,
804    ) -> Result<Vec<Account>, RPCError> {
805        let semaphore = Arc::new(Semaphore::new(params.concurrency));
806
807        // Sort the ids to maximize server-side cache hits
808        let mut sorted_ids = params.contract_ids;
809        sorted_ids.sort();
810
811        let (chunk_size, page_size) = resolve_chunk_size(
812            Some(
813                params
814                    .chunk_size
815                    .unwrap_or(CONTRACT_STATE_CHUNK_SIZE),
816            ),
817            StateRequestBody::effective_max_page_size(self.compression()),
818        );
819
820        let mut tasks = Vec::new();
821        for chunk in sorted_ids.chunks(chunk_size) {
822            let sem = semaphore.clone();
823            let base_params =
824                ContractStateParams::new(params.chain, params.protocol_system.as_str())
825                    .with_contract_ids(chunk.to_vec())
826                    .with_version(params.version.clone())
827                    .with_pagination(0, page_size);
828            tasks.push(async move {
829                let _permit = sem
830                    .acquire()
831                    .await
832                    .map_err(|_closed| RPCError::Fatal("Semaphore dropped".to_string()))?;
833                self.get_contract_state(base_params)
834                    .await
835            });
836        }
837
838        let pages = try_join_all(tasks).await?;
839
840        let accounts = pages
841            .into_iter()
842            .flat_map(|p| p.into_iter())
843            .collect();
844
845        Ok(accounts)
846    }
847
848    /// Retrieves protocol components matching the given filters.
849    ///
850    /// Pass `component_ids` to filter by specific IDs, or `tvl_gt` to filter by minimum TVL.
851    async fn get_protocol_components(
852        &self,
853        params: ProtocolComponentsParams,
854    ) -> Result<Page<Vec<ProtocolComponent>>, RPCError>;
855
856    /// Retrieves protocol components, fetching all pages automatically.
857    ///
858    /// If `chunk_size` is `None`, it defaults to the maximum page size.
859    async fn get_protocol_components_paginated(
860        &self,
861        params: ProtocolComponentsPaginatedParams,
862    ) -> Result<Vec<ProtocolComponent>, RPCError> {
863        let chain = params.chain;
864        let protocol_system = params.protocol_system;
865        let component_ids = params.component_ids;
866        let tvl_gt = params.tvl_gt;
867        let chunk_size = params.chunk_size;
868        let concurrency = params.concurrency;
869
870        let semaphore = Arc::new(Semaphore::new(concurrency));
871
872        let (chunk_size, page_size) = resolve_chunk_size(
873            chunk_size,
874            ProtocolComponentsRequestBody::effective_max_page_size(self.compression()),
875        );
876
877        // If a set of component IDs is specified, the maximum return size is already known,
878        // allowing us to pre-compute the number of requests to be made.
879        match component_ids {
880            Some(ids) => {
881                // The server pages within the requested ids, so every chunk is page 0.
882                let tasks: Vec<_> = ids
883                    .chunks(chunk_size)
884                    .map(|chunk| {
885                        let sem = semaphore.clone();
886                        let mut base =
887                            ProtocolComponentsParams::new(chain, protocol_system.as_str())
888                                .with_component_ids(chunk.to_vec())
889                                .with_pagination(0, page_size);
890                        if let Some(tvl) = tvl_gt {
891                            base = base.with_tvl_gt(tvl);
892                        }
893                        async move {
894                            let _permit = sem.acquire().await.map_err(|_closed| {
895                                RPCError::Fatal("Semaphore dropped".to_string())
896                            })?;
897                            self.get_protocol_components(base).await
898                        }
899                    })
900                    .collect();
901
902                try_join_all(tasks)
903                    .await
904                    .map(|pages| pages.into_iter().flatten().collect())
905            }
906            None => {
907                // If no component ids are specified, we need to make requests based on the total
908                // number of results from the first response.
909                let mut base_params =
910                    ProtocolComponentsParams::new(chain, protocol_system.as_str())
911                        .with_pagination(0, page_size);
912                if let Some(tvl) = tvl_gt {
913                    base_params = base_params.with_tvl_gt(tvl);
914                }
915
916                let first_page = self
917                    .get_protocol_components(base_params)
918                    .await?;
919
920                let total_items = first_page.total();
921                let total_pages = total_pages(total_items, page_size);
922
923                let mut all: Vec<ProtocolComponent> = first_page.into_data();
924
925                let mut page = 1;
926                while page < total_pages {
927                    let requests_in_this_iteration =
928                        (total_pages - page).min(saturating_i64(concurrency));
929
930                    let tasks: Vec<_> = (0..requests_in_this_iteration)
931                        .map(|iter| {
932                            let sem = semaphore.clone();
933                            let mut p =
934                                ProtocolComponentsParams::new(chain, protocol_system.as_str())
935                                    .with_pagination(page + iter, page_size);
936                            if let Some(tvl) = tvl_gt {
937                                p = p.with_tvl_gt(tvl);
938                            }
939                            async move {
940                                let _permit = sem.acquire().await.map_err(|_closed| {
941                                    RPCError::Fatal("Semaphore dropped".to_string())
942                                })?;
943                                self.get_protocol_components(p).await
944                            }
945                        })
946                        .collect();
947
948                    let responses = try_join_all(tasks).await?;
949
950                    for resp in responses {
951                        all.extend(resp);
952                    }
953
954                    page += requests_in_this_iteration;
955                }
956                Ok(all)
957            }
958        }
959    }
960
961    /// Retrieves a page of protocol component states.
962    ///
963    /// `block_number` pins the query to a specific block; pass `None` to use the latest state.
964    async fn get_protocol_states(
965        &self,
966        params: ProtocolStatesParams,
967    ) -> Result<Page<Vec<ProtocolComponentState>>, RPCError>;
968
969    /// Retrieves protocol states for a set of protocol IDs, fetching all pages automatically.
970    ///
971    /// If `chunk_size` is `None`, it defaults to the maximum page size.
972    async fn get_protocol_states_paginated(
973        &self,
974        params: ProtocolStatesPaginatedParams,
975    ) -> Result<Vec<ProtocolComponentState>, RPCError> {
976        let semaphore = Arc::new(Semaphore::new(params.concurrency));
977
978        let (chunk_size, page_size) = resolve_chunk_size(
979            params.chunk_size,
980            ProtocolStateRequestBody::effective_max_page_size(self.compression()),
981        );
982
983        let tasks: Vec<_> = params
984            .protocol_ids
985            .chunks(chunk_size)
986            .map(|c| {
987                let sem = semaphore.clone();
988                let p = ProtocolStatesParams::new(params.chain, params.protocol_system.as_str())
989                    .with_protocol_ids(c.to_vec())
990                    .with_include_balances(params.include_balances)
991                    .with_version(params.version.clone())
992                    .with_pagination(0, page_size);
993                async move {
994                    let _permit = sem
995                        .acquire()
996                        .await
997                        .map_err(|_closed| RPCError::Fatal("Semaphore dropped".to_string()))?;
998                    self.get_protocol_states(p).await
999                }
1000            })
1001            .collect();
1002
1003        try_join_all(tasks)
1004            .await
1005            .map(|pages| pages.into_iter().flatten().collect())
1006    }
1007
1008    /// Retrieves a page of tokens.
1009    ///
1010    /// Use `get_all_tokens` to fetch all matching tokens automatically.
1011    async fn get_tokens(&self, params: TokensParams) -> Result<Page<Vec<Token>>, RPCError>;
1012
1013    /// Retrieves all tokens matching the given criteria, fetching all pages automatically.
1014    ///
1015    /// If `chunk_size` is `None`, it defaults to the maximum page size.
1016    async fn get_all_tokens(&self, params: AllTokensParams) -> Result<Vec<Token>, RPCError> {
1017        let (_, page_size) = resolve_chunk_size(
1018            params.chunk_size,
1019            TokensRequestBody::effective_max_page_size(self.compression()),
1020        );
1021
1022        let semaphore = Arc::new(Semaphore::new(params.concurrency));
1023
1024        let mut base_params = TokensParams::new(params.chain).with_pagination(0, page_size);
1025        if let Some(q) = params.min_quality {
1026            base_params = base_params.with_min_quality(q);
1027        }
1028        if let Some(d) = params.traded_n_days_ago {
1029            base_params = base_params.with_traded_n_days_ago(d);
1030        }
1031
1032        let first_page = self.get_tokens(base_params).await?;
1033        let total_pages = total_pages(first_page.total(), page_size);
1034
1035        let mut all_tokens: Vec<Token> = first_page.into_data();
1036
1037        if total_pages <= 1 {
1038            return Ok(all_tokens);
1039        }
1040
1041        let tasks: Vec<_> = (1..total_pages)
1042            .map(|page| {
1043                let sem = semaphore.clone();
1044                let mut p = TokensParams::new(params.chain).with_pagination(page, page_size);
1045                if let Some(q) = params.min_quality {
1046                    p = p.with_min_quality(q);
1047                }
1048                if let Some(d) = params.traded_n_days_ago {
1049                    p = p.with_traded_n_days_ago(d);
1050                }
1051                async move {
1052                    let _permit = sem
1053                        .acquire()
1054                        .await
1055                        .map_err(|_closed| RPCError::Fatal("Semaphore dropped".to_string()))?;
1056                    self.get_tokens(p).await
1057                }
1058            })
1059            .collect();
1060
1061        let pages = try_join_all(tasks).await?;
1062        for page in pages {
1063            all_tokens.extend(page);
1064        }
1065
1066        Ok(all_tokens)
1067    }
1068
1069    /// Retrieves the protocol systems known to the server.
1070    async fn get_protocol_systems(
1071        &self,
1072        params: ProtocolSystemsParams,
1073    ) -> Result<Page<ProtocolSystems>, RPCError>;
1074
1075    /// Retrieves component TVL values.
1076    ///
1077    /// Filter by `component_ids` or by `protocol_system`; both are optional.
1078    async fn get_component_tvl(
1079        &self,
1080        params: ComponentTvlParams,
1081    ) -> Result<Page<HashMap<String, f64>>, RPCError>;
1082
1083    /// Retrieves component TVL values, fetching all pages automatically.
1084    ///
1085    /// If `chunk_size` is `None`, it defaults to the maximum page size.
1086    async fn get_component_tvl_paginated(
1087        &self,
1088        params: ComponentTvlPaginatedParams,
1089    ) -> Result<HashMap<String, f64>, RPCError> {
1090        let semaphore = Arc::new(Semaphore::new(params.concurrency));
1091
1092        let (chunk_size, page_size) = resolve_chunk_size(
1093            params.chunk_size,
1094            ComponentTvlRequestBody::effective_max_page_size(self.compression()),
1095        );
1096
1097        match params.component_ids {
1098            Some(ids) => {
1099                // The server pages within the requested ids, so every chunk is page 0.
1100                let tasks: Vec<_> = ids
1101                    .chunks(chunk_size)
1102                    .map(|chunk| {
1103                        let sem = semaphore.clone();
1104                        let mut p = ComponentTvlParams::new(params.chain)
1105                            .with_component_ids(chunk.to_vec())
1106                            .with_pagination(0, page_size);
1107                        if let Some(ref ps) = params.protocol_system {
1108                            p = p.with_protocol_system(ps.as_str());
1109                        }
1110                        async move {
1111                            let _permit = sem.acquire().await.map_err(|_closed| {
1112                                RPCError::Fatal("Semaphore dropped".to_string())
1113                            })?;
1114                            self.get_component_tvl(p).await
1115                        }
1116                    })
1117                    .collect();
1118
1119                let pages = try_join_all(tasks).await?;
1120
1121                let mut merged_tvl = HashMap::new();
1122                for page in pages {
1123                    for (key, value) in page {
1124                        *merged_tvl.entry(key).or_insert(0.0) = value;
1125                    }
1126                }
1127
1128                Ok(merged_tvl)
1129            }
1130            None => {
1131                let mut base = ComponentTvlParams::new(params.chain).with_pagination(0, page_size);
1132                if let Some(ref ps) = params.protocol_system {
1133                    base = base.with_protocol_system(ps.as_str());
1134                }
1135
1136                let first_page = self.get_component_tvl(base).await?;
1137                let total_items = first_page.total();
1138                let total_pages = total_pages(total_items, page_size);
1139
1140                let mut merged_tvl: HashMap<String, f64> = first_page.into_data();
1141
1142                let mut page = 1;
1143                while page < total_pages {
1144                    let requests_in_this_iteration =
1145                        (total_pages - page).min(saturating_i64(params.concurrency));
1146
1147                    let tasks: Vec<_> = (0..requests_in_this_iteration)
1148                        .map(|i| {
1149                            let sem = semaphore.clone();
1150                            let mut p = ComponentTvlParams::new(params.chain)
1151                                .with_pagination(page + i, page_size);
1152                            if let Some(ref ps) = params.protocol_system {
1153                                p = p.with_protocol_system(ps.as_str());
1154                            }
1155                            async move {
1156                                let _permit = sem.acquire().await.map_err(|_closed| {
1157                                    RPCError::Fatal("Semaphore dropped".to_string())
1158                                })?;
1159                                self.get_component_tvl(p).await
1160                            }
1161                        })
1162                        .collect();
1163
1164                    let responses = try_join_all(tasks).await?;
1165
1166                    for resp in responses {
1167                        for (key, value) in resp {
1168                            *merged_tvl.entry(key).or_insert(0.0) = value;
1169                        }
1170                    }
1171
1172                    page += requests_in_this_iteration;
1173                }
1174
1175                Ok(merged_tvl)
1176            }
1177        }
1178    }
1179
1180    /// Retrieves a page of traced entry points.
1181    ///
1182    /// Use `get_traced_entry_points_paginated` to fetch all pages automatically.
1183    async fn get_traced_entry_points(
1184        &self,
1185        params: TracedEntryPointsParams,
1186    ) -> Result<Page<TracedEntryPoints>, RPCError>;
1187
1188    /// Retrieves traced entry points for a set of component IDs, fetching all pages automatically.
1189    ///
1190    /// If `chunk_size` is `None`, it defaults to the maximum page size.
1191    async fn get_traced_entry_points_paginated(
1192        &self,
1193        params: TracedEntryPointsPaginatedParams,
1194    ) -> Result<TracedEntryPoints, RPCError> {
1195        let chain = params.chain;
1196        let protocol_system = params.protocol_system;
1197        let component_ids = params.component_ids;
1198        let chunk_size = params.chunk_size;
1199        let concurrency = params.concurrency;
1200
1201        let semaphore = Arc::new(Semaphore::new(concurrency));
1202
1203        let (chunk_size, page_size) = resolve_chunk_size(
1204            chunk_size,
1205            TracedEntryPointRequestBody::effective_max_page_size(self.compression()),
1206        );
1207
1208        let tasks: Vec<_> = component_ids
1209            .chunks(chunk_size)
1210            .map(|c| {
1211                let sem = semaphore.clone();
1212                let params = TracedEntryPointsParams::new(chain, protocol_system.as_str())
1213                    .with_component_ids(c.to_vec())
1214                    .with_pagination(0, page_size);
1215                async move {
1216                    let _permit = sem
1217                        .acquire()
1218                        .await
1219                        .map_err(|_closed| RPCError::Fatal("Semaphore dropped".to_string()))?;
1220                    self.get_traced_entry_points(params)
1221                        .await
1222                }
1223            })
1224            .collect();
1225
1226        try_join_all(tasks)
1227            .await
1228            .map(|pages| pages.into_iter().flatten().collect())
1229    }
1230
1231    // clippy false positive: `'a` is required by the trait method signature and is
1232    // used in `SnapshotParameters<'a>`, but `async_trait` makes Clippy miss it.
1233    async fn get_snapshots<'a>(
1234        &self,
1235        request: &SnapshotParameters<'a>,
1236        chunk_size: Option<usize>,
1237        concurrency: usize,
1238    ) -> Result<Snapshot, RPCError>;
1239}
1240
1241/// Configuration options for HttpRPCClient
1242#[derive(Debug, Clone)]
1243pub struct HttpRPCClientOptions {
1244    /// Optional API key for authentication
1245    pub auth_key: Option<String>,
1246    /// Enable compression for requests (default: true)
1247    /// When enabled, adds Accept-Encoding: zstd header
1248    pub compression: bool,
1249    /// Pre-serialized `X-Tycho-Client-Metadata` header value. `None` sends no header.
1250    pub client_metadata_header: Option<String>,
1251}
1252
1253impl Default for HttpRPCClientOptions {
1254    fn default() -> Self {
1255        Self::new()
1256    }
1257}
1258
1259impl HttpRPCClientOptions {
1260    /// Create new options with default values (compression enabled)
1261    pub fn new() -> Self {
1262        Self { auth_key: None, compression: true, client_metadata_header: None }
1263    }
1264
1265    /// Set the authentication key
1266    pub fn with_auth_key(mut self, auth_key: Option<String>) -> Self {
1267        self.auth_key = auth_key;
1268        self
1269    }
1270
1271    /// Set whether to enable compression (default: true)
1272    pub fn with_compression(mut self, compression: bool) -> Self {
1273        self.compression = compression;
1274        self
1275    }
1276
1277    /// Set the pre-serialized client-metadata header value. `None` sends no header.
1278    pub fn with_client_metadata_header(mut self, header: Option<String>) -> Self {
1279        self.client_metadata_header = header;
1280        self
1281    }
1282}
1283
1284#[derive(Debug, Clone)]
1285pub struct HttpRPCClient {
1286    http_client: Client,
1287    url: Url,
1288    retry_after: Arc<RwLock<Option<SystemTime>>>,
1289    backoff_policy: ExponentialBackoff,
1290    server_restart_duration: Duration,
1291    compression: bool,
1292}
1293
1294impl HttpRPCClient {
1295    #[expect(
1296        clippy::needless_pass_by_value,
1297        reason = "public API; taking `&HttpRPCClientOptions` is a breaking change"
1298    )]
1299    pub fn new(base_uri: &str, options: HttpRPCClientOptions) -> Result<Self, RPCError> {
1300        let uri = base_uri
1301            .parse::<Url>()
1302            .map_err(|e| RPCError::UrlParsing(base_uri.to_string(), e.to_string()))?;
1303
1304        // Add default headers
1305        let mut headers = header::HeaderMap::new();
1306        headers.insert(header::CONTENT_TYPE, header::HeaderValue::from_static("application/json"));
1307        let user_agent = format!("tycho-client-{version}", version = env!("CARGO_PKG_VERSION"));
1308        headers.insert(
1309            header::USER_AGENT,
1310            header::HeaderValue::from_str(&user_agent)
1311                .map_err(|e| RPCError::FormatRequest(format!("Invalid user agent format: {e}")))?,
1312        );
1313
1314        // Add Authorization if one is given
1315        if let Some(key) = options.auth_key.as_deref() {
1316            let mut auth_value = header::HeaderValue::from_str(key).map_err(|e| {
1317                RPCError::FormatRequest(format!("Invalid authorization key format: {e}"))
1318            })?;
1319            auth_value.set_sensitive(true);
1320            headers.insert(header::AUTHORIZATION, auth_value);
1321        }
1322
1323        // Add generic client metadata if one is given. Pre-validated by the serializer, but mirror
1324        // the auth pattern so any residual formatting error surfaces as a FormatRequest error.
1325        if let Some(metadata) = options
1326            .client_metadata_header
1327            .as_deref()
1328        {
1329            let value = header::HeaderValue::from_str(metadata).map_err(|e| {
1330                RPCError::FormatRequest(format!("Invalid client metadata format: {e}"))
1331            })?;
1332            headers.insert(header::HeaderName::from_static(CLIENT_METADATA_HEADER), value);
1333        }
1334
1335        let mut client_builder = ClientBuilder::new()
1336            .default_headers(headers)
1337            .http2_prior_knowledge();
1338
1339        // When compression is disabled, turn off all automatic compression
1340        if !options.compression {
1341            client_builder = client_builder.no_zstd();
1342        }
1343
1344        let client = client_builder
1345            .build()
1346            .map_err(|e| RPCError::HttpClient(e.to_string(), e))?;
1347
1348        Ok(Self {
1349            http_client: client,
1350            url: uri,
1351            retry_after: Arc::new(RwLock::new(None)),
1352            backoff_policy: ExponentialBackoffBuilder::new()
1353                .with_initial_interval(Duration::from_millis(250))
1354                // increase backoff time by 75% each failure
1355                .with_multiplier(1.75)
1356                // keep retrying every 30s
1357                .with_max_interval(Duration::from_secs(30))
1358                // if all retries take longer than 2m, give up
1359                .with_max_elapsed_time(Some(Duration::from_secs(125)))
1360                .build(),
1361            server_restart_duration: Duration::from_secs(120),
1362            compression: options.compression,
1363        })
1364    }
1365
1366    #[cfg(test)]
1367    pub fn with_test_backoff_policy(mut self) -> Self {
1368        // Extremely short intervals for very fast testing
1369        self.backoff_policy = ExponentialBackoffBuilder::new()
1370            .with_initial_interval(Duration::from_millis(1))
1371            .with_multiplier(1.1)
1372            .with_max_interval(Duration::from_millis(5))
1373            .with_max_elapsed_time(Some(Duration::from_millis(50)))
1374            .build();
1375        self.server_restart_duration = Duration::from_millis(50);
1376        self
1377    }
1378
1379    /// Converts a error response to a Result.
1380    ///
1381    /// Raises an error if the response status code id 429, 502, 503 or 504. In the 429
1382    /// case it will try to look for a retry-after header an parse it accordingly. The
1383    /// parsed value is then passed as part of the error.
1384    async fn error_for_response(
1385        &self,
1386        response: reqwest::Response,
1387    ) -> Result<reqwest::Response, RPCError> {
1388        match response.status() {
1389            StatusCode::TOO_MANY_REQUESTS => {
1390                let retry_after_raw = response
1391                    .headers()
1392                    .get(reqwest::header::RETRY_AFTER)
1393                    .and_then(|h| h.to_str().ok())
1394                    .and_then(parse_retry_value);
1395
1396                let reason = response
1397                    .text()
1398                    .await
1399                    .unwrap_or_default();
1400                warn!(reason, retry_after = ?retry_after_raw, "Rate limited by server");
1401
1402                Err(RPCError::RateLimited(retry_after_raw))
1403            }
1404            StatusCode::BAD_GATEWAY |
1405            StatusCode::SERVICE_UNAVAILABLE |
1406            StatusCode::GATEWAY_TIMEOUT => Err(RPCError::ServerUnreachable(
1407                response
1408                    .text()
1409                    .await
1410                    .unwrap_or_else(|_| "Server Unreachable".to_string()),
1411            )),
1412            _ => Ok(response),
1413        }
1414    }
1415
1416    /// Classifies errors into transient or permanent ones.
1417    ///
1418    /// Transient errors are retried with a potential backoff, permanent ones are not.
1419    /// If the error is RateLimited, this method will set the self.retry_after value so
1420    /// future requests wait until the rate limit has been reset.
1421    async fn handle_error_for_backoff(&self, e: RPCError) -> backoff::Error<RPCError> {
1422        match e {
1423            RPCError::ServerUnreachable(_) => {
1424                backoff::Error::retry_after(e, self.server_restart_duration)
1425            }
1426            RPCError::RateLimited(Some(until)) => {
1427                let mut retry_after_guard = self.retry_after.write().await;
1428                *retry_after_guard = Some(
1429                    retry_after_guard
1430                        .unwrap_or(until)
1431                        .max(until),
1432                );
1433
1434                if let Ok(duration) = until.duration_since(SystemTime::now()) {
1435                    backoff::Error::retry_after(e, duration)
1436                } else {
1437                    e.into()
1438                }
1439            }
1440            RPCError::RateLimited(None) => e.into(),
1441            RPCError::UrlParsing(..) |
1442            RPCError::FormatRequest(_) |
1443            RPCError::HttpClient(..) |
1444            RPCError::ParseResponse(_) |
1445            RPCError::StaleBlock(_) |
1446            RPCError::UnknownExtractor(_) |
1447            RPCError::Fatal(_) => backoff::Error::permanent(e),
1448        }
1449    }
1450
1451    /// Waits until the current rate limit time has passed.
1452    ///
1453    /// Only waits if there is a time and that time is in the future, else return
1454    /// immediately.
1455    async fn wait_until_retry_after(&self) {
1456        if let Some(&until) = self.retry_after.read().await.as_ref() {
1457            let now = SystemTime::now();
1458            if until > now {
1459                if let Ok(duration) = until.duration_since(now) {
1460                    sleep(duration).await
1461                }
1462            }
1463        }
1464    }
1465
1466    /// Makes a post request handling transient failures.
1467    ///
1468    /// If a retry-after header is received it will be respected. Else the configured
1469    /// backoff policy is used to deal with transient network or server errors.
1470    async fn make_post_request<T: Serialize + ?Sized>(
1471        &self,
1472        request: &T,
1473        uri: &String,
1474    ) -> Result<Response, RPCError> {
1475        self.wait_until_retry_after().await;
1476        let response = backoff::future::retry(self.backoff_policy.clone(), || async {
1477            let server_response = self
1478                .http_client
1479                .post(uri)
1480                .json(request)
1481                .send()
1482                .await
1483                .map_err(|e| RPCError::HttpClient(e.to_string(), e))?;
1484
1485            match self
1486                .error_for_response(server_response)
1487                .await
1488            {
1489                Ok(response) => Ok(response),
1490                Err(e) => Err(self.handle_error_for_backoff(e).await),
1491            }
1492        })
1493        .await?;
1494        Ok(response)
1495    }
1496}
1497
1498/// Resolves a requested chunk size against the server's maximum page size. Returns the chunk size
1499/// as an item count and as the page size to put in a request; `None` selects the maximum.
1500fn resolve_chunk_size(requested: Option<usize>, max_page_size: i64) -> (usize, i64) {
1501    let max_chunk_size =
1502        usize::try_from(max_page_size).expect("maximum page sizes are positive constants");
1503    let chunk_size = requested
1504        .unwrap_or(max_chunk_size)
1505        .min(max_chunk_size);
1506    let page_size =
1507        i64::try_from(chunk_size).expect("the chunk size is clamped to the maximum page size");
1508    (chunk_size, page_size)
1509}
1510
1511/// Number of pages of `page_size` items needed to hold `total_items`.
1512fn total_pages(total_items: i64, page_size: i64) -> i64 {
1513    total_items / page_size + i64::from(total_items % page_size > 0)
1514}
1515
1516/// Converts a count to `i64`, clamping to `i64::MAX`.
1517fn saturating_i64(count: usize) -> i64 {
1518    i64::try_from(count).unwrap_or(i64::MAX)
1519}
1520
1521fn parse_retry_value(val: &str) -> Option<SystemTime> {
1522    if let Ok(secs) = val.parse::<u64>() {
1523        return Some(SystemTime::now() + Duration::from_secs(secs));
1524    }
1525    if let Ok(date) = OffsetDateTime::parse(val, &Rfc2822) {
1526        return Some(date.into());
1527    }
1528    None
1529}
1530
1531#[async_trait]
1532impl RPCClient for HttpRPCClient {
1533    fn compression(&self) -> bool {
1534        self.compression
1535    }
1536
1537    #[instrument(skip(self))]
1538    async fn get_contract_state(
1539        &self,
1540        params: ContractStateParams,
1541    ) -> Result<Page<Vec<Account>>, RPCError> {
1542        if params
1543            .contract_ids
1544            .as_ref()
1545            .is_none_or(|ids| ids.is_empty())
1546        {
1547            warn!("No contract ids specified in request.");
1548        }
1549
1550        let request = StateRequestBody {
1551            contract_ids: params.contract_ids,
1552            protocol_system: params.protocol_system,
1553            chain: params.chain.into(),
1554            version: params.version,
1555            pagination: PaginationParams { page: params.page, page_size: params.page_size },
1556        };
1557
1558        let uri = format!(
1559            "{}/{}/contract_state",
1560            self.url
1561                .to_string()
1562                .trim_end_matches('/'),
1563            TYCHO_SERVER_VERSION
1564        );
1565        debug!(%uri, "Sending contract_state request to Tycho server");
1566        trace!(?request, "Sending request to Tycho server");
1567        let response = self
1568            .make_post_request(&request, &uri)
1569            .await?;
1570        trace!(?response, "Received response from Tycho server");
1571
1572        let body = response
1573            .text()
1574            .await
1575            .map_err(|e| RPCError::ParseResponse(e.to_string()))?;
1576        if body.is_empty() {
1577            // Pure native protocols will return empty contract states
1578            return Ok(Page::new(vec![], 0, 0, 0));
1579        }
1580
1581        // Decoding a large response takes seconds of CPU: run it on the blocking pool, so the
1582        // runtime stays responsive and concurrent requests decode in parallel.
1583        let (data, pagination) = tokio::task::spawn_blocking(move || {
1584            let dto_response = serde_json::from_str::<StateRequestResponse>(&body)
1585                .map_err(|err| RPCError::from_parse_error(&err, &body))?;
1586            trace!(?dto_response, "Received contract_state response from Tycho server");
1587
1588            let data: Vec<Account> = dto_response
1589                .accounts
1590                .into_iter()
1591                .map(Account::from)
1592                .collect();
1593            Ok::<_, RPCError>((data, dto_response.pagination))
1594        })
1595        .await
1596        .map_err(|err| RPCError::Fatal(format!("Decoding contract_state failed: {err}")))??;
1597        Ok(Page::new(data, pagination.total, pagination.page, pagination.page_size))
1598    }
1599
1600    async fn get_protocol_components(
1601        &self,
1602        params: ProtocolComponentsParams,
1603    ) -> Result<Page<Vec<ProtocolComponent>>, RPCError> {
1604        let request = ProtocolComponentsRequestBody {
1605            protocol_system: params.protocol_system,
1606            component_ids: params.component_ids,
1607            tvl_gt: params.tvl_gt,
1608            chain: params.chain.into(),
1609            pagination: PaginationParams { page: params.page, page_size: params.page_size },
1610        };
1611
1612        let uri = format!(
1613            "{}/{}/protocol_components",
1614            self.url
1615                .to_string()
1616                .trim_end_matches('/'),
1617            TYCHO_SERVER_VERSION,
1618        );
1619        debug!(%uri, "Sending protocol_components request to Tycho server");
1620        trace!(?request, "Sending request to Tycho server");
1621
1622        let response = self
1623            .make_post_request(&request, &uri)
1624            .await?;
1625
1626        trace!(?response, "Received response from Tycho server");
1627
1628        let body = response
1629            .text()
1630            .await
1631            .map_err(|e| RPCError::ParseResponse(e.to_string()))?;
1632        let dto_response = serde_json::from_str::<ProtocolComponentRequestResponse>(&body)
1633            .map_err(|err| RPCError::from_parse_error(&err, &body))?;
1634        trace!(?dto_response, "Received protocol_components response from Tycho server");
1635
1636        let data: Vec<ProtocolComponent> = dto_response
1637            .protocol_components
1638            .into_iter()
1639            .map(ProtocolComponent::from)
1640            .collect();
1641        Ok(Page::new(
1642            data,
1643            dto_response.pagination.total,
1644            dto_response.pagination.page,
1645            dto_response.pagination.page_size,
1646        ))
1647    }
1648
1649    async fn get_protocol_states(
1650        &self,
1651        params: ProtocolStatesParams,
1652    ) -> Result<Page<Vec<ProtocolComponentState>>, RPCError> {
1653        if params
1654            .protocol_ids
1655            .as_ref()
1656            .is_none_or(|ids| ids.is_empty())
1657        {
1658            warn!("No protocol ids specified in request.");
1659        }
1660
1661        let request = ProtocolStateRequestBody {
1662            protocol_ids: params.protocol_ids,
1663            protocol_system: params.protocol_system,
1664            chain: params.chain.into(),
1665            include_balances: params.include_balances,
1666            version: params.version,
1667            pagination: PaginationParams { page: params.page, page_size: params.page_size },
1668        };
1669
1670        let uri = format!(
1671            "{}/{}/protocol_state",
1672            self.url
1673                .to_string()
1674                .trim_end_matches('/'),
1675            TYCHO_SERVER_VERSION
1676        );
1677        debug!(%uri, "Sending protocol_states request to Tycho server");
1678        trace!(?request, "Sending request to Tycho server");
1679
1680        let response = self
1681            .make_post_request(&request, &uri)
1682            .await?;
1683        trace!(?response, "Received response from Tycho server");
1684
1685        let body = response
1686            .text()
1687            .await
1688            .map_err(|e| RPCError::ParseResponse(e.to_string()))?;
1689
1690        if body.is_empty() {
1691            // Pure VM protocols will return empty states
1692            return Ok(Page::new(vec![], 0, 0, 0));
1693        }
1694
1695        let dto_response = serde_json::from_str::<ProtocolStateRequestResponse>(&body)
1696            .map_err(|err| RPCError::from_parse_error(&err, &body))?;
1697        trace!(?dto_response, "Received protocol_states response from Tycho server");
1698
1699        let data: Vec<ProtocolComponentState> = dto_response
1700            .states
1701            .into_iter()
1702            .map(ProtocolComponentState::from)
1703            .collect();
1704        Ok(Page::new(
1705            data,
1706            dto_response.pagination.total,
1707            dto_response.pagination.page,
1708            dto_response.pagination.page_size,
1709        ))
1710    }
1711
1712    async fn get_tokens(&self, params: TokensParams) -> Result<Page<Vec<Token>>, RPCError> {
1713        let request = TokensRequestBody {
1714            token_addresses: params.token_addresses,
1715            min_quality: params.min_quality,
1716            traded_n_days_ago: params.traded_n_days_ago,
1717            pagination: PaginationParams { page: params.page, page_size: params.page_size },
1718            chain: params.chain.into(),
1719        };
1720
1721        let uri = format!(
1722            "{}/{}/tokens",
1723            self.url
1724                .to_string()
1725                .trim_end_matches('/'),
1726            TYCHO_SERVER_VERSION
1727        );
1728        debug!(%uri, "Sending tokens request to Tycho server");
1729
1730        let response = self
1731            .make_post_request(&request, &uri)
1732            .await?;
1733
1734        let body = response
1735            .text()
1736            .await
1737            .map_err(|e| RPCError::ParseResponse(e.to_string()))?;
1738        let dto_response = serde_json::from_str::<TokensRequestResponse>(&body)
1739            .map_err(|err| RPCError::ParseResponse(format!("Error: {err}, Body: {body}")))?;
1740
1741        let data: Vec<Token> = dto_response
1742            .tokens
1743            .into_iter()
1744            .map(Token::from)
1745            .collect();
1746        Ok(Page::new(
1747            data,
1748            dto_response.pagination.total,
1749            dto_response.pagination.page,
1750            dto_response.pagination.page_size,
1751        ))
1752    }
1753
1754    async fn get_protocol_systems(
1755        &self,
1756        params: ProtocolSystemsParams,
1757    ) -> Result<Page<ProtocolSystems>, RPCError> {
1758        let request = ProtocolSystemsRequestBody {
1759            chain: params.chain.into(),
1760            pagination: PaginationParams { page: params.page, page_size: params.page_size },
1761        };
1762
1763        let uri = format!(
1764            "{}/{}/protocol_systems",
1765            self.url
1766                .to_string()
1767                .trim_end_matches('/'),
1768            TYCHO_SERVER_VERSION
1769        );
1770        debug!(%uri, "Sending protocol_systems request to Tycho server");
1771        trace!(?request, "Sending request to Tycho server");
1772        let response = self
1773            .make_post_request(&request, &uri)
1774            .await?;
1775        trace!(?response, "Received response from Tycho server");
1776        let body = response
1777            .text()
1778            .await
1779            .map_err(|e| RPCError::ParseResponse(e.to_string()))?;
1780        let dto = serde_json::from_str::<ProtocolSystemsRequestResponse>(&body)
1781            .map_err(|err| RPCError::ParseResponse(format!("Error: {err}, Body: {body}")))?;
1782        trace!(?dto, "Received protocol_systems response from Tycho server");
1783        Ok(Page::new(
1784            ProtocolSystems::new(dto.protocol_systems, dto.dci_protocols),
1785            dto.pagination.total,
1786            dto.pagination.page,
1787            dto.pagination.page_size,
1788        ))
1789    }
1790
1791    async fn get_component_tvl(
1792        &self,
1793        params: ComponentTvlParams,
1794    ) -> Result<Page<HashMap<String, f64>>, RPCError> {
1795        let request = ComponentTvlRequestBody {
1796            chain: params.chain.into(),
1797            protocol_system: params.protocol_system,
1798            component_ids: params.component_ids,
1799            pagination: PaginationParams { page: params.page, page_size: params.page_size },
1800        };
1801
1802        let uri = format!(
1803            "{}/{}/component_tvl",
1804            self.url
1805                .to_string()
1806                .trim_end_matches('/'),
1807            TYCHO_SERVER_VERSION
1808        );
1809        debug!(%uri, "Sending get_component_tvl request to Tycho server");
1810        trace!(?request, "Sending request to Tycho server");
1811        let response = self
1812            .make_post_request(&request, &uri)
1813            .await?;
1814        trace!(?response, "Received response from Tycho server");
1815        let body = response
1816            .text()
1817            .await
1818            .map_err(|e| RPCError::ParseResponse(e.to_string()))?;
1819        let dto_response =
1820            serde_json::from_str::<ComponentTvlRequestResponse>(&body).map_err(|err| {
1821                error!("Failed to parse component_tvl response: {:?}", &body);
1822                RPCError::ParseResponse(format!("Error: {err}, Body: {body}"))
1823            })?;
1824        trace!(?dto_response, "Received component_tvl response from Tycho server");
1825        Ok(Page::new(
1826            dto_response.tvl,
1827            dto_response.pagination.total,
1828            dto_response.pagination.page,
1829            dto_response.pagination.page_size,
1830        ))
1831    }
1832
1833    async fn get_traced_entry_points(
1834        &self,
1835        params: TracedEntryPointsParams,
1836    ) -> Result<Page<TracedEntryPoints>, RPCError> {
1837        let request = TracedEntryPointRequestBody {
1838            chain: params.chain.into(),
1839            protocol_system: params.protocol_system,
1840            component_ids: params.component_ids,
1841            pagination: PaginationParams { page: params.page, page_size: params.page_size },
1842        };
1843
1844        let uri = format!(
1845            "{}/{TYCHO_SERVER_VERSION}/traced_entry_points",
1846            self.url
1847                .to_string()
1848                .trim_end_matches('/')
1849        );
1850        debug!(%uri, "Sending traced_entry_points request to Tycho server");
1851        trace!(?request, "Sending request to Tycho server");
1852
1853        let response = self
1854            .make_post_request(&request, &uri)
1855            .await?;
1856
1857        trace!(?response, "Received response from Tycho server");
1858
1859        let body = response
1860            .text()
1861            .await
1862            .map_err(|e| RPCError::ParseResponse(e.to_string()))?;
1863        let dto_response =
1864            serde_json::from_str::<TracedEntryPointRequestResponse>(&body).map_err(|err| {
1865                error!("Failed to parse traced_entry_points response: {:?}", &body);
1866                RPCError::ParseResponse(format!("Error: {err}, Body: {body}"))
1867            })?;
1868        trace!(?dto_response, "Received traced_entry_points response from Tycho server");
1869        let data: TracedEntryPoints = dto_response
1870            .traced_entry_points
1871            .into_iter()
1872            .map(|(k, v)| {
1873                (
1874                    k,
1875                    v.into_iter()
1876                        .map(|(ep, tr)| {
1877                            (EntryPointWithTracingParams::from(ep), TracingResult::from(tr))
1878                        })
1879                        .collect(),
1880                )
1881            })
1882            .collect();
1883        Ok(Page::new(
1884            data,
1885            dto_response.pagination.total,
1886            dto_response.pagination.page,
1887            dto_response.pagination.page_size,
1888        ))
1889    }
1890
1891    async fn get_snapshots<'a>(
1892        &self,
1893        request: &SnapshotParameters<'a>,
1894        chunk_size: Option<usize>,
1895        concurrency: usize,
1896    ) -> Result<Snapshot, RPCError> {
1897        let component_ids: Vec<_> = request
1898            .components
1899            .keys()
1900            .cloned()
1901            .collect();
1902
1903        let component_tvl = if request.include_tvl && !component_ids.is_empty() {
1904            self.get_component_tvl_paginated(
1905                ComponentTvlPaginatedParams::new(request.chain, concurrency)
1906                    .with_component_ids(component_ids.clone()),
1907            )
1908            .await?
1909        } else {
1910            HashMap::new()
1911        };
1912
1913        let version = VersionParam::at_block(request.chain.into(), request.block_number);
1914
1915        let mut protocol_states = if !component_ids.is_empty() {
1916            self.get_protocol_states_paginated(
1917                ProtocolStatesPaginatedParams::new(
1918                    request.chain,
1919                    request.protocol_system,
1920                    concurrency,
1921                )
1922                .with_protocol_ids(component_ids.clone())
1923                .with_include_balances(request.include_balances)
1924                .with_version(version.clone()),
1925            )
1926            .await?
1927            .into_iter()
1928            .map(|state| (state.component_id.clone(), state))
1929            .collect()
1930        } else {
1931            HashMap::new()
1932        };
1933
1934        // Convert to ComponentWithState, which includes entrypoint information.
1935        let states = request
1936            .components
1937            .values()
1938            .filter_map(|component| {
1939                if let Some(state) = protocol_states.remove(&component.id) {
1940                    Some((
1941                        component.id.clone(),
1942                        ComponentWithState {
1943                            state,
1944                            component: component.clone(),
1945                            component_tvl: component_tvl
1946                                .get(&component.id)
1947                                .cloned(),
1948                            entrypoints: request
1949                                .entrypoints
1950                                .as_ref()
1951                                .and_then(|map| map.get(&component.id))
1952                                .cloned()
1953                                .unwrap_or_default(),
1954                        },
1955                    ))
1956                } else if component_ids.contains(&component.id) {
1957                    // only emit error event if we requested this component
1958                    let component_id = &component.id;
1959                    error!(?component_id, "Missing state for native component!");
1960                    None
1961                } else {
1962                    None
1963                }
1964            })
1965            .collect();
1966
1967        let vm_storage = if !request.contract_ids.is_empty() {
1968            let mut cp_params = ContractStatePaginatedParams::new(
1969                request.chain,
1970                request.protocol_system,
1971                concurrency,
1972            )
1973            .with_contract_ids(request.contract_ids.to_vec())
1974            .with_version(version.clone());
1975            if let Some(cs) = chunk_size {
1976                cp_params = cp_params.with_chunk_size(cs);
1977            }
1978            let contract_states = self
1979                .get_contract_state_paginated(cp_params)
1980                .await?
1981                .into_iter()
1982                .map(|acc| (acc.address.clone(), acc))
1983                .collect::<HashMap<_, _>>();
1984
1985            trace!(states=?&contract_states, "Retrieved ContractState");
1986
1987            let contract_address_to_components = request
1988                .components
1989                .iter()
1990                .filter_map(|(id, comp)| {
1991                    if component_ids.contains(id) {
1992                        Some(
1993                            comp.contract_addresses
1994                                .iter()
1995                                .map(|address| (address.clone(), comp.id.clone())),
1996                        )
1997                    } else {
1998                        None
1999                    }
2000                })
2001                .flatten()
2002                .fold(HashMap::<Bytes, Vec<String>>::new(), |mut acc, (addr, c_id)| {
2003                    acc.entry(addr).or_default().push(c_id);
2004                    acc
2005                });
2006
2007            request
2008                .contract_ids
2009                .iter()
2010                .filter_map(|address| {
2011                    if let Some(state) = contract_states.get(address) {
2012                        Some((address.clone(), state.clone()))
2013                    } else if let Some(ids) = contract_address_to_components.get(address) {
2014                        // only emit error even if we did actually request this address
2015                        error!(
2016                            ?address,
2017                            ?ids,
2018                            "Component with lacking contract storage encountered!"
2019                        );
2020                        None
2021                    } else {
2022                        None
2023                    }
2024                })
2025                .collect()
2026        } else {
2027            HashMap::new()
2028        };
2029
2030        Ok(Snapshot { states, vm_storage })
2031    }
2032}
2033
2034#[cfg(test)]
2035mod tests {
2036    use std::{
2037        collections::{HashMap, HashSet},
2038        str::FromStr,
2039    };
2040
2041    use mockito::Server;
2042    use rstest::rstest;
2043    use tycho_common::models::blockchain::AddressStorageLocation;
2044
2045    use super::*;
2046
2047    // Dummy implementation of `get_protocol_states_paginated` for backwards compatibility testing
2048    // purposes
2049    impl MockRPCClient {
2050        #[expect(
2051            clippy::too_many_arguments,
2052            reason = "mirrors the parameters of the paginated RPC method it records calls for"
2053        )]
2054        async fn test_get_protocol_states_paginated<T>(
2055            &self,
2056            chain: Chain,
2057            ids: &[T],
2058            protocol_system: &str,
2059            include_balances: bool,
2060            block_number: Option<u64>,
2061            chunk_size: usize,
2062            _concurrency: usize,
2063        ) -> Vec<(Chain, Vec<String>, String, bool, Option<u64>, PaginationParams)>
2064        where
2065            T: AsRef<str> + Clone + Send + Sync + 'static,
2066        {
2067            ids.chunks(chunk_size)
2068                .map(|chunk| {
2069                    (
2070                        chain,
2071                        chunk
2072                            .iter()
2073                            .map(|id| id.as_ref().to_string())
2074                            .collect(),
2075                        protocol_system.to_string(),
2076                        include_balances,
2077                        block_number,
2078                        PaginationParams { page: 0, page_size: i64::try_from(chunk_size).unwrap() },
2079                    )
2080                })
2081                .collect()
2082        }
2083    }
2084
2085    const GET_CONTRACT_STATE_RESP: &str = r#"
2086        {
2087            "accounts": [
2088                {
2089                    "chain": "ethereum",
2090                    "address": "0x0000000000000000000000000000000000000000",
2091                    "title": "",
2092                    "slots": {},
2093                    "native_balance": "0x01f4",
2094                    "token_balances": {},
2095                    "code": "0x00",
2096                    "code_hash": "0x5c06b7c5b3d910fd33bc2229846f9ddaf91d584d9b196e16636901ac3a77077e",
2097                    "balance_modify_tx": "0x0000000000000000000000000000000000000000000000000000000000000000",
2098                    "code_modify_tx": "0x0000000000000000000000000000000000000000000000000000000000000000",
2099                    "creation_tx": null
2100                }
2101            ],
2102            "pagination": {
2103                "page": 0,
2104                "page_size": 20,
2105                "total": 10
2106            }
2107        }
2108        "#;
2109
2110    #[rstest]
2111    #[case::string_input(vec![
2112        "id1".to_string(),
2113        "id2".to_string()
2114    ])]
2115    #[tokio::test]
2116    async fn test_get_protocol_states_paginated<T>(#[case] ids: Vec<T>)
2117    where
2118        T: AsRef<str> + Clone + Send + Sync + 'static,
2119    {
2120        let mock_client = MockRPCClient::new();
2121
2122        let request_args = mock_client
2123            .test_get_protocol_states_paginated(
2124                Chain::Ethereum,
2125                &ids,
2126                "test_system",
2127                true,
2128                None,
2129                2,
2130                2,
2131            )
2132            .await;
2133
2134        // Verify that the request args have been split into chunks correctly
2135        assert_eq!(request_args.len(), 1);
2136        assert_eq!(request_args[0].1.len(), 2);
2137    }
2138
2139    #[tokio::test]
2140    async fn test_get_contract_state() {
2141        let mut server = Server::new_async().await;
2142        let server_resp = GET_CONTRACT_STATE_RESP;
2143        // test that the response is deserialized correctly
2144        serde_json::from_str::<StateRequestResponse>(server_resp).expect("deserialize");
2145
2146        let mocked_server = server
2147            .mock("POST", "/v1/contract_state")
2148            .expect(1)
2149            .with_body(server_resp)
2150            .create_async()
2151            .await;
2152
2153        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2154            .expect("create client");
2155
2156        let accounts = client
2157            .get_contract_state(ContractStateParams::new(Chain::Ethereum, ""))
2158            .await
2159            .expect("get state");
2160
2161        mocked_server.assert();
2162        assert_eq!(accounts.data().len(), 1);
2163        assert_eq!(accounts.data()[0].slots, HashMap::new());
2164        assert_eq!(accounts.data()[0].native_balance, Bytes::from(500u16.to_be_bytes()));
2165        assert_eq!(accounts.data()[0].code, [0].to_vec());
2166        assert_eq!(
2167            accounts.data()[0].code_hash,
2168            hex::decode("5c06b7c5b3d910fd33bc2229846f9ddaf91d584d9b196e16636901ac3a77077e")
2169                .unwrap()
2170        );
2171    }
2172
2173    #[tokio::test]
2174    async fn test_get_contract_state_paginated_requests_default_sized_chunks() {
2175        let mut server = Server::new_async().await;
2176        let full_chunks = server
2177            .mock("POST", "/v1/contract_state")
2178            .match_body(mockito::Matcher::PartialJson(serde_json::json!({
2179                "pagination": {"page": 0, "page_size": 100}
2180            })))
2181            .expect(3)
2182            .with_body(GET_CONTRACT_STATE_RESP)
2183            .create_async()
2184            .await;
2185        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2186            .expect("create client");
2187        let ids: Vec<Bytes> = (0..250u16)
2188            .map(|i| Bytes::from(i.to_be_bytes()))
2189            .collect();
2190
2191        let accounts = client
2192            .get_contract_state_paginated(
2193                ContractStatePaginatedParams::new(Chain::Ethereum, "", RPC_CLIENT_CONCURRENCY)
2194                    .with_contract_ids(ids),
2195            )
2196            .await
2197            .expect("get state");
2198
2199        full_chunks.assert();
2200        assert_eq!(accounts.len(), 3);
2201    }
2202
2203    #[tokio::test]
2204    async fn test_get_protocol_components_paginated_requests_every_id_chunk_as_page_zero() {
2205        let mut server = Server::new_async().await;
2206        let chunks = server
2207            .mock("POST", "/v1/protocol_components")
2208            .match_body(mockito::Matcher::PartialJson(serde_json::json!({
2209                "pagination": {"page": 0, "page_size": 100}
2210            })))
2211            .expect(3)
2212            .with_body(r#"{"protocol_components": [], "pagination": {"page": 0, "page_size": 100, "total": 0}}"#)
2213            .create_async()
2214            .await;
2215        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2216            .expect("create client");
2217        let ids: Vec<String> = (0..250)
2218            .map(|i| format!("component_{i}"))
2219            .collect();
2220
2221        client
2222            .get_protocol_components_paginated(
2223                ProtocolComponentsPaginatedParams::new(
2224                    Chain::Ethereum,
2225                    "uniswap_v2",
2226                    RPC_CLIENT_CONCURRENCY,
2227                )
2228                .with_component_ids(ids)
2229                .with_chunk_size(100),
2230            )
2231            .await
2232            .expect("get components");
2233
2234        chunks.assert();
2235    }
2236
2237    #[tokio::test]
2238    async fn test_get_component_tvl_paginated_requests_every_id_chunk_as_page_zero() {
2239        let mut server = Server::new_async().await;
2240        let chunks = server
2241            .mock("POST", "/v1/component_tvl")
2242            .match_body(mockito::Matcher::PartialJson(serde_json::json!({
2243                "pagination": {"page": 0, "page_size": 100}
2244            })))
2245            .expect(3)
2246            .with_body(r#"{"tvl": {}, "pagination": {"page": 0, "page_size": 100, "total": 0}}"#)
2247            .create_async()
2248            .await;
2249        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2250            .expect("create client");
2251        let ids: Vec<String> = (0..250)
2252            .map(|i| format!("component_{i}"))
2253            .collect();
2254
2255        client
2256            .get_component_tvl_paginated(
2257                ComponentTvlPaginatedParams::new(Chain::Ethereum, RPC_CLIENT_CONCURRENCY)
2258                    .with_component_ids(ids)
2259                    .with_chunk_size(100),
2260            )
2261            .await
2262            .expect("get component tvl");
2263
2264        chunks.assert();
2265    }
2266
2267    #[tokio::test]
2268    async fn test_get_protocol_components_paginated_clamps_chunk_size_to_max_page_size() {
2269        let mut server = Server::new_async().await;
2270        let max = ProtocolComponentsRequestBody::MAX_PAGE_SIZE_COMPRESSED;
2271        let chunks = server
2272            .mock("POST", "/v1/protocol_components")
2273            .match_body(mockito::Matcher::PartialJson(serde_json::json!({
2274                "pagination": {"page": 0, "page_size": max}
2275            })))
2276            .expect(2)
2277            .with_body(format!(
2278                r#"{{"protocol_components": [], "pagination": {{"page": 0, "page_size": {max}, "total": 0}}}}"#
2279            ))
2280            .create_async()
2281            .await;
2282        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2283            .expect("create client");
2284        let ids: Vec<String> = (0..max + 1)
2285            .map(|i| format!("component_{i}"))
2286            .collect();
2287
2288        client
2289            .get_protocol_components_paginated(
2290                ProtocolComponentsPaginatedParams::new(
2291                    Chain::Ethereum,
2292                    "uniswap_v2",
2293                    RPC_CLIENT_CONCURRENCY,
2294                )
2295                .with_component_ids(ids)
2296                .with_chunk_size(usize::try_from(max).unwrap() * 2),
2297            )
2298            .await
2299            .expect("get components");
2300
2301        chunks.assert();
2302    }
2303
2304    #[tokio::test]
2305    async fn test_get_component_tvl_paginated_clamps_chunk_size_to_max_page_size() {
2306        let mut server = Server::new_async().await;
2307        let max = ComponentTvlRequestBody::MAX_PAGE_SIZE_COMPRESSED;
2308        let chunks = server
2309            .mock("POST", "/v1/component_tvl")
2310            .match_body(mockito::Matcher::PartialJson(serde_json::json!({
2311                "pagination": {"page": 0, "page_size": max}
2312            })))
2313            .expect(2)
2314            .with_body(format!(
2315                r#"{{"tvl": {{}}, "pagination": {{"page": 0, "page_size": {max}, "total": 0}}}}"#
2316            ))
2317            .create_async()
2318            .await;
2319        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2320            .expect("create client");
2321        let ids: Vec<String> = (0..max + 1)
2322            .map(|i| format!("component_{i}"))
2323            .collect();
2324
2325        client
2326            .get_component_tvl_paginated(
2327                ComponentTvlPaginatedParams::new(Chain::Ethereum, RPC_CLIENT_CONCURRENCY)
2328                    .with_component_ids(ids)
2329                    .with_chunk_size(usize::try_from(max).unwrap() * 2),
2330            )
2331            .await
2332            .expect("get component tvl");
2333
2334        chunks.assert();
2335    }
2336
2337    #[tokio::test]
2338    async fn test_client_metadata_header_sent_when_set() {
2339        let mut server = Server::new_async().await;
2340        let expected_ua = format!("tycho-client-{}", env!("CARGO_PKG_VERSION"));
2341        let mock = server
2342            .mock("POST", "/v1/contract_state")
2343            .match_header(CLIENT_METADATA_HEADER, "fynd_version=0.57.0;preset=best")
2344            .match_header("user-agent", expected_ua.as_str())
2345            .with_body(GET_CONTRACT_STATE_RESP)
2346            .expect(1)
2347            .create_async()
2348            .await;
2349
2350        let client = HttpRPCClient::new(
2351            server.url().as_str(),
2352            HttpRPCClientOptions::default()
2353                .with_client_metadata_header(Some("fynd_version=0.57.0;preset=best".to_string())),
2354        )
2355        .expect("create client");
2356
2357        client
2358            .get_contract_state(ContractStateParams::new(Chain::Ethereum, ""))
2359            .await
2360            .expect("get state");
2361
2362        mock.assert_async().await;
2363    }
2364
2365    #[tokio::test]
2366    async fn test_no_client_metadata_header_when_unset() {
2367        let mut server = Server::new_async().await;
2368        let expected_ua = format!("tycho-client-{}", env!("CARGO_PKG_VERSION"));
2369        let mock = server
2370            .mock("POST", "/v1/contract_state")
2371            .match_header(CLIENT_METADATA_HEADER, mockito::Matcher::Missing)
2372            .match_header("user-agent", expected_ua.as_str())
2373            .with_body(GET_CONTRACT_STATE_RESP)
2374            .expect(1)
2375            .create_async()
2376            .await;
2377
2378        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2379            .expect("create client");
2380
2381        client
2382            .get_contract_state(ContractStateParams::new(Chain::Ethereum, ""))
2383            .await
2384            .expect("get state");
2385
2386        mock.assert_async().await;
2387    }
2388
2389    #[tokio::test]
2390    async fn test_get_protocol_components() {
2391        let mut server = Server::new_async().await;
2392        let server_resp = r#"
2393        {
2394            "protocol_components": [
2395                {
2396                    "id": "State1",
2397                    "protocol_system": "ambient",
2398                    "protocol_type_name": "Pool",
2399                    "chain": "ethereum",
2400                    "tokens": [
2401                        "0x0000000000000000000000000000000000000000",
2402                        "0x0000000000000000000000000000000000000001"
2403                    ],
2404                    "contract_ids": [
2405                        "0x0000000000000000000000000000000000000000"
2406                    ],
2407                    "static_attributes": {
2408                        "attribute_1": "0x00000000000003e8"
2409                    },
2410                    "change": "Creation",
2411                    "creation_tx": "0x0000000000000000000000000000000000000000000000000000000000000000",
2412                    "created_at": "2022-01-01T00:00:00"
2413                }
2414            ],
2415            "pagination": {
2416                "page": 0,
2417                "page_size": 20,
2418                "total": 10
2419            }
2420        }
2421        "#;
2422        // test that the response is deserialized correctly
2423        serde_json::from_str::<ProtocolComponentRequestResponse>(server_resp).expect("deserialize");
2424
2425        let mocked_server = server
2426            .mock("POST", "/v1/protocol_components")
2427            .expect(1)
2428            .with_body(server_resp)
2429            .create_async()
2430            .await;
2431
2432        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2433            .expect("create client");
2434
2435        let components = client
2436            .get_protocol_components(ProtocolComponentsParams::new(Chain::Ethereum, ""))
2437            .await
2438            .expect("get state");
2439
2440        mocked_server.assert();
2441        assert_eq!(components.data().len(), 1);
2442        assert_eq!(components.data()[0].id, "State1");
2443        assert_eq!(components.data()[0].protocol_system, "ambient");
2444        assert_eq!(components.data()[0].protocol_type_name, "Pool");
2445        assert_eq!(components.data()[0].tokens.len(), 2);
2446        let expected_attributes =
2447            [("attribute_1".to_string(), Bytes::from(1000_u64.to_be_bytes()))]
2448                .iter()
2449                .cloned()
2450                .collect::<HashMap<String, Bytes>>();
2451        assert_eq!(components.data()[0].static_attributes, expected_attributes);
2452    }
2453
2454    #[tokio::test]
2455    async fn test_get_protocol_states() {
2456        let mut server = Server::new_async().await;
2457        let server_resp = r#"
2458        {
2459            "states": [
2460                {
2461                    "component_id": "State1",
2462                    "attributes": {
2463                        "attribute_1": "0x00000000000003e8"
2464                    },
2465                    "balances": {
2466                        "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2": "0x01f4"
2467                    }
2468                }
2469            ],
2470            "pagination": {
2471                "page": 0,
2472                "page_size": 20,
2473                "total": 10
2474            }
2475        }
2476        "#;
2477        // test that the response is deserialized correctly
2478        serde_json::from_str::<ProtocolStateRequestResponse>(server_resp).expect("deserialize");
2479
2480        let mocked_server = server
2481            .mock("POST", "/v1/protocol_state")
2482            .expect(1)
2483            .with_body(server_resp)
2484            .create_async()
2485            .await;
2486        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2487            .expect("create client");
2488
2489        let states = client
2490            .get_protocol_states(
2491                ProtocolStatesParams::new(Chain::Ethereum, "").with_include_balances(true),
2492            )
2493            .await
2494            .expect("get state");
2495
2496        mocked_server.assert();
2497        assert_eq!(states.data().len(), 1);
2498        assert_eq!(states.data()[0].component_id, "State1");
2499        let expected_attributes =
2500            [("attribute_1".to_string(), Bytes::from(1000_u64.to_be_bytes()))]
2501                .iter()
2502                .cloned()
2503                .collect::<HashMap<String, Bytes>>();
2504        assert_eq!(states.data()[0].attributes, expected_attributes);
2505        let expected_balances = [(
2506            Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")
2507                .expect("Unsupported address format"),
2508            Bytes::from_str("0x01f4").unwrap(),
2509        )]
2510        .iter()
2511        .cloned()
2512        .collect::<HashMap<Bytes, Bytes>>();
2513        assert_eq!(states.data()[0].balances, expected_balances);
2514    }
2515
2516    #[tokio::test]
2517    async fn test_get_tokens() {
2518        let mut server = Server::new_async().await;
2519        let server_resp = r#"
2520        {
2521            "tokens": [
2522              {
2523                "chain": "ethereum",
2524                "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
2525                "symbol": "WETH",
2526                "decimals": 18,
2527                "tax": 0,
2528                "gas": [
2529                  29962
2530                ],
2531                "quality": 100
2532              },
2533              {
2534                "chain": "ethereum",
2535                "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
2536                "symbol": "USDC",
2537                "decimals": 6,
2538                "tax": 0,
2539                "gas": [
2540                  40652
2541                ],
2542                "quality": 100
2543              }
2544            ],
2545            "pagination": {
2546              "page": 0,
2547              "page_size": 20,
2548              "total": 10
2549            }
2550          }
2551        "#;
2552        // test that the response is deserialized correctly
2553        serde_json::from_str::<TokensRequestResponse>(server_resp).expect("deserialize");
2554
2555        let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
2556        let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
2557        let mocked_server = server
2558            .mock("POST", "/v1/tokens")
2559            .match_body(mockito::Matcher::PartialJson(serde_json::json!({
2560                "token_addresses": [weth.to_string(), usdc.to_string()],
2561            })))
2562            .expect(1)
2563            .with_body(server_resp)
2564            .create_async()
2565            .await;
2566        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2567            .expect("create client");
2568
2569        let tokens = client
2570            .get_tokens(TokensParams::new(Chain::Ethereum).with_token_addresses(vec![weth, usdc]))
2571            .await
2572            .expect("get tokens");
2573
2574        let expected = vec![
2575            Token {
2576                chain: tycho_common::models::Chain::Ethereum,
2577                address: Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(),
2578                symbol: "WETH".to_string(),
2579                decimals: 18,
2580                tax: 0,
2581                gas: vec![Some(29962)],
2582                quality: 100,
2583            },
2584            Token {
2585                chain: tycho_common::models::Chain::Ethereum,
2586                address: Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(),
2587                symbol: "USDC".to_string(),
2588                decimals: 6,
2589                tax: 0,
2590                gas: vec![Some(40652)],
2591                quality: 100,
2592            },
2593        ];
2594
2595        mocked_server.assert();
2596        assert_eq!(*tokens.data(), expected);
2597    }
2598
2599    #[rstest]
2600    #[case::with_dci(Some(vec!["system2"]), vec!["system2"])]
2601    #[case::backward_compat(None, vec![])]
2602    #[tokio::test]
2603    async fn test_get_protocol_systems(
2604        #[case] dci_protocols: Option<Vec<&str>>,
2605        #[case] expected_dci: Vec<&str>,
2606    ) {
2607        use serde_json::json;
2608
2609        let mut json_value = json!({
2610            "protocol_systems": ["system1", "system2"],
2611            "pagination": { "page": 0, "page_size": 20, "total": 2 }
2612        });
2613        if let Some(dci) = dci_protocols {
2614            json_value["dci_protocols"] = json!(dci);
2615        }
2616        let server_resp = serde_json::to_string(&json_value).unwrap();
2617
2618        let mut server = Server::new_async().await;
2619        let mocked_server = server
2620            .mock("POST", "/v1/protocol_systems")
2621            .expect(1)
2622            .with_body(&server_resp)
2623            .create_async()
2624            .await;
2625        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2626            .expect("create client");
2627
2628        let response = client
2629            .get_protocol_systems(ProtocolSystemsParams::new(Chain::Ethereum))
2630            .await
2631            .expect("get protocol systems");
2632
2633        mocked_server.assert();
2634        assert_eq!(response.data().protocol_systems(), ["system1", "system2"]);
2635        assert_eq!(response.data().dci_protocols(), expected_dci.as_slice());
2636    }
2637
2638    #[tokio::test]
2639    async fn test_get_component_tvl() {
2640        let mut server = Server::new_async().await;
2641        let server_resp = r#"
2642        {
2643            "tvl": {
2644                "component1": 100.0
2645            },
2646            "pagination": {
2647                "page": 0,
2648                "page_size": 20,
2649                "total": 10
2650            }
2651        }
2652        "#;
2653        // test that the response is deserialized correctly
2654        serde_json::from_str::<ComponentTvlRequestResponse>(server_resp).expect("deserialize");
2655
2656        let mocked_server = server
2657            .mock("POST", "/v1/component_tvl")
2658            .expect(1)
2659            .with_body(server_resp)
2660            .create_async()
2661            .await;
2662        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2663            .expect("create client");
2664
2665        let component_tvl = client
2666            .get_component_tvl(ComponentTvlParams::new(Chain::Ethereum))
2667            .await
2668            .expect("get component tvl");
2669
2670        mocked_server.assert();
2671        assert_eq!(component_tvl.data().get("component1"), Some(&100.0));
2672    }
2673
2674    #[tokio::test]
2675    async fn test_get_traced_entry_points() {
2676        let mut server = Server::new_async().await;
2677        let server_resp = r#"
2678        {
2679            "traced_entry_points": {
2680                "component_1": [
2681                    [
2682                        {
2683                            "entry_point": {
2684                                "external_id": "entrypoint_a",
2685                                "target": "0x0000000000000000000000000000000000000001",
2686                                "signature": "sig()"
2687                            },
2688                            "params": {
2689                                "method": "rpctracer",
2690                                "caller": "0x000000000000000000000000000000000000000a",
2691                                "calldata": "0x000000000000000000000000000000000000000b"
2692                            }
2693                        },
2694                        {
2695                            "retriggers": [
2696                                [
2697                                    "0x00000000000000000000000000000000000000aa",
2698                                    {"key": "0x0000000000000000000000000000000000000aaa", "offset": 12}
2699                                ]
2700                            ],
2701                            "accessed_slots": {
2702                                "0x0000000000000000000000000000000000aaaa": [
2703                                    "0x0000000000000000000000000000000000aaaa"
2704                                ]
2705                            }
2706                        }
2707                    ]
2708                ]
2709            },
2710            "pagination": {
2711                "page": 0,
2712                "page_size": 20,
2713                "total": 1
2714            }
2715        }
2716        "#;
2717        // test that the response is deserialized correctly
2718        serde_json::from_str::<TracedEntryPointRequestResponse>(server_resp).expect("deserialize");
2719
2720        let mocked_server = server
2721            .mock("POST", "/v1/traced_entry_points")
2722            .expect(1)
2723            .with_body(server_resp)
2724            .create_async()
2725            .await;
2726        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2727            .expect("create client");
2728
2729        let entrypoints = client
2730            .get_traced_entry_points(TracedEntryPointsParams::new(Chain::Ethereum, ""))
2731            .await
2732            .expect("get traced entry points");
2733
2734        mocked_server.assert();
2735        assert_eq!(entrypoints.data().len(), 1);
2736        let comp1_entrypoints = entrypoints
2737            .data()
2738            .get("component_1")
2739            .expect("component_1 entrypoints should exist");
2740        assert_eq!(comp1_entrypoints.len(), 1);
2741
2742        let (entrypoint, trace_result) = &comp1_entrypoints[0];
2743        assert_eq!(entrypoint.entry_point.external_id, "entrypoint_a");
2744        assert_eq!(
2745            entrypoint.entry_point.target,
2746            Bytes::from_str("0x0000000000000000000000000000000000000001").unwrap()
2747        );
2748        assert_eq!(entrypoint.entry_point.signature, "sig()");
2749        let tycho_common::models::blockchain::TracingParams::RPCTracer(rpc_params) =
2750            &entrypoint.params;
2751        assert_eq!(
2752            rpc_params.caller,
2753            Some(Bytes::from("0x000000000000000000000000000000000000000a"))
2754        );
2755        assert_eq!(rpc_params.calldata, Bytes::from("0x000000000000000000000000000000000000000b"));
2756
2757        assert_eq!(
2758            trace_result.retriggers,
2759            HashSet::from([(
2760                Bytes::from("0x00000000000000000000000000000000000000aa"),
2761                AddressStorageLocation::new(
2762                    Bytes::from("0x0000000000000000000000000000000000000aaa"),
2763                    12
2764                )
2765            )])
2766        );
2767        assert_eq!(trace_result.accessed_slots.len(), 1);
2768        assert_eq!(
2769            trace_result.accessed_slots,
2770            HashMap::from([(
2771                Bytes::from("0x0000000000000000000000000000000000aaaa"),
2772                HashSet::from([Bytes::from("0x0000000000000000000000000000000000aaaa")])
2773            )])
2774        );
2775    }
2776
2777    #[tokio::test]
2778    async fn test_parse_retry_value_numeric() {
2779        let result = parse_retry_value("60");
2780        assert!(result.is_some());
2781
2782        let expected_time = SystemTime::now() + Duration::from_secs(60);
2783        let actual_time = result.unwrap();
2784
2785        // Allow for small timing differences during test execution
2786        let diff = if actual_time > expected_time {
2787            actual_time
2788                .duration_since(expected_time)
2789                .unwrap()
2790        } else {
2791            expected_time
2792                .duration_since(actual_time)
2793                .unwrap()
2794        };
2795        assert!(diff < Duration::from_secs(1), "Time difference too large: {:?}", diff);
2796    }
2797
2798    #[tokio::test]
2799    async fn test_parse_retry_value_rfc2822() {
2800        // Use a fixed future date in RFC2822 format
2801        let rfc2822_date = "Sat, 01 Jan 2030 12:00:00 +0000";
2802        let result = parse_retry_value(rfc2822_date);
2803        assert!(result.is_some());
2804
2805        let parsed_time = result.unwrap();
2806        assert!(parsed_time > SystemTime::now());
2807    }
2808
2809    #[tokio::test]
2810    async fn test_parse_retry_value_invalid_formats() {
2811        // Test various invalid formats
2812        assert!(parse_retry_value("invalid").is_none());
2813        assert!(parse_retry_value("").is_none());
2814        assert!(parse_retry_value("not_a_number").is_none());
2815        assert!(parse_retry_value("Mon, 32 Jan 2030 25:00:00 +0000").is_none());
2816        // Invalid date
2817    }
2818
2819    #[tokio::test]
2820    async fn test_parse_retry_value_zero_seconds() {
2821        let result = parse_retry_value("0");
2822        assert!(result.is_some());
2823
2824        let expected_time = SystemTime::now();
2825        let actual_time = result.unwrap();
2826
2827        // Should be very close to current time
2828        let diff = if actual_time > expected_time {
2829            actual_time
2830                .duration_since(expected_time)
2831                .unwrap()
2832        } else {
2833            expected_time
2834                .duration_since(actual_time)
2835                .unwrap()
2836        };
2837        assert!(diff < Duration::from_secs(1));
2838    }
2839
2840    #[tokio::test]
2841    async fn test_error_for_response_rate_limited() {
2842        let mut server = Server::new_async().await;
2843        let mock = server
2844            .mock("GET", "/test")
2845            .with_status(429)
2846            .with_header("Retry-After", "60")
2847            .create_async()
2848            .await;
2849
2850        let client = reqwest::Client::new();
2851        let response = client
2852            .get(format!("{}/test", server.url()))
2853            .send()
2854            .await
2855            .unwrap();
2856
2857        let http_client =
2858            HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2859                .unwrap()
2860                .with_test_backoff_policy();
2861        let result = http_client
2862            .error_for_response(response)
2863            .await;
2864
2865        mock.assert();
2866        assert!(matches!(result, Err(RPCError::RateLimited(_))));
2867        if let Err(RPCError::RateLimited(retry_after)) = result {
2868            assert!(retry_after.is_some());
2869        }
2870    }
2871
2872    #[tokio::test]
2873    async fn test_error_for_response_rate_limited_no_header() {
2874        let mut server = Server::new_async().await;
2875        let mock = server
2876            .mock("GET", "/test")
2877            .with_status(429)
2878            .create_async()
2879            .await;
2880
2881        let client = reqwest::Client::new();
2882        let response = client
2883            .get(format!("{}/test", server.url()))
2884            .send()
2885            .await
2886            .unwrap();
2887
2888        let http_client =
2889            HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2890                .unwrap()
2891                .with_test_backoff_policy();
2892        let result = http_client
2893            .error_for_response(response)
2894            .await;
2895
2896        mock.assert();
2897        assert!(matches!(result, Err(RPCError::RateLimited(None))));
2898    }
2899
2900    #[tokio::test]
2901    async fn test_error_for_response_server_errors() {
2902        let test_cases =
2903            vec![(502, "Bad Gateway"), (503, "Service Unavailable"), (504, "Gateway Timeout")];
2904
2905        for (status_code, expected_body) in test_cases {
2906            let mut server = Server::new_async().await;
2907            let mock = server
2908                .mock("GET", "/test")
2909                .with_status(status_code)
2910                .with_body(expected_body)
2911                .create_async()
2912                .await;
2913
2914            let client = reqwest::Client::new();
2915            let response = client
2916                .get(format!("{}/test", server.url()))
2917                .send()
2918                .await
2919                .unwrap();
2920
2921            let http_client =
2922                HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2923                    .unwrap()
2924                    .with_test_backoff_policy();
2925            let result = http_client
2926                .error_for_response(response)
2927                .await;
2928
2929            mock.assert();
2930            assert!(matches!(result, Err(RPCError::ServerUnreachable(_))));
2931            if let Err(RPCError::ServerUnreachable(body)) = result {
2932                assert_eq!(body, expected_body);
2933            }
2934        }
2935    }
2936
2937    #[tokio::test]
2938    async fn test_error_for_response_success() {
2939        let mut server = Server::new_async().await;
2940        let mock = server
2941            .mock("GET", "/test")
2942            .with_status(200)
2943            .with_body("success")
2944            .create_async()
2945            .await;
2946
2947        let client = reqwest::Client::new();
2948        let response = client
2949            .get(format!("{}/test", server.url()))
2950            .send()
2951            .await
2952            .unwrap();
2953
2954        let http_client =
2955            HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
2956                .unwrap()
2957                .with_test_backoff_policy();
2958        let result = http_client
2959            .error_for_response(response)
2960            .await;
2961
2962        mock.assert();
2963        assert!(result.is_ok());
2964
2965        let response = result.unwrap();
2966        assert_eq!(response.status(), 200);
2967    }
2968
2969    #[tokio::test]
2970    async fn test_handle_error_for_backoff_server_unreachable() {
2971        let http_client =
2972            HttpRPCClient::new("http://localhost:8080", HttpRPCClientOptions::default())
2973                .unwrap()
2974                .with_test_backoff_policy();
2975        let error = RPCError::ServerUnreachable("Service down".to_string());
2976
2977        let backoff_error = http_client
2978            .handle_error_for_backoff(error)
2979            .await;
2980
2981        match backoff_error {
2982            backoff::Error::Transient { err: RPCError::ServerUnreachable(msg), retry_after } => {
2983                assert_eq!(msg, "Service down");
2984                assert_eq!(retry_after, Some(Duration::from_millis(50))); // Fast test duration
2985            }
2986            backoff::Error::Permanent(_) | backoff::Error::Transient { .. } => {
2987                panic!("Expected transient error for ServerUnreachable")
2988            }
2989        }
2990    }
2991
2992    #[tokio::test]
2993    async fn test_handle_error_for_backoff_rate_limited_with_retry_after() {
2994        let http_client =
2995            HttpRPCClient::new("http://localhost:8080", HttpRPCClientOptions::default())
2996                .unwrap()
2997                .with_test_backoff_policy();
2998        let future_time = SystemTime::now() + Duration::from_secs(30);
2999        let error = RPCError::RateLimited(Some(future_time));
3000
3001        let backoff_error = http_client
3002            .handle_error_for_backoff(error)
3003            .await;
3004
3005        match backoff_error {
3006            backoff::Error::Transient { err: RPCError::RateLimited(retry_after), .. } => {
3007                assert_eq!(retry_after, Some(future_time));
3008            }
3009            backoff::Error::Permanent(_) | backoff::Error::Transient { .. } => {
3010                panic!("Expected transient error for RateLimited")
3011            }
3012        }
3013
3014        // Verify that retry_after was stored in the client state
3015        let stored_retry_after = http_client.retry_after.read().await;
3016        assert_eq!(*stored_retry_after, Some(future_time));
3017    }
3018
3019    #[tokio::test]
3020    async fn test_handle_error_for_backoff_rate_limited_no_retry_after() {
3021        let http_client =
3022            HttpRPCClient::new("http://localhost:8080", HttpRPCClientOptions::default())
3023                .unwrap()
3024                .with_test_backoff_policy();
3025        let error = RPCError::RateLimited(None);
3026
3027        let backoff_error = http_client
3028            .handle_error_for_backoff(error)
3029            .await;
3030
3031        match backoff_error {
3032            backoff::Error::Transient { err: RPCError::RateLimited(None), .. } => {
3033                // This is expected - no retry-after still allows retries with default policy
3034            }
3035            backoff::Error::Permanent(_) | backoff::Error::Transient { .. } => {
3036                panic!("Expected transient error for RateLimited without retry-after")
3037            }
3038        }
3039    }
3040
3041    #[tokio::test]
3042    async fn test_handle_error_for_backoff_other_errors() {
3043        let http_client =
3044            HttpRPCClient::new("http://localhost:8080", HttpRPCClientOptions::default())
3045                .unwrap()
3046                .with_test_backoff_policy();
3047        let error = RPCError::ParseResponse("Invalid JSON".to_string());
3048
3049        let backoff_error = http_client
3050            .handle_error_for_backoff(error)
3051            .await;
3052
3053        match backoff_error {
3054            backoff::Error::Permanent(RPCError::ParseResponse(msg)) => {
3055                assert_eq!(msg, "Invalid JSON");
3056            }
3057            backoff::Error::Permanent(_) | backoff::Error::Transient { .. } => {
3058                panic!("Expected permanent error for ParseResponse")
3059            }
3060        }
3061    }
3062
3063    #[tokio::test]
3064    async fn test_wait_until_retry_after_no_retry_time() {
3065        let http_client =
3066            HttpRPCClient::new("http://localhost:8080", HttpRPCClientOptions::default())
3067                .unwrap()
3068                .with_test_backoff_policy();
3069
3070        let start = std::time::Instant::now();
3071        http_client
3072            .wait_until_retry_after()
3073            .await;
3074        let elapsed = start.elapsed();
3075
3076        // Should return immediately if no retry time is set
3077        assert!(elapsed < Duration::from_millis(100));
3078    }
3079
3080    #[tokio::test]
3081    async fn test_wait_until_retry_after_past_time() {
3082        let http_client =
3083            HttpRPCClient::new("http://localhost:8080", HttpRPCClientOptions::default())
3084                .unwrap()
3085                .with_test_backoff_policy();
3086
3087        // Set a retry time in the past
3088        let past_time = SystemTime::now() - Duration::from_secs(10);
3089        *http_client.retry_after.write().await = Some(past_time);
3090
3091        let start = std::time::Instant::now();
3092        http_client
3093            .wait_until_retry_after()
3094            .await;
3095        let elapsed = start.elapsed();
3096
3097        // Should return immediately if retry time is in the past
3098        assert!(elapsed < Duration::from_millis(100));
3099    }
3100
3101    #[tokio::test]
3102    async fn test_wait_until_retry_after_future_time() {
3103        let http_client =
3104            HttpRPCClient::new("http://localhost:8080", HttpRPCClientOptions::default())
3105                .unwrap()
3106                .with_test_backoff_policy();
3107
3108        // Set a retry time 100ms in the future
3109        let future_time = SystemTime::now() + Duration::from_millis(100);
3110        *http_client.retry_after.write().await = Some(future_time);
3111
3112        let start = std::time::Instant::now();
3113        http_client
3114            .wait_until_retry_after()
3115            .await;
3116        let elapsed = start.elapsed();
3117
3118        // Should wait approximately the specified duration
3119        assert!(elapsed >= Duration::from_millis(80)); // Allow some tolerance
3120        assert!(elapsed <= Duration::from_millis(200)); // Upper bound for test stability
3121    }
3122
3123    #[tokio::test]
3124    async fn test_make_post_request_success() {
3125        let mut server = Server::new_async().await;
3126        let server_resp = r#"{"success": true}"#;
3127
3128        let mock = server
3129            .mock("POST", "/test")
3130            .with_status(200)
3131            .with_body(server_resp)
3132            .create_async()
3133            .await;
3134
3135        let http_client =
3136            HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3137                .unwrap()
3138                .with_test_backoff_policy();
3139        let request_body = serde_json::json!({"test": "data"});
3140        let uri = format!("{}/test", server.url());
3141
3142        let result = http_client
3143            .make_post_request(&request_body, &uri)
3144            .await;
3145
3146        mock.assert();
3147        assert!(result.is_ok());
3148
3149        let response = result.unwrap();
3150        assert_eq!(response.status(), 200);
3151        assert_eq!(response.text().await.unwrap(), server_resp);
3152    }
3153
3154    #[tokio::test]
3155    async fn test_make_post_request_retry_on_server_error() {
3156        let mut server = Server::new_async().await;
3157        // First request fails with 503, second succeeds
3158        let error_mock = server
3159            .mock("POST", "/test")
3160            .with_status(503)
3161            .with_body("Service Unavailable")
3162            .expect(1)
3163            .create_async()
3164            .await;
3165
3166        let success_mock = server
3167            .mock("POST", "/test")
3168            .with_status(200)
3169            .with_body(r#"{"success": true}"#)
3170            .expect(1)
3171            .create_async()
3172            .await;
3173
3174        let http_client =
3175            HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3176                .unwrap()
3177                .with_test_backoff_policy();
3178        let request_body = serde_json::json!({"test": "data"});
3179        let uri = format!("{}/test", server.url());
3180
3181        let result = http_client
3182            .make_post_request(&request_body, &uri)
3183            .await;
3184
3185        error_mock.assert();
3186        success_mock.assert();
3187        assert!(result.is_ok());
3188    }
3189
3190    #[tokio::test]
3191    async fn test_make_post_request_respect_retry_after_header() {
3192        let mut server = Server::new_async().await;
3193
3194        // First request returns 429 with retry-after, second succeeds
3195        let rate_limit_mock = server
3196            .mock("POST", "/test")
3197            .with_status(429)
3198            .with_header("Retry-After", "1") // 1 second
3199            .expect(1)
3200            .create_async()
3201            .await;
3202
3203        let success_mock = server
3204            .mock("POST", "/test")
3205            .with_status(200)
3206            .with_body(r#"{"success": true}"#)
3207            .expect(1)
3208            .create_async()
3209            .await;
3210
3211        let http_client =
3212            HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3213                .unwrap()
3214                .with_test_backoff_policy();
3215        let request_body = serde_json::json!({"test": "data"});
3216        let uri = format!("{}/test", server.url());
3217
3218        let start = std::time::Instant::now();
3219        let result = http_client
3220            .make_post_request(&request_body, &uri)
3221            .await;
3222        let elapsed = start.elapsed();
3223
3224        rate_limit_mock.assert();
3225        success_mock.assert();
3226        assert!(result.is_ok());
3227
3228        // Should have waited at least 1 second due to retry-after header
3229        assert!(elapsed >= Duration::from_millis(900)); // Allow some tolerance
3230        assert!(elapsed <= Duration::from_millis(2000)); // Upper bound for test stability
3231    }
3232
3233    #[tokio::test]
3234    async fn test_make_post_request_permanent_error() {
3235        let mut server = Server::new_async().await;
3236
3237        let mock = server
3238            .mock("POST", "/test")
3239            .with_status(400) // Bad Request - should not be retried
3240            .with_body("Bad Request")
3241            .expect(1)
3242            .create_async()
3243            .await;
3244
3245        let http_client =
3246            HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3247                .unwrap()
3248                .with_test_backoff_policy();
3249        let request_body = serde_json::json!({"test": "data"});
3250        let uri = format!("{}/test", server.url());
3251
3252        let result = http_client
3253            .make_post_request(&request_body, &uri)
3254            .await;
3255
3256        mock.assert();
3257        assert!(result.is_ok()); // 400 doesn't trigger retry logic, just returns the response
3258
3259        let response = result.unwrap();
3260        assert_eq!(response.status(), 400);
3261    }
3262
3263    #[tokio::test]
3264    async fn test_concurrent_requests_with_different_retry_after() {
3265        let mut server = Server::new_async().await;
3266
3267        // First request gets rate limited with 1 second retry-after
3268        let rate_limit_mock_1 = server
3269            .mock("POST", "/test1")
3270            .with_status(429)
3271            .with_header("Retry-After", "1")
3272            .expect(1)
3273            .create_async()
3274            .await;
3275
3276        // Second request gets rate limited with 2 second retry-after
3277        let rate_limit_mock_2 = server
3278            .mock("POST", "/test2")
3279            .with_status(429)
3280            .with_header("Retry-After", "2")
3281            .expect(1)
3282            .create_async()
3283            .await;
3284
3285        // Success mocks for retries
3286        let success_mock_1 = server
3287            .mock("POST", "/test1")
3288            .with_status(200)
3289            .with_body(r#"{"result": "success1"}"#)
3290            .expect(1)
3291            .create_async()
3292            .await;
3293
3294        let success_mock_2 = server
3295            .mock("POST", "/test2")
3296            .with_status(200)
3297            .with_body(r#"{"result": "success2"}"#)
3298            .expect(1)
3299            .create_async()
3300            .await;
3301
3302        let http_client =
3303            HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3304                .unwrap()
3305                .with_test_backoff_policy();
3306        let request_body = serde_json::json!({"test": "data"});
3307
3308        let uri1 = format!("{}/test1", server.url());
3309        let uri2 = format!("{}/test2", server.url());
3310
3311        // Start both requests concurrently
3312        let start = std::time::Instant::now();
3313        let (result1, result2) = tokio::join!(
3314            http_client.make_post_request(&request_body, &uri1),
3315            http_client.make_post_request(&request_body, &uri2)
3316        );
3317        let elapsed = start.elapsed();
3318
3319        rate_limit_mock_1.assert();
3320        rate_limit_mock_2.assert();
3321        success_mock_1.assert();
3322        success_mock_2.assert();
3323
3324        assert!(result1.is_ok());
3325        assert!(result2.is_ok());
3326
3327        // Both requests should succeed, but the second should take longer due to the 2s retry-after
3328        // The total time should be at least 2 seconds since the shared retry_after state
3329        // gets updated by both requests
3330        assert!(elapsed >= Duration::from_millis(1800)); // Allow some tolerance
3331        assert!(elapsed <= Duration::from_millis(3000)); // Upper bound
3332
3333        // Check the final retry_after state - should be the latest (higher) value
3334        let final_retry_after = http_client.retry_after.read().await;
3335        assert!(final_retry_after.is_some());
3336
3337        // The retry_after should be set to the latest (higher) value from the two requests
3338        if let Some(retry_time) = *final_retry_after {
3339            // The retry_after time might be in the past now since we waited,
3340            // but it should be reasonable (not too far in past/future)
3341            let now = SystemTime::now();
3342            let diff = if retry_time > now {
3343                retry_time.duration_since(now).unwrap()
3344            } else {
3345                now.duration_since(retry_time).unwrap()
3346            };
3347
3348            // Should be within a reasonable range (the 2s retry-after plus some buffer)
3349            assert!(diff <= Duration::from_secs(3), "Retry time difference too large: {:?}", diff);
3350        }
3351    }
3352
3353    #[tokio::test]
3354    async fn test_get_snapshots() {
3355        let mut server = Server::new_async().await;
3356
3357        // Mock protocol states response
3358        let protocol_states_resp = r#"
3359        {
3360            "states": [
3361                {
3362                    "component_id": "component1",
3363                    "attributes": {
3364                        "attribute_1": "0x00000000000003e8"
3365                    },
3366                    "balances": {
3367                        "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2": "0x01f4"
3368                    }
3369                }
3370            ],
3371            "pagination": {
3372                "page": 0,
3373                "page_size": 100,
3374                "total": 1
3375            }
3376        }
3377        "#;
3378
3379        // Mock contract state response
3380        let contract_state_resp = r#"
3381        {
3382            "accounts": [
3383                {
3384                    "chain": "ethereum",
3385                    "address": "0x1111111111111111111111111111111111111111",
3386                    "title": "",
3387                    "slots": {},
3388                    "native_balance": "0x01f4",
3389                    "token_balances": {},
3390                    "code": "0x00",
3391                    "code_hash": "0x5c06b7c5b3d910fd33bc2229846f9ddaf91d584d9b196e16636901ac3a77077e",
3392                    "balance_modify_tx": "0x0000000000000000000000000000000000000000000000000000000000000000",
3393                    "code_modify_tx": "0x0000000000000000000000000000000000000000000000000000000000000000",
3394                    "creation_tx": null
3395                }
3396            ],
3397            "pagination": {
3398                "page": 0,
3399                "page_size": 100,
3400                "total": 1
3401            }
3402        }
3403        "#;
3404
3405        // Mock component TVL response
3406        let tvl_resp = r#"
3407        {
3408            "tvl": {
3409                "component1": 1000000.0
3410            },
3411            "pagination": {
3412                "page": 0,
3413                "page_size": 100,
3414                "total": 1
3415            }
3416        }
3417        "#;
3418
3419        let protocol_states_mock = server
3420            .mock("POST", "/v1/protocol_state")
3421            .expect(1)
3422            .with_body(protocol_states_resp)
3423            .create_async()
3424            .await;
3425
3426        let contract_state_mock = server
3427            .mock("POST", "/v1/contract_state")
3428            .expect(1)
3429            .with_body(contract_state_resp)
3430            .create_async()
3431            .await;
3432
3433        let tvl_mock = server
3434            .mock("POST", "/v1/component_tvl")
3435            .expect(1)
3436            .with_body(tvl_resp)
3437            .create_async()
3438            .await;
3439
3440        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3441            .expect("create client");
3442
3443        let component = tycho_common::models::protocol::ProtocolComponent {
3444            id: "component1".to_string(),
3445            protocol_system: "test_protocol".to_string(),
3446            protocol_type_name: "test_type".to_string(),
3447            chain: Chain::Ethereum,
3448            tokens: vec![],
3449            contract_addresses: vec![
3450                Bytes::from_str("0x1111111111111111111111111111111111111111").unwrap()
3451            ],
3452            static_attributes: HashMap::new(),
3453            change: tycho_common::models::ChangeType::Creation,
3454            creation_tx: Bytes::from_str(
3455                "0x0000000000000000000000000000000000000000000000000000000000000000",
3456            )
3457            .unwrap(),
3458            created_at: chrono::Utc::now().naive_utc(),
3459        };
3460
3461        let mut components = HashMap::new();
3462        components.insert("component1".to_string(), component);
3463
3464        let contract_ids =
3465            vec![Bytes::from_str("0x1111111111111111111111111111111111111111").unwrap()];
3466
3467        let request = SnapshotParameters::new(
3468            Chain::Ethereum,
3469            "test_protocol",
3470            &components,
3471            &contract_ids,
3472            12345,
3473        );
3474
3475        let response = client
3476            .get_snapshots(&request, None, RPC_CLIENT_CONCURRENCY)
3477            .await
3478            .expect("get snapshots");
3479
3480        // Verify all mocks were called
3481        protocol_states_mock.assert();
3482        contract_state_mock.assert();
3483        tvl_mock.assert();
3484
3485        // Assert states
3486        assert_eq!(response.states.len(), 1);
3487        assert!(response
3488            .states
3489            .contains_key("component1"));
3490
3491        // Check that the state has the expected TVL
3492        let component_state = response
3493            .states
3494            .get("component1")
3495            .unwrap();
3496        assert_eq!(component_state.component_tvl, Some(1000000.0));
3497
3498        // Assert VM storage
3499        assert_eq!(response.vm_storage.len(), 1);
3500        let contract_addr = Bytes::from_str("0x1111111111111111111111111111111111111111").unwrap();
3501        assert!(response
3502            .vm_storage
3503            .contains_key(&contract_addr));
3504    }
3505
3506    #[tokio::test]
3507    async fn test_get_snapshots_empty_components() {
3508        let server = Server::new_async().await;
3509        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3510            .expect("create client");
3511
3512        let components = HashMap::new();
3513        let contract_ids = vec![];
3514
3515        let request = SnapshotParameters::new(
3516            Chain::Ethereum,
3517            "test_protocol",
3518            &components,
3519            &contract_ids,
3520            12345,
3521        );
3522
3523        let response = client
3524            .get_snapshots(&request, None, RPC_CLIENT_CONCURRENCY)
3525            .await
3526            .expect("get snapshots");
3527
3528        // Should return empty response without making any requests
3529        assert!(response.states.is_empty());
3530        assert!(response.vm_storage.is_empty());
3531    }
3532
3533    #[tokio::test]
3534    async fn test_get_snapshots_without_tvl() {
3535        let mut server = Server::new_async().await;
3536
3537        let protocol_states_resp = r#"
3538        {
3539            "states": [
3540                {
3541                    "component_id": "component1",
3542                    "attributes": {},
3543                    "balances": {}
3544                }
3545            ],
3546            "pagination": {
3547                "page": 0,
3548                "page_size": 100,
3549                "total": 1
3550            }
3551        }
3552        "#;
3553
3554        let protocol_states_mock = server
3555            .mock("POST", "/v1/protocol_state")
3556            .expect(1)
3557            .with_body(protocol_states_resp)
3558            .create_async()
3559            .await;
3560
3561        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3562            .expect("create client");
3563
3564        // Create test component
3565        let component = tycho_common::models::protocol::ProtocolComponent {
3566            id: "component1".to_string(),
3567            protocol_system: "test_protocol".to_string(),
3568            protocol_type_name: "test_type".to_string(),
3569            chain: Chain::Ethereum,
3570            tokens: vec![],
3571            contract_addresses: vec![],
3572            static_attributes: HashMap::new(),
3573            change: tycho_common::models::ChangeType::Creation,
3574            creation_tx: Bytes::from_str(
3575                "0x0000000000000000000000000000000000000000000000000000000000000000",
3576            )
3577            .unwrap(),
3578            created_at: chrono::Utc::now().naive_utc(),
3579        };
3580
3581        let mut components = HashMap::new();
3582        components.insert("component1".to_string(), component);
3583        let contract_ids = vec![];
3584
3585        let request = SnapshotParameters::new(
3586            Chain::Ethereum,
3587            "test_protocol",
3588            &components,
3589            &contract_ids,
3590            12345,
3591        )
3592        .include_balances(false)
3593        .include_tvl(false);
3594
3595        let response = client
3596            .get_snapshots(&request, None, RPC_CLIENT_CONCURRENCY)
3597            .await
3598            .expect("get snapshots");
3599
3600        // Verify only necessary mocks were called
3601        protocol_states_mock.assert();
3602        // No contract_state_mock.assert() since contract_ids is empty
3603        // No tvl_mock.assert() since include_tvl is false
3604
3605        assert_eq!(response.states.len(), 1);
3606        // Check that TVL is None since we didn't request it
3607        let component_state = response
3608            .states
3609            .get("component1")
3610            .unwrap();
3611        assert_eq!(component_state.component_tvl, None);
3612    }
3613
3614    #[tokio::test]
3615    async fn test_compression_enabled() {
3616        let mut server = Server::new_async().await;
3617        let server_resp = GET_CONTRACT_STATE_RESP;
3618
3619        // Compress the response using zstd
3620        let compressed_body =
3621            zstd::encode_all(server_resp.as_bytes(), 0).expect("compression failed");
3622
3623        let mocked_server = server
3624            .mock("POST", "/v1/contract_state")
3625            .expect(1)
3626            .with_header("Content-Encoding", "zstd")
3627            .with_body(compressed_body)
3628            .create_async()
3629            .await;
3630
3631        // Create client with compression enabled
3632        let client = HttpRPCClient::new(
3633            server.url().as_str(),
3634            HttpRPCClientOptions::new().with_compression(true),
3635        )
3636        .expect("create client");
3637
3638        let response = client
3639            .get_contract_state(ContractStateParams::new(Chain::Ethereum, ""))
3640            .await
3641            .expect("get state");
3642        let accounts = response;
3643
3644        mocked_server.assert();
3645        assert_eq!(accounts.data().len(), 1);
3646        assert_eq!(accounts.data()[0].native_balance, Bytes::from(500u16.to_be_bytes()));
3647    }
3648
3649    #[tokio::test]
3650    async fn test_compression_disabled() {
3651        let mut server = Server::new_async().await;
3652        let server_resp = GET_CONTRACT_STATE_RESP;
3653
3654        // Verify client does NOT send Accept-Encoding: zstd when compression is disabled
3655        // Instead, server should receive request without compression headers
3656        let mocked_server = server
3657            .mock("POST", "/v1/contract_state")
3658            .expect(1)
3659            .match_header("Accept-Encoding", mockito::Matcher::Missing)
3660            .with_status(200)
3661            .with_body(server_resp)
3662            .create_async()
3663            .await;
3664
3665        // Create client with compression disabled
3666        let client = HttpRPCClient::new(
3667            server.url().as_str(),
3668            HttpRPCClientOptions::new().with_compression(false),
3669        )
3670        .expect("create client");
3671
3672        let response = client
3673            .get_contract_state(ContractStateParams::new(Chain::Ethereum, ""))
3674            .await
3675            .expect("get state");
3676        let accounts = response;
3677
3678        // Verify the mock was called (client sent request without Accept-Encoding header)
3679        mocked_server.assert();
3680        assert_eq!(accounts.data().len(), 1);
3681        assert_eq!(accounts.data()[0].native_balance, Bytes::from(500u16.to_be_bytes()));
3682    }
3683
3684    #[rstest]
3685    #[case::single_page(2, 1000)]
3686    #[case::multiple_pages_within_concurrency(10, 2)]
3687    #[case::exceeds_concurrency_limit(60, 2)]
3688    #[tokio::test]
3689    async fn test_get_all_tokens_pagination_and_concurrency(
3690        #[case] total_tokens: usize,
3691        #[case] page_size: usize,
3692    ) {
3693        use std::sync::atomic::{AtomicUsize, Ordering};
3694
3695        let allowed_concurrency = 10;
3696
3697        let concurrent_requests = Arc::new(AtomicUsize::new(0));
3698        let max_concurrent = Arc::new(AtomicUsize::new(0));
3699
3700        let mut server = Server::new_async().await;
3701
3702        let total_pages = total_tokens.div_ceil(page_size);
3703
3704        // Mock all required pages
3705        for page in 0..total_pages {
3706            let concurrent = concurrent_requests.clone();
3707            let max_conc = max_concurrent.clone();
3708
3709            let tokens_in_page = {
3710                let start_idx = page * page_size;
3711                let end_idx = ((page + 1) * page_size).min(total_tokens);
3712                (start_idx..end_idx)
3713                    .map(|i| {
3714                        format!(
3715                            r#"{{
3716                            "chain": "ethereum",
3717                            "address": "0x{i:040x}",
3718                            "symbol": "TOKEN_{i}",
3719                            "decimals": 18,
3720                            "tax": 0,
3721                            "gas": [30000],
3722                            "quality": 100
3723                        }}"#
3724                        )
3725                    })
3726                    .collect::<Vec<_>>()
3727            };
3728
3729            let tokens_json = tokens_in_page.join(",");
3730            let response = format!(
3731                r#"{{
3732                    "tokens": [{tokens_json}],
3733                    "pagination": {{
3734                        "page": {page},
3735                        "page_size": {page_size},
3736                        "total": {total_tokens}
3737                    }}
3738                }}"#,
3739            );
3740
3741            server
3742                .mock("POST", "/v1/tokens")
3743                .expect(1)
3744                .with_chunked_body(move |w| {
3745                    // Track concurrent requests
3746                    let current = concurrent.fetch_add(1, Ordering::SeqCst);
3747                    max_conc.fetch_max(current + 1, Ordering::SeqCst);
3748
3749                    // Simulate some work to increase likelihood of concurrent requests
3750                    std::thread::sleep(Duration::from_millis(10));
3751
3752                    concurrent.fetch_sub(1, Ordering::SeqCst);
3753
3754                    w.write_all(response.as_bytes())
3755                })
3756                .create_async()
3757                .await;
3758        }
3759
3760        let client = HttpRPCClient::new(server.url().as_str(), HttpRPCClientOptions::default())
3761            .expect("create client");
3762
3763        let tokens = client
3764            .get_all_tokens(
3765                AllTokensParams::new(Chain::Ethereum, allowed_concurrency)
3766                    .with_chunk_size(page_size),
3767            )
3768            .await
3769            .expect("get all tokens");
3770
3771        // Verify concurrency was respected
3772        let max = max_concurrent.load(Ordering::SeqCst);
3773        let expected_max_concurrency = total_pages
3774            .saturating_sub(1)
3775            .min(allowed_concurrency);
3776        assert!(
3777            max <= allowed_concurrency,
3778            "Expected max concurrent requests <= {allowed_concurrency}, got {max}"
3779        );
3780
3781        // For cases with multiple pages, verify we actually used concurrency
3782        if total_pages > 1 && expected_max_concurrency > 1 {
3783            assert!(
3784                max > 0,
3785                "Expected some concurrent requests for multi-page response, got {max}"
3786            );
3787        }
3788
3789        // Verify we got all expected tokens
3790        assert_eq!(
3791            tokens.len(),
3792            total_tokens,
3793            "Expected {total_tokens} tokens, got {}",
3794            tokens.len()
3795        );
3796
3797        // Verify tokens are in the expected order
3798        for (i, token) in tokens.iter().enumerate() {
3799            assert_eq!(token.symbol, format!("TOKEN_{i}"), "Token at index {i} has wrong symbol");
3800        }
3801    }
3802}