polymarket-sdk 0.0.5

Rust SDK for Polymarket prediction markets - REST APIs, WebSocket streams, order signing, and Safe wallet integration
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
//! Polymarket CLOB REST API Client
//!
//! This module provides a high-level client for interacting with the
//! Polymarket CLOB (Central Limit Order Book) REST API.
//!
//! ## Features
//!
//! - **API Credential Management**: Create, derive, and manage API credentials
//! - **Order Submission**: Submit, cancel, and query orders
//! - **Market Data**: Get order books and market information
//!
//! ## Example
//!
//! ```rust,ignore
//! use polymarket_sdk::clob::{ClobClient, ClobConfig};
//! use alloy_signer_local::PrivateKeySigner;
//!
//! let signer: PrivateKeySigner = "0x...".parse()?;
//! let client = ClobClient::new(ClobConfig::default(), signer)?;
//!
//! // Create API credentials (first time)
//! let creds = client.create_api_credentials().await?;
//!
//! // Submit an order
//! let order = client.submit_order(&signed_order).await?;
//! ```

use std::collections::HashMap;
use std::num::NonZeroU32;
use std::sync::Arc;
use std::time::Duration;

use alloy_primitives::U256;
use alloy_signer_local::PrivateKeySigner;
use governor::{Quota, RateLimiter as GovRateLimiter};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use tracing::{debug, info, instrument, warn};

use crate::auth::{
    create_l1_headers, create_l2_headers, create_l2_headers_with_body_string,
    get_current_unix_time_secs,
};
use crate::core::clob_api_url;
use crate::core::{PolymarketError, Result};
use crate::types::{ApiCredentials, SignedOrderRequest};

// Builder API authentication
use crate::auth::{BuilderApiKeyCreds, BuilderSigner};

type RateLimiter = GovRateLimiter<
    governor::state::NotKeyed,
    governor::state::InMemoryState,
    governor::clock::DefaultClock,
>;

/// CLOB API configuration
#[derive(Debug, Clone)]
pub struct ClobConfig {
    /// CLOB API base URL
    pub base_url: String,
    /// Request timeout
    pub timeout: Duration,
    /// Rate limit (requests per second)
    pub rate_limit_per_second: u32,
    /// User agent string
    pub user_agent: String,
}

impl Default for ClobConfig {
    fn default() -> Self {
        Self {
            // Use helper function to support env var override (POLYMARKET_CLOB_URL)
            base_url: clob_api_url(),
            timeout: Duration::from_secs(30),
            rate_limit_per_second: 5,
            user_agent: "polymarket-sdk/0.1.0".to_string(),
        }
    }
}

impl ClobConfig {
    /// Create a new configuration builder with defaults.
    #[must_use]
    pub fn builder() -> Self {
        Self::default()
    }

    /// Create config from environment variables.
    ///
    /// **Deprecated**: Use `ClobConfig::default()` instead.
    /// The default implementation already supports `POLYMARKET_CLOB_URL` env var override.
    #[must_use]
    #[deprecated(
        since = "0.1.0",
        note = "Use ClobConfig::default() instead. URL override via POLYMARKET_CLOB_URL env var is already supported."
    )]
    pub fn from_env() -> Self {
        Self::default()
    }

    /// Set base URL
    #[must_use]
    pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

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

    /// Set rate limit (requests per second)
    #[must_use]
    pub fn with_rate_limit(mut self, rate_limit: u32) -> Self {
        self.rate_limit_per_second = rate_limit;
        self
    }

    /// Set user agent string
    #[must_use]
    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.user_agent = user_agent.into();
        self
    }
}

/// Derive API key response
#[derive(Debug, Deserialize)]
pub struct DeriveApiKeyResponse {
    /// The derived API key
    #[serde(rename = "apiKey")]
    pub api_key: String,
    /// The API secret (base64 encoded)
    pub secret: String,
    /// The passphrase
    pub passphrase: String,
}

/// API key response
#[derive(Debug, Deserialize)]
pub struct ApiKeyResponse {
    /// API key
    #[serde(rename = "apiKey")]
    pub api_key: String,
    /// API secret
    pub secret: String,
    /// Passphrase
    pub passphrase: String,
}

/// Create API key request
#[derive(Debug, Serialize)]
struct CreateApiKeyRequest {
    /// Nonce from derive endpoint
    nonce: String,
}

