aptos-sdk 0.4.1

A user-friendly, idiomatic Rust SDK for the Aptos blockchain
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
//! Network configuration for the Aptos SDK.
//!
//! This module provides configuration options for connecting to different
//! Aptos networks (mainnet, testnet, devnet) or custom endpoints.

use crate::error::{AptosError, AptosResult};
use crate::retry::RetryConfig;
use crate::types::ChainId;
use std::time::Duration;
use url::Url;

/// Validates that a URL uses a safe scheme (http or https).
///
/// # Security
///
/// This prevents SSRF attacks via dangerous URL schemes like `file://`, `gopher://`, etc.
/// For production use, HTTPS is strongly recommended. HTTP is permitted (e.g., for local
/// development) but no host restrictions are enforced by this function.
///
/// # Errors
///
/// Returns [`AptosError::Config`] if the URL scheme is not `http` or `https`.
pub fn validate_url_scheme(url: &Url) -> AptosResult<()> {
    match url.scheme() {
        "https" => Ok(()),
        "http" => {
            // HTTP is allowed for local development and testing
            Ok(())
        }
        scheme => Err(AptosError::Config(format!(
            "unsupported URL scheme '{scheme}': only 'http' and 'https' are allowed"
        ))),
    }
}

/// Reads a response body with an enforced size limit, aborting early if exceeded.
///
/// Unlike `response.bytes().await?` which buffers the entire response in memory
/// before any size check, this function:
/// 1. Pre-checks the `Content-Length` header (if present) to reject obviously
///    oversized responses before reading any body data.
/// 2. Reads the body incrementally via chunked streaming, aborting as soon as
///    the accumulated size exceeds `max_size`.
///
/// This prevents memory exhaustion from malicious servers that send huge
/// responses (including chunked transfer-encoding without `Content-Length`).
///
/// # Errors
///
/// Returns [`AptosError::Api`] with error code `RESPONSE_TOO_LARGE` if the
/// response body exceeds `max_size` bytes.
pub async fn read_response_bounded(
    mut response: reqwest::Response,
    max_size: usize,
) -> AptosResult<Vec<u8>> {
    // Pre-check Content-Length header for early rejection (avoids reading any body)
    if let Some(content_length) = response.content_length()
        && content_length > max_size as u64
    {
        return Err(AptosError::Api {
            status_code: response.status().as_u16(),
            message: format!(
                "response too large: Content-Length {content_length} bytes exceeds limit of {max_size} bytes"
            ),
            error_code: Some("RESPONSE_TOO_LARGE".into()),
            vm_error_code: None,
        });
    }

    // Read body incrementally, aborting if accumulated size exceeds the limit.
    // This protects against chunked transfer-encoding that bypasses Content-Length.
    let mut body = Vec::with_capacity(std::cmp::min(max_size, 1024 * 1024));
    while let Some(chunk) = response.chunk().await? {
        if body.len().saturating_add(chunk.len()) > max_size {
            return Err(AptosError::Api {
                status_code: response.status().as_u16(),
                message: format!(
                    "response too large: exceeded limit of {max_size} bytes during streaming"
                ),
                error_code: Some("RESPONSE_TOO_LARGE".into()),
                vm_error_code: None,
            });
        }
        body.extend_from_slice(&chunk);
    }

    Ok(body)
}

/// Configuration for HTTP connection pooling.
///
/// Controls how connections are reused across requests for better performance.
#[derive(Debug, Clone)]
pub struct PoolConfig {
    /// Maximum number of idle connections per host.
    /// Default: unlimited (no limit)
    pub max_idle_per_host: Option<usize>,
    /// Maximum total idle connections in the pool.
    /// Default: 100
    pub max_idle_total: usize,
    /// How long to keep idle connections alive.
    /// Default: 90 seconds
    pub idle_timeout: Duration,
    /// Whether to enable TCP keepalive.
    /// Default: true
    pub tcp_keepalive: Option<Duration>,
    /// Whether to enable TCP nodelay (disable Nagle's algorithm).
    /// Default: true
    pub tcp_nodelay: bool,
    /// Maximum response body size in bytes.
    /// Default: 10 MB (`10_485_760` bytes)
    ///
    /// # Security
    ///
    /// This limit helps prevent memory exhaustion from extremely large responses.
    /// The Aptos API responses are typically much smaller than this limit.
    pub max_response_size: usize,
}

/// Default maximum response size: 10 MB
///
/// # Security
///
/// This limit helps prevent memory exhaustion from malicious or compromised
/// servers sending extremely large responses. The default of 10 MB is generous
/// for normal Aptos API responses (typically under 1 MB). If you need to
/// handle larger responses (e.g., bulk data exports), increase this via
/// [`PoolConfigBuilder::max_response_size`].
const DEFAULT_MAX_RESPONSE_SIZE: usize = 10 * 1024 * 1024;

impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            max_idle_per_host: None, // unlimited
            max_idle_total: 100,
            idle_timeout: Duration::from_secs(90),
            tcp_keepalive: Some(Duration::from_secs(60)),
            tcp_nodelay: true,
            max_response_size: DEFAULT_MAX_RESPONSE_SIZE,
        }
    }
}

impl PoolConfig {
    /// Creates a new pool configuration builder.
    pub fn builder() -> PoolConfigBuilder {
        PoolConfigBuilder::default()
    }

    /// Creates a configuration optimized for high-throughput scenarios.
    ///
    /// - More idle connections
    /// - Longer idle timeout
    /// - TCP keepalive enabled
    pub fn high_throughput() -> Self {
        Self {
            max_idle_per_host: Some(32),
            max_idle_total: 256,
            idle_timeout: Duration::from_secs(300),
            tcp_keepalive: Some(Duration::from_secs(30)),
            tcp_nodelay: true,
            max_response_size: DEFAULT_MAX_RESPONSE_SIZE,
        }
    }

    /// Creates a configuration optimized for low-latency scenarios.
    ///
    /// - Fewer idle connections (fresher connections)
    /// - Shorter idle timeout
    /// - TCP nodelay enabled
    pub fn low_latency() -> Self {
        Self {
            max_idle_per_host: Some(8),
            max_idle_total: 32,
            idle_timeout: Duration::from_secs(30),
            tcp_keepalive: Some(Duration::from_secs(15)),
            tcp_nodelay: true,
            max_response_size: DEFAULT_MAX_RESPONSE_SIZE,
        }
    }

    /// Creates a minimal configuration for constrained environments.
    ///
    /// - Minimal idle connections
    /// - Short idle timeout
    pub fn minimal() -> Self {
        Self {
            max_idle_per_host: Some(2),
            max_idle_total: 8,
            idle_timeout: Duration::from_secs(10),
            tcp_keepalive: None,
            tcp_nodelay: true,
            max_response_size: DEFAULT_MAX_RESPONSE_SIZE,
        }
    }
}

/// Builder for `PoolConfig`.
#[derive(Debug, Clone, Default)]
#[allow(clippy::option_option)] // Intentional: distinguishes "not set" from "explicitly set to None"
pub struct PoolConfigBuilder {
    max_idle_per_host: Option<usize>,
    max_idle_total: Option<usize>,
    idle_timeout: Option<Duration>,
    /// None = not set (use default), Some(None) = explicitly disabled, Some(Some(d)) = explicitly set
    tcp_keepalive: Option<Option<Duration>>,
    tcp_nodelay: Option<bool>,
    max_response_size: Option<usize>,
}

impl PoolConfigBuilder {
    /// Sets the maximum idle connections per host.
    #[must_use]
    pub fn max_idle_per_host(mut self, max: usize) -> Self {
        self.max_idle_per_host = Some(max);
        self
    }

    /// Removes the limit on idle connections per host.
    #[must_use]
    pub fn unlimited_idle_per_host(mut self) -> Self {
        self.max_idle_per_host = None;
        self
    }

    /// Sets the maximum total idle connections.
    #[must_use]
    pub fn max_idle_total(mut self, max: usize) -> Self {
        self.max_idle_total = Some(max);
        self
    }

    /// Sets the idle connection timeout.
    #[must_use]
    pub fn idle_timeout(mut self, timeout: Duration) -> Self {
        self.idle_timeout = Some(timeout);
        self
    }

    /// Sets the TCP keepalive interval.
    #[must_use]
    pub fn tcp_keepalive(mut self, interval: Duration) -> Self {
        self.tcp_keepalive = Some(Some(interval));
        self
    }

    /// Disables TCP keepalive.
    #[must_use]
    pub fn no_tcp_keepalive(mut self) -> Self {
        self.tcp_keepalive = Some(None);
        self
    }