/// Order response from CLOB
/// Matches official Polymarket API response format
#[derive(Debug, Deserialize)]
pub struct OrderResponse {
    /// Whether the order submission was successful
    pub success: bool,
    /// Error message (empty string if no error)
    #[serde(rename = "errorMsg")]
    pub error_msg: String,
    /// Order ID assigned by the CLOB
    #[serde(rename = "orderID")]
    pub order_id: String,
    /// Transaction hashes if any
    #[serde(rename = "transactionsHashes", default)]
    pub transactions_hashes: Vec<String>,
    /// Order status
    pub status: String,
}

/// Paginated response wrapper from CLOB /data/* endpoints
#[derive(Debug, Deserialize)]
pub struct PaginatedResponse<T> {
    /// Limit used for this page
    pub limit: Option<i32>,
    /// Total count
    pub count: Option<i32>,
    /// Cursor for next page
    pub next_cursor: String,
    /// Data items
    pub data: Vec<T>,
}

/// Open order from CLOB /data/orders endpoint
/// Field names match the actual API response (TypeScript client format)
#[derive(Debug, Deserialize)]
pub struct OpenOrder {
    /// Order ID
    pub id: String,
    /// Order status
    pub status: String,
    /// Owner address
    #[serde(default)]
    pub owner: Option<String>,
    /// Maker address
    pub maker_address: String,
    /// Market slug
    #[serde(default)]
    pub market: Option<String>,
    /// Asset ID (token ID)
    pub asset_id: String,
    /// Side (BUY/SELL)
    pub side: String,
    /// Original size
    pub original_size: String,
    /// Size matched
    pub size_matched: String,
    /// Price
    pub price: String,
    /// Associated trades
    #[serde(default)]
    pub associate_trades: Option<Vec<String>>,
    /// Outcome
    #[serde(default)]
    pub outcome: Option<String>,
    /// Created at timestamp (unix timestamp as number)
    pub created_at: Option<i64>,
    /// Expiration
    #[serde(default)]
    pub expiration: Option<String>,
    /// Order type
    #[serde(default)]
    pub order_type: Option<String>,
}

impl OpenOrder {
    /// Get token_id (alias for asset_id for backward compatibility)
    #[must_use]
    pub fn token_id(&self) -> &str {
        &self.asset_id
    }

    /// Get maker (alias for maker_address for backward compatibility)
    #[must_use]
    pub fn maker(&self) -> &str {
        &self.maker_address
    }

    /// Get signer (same as maker for CLOB orders)
    #[must_use]
    pub fn signer(&self) -> &str {
        // Note: CLOB API doesn't return signer separately, it's the same as maker
        &self.maker_address
    }
}

/// Cancel orders request
#[derive(Debug, Serialize)]
struct CancelOrdersRequest {
    /// Order IDs to cancel
    #[serde(rename = "orderIds")]
    order_ids: Vec<String>,
}

/// Cancel response
#[derive(Debug, Deserialize)]
pub struct CancelResponse {
    /// Cancelled order IDs
    pub canceled: Vec<String>,
    /// Failed cancellations
    pub failed: Option<Vec<String>>,
}

/// Neg risk response from /neg-risk endpoint
#[derive(Debug, Deserialize)]
pub struct NegRiskResponse {
    /// Whether the token is a negative risk market
    pub neg_risk: bool,
}

/// Tick size response from /tick-size endpoint
#[derive(Debug, Deserialize)]
pub struct TickSizeResponse {
    /// The tick size for the token (e.g., "0.01", "0.001")
    pub minimum_tick_size: String,
}

/// Fee rate response from /fee-rate endpoint
#[derive(Debug, Deserialize)]
pub struct FeeRateResponse {
    /// The base fee rate in basis points (e.g., 0 for 0%, 100 for 1%)
    pub base_fee: u32,
}

/// CLOB API client
#[derive(Clone)]
pub struct ClobClient {
    config: ClobConfig,
    client: Client,
    signer: PrivateKeySigner,
    rate_limiter: Arc<RateLimiter>,
    /// Stored API credentials (set after creation)
    api_credentials: Option<ApiCredentials>,
    /// Optional explicit address for Builder API auth (when signer ≠ API key owner)
    auth_address: Option<String>,
    /// Optional Builder signer for Integrator authentication
    builder_signer: Option<BuilderSigner>,
}