    /// Sets whether to enable TCP nodelay.
    #[must_use]
    pub fn tcp_nodelay(mut self, enabled: bool) -> Self {
        self.tcp_nodelay = Some(enabled);
        self
    }

    /// Sets the maximum response body size in bytes.
    ///
    /// # Security
    ///
    /// This helps prevent memory exhaustion from extremely large responses.
    #[must_use]
    pub fn max_response_size(mut self, size: usize) -> Self {
        self.max_response_size = Some(size);
        self
    }

    /// Builds the pool configuration.
    pub fn build(self) -> PoolConfig {
        let default = PoolConfig::default();
        PoolConfig {
            max_idle_per_host: self.max_idle_per_host.or(default.max_idle_per_host),
            max_idle_total: self.max_idle_total.unwrap_or(default.max_idle_total),
            idle_timeout: self.idle_timeout.unwrap_or(default.idle_timeout),
            tcp_keepalive: self.tcp_keepalive.unwrap_or(default.tcp_keepalive),
            tcp_nodelay: self.tcp_nodelay.unwrap_or(default.tcp_nodelay),
            max_response_size: self.max_response_size.unwrap_or(default.max_response_size),
        }
    }
}

/// Configuration for the Aptos client.
///
/// Use the builder methods to customize the configuration, or use one of the
/// preset configurations like [`AptosConfig::mainnet()`], [`AptosConfig::testnet()`],
/// or [`AptosConfig::devnet()`].
///
/// # Example
///
/// ```rust
/// use aptos_sdk::AptosConfig;
/// use aptos_sdk::retry::RetryConfig;
/// use aptos_sdk::config::PoolConfig;
///
/// // Use testnet with default settings
/// let config = AptosConfig::testnet();
///
/// // Custom configuration with retry and connection pooling
/// let config = AptosConfig::testnet()
///     .with_timeout(std::time::Duration::from_secs(30))
///     .with_retry(RetryConfig::aggressive())
///     .with_pool(PoolConfig::high_throughput());
/// ```
#[derive(Debug, Clone)]
pub struct AptosConfig {
    /// The network to connect to
    pub(crate) network: Network,
    /// REST API URL (fullnode)
    pub(crate) fullnode_url: Url,
    /// Indexer GraphQL URL (optional)
    pub(crate) indexer_url: Option<Url>,
    /// Faucet URL (optional, for testnets)
    pub(crate) faucet_url: Option<Url>,
    /// Request timeout
    pub(crate) timeout: Duration,
    /// Retry configuration for transient failures
    pub(crate) retry_config: RetryConfig,
    /// Connection pool configuration
    pub(crate) pool_config: PoolConfig,
    /// Optional API key for authenticated access
    pub(crate) api_key: Option<String>,
}

/// Known Aptos networks.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Network {
    /// Aptos mainnet
    Mainnet,
    /// Aptos testnet
    Testnet,
    /// Aptos devnet
    Devnet,
    /// Local development network
    Local,
    /// Custom network
    Custom,
}

impl Network {
    /// Returns the chain ID for this network.
    pub fn chain_id(&self) -> ChainId {
        match self {
            Network::Mainnet => ChainId::mainnet(),
            Network::Testnet => ChainId::testnet(),
            Network::Devnet => ChainId::new(165), // Devnet chain ID
            Network::Local => ChainId::new(4),    // Local testing chain ID
            Network::Custom => ChainId::new(0),   // Must be set manually
        }
    }

    /// Returns the network name as a string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Network::Mainnet => "mainnet",
            Network::Testnet => "testnet",
            Network::Devnet => "devnet",
            Network::Local => "local",
            Network::Custom => "custom",
        }
    }
}

impl Default for AptosConfig {
    fn default() -> Self {
        Self::devnet()
    }
}

impl AptosConfig {
    /// Creates a configuration for Aptos mainnet.
    ///
    /// # Example
    ///
    /// ```rust
    /// use aptos_sdk::AptosConfig;
    ///
    /// let config = AptosConfig::mainnet();
    /// ```
    #[allow(clippy::missing_panics_doc)]
    #[must_use]
    pub fn mainnet() -> Self {
        Self {
            network: Network::Mainnet,
            fullnode_url: Url::parse("https://fullnode.mainnet.aptoslabs.com/v1")
                .expect("valid mainnet URL"),
            indexer_url: Some(
                Url::parse("https://indexer.mainnet.aptoslabs.com/v1/graphql")
                    .expect("valid indexer URL"),
            ),
            faucet_url: None, // No faucet on mainnet
            timeout: Duration::from_secs(30),
            retry_config: RetryConfig::conservative(), // More conservative for mainnet
            pool_config: PoolConfig::default(),
            api_key: None,
        }
    }

    /// Creates a configuration for Aptos testnet.
    ///
    /// # Example
    ///
    /// ```rust
    /// use aptos_sdk::AptosConfig;
    ///
    /// let config = AptosConfig::testnet();
    /// ```
    #[allow(clippy::missing_panics_doc)]
    #[must_use]
    pub fn testnet() -> Self {
        Self {
            network: Network::Testnet,
            fullnode_url: Url::parse("https://fullnode.testnet.aptoslabs.com/v1")
                .expect("valid testnet URL"),
            indexer_url: Some(
                Url::parse("https://indexer.testnet.aptoslabs.com/v1/graphql")
                    .expect("valid indexer URL"),
            ),
            faucet_url: Some(
                Url::parse("https://faucet.testnet.aptoslabs.com").expect("valid faucet URL"),
            ),
            timeout: Duration::from_secs(30),
            retry_config: RetryConfig::default(),
            pool_config: PoolConfig::default(),
            api_key: None,
        }
    }

    /// Creates a configuration for Aptos devnet.
    ///
    /// # Example
    ///
    /// ```rust
    /// use aptos_sdk::AptosConfig;
    ///
    /// let config = AptosConfig::devnet();
    /// ```
    #[allow(clippy::missing_panics_doc)]
    #[must_use]
    pub fn devnet() -> Self {
        Self {
            network: Network::Devnet,
            fullnode_url: Url::parse("https://fullnode.devnet.aptoslabs.com/v1")
                .expect("valid devnet URL"),
            indexer_url: Some(
                Url::parse("https://indexer.devnet.aptoslabs.com/v1/graphql")
                    .expect("valid indexer URL"),
            ),
            faucet_url: Some(
                Url::parse("https://faucet.devnet.aptoslabs.com").expect("valid faucet URL"),
            ),
            timeout: Duration::from_secs(30),
            retry_config: RetryConfig::default(),
            pool_config: PoolConfig::default(),
            api_key: None,
        }
    }

    /// Creates a configuration for a local development network.
    ///
    /// This assumes the local network is running on the default ports
    /// (REST API on 8080, faucet on 8081).
    ///
    /// # Example
    ///
    /// ```rust
    /// use aptos_sdk::AptosConfig;
    ///
    /// let config = AptosConfig::local();
    /// ```
    #[allow(clippy::missing_panics_doc)]
    #[must_use]
    pub fn local() -> Self {
        Self {
            network: Network::Local,
            fullnode_url: Url::parse("http://127.0.0.1:8080/v1").expect("valid local URL"),
            indexer_url: None,
            faucet_url: Some(Url::parse("http://127.0.0.1:8081").expect("valid local faucet URL")),
            timeout: Duration::from_secs(10),
            retry_config: RetryConfig::aggressive(), // Fast retries for local dev
            pool_config: PoolConfig::low_latency(),  // Low latency for local dev
            api_key: None,
        }
    }

    /// Creates a custom configuration with the specified fullnode URL.
    ///
    /// # Security
    ///
    /// Only `http://` and `https://` URL schemes are allowed. Using `https://` is
    /// strongly recommended for production. HTTP is acceptable only for localhost
    /// development environments.
    ///
    /// # Errors
    ///
    /// Returns an error if the `fullnode_url` cannot be parsed as a valid URL
    /// or uses an unsupported scheme (e.g., `file://`, `ftp://`).
    ///
    /// # Example
    ///
    /// ```rust
    /// use aptos_sdk::AptosConfig;
    ///
    /// let config = AptosConfig::custom("https://my-node.example.com/v1").unwrap();
    /// ```
    pub fn custom(fullnode_url: &str) -> AptosResult<Self> {
        let url = Url::parse(fullnode_url)?;
        validate_url_scheme(&url)?;
        Ok(Self {
            network: Network::Custom,
            fullnode_url: url,
            indexer_url: None,
            faucet_url: None,
            timeout: Duration::from_secs(30),
            retry_config: RetryConfig::default(),
            pool_config: PoolConfig::default(),
            api_key: None,
        })
    }