impl ClobClient {
    /// Create a new CLOB client
    pub fn new(config: ClobConfig, signer: PrivateKeySigner) -> Result<Self> {
        let client = Client::builder()
            .timeout(config.timeout)
            .user_agent(&config.user_agent)
            .gzip(true)
            .build()
            .map_err(|e| PolymarketError::config(format!("Failed to create HTTP client: {e}")))?;

        let quota = Quota::per_second(
            NonZeroU32::new(config.rate_limit_per_second).unwrap_or(NonZeroU32::new(5).unwrap()),
        );
        let rate_limiter = Arc::new(GovRateLimiter::direct(quota));

        Ok(Self {
            config,
            client,
            signer,
            rate_limiter,
            api_credentials: None,
            auth_address: None,
            builder_signer: None,
        })
    }

    /// Create client from environment variables.
    ///
    /// **Deprecated**: Use `ClobClient::new(ClobConfig::default(), signer)` instead.
    #[deprecated(
        since = "0.1.0",
        note = "Use ClobClient::new(ClobConfig::default(), signer) instead"
    )]
    #[allow(deprecated)]
    pub fn from_env(signer: PrivateKeySigner) -> Result<Self> {
        Self::new(ClobConfig::from_env(), signer)
    }

    /// Set API credentials for L2 authentication
    #[must_use]
    pub fn with_api_credentials(mut self, credentials: ApiCredentials) -> Self {
        self.api_credentials = Some(credentials);
        self
    }

    /// Set explicit auth address (for Builder API authentication)
    ///
    /// Use this when the order signer and API credentials owner are different addresses.
    /// This is common when using Builder API credentials.
    #[must_use]
    pub fn with_auth_address(mut self, address: impl Into<String>) -> Self {
        self.auth_address = Some(address.into());
        self
    }

    /// Set Builder API signer for Integrator authentication
    ///
    /// Use this when submitting orders on behalf of users via Builder API.
    /// The Builder credentials authenticate the Integrator, while the order
    /// signature authenticates the user's wallet.
    ///
    /// # Example
    /// ```ignore
    /// let clob_client = ClobClient::from_env(dummy_signer)
    ///     .with_api_credentials(builder_credentials)
    ///     .with_builder_signer(builder_credentials)  // Enables Builder headers
    ///     .with_auth_address(server_wallet_address);
    /// ```
    #[must_use]
    pub fn with_builder_signer(mut self, credentials: ApiCredentials) -> Self {
        let builder_creds = BuilderApiKeyCreds {
            key: credentials.api_key,
            secret: credentials.secret, // Base64 encoded
            passphrase: credentials.passphrase,
        };
        self.builder_signer = Some(BuilderSigner::new(builder_creds));
        self
    }

    /// Get the signer's address
    #[must_use]
    pub fn address(&self) -> String {
        format!("{:?}", self.signer.address())
    }

    /// Wait for rate limiter
    async fn wait_for_rate_limit(&self) {
        self.rate_limiter.until_ready().await;
    }

    /// Derive API key (step 1 of credential creation)
    ///
    /// This gets a nonce and derives the API key from the wallet signature.
    #[instrument(skip(self))]
    pub async fn derive_api_key(&self, nonce: Option<U256>) -> Result<DeriveApiKeyResponse> {
        self.wait_for_rate_limit().await;

        let endpoint = "/auth/derive-api-key";
        let url = format!("{}{}", self.config.base_url, endpoint);

        let headers = create_l1_headers(&self.signer, nonce)?;

        debug!(address = %self.address(), "Deriving API key");

        let mut req_builder = self.client.get(&url);
        for (key, value) in &headers {
            req_builder = req_builder.header(*key, value);
        }

        let response = req_builder.send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: DeriveApiKeyResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse derive response: {e}"), e)
        })?;

        info!(address = %self.address(), "API key derived successfully");

        Ok(result)
    }

    /// Derive API key with pre-computed signature (for Privy ServerWallet)
    ///
    /// This variant accepts a pre-computed EIP-712 signature, useful when using
    /// external signing services like Privy ServerWallet that don't expose private keys.
    ///
    /// # Workflow
    ///
    /// 1. Generate timestamp and nonce
    /// 2. Use `build_clob_auth_typed_data` from auth module to construct typed data
    /// 3. Call Privy ServerWallet API to sign the typed data
    /// 4. Call this method with the signature
    ///
    /// # Arguments
    ///
    /// * `address` - Wallet address (hex string with or without 0x prefix)
    /// * `signature` - EIP-712 signature (hex string with 0x prefix)
    /// * `timestamp` - Unix timestamp string used in signature
    /// * `nonce` - Nonce value used in signature
    ///
    /// # Returns
    ///
    /// Derived API credentials (api_key, secret, passphrase)
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use polymarket_sdk::auth::{build_clob_auth_typed_data, get_current_unix_time_secs};
    /// use alloy_primitives::{Address, U256};
    ///
    /// // 1. Prepare signature data
    /// let address: Address = "0x1234...".parse()?;
    /// let timestamp = get_current_unix_time_secs().to_string();
    /// let nonce = U256::ZERO;
    ///
    /// // 2. Build typed data for Privy
    /// let typed_data = build_clob_auth_typed_data(address, &timestamp, nonce);
    ///
    /// // 3. Call Privy ServerWallet API (pseudo-code)
    /// let signature = privy_client.sign_typed_data(server_wallet_id, typed_data).await?;
    ///
    /// // 4. Derive API key with signature
    /// let credentials = clob_client
    ///     .derive_api_key_with_signature(&format!("{:?}", address), &signature, &timestamp, nonce)
    ///     .await?;
    /// ```
    #[instrument(skip(self, signature))]
    pub async fn derive_api_key_with_signature(
        &self,
        address: &str,
        signature: &str,
        timestamp: &str,
        nonce: U256,
    ) -> Result<DeriveApiKeyResponse> {
        self.wait_for_rate_limit().await;

        let endpoint = "/auth/derive-api-key";
        let url = format!("{}{}", self.config.base_url, endpoint);

        // Ensure address has 0x prefix
        let address = if address.starts_with("0x") {
            address.to_string()
        } else {
            format!("0x{}", address)
        };

        // Ensure signature has 0x prefix
        let signature = if signature.starts_with("0x") {
            signature.to_string()
        } else {
            format!("0x{}", signature)
        };

        // Build L1 auth headers manually with pre-computed signature
        let headers = HashMap::from([
            ("poly_address", address.clone()),
            ("poly_signature", signature),
            ("poly_timestamp", timestamp.to_string()),
            ("poly_nonce", nonce.to_string()),
        ]);

        debug!(address = %address, "Deriving API key with pre-computed signature");

        let mut req_builder = self.client.get(&url);
        for (key, value) in &headers {
            req_builder = req_builder.header(*key, value);
        }

        let response = req_builder.send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: DeriveApiKeyResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse derive response: {e}"), e)
        })?;

        info!(address = %address, "API key derived successfully with pre-computed signature");

        Ok(result)
    }

    /// Create API key with pre-computed signature (for server wallet registration)
    ///
    /// This is the first-time registration step that tells Polymarket about this wallet.
    /// After calling this once, you can use derive_api_key_with_signature for subsequent requests.
    #[instrument(skip(self, signature))]
    pub async fn create_api_key_with_signature(
        &self,
        address: &str,
        signature: &str,
        timestamp: &str,
        nonce: U256,
    ) -> Result<ApiKeyResponse> {
        self.wait_for_rate_limit().await;

        let endpoint = "/auth/api-key";
        let url = format!("{}{}", self.config.base_url, endpoint);

        // Ensure address has 0x prefix
        let address = if address.starts_with("0x") {
            address.to_string()
        } else {
            format!("0x{}", address)
        };

        // Ensure signature has 0x prefix
        let signature = if signature.starts_with("0x") {
            signature.to_string()
        } else {
            format!("0x{}", signature)
        };

        // Build L1 auth headers manually with pre-computed signature
        let headers = HashMap::from([
            ("poly_address", address.clone()),
            ("poly_signature", signature),
            ("poly_timestamp", timestamp.to_string()),
            ("poly_nonce", nonce.to_string()),
        ]);

        let body = CreateApiKeyRequest {
            nonce: nonce.to_string(),
        };

        debug!(address = %address, "Creating API key with pre-computed signature (first-time registration)");

        let mut req_builder = self.client.post(&url).json(&body);
        for (key, value) in &headers {
            req_builder = req_builder.header(*key, value);
        }

        let response = req_builder.send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: ApiKeyResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse create response: {e}"), e)
        })?;

        info!(address = %address, "API key created successfully (wallet registered)");

        Ok(result)
    }

    /// Derive or create API key with pre-computed signature (for Privy ServerWallet)
    ///
    /// This is a convenience method that handles the common case where a wallet
    /// may or may not have been registered with Polymarket CLOB API yet.
    ///
    /// # Workflow
    ///
    /// 1. Try to derive API key (for existing registrations)
    /// 2. If "Could not derive api key" error (wallet not registered):
    ///    - First call create_api_key_with_signature to register the wallet
    ///    - Then retry derive_api_key_with_signature
    /// 3. Return the derived credentials
    ///
    /// # Arguments
    ///
    /// * `address` - Wallet address (hex string with or without 0x prefix)
    /// * `signature` - EIP-712 signature (hex string with 0x prefix)
    /// * `timestamp` - Unix timestamp string used in signature
    /// * `nonce` - Nonce value used in signature
    ///
    /// # Returns
    ///
    /// Derived API credentials (api_key, secret, passphrase)
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use polymarket_sdk::auth::{build_clob_auth_typed_data, get_current_unix_time_secs};
    /// use alloy_primitives::{Address, U256};
    ///
    /// // Prepare and sign typed data (see derive_api_key_with_signature for details)
    /// let credentials = clob_client
    ///     .derive_or_create_api_key(&address, &signature, &timestamp, nonce)
    ///     .await?;
    /// ```
    #[instrument(skip(self, signature))]
    pub async fn derive_or_create_api_key(
        &self,
        address: &str,
        signature: &str,
        timestamp: &str,
        nonce: U256,
    ) -> Result<DeriveApiKeyResponse> {
        // Try derive first (common case: wallet already registered)
        match self
            .derive_api_key_with_signature(address, signature, timestamp, nonce)
            .await
        {
            Ok(response) => Ok(response),
            Err(e) if e.is_wallet_not_registered() => {
                // Wallet not registered - register it first, then retry derive
                info!(
                    address = %address,
                    "Wallet not registered with CLOB API, registering first"
                );

                // Step 1: Create/register the API key (first-time registration)
                self.create_api_key_with_signature(address, signature, timestamp, nonce)
                    .await?;

                // Step 2: Retry derive (should succeed now)
                info!(address = %address, "Wallet registered, retrying derive");
                self.derive_api_key_with_signature(address, signature, timestamp, nonce)
                    .await
            }
            Err(e) => Err(e),
        }
    }

    /// Create API key (step 2 of credential creation)
    ///
    /// This registers the derived API key with the CLOB.
    #[instrument(skip(self))]
    pub async fn create_api_key(&self, nonce: U256) -> Result<ApiKeyResponse> {
        self.wait_for_rate_limit().await;

        let endpoint = "/auth/api-key";
        let url = format!("{}{}", self.config.base_url, endpoint);

        let headers = create_l1_headers(&self.signer, Some(nonce))?;

        let body = CreateApiKeyRequest {
            nonce: nonce.to_string(),
        };

        debug!(address = %self.address(), "Creating API key");

        let mut req_builder = self.client.post(&url).json(&body);
        for (key, value) in &headers {
            req_builder = req_builder.header(*key, value);
        }

        let response = req_builder.send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: ApiKeyResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse create response: {e}"), e)
        })?;

        info!(address = %self.address(), "API key created successfully");

        Ok(result)
    }

    /// Create API credentials (complete flow)
    ///
    /// This performs the full API credential creation flow:
    /// 1. Derive API key with L1 signature
    /// 2. Create/register API key
    ///
    /// # Returns
    /// Complete API credentials ready for L2 authentication
    #[instrument(skip(self))]
    pub async fn create_api_credentials(&self) -> Result<ApiCredentials> {
        info!(address = %self.address(), "Creating API credentials");

        // Step 1: Derive API key (ensures the key derivation is set up)
        let _derive_result = self.derive_api_key(None).await?;

        // Step 2: Create API key with nonce 0 (typical for new credentials)
        let create_result = self.create_api_key(U256::ZERO).await?;

        let credentials = ApiCredentials {
            api_key: create_result.api_key,
            secret: create_result.secret,
            passphrase: create_result.passphrase,
        };

        info!(address = %self.address(), "API credentials created successfully");

        Ok(credentials)
    }

    /// Submit an order
    ///
    /// Requires API credentials to be set via `with_api_credentials`.
    #[instrument(skip(self, order))]
    pub async fn submit_order(&self, order: &SignedOrderRequest) -> Result<OrderResponse> {
        use crate::types::{NewOrder, OrderType};

        self.wait_for_rate_limit().await;

        let api_creds = self.api_credentials.as_ref().ok_or_else(|| {
            PolymarketError::config("API credentials required for order submission")
        })?;

        let endpoint = "/order";
        let url = format!("{}{}", self.config.base_url, endpoint);

        // IMPORTANT: Convert SignedOrderRequest to NewOrder format
        // - Use api_key as owner (NOT wallet address) - matches TypeScript SDK behavior
        // - Wrap order data in nested structure with orderType and deferExec
        let new_order =
            NewOrder::from_signed_order(order, &api_creds.api_key, OrderType::GTC, false);

        // CRITICAL FIX: Serialize JSON ONCE and reuse for all operations
        // This ensures L2 HMAC, Builder HMAC, and HTTP body all use identical JSON
        let body_str = serde_json::to_string(&new_order)
            .map_err(|e| PolymarketError::parse(format!("Failed to serialize order: {}", e)))?;

        // CRITICAL FIX: Get timestamp ONCE and reuse for L2 and Builder headers
        // This ensures both headers use the same timestamp, avoiding signature mismatches
        let timestamp = get_current_unix_time_secs();

        // Get the auth address (either explicit or derived from signer)
        let address = if let Some(ref addr) = self.auth_address {
            if addr.starts_with("0x") {
                addr.clone()
            } else {
                format!("0x{}", addr)
            }
        } else {
            format!("{:?}", self.signer.address())
        };

        // 1. Create standard L2 headers (POLY_*) using pre-serialized body string and shared timestamp
        let mut headers = create_l2_headers_with_body_string(
            &address, api_creds, "POST", endpoint, &body_str, timestamp,
        )?;

        // 2. Inject Builder headers (POLY_BUILDER_*) if Builder signer is configured
        // Use the SAME body_str and SAME timestamp for consistency
        if let Some(ref builder) = self.builder_signer {
            info!("Builder signer configured, generating Builder headers");

            let builder_headers = builder
                .create_builder_header_payload(
                    "POST",
                    endpoint,
                    Some(&body_str),
                    Some(timestamp as i64),
                )
                .map_err(|e| PolymarketError::internal(format!("Builder header error: {}", e)))?;

            info!(
                header_count = builder_headers.len(),
                timestamp = timestamp,
                "Builder headers generated with shared timestamp"
            );

            // Merge Builder headers into existing headers
            // Note: We leak the strings to get 'static lifetime for HashMap keys
            for (key, value) in builder_headers {
                let static_key: &'static str = Box::leak(key.into_boxed_str());
                headers.insert(static_key, value);
            }

            info!(token_id = %order.token_id, side = %order.side, "Submitting order with Builder authentication");
        } else {
            warn!("Builder signer NOT configured - order may fail with 401 Unauthorized");
            info!(token_id = %order.token_id, side = %order.side, "Submitting order WITHOUT Builder authentication");
        }

        // Debug: Print the actual JSON being sent (NewOrder format)
        info!(order_json = %body_str, timestamp = timestamp, "Order JSON payload being sent to Polymarket (NewOrder format)");

        // CRITICAL FIX: Use pre-serialized body string instead of re-serializing with .json()
        // This ensures HTTP body matches exactly what was used for HMAC calculation
        let mut req_builder = self
            .client
            .post(&url)
            .header("Content-Type", "application/json")
            .body(body_str.clone());
        for (key, value) in &headers {
            req_builder = req_builder.header(*key, value);
        }

        let response = req_builder.send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: OrderResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse order response: {e}"), e)
        })?;

        // Check if order submission was successful
        if !result.success {
            return Err(PolymarketError::api(
                400,
                format!(
                    "Order rejected: {} (status: {})",
                    result.error_msg, result.status
                ),
            ));
        }

        info!(
            order_id = %result.order_id,
            status = %result.status,
            success = result.success,
            "Order submitted successfully"
        );

        Ok(result)
    }

    /// Get open orders for the signer
    #[instrument(skip(self))]
    pub async fn get_open_orders(&self) -> Result<Vec<OpenOrder>> {
        self.wait_for_rate_limit().await;

        let api_creds = self.api_credentials.as_ref().ok_or_else(|| {
            PolymarketError::config("API credentials required for querying orders")
        })?;

        // IMPORTANT: Use /data/orders endpoint for GET (not /orders which is for POST)
        let endpoint = "/data/orders";
        let url = format!("{}{}", self.config.base_url, endpoint);

        let headers = create_l2_headers::<String>(&self.signer, api_creds, "GET", endpoint, None)?;

        debug!(address = %self.address(), "Getting open orders");

        let mut req_builder = self.client.get(&url);
        for (key, value) in &headers {
            req_builder = req_builder.header(*key, value);
        }

        let response = req_builder.send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        // Parse as paginated response
        let paginated: PaginatedResponse<OpenOrder> = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse orders response: {e}"), e)
        })?;

        debug!(count = %paginated.data.len(), "Retrieved open orders");

        Ok(paginated.data)
    }

    /// Cancel orders by IDs
    #[instrument(skip(self))]
    pub async fn cancel_orders(&self, order_ids: Vec<String>) -> Result<CancelResponse> {
        self.wait_for_rate_limit().await;

        let api_creds = self.api_credentials.as_ref().ok_or_else(|| {
            PolymarketError::config("API credentials required for cancelling orders")
        })?;

        let endpoint = "/order";
        let url = format!("{}{}", self.config.base_url, endpoint);

        let body = CancelOrdersRequest { order_ids };
        let headers = create_l2_headers(&self.signer, api_creds, "DELETE", endpoint, Some(&body))?;

        debug!("Cancelling orders");

        let mut req_builder = self.client.delete(&url).json(&body);
        for (key, value) in &headers {
            req_builder = req_builder.header(*key, value);
        }

        let response = req_builder.send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: CancelResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse cancel response: {e}"), e)
        })?;

        info!(cancelled = ?result.canceled, "Orders cancelled");

        Ok(result)
    }

    /// Cancel all open orders
    #[instrument(skip(self))]
    pub async fn cancel_all_orders(&self) -> Result<CancelResponse> {
        self.wait_for_rate_limit().await;

        let api_creds = self.api_credentials.as_ref().ok_or_else(|| {
            PolymarketError::config("API credentials required for cancelling orders")
        })?;

        let endpoint = "/order/cancel-all";
        let url = format!("{}{}", self.config.base_url, endpoint);

        let headers =
            create_l2_headers::<String>(&self.signer, api_creds, "DELETE", endpoint, None)?;

        debug!(address = %self.address(), "Cancelling all orders");

        let mut req_builder = self.client.delete(&url);
        for (key, value) in &headers {
            req_builder = req_builder.header(*key, value);
        }

        let response = req_builder.send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: CancelResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse cancel response: {e}"), e)
        })?;

        info!(cancelled = ?result.canceled, "All orders cancelled");

        Ok(result)
    }

    /// Get the neg_risk status for a token ID
    ///
    /// Queries the Polymarket CLOB API to determine if a market/token uses
    /// negative risk contracts. This is crucial for signing orders with the
    /// correct exchange contract address.
    ///
    /// # Arguments
    /// * `token_id` - The token ID (condition token) to check
    ///
    /// # Returns
    /// * `true` if the market uses negative risk contracts (use negRiskExchange)
    /// * `false` if the market uses standard contracts (use exchange)
    #[instrument(skip(self))]
    pub async fn get_neg_risk(&self, token_id: &str) -> Result<bool> {
        self.wait_for_rate_limit().await;

        let endpoint = "/neg-risk";
        let url = format!("{}{}?token_id={}", self.config.base_url, endpoint, token_id);

        debug!(token_id = %token_id, "Querying neg_risk status");

        let response = self.client.get(&url).send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: NegRiskResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse neg_risk response: {e}"), e)
        })?;

        debug!(token_id = %token_id, neg_risk = %result.neg_risk, "Got neg_risk status");

        Ok(result.neg_risk)
    }

    /// Get the tick size for a token ID
    ///
    /// Queries the Polymarket CLOB API to get the minimum tick size for
    /// price rounding on a specific market.
    ///
    /// # Arguments
    /// * `token_id` - The token ID (condition token) to check
    ///
    /// # Returns
    /// The tick size as a string (e.g., "0.01", "0.001", "0.0001")
    #[instrument(skip(self))]
    pub async fn get_tick_size(&self, token_id: &str) -> Result<String> {
        self.wait_for_rate_limit().await;

        let endpoint = "/tick-size";
        let url = format!("{}{}?token_id={}", self.config.base_url, endpoint, token_id);

        debug!(token_id = %token_id, "Querying tick size");

        let response = self.client.get(&url).send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: TickSizeResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(
                format!("Failed to parse tick_size response: {e}"),
                e,
            )
        })?;

        debug!(token_id = %token_id, tick_size = %result.minimum_tick_size, "Got tick size");

        Ok(result.minimum_tick_size)
    }

    /// Get the fee rate for a token ID
    ///
    /// Queries the Polymarket CLOB API to get the fee rate (in basis points)
    /// for a specific market/token. This is crucial for signing orders with
    /// the correct fee rate that matches what the API expects.
    ///
    /// # Arguments
    /// * `token_id` - The token ID (condition token) to check
    ///
    /// # Returns
    /// The fee rate in basis points (e.g., 0 for 0%, 100 for 1%)
    ///
    /// # Note
    /// Orders MUST use the fee rate returned by this API, otherwise the
    /// EIP-712 signature will not match and the order will be rejected
    /// with "invalid signature".
    #[instrument(skip(self))]
    pub async fn get_fee_rate(&self, token_id: &str) -> Result<u32> {
        self.wait_for_rate_limit().await;

        let endpoint = "/fee-rate";
        let url = format!("{}{}?token_id={}", self.config.base_url, endpoint, token_id);

        debug!(token_id = %token_id, "Querying fee rate");

        let response = self.client.get(&url).send().await?;
        let status = response.status();

        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        let result: FeeRateResponse = response.json().await.map_err(|e| {
            PolymarketError::parse_with_source(format!("Failed to parse fee_rate response: {e}"), e)
        })?;

        debug!(token_id = %token_id, fee_rate_bps = %result.base_fee, "Got fee rate");

        Ok(result.base_fee)
    }

    /// Check if an orderbook exists for a token ID
    ///
    /// Queries the Polymarket CLOB `/book` endpoint to verify that an
    /// active orderbook exists for the given token. This should be called
    /// before submitting orders to avoid "orderbook does not exist" errors.
    ///
    /// # Arguments
    /// * `token_id` - The token ID (CLOB token / asset ID) to check
    ///
    /// # Returns
    /// * `true` if the orderbook exists and is active
    /// * `false` if the orderbook does not exist or the market is closed
    ///
    /// # Note
    /// Markets can have valid neg_risk and fee_rate data but no active
    /// orderbook (e.g., resolved or closed markets). Always verify
    /// orderbook existence before submitting orders.
    #[instrument(skip(self))]
    pub async fn check_orderbook_exists(&self, token_id: &str) -> Result<bool> {
        self.wait_for_rate_limit().await;

        let endpoint = "/book";
        let url = format!("{}{}?token_id={}", self.config.base_url, endpoint, token_id);

        debug!(token_id = %token_id, "Checking orderbook existence");

        let response = self.client.get(&url).send().await?;
        let status = response.status();

        // Check response body for error
        let body = response.text().await.unwrap_or_default();

        // API returns 200 with {"error": "..."} for non-existent orderbooks
        if body.contains("does not exist") || body.contains("No orderbook") {
            info!(token_id = %token_id, "Orderbook does not exist");
            return Ok(false);
        }

        if !status.is_success() {
            // Other API errors
            return Err(PolymarketError::api(status.as_u16(), body));
        }

        // If we got here, the orderbook exists
        debug!(token_id = %token_id, "Orderbook exists");
        Ok(true)
    }
}

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

    #[test]
    fn test_clob_config_default() {
        let config = ClobConfig::default();
        // URL uses helper function which may be overridden by env var
        assert_eq!(config.base_url, clob_api_url());
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert_eq!(config.rate_limit_per_second, 5);
    }

    #[test]
    fn test_clob_config_with_base_url() {
        let config = ClobConfig::default().with_base_url("https://custom.example.com");
        assert_eq!(config.base_url, "https://custom.example.com");
    }
}