    /// Sets the request timeout.
    #[must_use]
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets the retry configuration for transient failures.
    ///
    /// # Example
    ///
    /// ```rust
    /// use aptos_sdk::AptosConfig;
    /// use aptos_sdk::retry::RetryConfig;
    ///
    /// let config = AptosConfig::testnet()
    ///     .with_retry(RetryConfig::aggressive());
    /// ```
    #[must_use]
    pub fn with_retry(mut self, retry_config: RetryConfig) -> Self {
        self.retry_config = retry_config;
        self
    }

    /// Disables automatic retry for API calls.
    ///
    /// This is equivalent to `with_retry(RetryConfig::no_retry())`.
    #[must_use]
    pub fn without_retry(mut self) -> Self {
        self.retry_config = RetryConfig::no_retry();
        self
    }

    /// Sets the maximum number of retries for transient failures.
    ///
    /// This is a convenience method that modifies the retry config.
    #[must_use]
    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
        self.retry_config = RetryConfig::builder()
            .max_retries(max_retries)
            .initial_delay_ms(self.retry_config.initial_delay_ms)
            .max_delay_ms(self.retry_config.max_delay_ms)
            .exponential_base(self.retry_config.exponential_base)
            .jitter(self.retry_config.jitter)
            .build();
        self
    }

    /// Sets the connection pool configuration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use aptos_sdk::AptosConfig;
    /// use aptos_sdk::config::PoolConfig;
    ///
    /// let config = AptosConfig::testnet()
    ///     .with_pool(PoolConfig::high_throughput());
    /// ```
    #[must_use]
    pub fn with_pool(mut self, pool_config: PoolConfig) -> Self {
        self.pool_config = pool_config;
        self
    }

    /// Sets an API key for authenticated access.
    ///
    /// This is useful when using Aptos Build or other services that
    /// provide higher rate limits with API keys.
    #[must_use]
    pub fn with_api_key(mut self, api_key: impl Into<String>) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// Sets a custom indexer URL.
    ///
    /// # Security
    ///
    /// Only `http://` and `https://` URL schemes are allowed.
    ///
    /// # Errors
    ///
    /// Returns an error if the `url` cannot be parsed as a valid URL
    /// or uses an unsupported scheme.
    pub fn with_indexer_url(mut self, url: &str) -> AptosResult<Self> {
        let parsed = Url::parse(url)?;
        validate_url_scheme(&parsed)?;
        self.indexer_url = Some(parsed);
        Ok(self)
    }

    /// Sets a custom faucet URL.
    ///
    /// # Security
    ///
    /// Only `http://` and `https://` URL schemes are allowed.
    ///
    /// # Errors
    ///
    /// Returns an error if the `url` cannot be parsed as a valid URL
    /// or uses an unsupported scheme.
    pub fn with_faucet_url(mut self, url: &str) -> AptosResult<Self> {
        let parsed = Url::parse(url)?;
        validate_url_scheme(&parsed)?;
        self.faucet_url = Some(parsed);
        Ok(self)
    }

    /// Returns the network this config is for.
    pub fn network(&self) -> Network {
        self.network
    }

    /// Returns the fullnode URL.
    pub fn fullnode_url(&self) -> &Url {
        &self.fullnode_url
    }

    /// Returns the indexer URL, if configured.
    pub fn indexer_url(&self) -> Option<&Url> {
        self.indexer_url.as_ref()
    }

    /// Returns the faucet URL, if configured.
    pub fn faucet_url(&self) -> Option<&Url> {
        self.faucet_url.as_ref()
    }

    /// Returns the chain ID for this configuration.
    pub fn chain_id(&self) -> ChainId {
        self.network.chain_id()
    }

    /// Returns the retry configuration.
    pub fn retry_config(&self) -> &RetryConfig {
        &self.retry_config
    }

    /// Returns the request timeout.
    pub fn timeout(&self) -> Duration {
        self.timeout
    }

    /// Returns the connection pool configuration.
    pub fn pool_config(&self) -> &PoolConfig {
        &self.pool_config
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mainnet_config() {
        let config = AptosConfig::mainnet();
        assert_eq!(config.network(), Network::Mainnet);
        assert!(config.fullnode_url().as_str().contains("mainnet"));
        assert!(config.faucet_url().is_none());
    }

    #[test]
    fn test_testnet_config() {
        let config = AptosConfig::testnet();
        assert_eq!(config.network(), Network::Testnet);
        assert!(config.fullnode_url().as_str().contains("testnet"));
        assert!(config.faucet_url().is_some());
    }

    #[test]
    fn test_devnet_config() {
        let config = AptosConfig::devnet();
        assert_eq!(config.network(), Network::Devnet);
        assert!(config.fullnode_url().as_str().contains("devnet"));
        assert!(config.faucet_url().is_some());
        assert!(config.indexer_url().is_some());
    }

    #[test]
    fn test_local_config() {
        let config = AptosConfig::local();
        assert_eq!(config.network(), Network::Local);
        assert!(config.fullnode_url().as_str().contains("127.0.0.1"));
        assert!(config.faucet_url().is_some());
        assert!(config.indexer_url().is_none());
    }

    #[test]
    fn test_custom_config() {
        let config = AptosConfig::custom("https://custom.example.com/v1").unwrap();
        assert_eq!(config.network(), Network::Custom);
        assert_eq!(
            config.fullnode_url().as_str(),
            "https://custom.example.com/v1"
        );
    }

    #[test]
    fn test_custom_config_invalid_url() {
        let result = AptosConfig::custom("not a valid url");
        assert!(result.is_err());
    }

    #[test]
    fn test_builder_methods() {
        let config = AptosConfig::testnet()
            .with_timeout(Duration::from_secs(60))
            .with_max_retries(5)
            .with_api_key("test-key");

        assert_eq!(config.timeout, Duration::from_secs(60));
        assert_eq!(config.retry_config.max_retries, 5);
        assert_eq!(config.api_key, Some("test-key".to_string()));
    }

    #[test]
    fn test_retry_config() {
        let config = AptosConfig::testnet().with_retry(RetryConfig::aggressive());

        assert_eq!(config.retry_config.max_retries, 5);
        assert_eq!(config.retry_config.initial_delay_ms, 50);

        let config = AptosConfig::testnet().without_retry();
        assert_eq!(config.retry_config.max_retries, 0);
    }

    #[test]
    fn test_network_retry_defaults() {
        // Mainnet should be conservative
        let mainnet = AptosConfig::mainnet();
        assert_eq!(mainnet.retry_config.max_retries, 3);

        // Local should be aggressive
        let local = AptosConfig::local();
        assert_eq!(local.retry_config.max_retries, 5);
    }

    #[test]
    fn test_pool_config_default() {
        let config = PoolConfig::default();
        assert_eq!(config.max_idle_total, 100);
        assert_eq!(config.idle_timeout, Duration::from_secs(90));
        assert!(config.tcp_nodelay);
    }

    #[test]
    fn test_pool_config_presets() {
        let high = PoolConfig::high_throughput();
        assert_eq!(high.max_idle_per_host, Some(32));
        assert_eq!(high.max_idle_total, 256);

        let low = PoolConfig::low_latency();
        assert_eq!(low.max_idle_per_host, Some(8));
        assert_eq!(low.idle_timeout, Duration::from_secs(30));

        let minimal = PoolConfig::minimal();
        assert_eq!(minimal.max_idle_per_host, Some(2));
        assert_eq!(minimal.max_idle_total, 8);
    }

    #[test]
    fn test_pool_config_builder() {
        let config = PoolConfig::builder()
            .max_idle_per_host(16)
            .max_idle_total(64)
            .idle_timeout(Duration::from_secs(60))
            .tcp_nodelay(false)
            .build();

        assert_eq!(config.max_idle_per_host, Some(16));
        assert_eq!(config.max_idle_total, 64);
        assert_eq!(config.idle_timeout, Duration::from_secs(60));
        assert!(!config.tcp_nodelay);
    }

    #[test]
    fn test_pool_config_builder_tcp_keepalive() {
        let config = PoolConfig::builder()
            .tcp_keepalive(Duration::from_secs(30))
            .build();
        assert_eq!(config.tcp_keepalive, Some(Duration::from_secs(30)));

        let config = PoolConfig::builder().no_tcp_keepalive().build();
        assert_eq!(config.tcp_keepalive, None);
    }

    #[test]
    fn test_pool_config_builder_unlimited_idle() {
        let config = PoolConfig::builder().unlimited_idle_per_host().build();
        assert_eq!(config.max_idle_per_host, None);
    }

    #[test]
    fn test_aptos_config_with_pool() {
        let config = AptosConfig::testnet().with_pool(PoolConfig::high_throughput());

        assert_eq!(config.pool_config.max_idle_total, 256);
    }

    #[test]
    fn test_aptos_config_with_indexer_url() {
        let config = AptosConfig::testnet()
            .with_indexer_url("https://custom-indexer.example.com/graphql")
            .unwrap();
        assert_eq!(
            config.indexer_url().unwrap().as_str(),
            "https://custom-indexer.example.com/graphql"
        );
    }

    #[test]
    fn test_aptos_config_with_faucet_url() {
        let config = AptosConfig::mainnet()
            .with_faucet_url("https://custom-faucet.example.com")
            .unwrap();
        assert_eq!(
            config.faucet_url().unwrap().as_str(),
            "https://custom-faucet.example.com/"
        );
    }

    #[test]
    fn test_aptos_config_default() {
        let config = AptosConfig::default();
        assert_eq!(config.network(), Network::Devnet);
    }

    #[test]
    fn test_network_chain_id() {
        assert_eq!(Network::Mainnet.chain_id().id(), 1);
        assert_eq!(Network::Testnet.chain_id().id(), 2);
        assert_eq!(Network::Devnet.chain_id().id(), 165);
        assert_eq!(Network::Local.chain_id().id(), 4);
        assert_eq!(Network::Custom.chain_id().id(), 0);
    }

    #[test]
    fn test_network_as_str() {
        assert_eq!(Network::Mainnet.as_str(), "mainnet");
        assert_eq!(Network::Testnet.as_str(), "testnet");
        assert_eq!(Network::Devnet.as_str(), "devnet");
        assert_eq!(Network::Local.as_str(), "local");
        assert_eq!(Network::Custom.as_str(), "custom");
    }

    #[test]
    fn test_aptos_config_getters() {
        let config = AptosConfig::testnet();

        assert_eq!(config.timeout(), Duration::from_secs(30));
        assert!(config.retry_config().max_retries > 0);
        assert!(config.pool_config().max_idle_total > 0);
        assert_eq!(config.chain_id().id(), 2);
    }

    #[tokio::test]
    async fn test_read_response_bounded_normal() {
        use wiremock::{Mock, MockServer, ResponseTemplate, matchers::method};
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(200).set_body_string("hello world"))
            .mount(&server)
            .await;

        let response = reqwest::get(server.uri()).await.unwrap();
        let body = read_response_bounded(response, 1024).await.unwrap();
        assert_eq!(body, b"hello world");
    }

    #[tokio::test]
    async fn test_read_response_bounded_rejects_oversized_content_length() {
        use wiremock::{Mock, MockServer, ResponseTemplate, matchers::method};
        let server = MockServer::start().await;
        // Send a body whose accurate Content-Length exceeds the limit.
        // The function should reject based on Content-Length pre-check
        // before streaming the full body.
        let body = "x".repeat(200);
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(200).set_body_string(body))
            .mount(&server)
            .await;

        let response = reqwest::get(server.uri()).await.unwrap();
        // Limit is 100 but body is 200 -- should be rejected via Content-Length pre-check
        let result = read_response_bounded(response, 100).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("response too large"));
    }

    #[tokio::test]
    async fn test_read_response_bounded_rejects_oversized_body() {
        use wiremock::{Mock, MockServer, ResponseTemplate, matchers::method};
        let server = MockServer::start().await;
        let large_body = "x".repeat(500);
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(200).set_body_string(large_body))
            .mount(&server)
            .await;

        let response = reqwest::get(server.uri()).await.unwrap();
        let result = read_response_bounded(response, 100).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_read_response_bounded_exact_limit() {
        use wiremock::{Mock, MockServer, ResponseTemplate, matchers::method};
        let server = MockServer::start().await;
        let body = "x".repeat(100);
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(200).set_body_string(body.clone()))
            .mount(&server)
            .await;

        let response = reqwest::get(server.uri()).await.unwrap();
        let result = read_response_bounded(response, 100).await.unwrap();
        assert_eq!(result.len(), 100);
    }

    #[tokio::test]
    async fn test_read_response_bounded_empty() {
        use wiremock::{Mock, MockServer, ResponseTemplate, matchers::method};
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&server)
            .await;

        let response = reqwest::get(server.uri()).await.unwrap();
        let result = read_response_bounded(response, 1024).await.unwrap();
        assert!(result.is_empty());
    }
}