polymarket-hft 0.0.7

A high-frequency trading system for Polymarket with built-in API clients (Data API, CLOB, CLOB WebSocket, Gamma, RTDS) and CLI
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
use reqwest::Method;
use reqwest_middleware::ClientWithMiddleware;

use super::model::*;
use crate::client::http::{self, HttpClientConfig};

const BASE_URL: &str = "https://pro-api.coinmarketcap.com";

/// Helper macro to add optional query parameters to a request.
macro_rules! add_optional_query {
    ($req:expr, $($key:literal => $value:expr),* $(,)?) => {{
        let mut req = $req;
        $(
            if let Some(ref v) = $value {
                req = req.query(&[($key, v)]);
            }
        )*
        req
    }};
}

/// CoinMarketCap API client.
#[derive(Clone)]
pub struct Client {
    inner: ClientWithMiddleware,
    api_key: String,
    base_url: String,
}

impl Client {
    /// Creates a new CoinMarketCap API client.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            inner: http::build_default_client().expect("Failed to build default HTTP client"),
            api_key: api_key.into(),
            base_url: BASE_URL.to_string(),
        }
    }

    /// Creates a new CoinMarketCap API client with custom configuration.
    pub fn with_config(api_key: impl Into<String>, config: HttpClientConfig) -> Self {
        Self {
            inner: config
                .build()
                .expect("Failed to build HTTP client with config"),
            api_key: api_key.into(),
            base_url: BASE_URL.to_string(),
        }
    }

    /// Sets the base URL (internal use or testing).
    #[allow(dead_code)]
    pub(crate) fn with_base_url(mut self, base_url: String) -> Self {
        self.base_url = base_url;
        self
    }

    /// Helper to create a request builder with the API key header.
    fn request(&self, method: Method, path: &str) -> reqwest_middleware::RequestBuilder {
        let url = format!("{}{}", self.base_url, path);
        self.inner
            .request(method, &url)
            .header("X-CMC_PRO_API_KEY", &self.api_key)
            .header("Accept", "application/json")
    }

    /// Check response status and return error if API returned an error.
    fn check_status(status: &Status) -> Result<(), CmcError> {
        if status.error_code != 0 {
            return Err(CmcError::Api {
                code: status.error_code,
                message: status.error_message.clone().unwrap_or_default(),
            });
        }
        Ok(())
    }

    /// Get latest cryptocurrency listings.
    pub async fn get_listings_latest(
        &self,
        request: GetListingsLatestRequest,
    ) -> Result<ListingsLatestResponse, CmcError> {
        let req = self.request(Method::GET, "/v1/cryptocurrency/listings/latest");

        let req = add_optional_query!(req,
            "start" => request.start,
            "limit" => request.limit,
            "price_min" => request.price_min,
            "price_max" => request.price_max,
            "market_cap_min" => request.market_cap_min,
            "market_cap_max" => request.market_cap_max,
            "volume_24h_min" => request.volume_24h_min,
            "volume_24h_max" => request.volume_24h_max,
            "circulating_supply_min" => request.circulating_supply_min,
            "circulating_supply_max" => request.circulating_supply_max,
            "percent_change_24h_min" => request.percent_change_24h_min,
            "percent_change_24h_max" => request.percent_change_24h_max,
            "convert" => request.convert,
            "convert_id" => request.convert_id,
            "sort" => request.sort,
            "sort_dir" => request.sort_dir,
            "cryptocurrency_type" => request.cryptocurrency_type,
            "tag" => request.tag,
            "aux" => request.aux,
        );

        let response = req.send().await?;
        let data = response.json::<ListingsLatestResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }

    /// Get latest global metrics quotes.
    pub async fn get_global_metrics_quotes_latest(
        &self,
        request: GetGlobalMetricsQuotesLatestRequest,
    ) -> Result<GlobalMetricsQuotesLatestResponse, CmcError> {
        let req = self.request(Method::GET, "/v1/global-metrics/quotes/latest");

        let req = add_optional_query!(req,
            "convert" => request.convert,
            "convert_id" => request.convert_id,
        );

        let response = req.send().await?;
        let data = response.json::<GlobalMetricsQuotesLatestResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }

    /// Get latest fear and greed index.
    pub async fn get_fear_and_greed_latest(
        &self,
        _request: GetFearAndGreedLatestRequest,
    ) -> Result<FearAndGreedResponse, CmcError> {
        let req = self.request(Method::GET, "/v3/fear-and-greed/latest");
        let response = req.send().await?;
        let data = response.json::<FearAndGreedResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }

    /// Get API key usage information.
    ///
    /// Returns information about your API plan and current usage,
    /// including daily and monthly credit limits and consumption.
    pub async fn get_key_info(&self) -> Result<KeyInfoResponse, CmcError> {
        let req = self.request(Method::GET, "/v1/key/info");
        let response = req.send().await?;
        let data = response.json::<KeyInfoResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }

    /// Get cryptocurrency ID map.
    ///
    /// Returns a paginated list of all active cryptocurrencies with their CoinMarketCap IDs.
    /// This is useful for mapping symbols to IDs for other API calls.
    pub async fn get_cryptocurrency_map(
        &self,
        request: GetCryptocurrencyMapRequest,
    ) -> Result<CryptocurrencyMapResponse, CmcError> {
        let req = self.request(Method::GET, "/v1/cryptocurrency/map");

        let req = add_optional_query!(req,
            "listing_status" => request.listing_status,
            "start" => request.start,
            "limit" => request.limit,
            "sort" => request.sort,
            "symbol" => request.symbol,
            "aux" => request.aux,
        );

        let response = req.send().await?;
        let data = response.json::<CryptocurrencyMapResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }

    /// Get cryptocurrency metadata (info).
    ///
    /// Returns static metadata for one or more cryptocurrencies including
    /// logo, description, URLs, and other details.
    pub async fn get_cryptocurrency_info(
        &self,
        request: GetCryptocurrencyInfoRequest,
    ) -> Result<CryptocurrencyInfoResponse, CmcError> {
        let req = self.request(Method::GET, "/v1/cryptocurrency/info");

        let skip_invalid_str = request.skip_invalid.map(|b| b.to_string());
        let req = add_optional_query!(req,
            "id" => request.id,
            "slug" => request.slug,
            "symbol" => request.symbol,
            "address" => request.address,
            "aux" => request.aux,
            "skip_invalid" => skip_invalid_str,
        );

        let response = req.send().await?;
        let data = response.json::<CryptocurrencyInfoResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }

    /// Get latest quotes for specific cryptocurrencies.
    ///
    /// Returns the latest market quote for one or more cryptocurrencies.
    /// Use this to get prices for specific coins by ID, slug, or symbol.
    pub async fn get_quotes_latest(
        &self,
        request: GetQuotesLatestRequest,
    ) -> Result<QuotesLatestResponse, CmcError> {
        let req = self.request(Method::GET, "/v2/cryptocurrency/quotes/latest");

        let skip_invalid_str = request.skip_invalid.map(|b| b.to_string());
        let req = add_optional_query!(req,
            "id" => request.id,
            "slug" => request.slug,
            "symbol" => request.symbol,
            "convert" => request.convert,
            "convert_id" => request.convert_id,
            "aux" => request.aux,
            "skip_invalid" => skip_invalid_str,
        );

        let response = req.send().await?;
        let data = response.json::<QuotesLatestResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }

    /// Get fiat currency ID map.
    ///
    /// Returns a list of all supported fiat currencies with their CoinMarketCap IDs.
    pub async fn get_fiat_map(
        &self,
        request: GetFiatMapRequest,
    ) -> Result<FiatMapResponse, CmcError> {
        let req = self.request(Method::GET, "/v1/fiat/map");

        let include_metals_str = request.include_metals.map(|b| b.to_string());
        let req = add_optional_query!(req,
            "start" => request.start,
            "limit" => request.limit,
            "sort" => request.sort,
            "include_metals" => include_metals_str,
        );

        let response = req.send().await?;
        let data = response.json::<FiatMapResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }

    /// Convert an amount of one currency into another.
    ///
    /// This endpoint can be used for crypto-to-crypto, fiat-to-fiat,
    /// or crypto-to-fiat conversions.
    pub async fn get_price_conversion(
        &self,
        request: PriceConversionRequest,
    ) -> Result<PriceConversionResponse, CmcError> {
        let req = self.request(Method::GET, "/v1/tools/price-conversion");

        let amount_str = Some(request.amount.to_string());
        let id_str = request.id.map(|i| i.to_string());
        let req = add_optional_query!(req,
            "amount" => amount_str,
            "id" => id_str,
            "symbol" => request.symbol,
            "convert" => request.convert,
            "convert_id" => request.convert_id,
            "time" => request.time,
        );

        let response = req.send().await?;
        let data = response.json::<PriceConversionResponse>().await?;
        Self::check_status(&data.status)?;
        Ok(data)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    #[tokio::test]
    async fn test_get_listings_latest() {
        let mock_server = MockServer::start().await;
        let client = Client::new("test-key").with_base_url(mock_server.uri());

        let response_body = r#"{
            "status": {
                "timestamp": "2024-01-01T00:00:00.000Z",
                "error_code": 0,
                "error_message": null,
                "elapsed": 10,
                "credit_count": 1,
                "notice": null
            },
            "data": [
                {
                    "id": 1,
                    "name": "Bitcoin",
                    "symbol": "BTC",
                    "slug": "bitcoin",
                    "num_market_pairs": 1000,
                    "date_added": "2010-07-13T00:00:00.000Z",
                    "tags": ["mineable"],
                    "max_supply": 21000000,
                    "circulating_supply": 19000000.0,
                    "total_supply": 19000000.0,
                    "infinite_supply": false,
                    "platform": null,
                    "cmc_rank": 1,
                    "self_reported_circulating_supply": null,
                    "self_reported_market_cap": null,
                    "tvl_ratio": null,
                    "last_updated": "2024-01-01T00:00:00.000Z",
                    "quote": {
                        "USD": {
                            "price": 50000.0,
                            "volume_24h": 1000000000.0,
                            "volume_change_24h": 0.5,
                            "percent_change_1h": 0.1,
                            "percent_change_24h": 1.5,
                            "percent_change_7d": 5.0,
                            "market_cap": 950000000000.0,
                            "market_cap_dominance": 50.0,
                            "fully_diluted_market_cap": 1050000000000.0,
                            "last_updated": "2024-01-01T00:00:00.000Z"
                        }
                    }
                }
            ]
        }"#;

        Mock::given(method("GET"))
            .and(path("/v1/cryptocurrency/listings/latest"))
            .respond_with(ResponseTemplate::new(200).set_body_string(response_body))
            .mount(&mock_server)
            .await;

        let request = GetListingsLatestRequest {
            limit: Some(1),
            ..Default::default()
        };

        let result = client.get_listings_latest(request).await;
        assert!(result.is_ok());
        let response = result.unwrap();
        assert_eq!(response.data.len(), 1);
        assert_eq!(response.data[0].symbol, "BTC");
    }

    #[tokio::test]
    async fn test_get_global_metrics_quotes_latest() {
        let mock_server = MockServer::start().await;
        let client = Client::new("test-key").with_base_url(mock_server.uri());

        let response_body = r#"{
            "status": {
                "timestamp": "2024-01-01T00:00:00.000Z",
                "error_code": 0,
                "error_message": null,
                "elapsed": 10,
                "credit_count": 1,
                "notice": null
            },
            "data": {
                "active_cryptocurrencies": 10000,
                "total_cryptocurrencies": 20000,
                "active_market_pairs": 50000,
                "active_exchanges": 500,
                "total_exchanges": 1000,
                "eth_dominance": 18.5,
                "btc_dominance": 50.5,
                "eth_dominance_yesterday": 18.0,
                "btc_dominance_yesterday": 50.0,
                "defi_volume_24h_reported": 5000000000.0,
                "stablecoin_volume_24h_reported": 40000000000.0,
                "der_volume_24h_reported": 30000000000.0,
                "quote": {
                    "USD": {
                        "total_market_cap": 2000000000000.0,
                        "total_volume_24h": 60000000000.0,
                        "total_volume_24h_reported": 60000000000.0,
                        "altcoin_volume_24h": 30000000000.0,
                        "altcoin_market_cap": 1000000000000.0,
                        "defi_volume_24h": 5000000000.0,
                        "defi_market_cap": 80000000000.0,
                        "defi_24h_percentage_change": 1.2,
                        "stablecoin_volume_24h": 40000000000.0,
                        "stablecoin_market_cap": 150000000000.0,
                        "stablecoin_24h_percentage_change": 0.1,
                        "der_volume_24h": 30000000000.0,
                        "der_24h_percentage_change": 2.5,
                        "last_updated": "2024-01-01T00:00:00.000Z"
                    }
                }
            }
        }"#;

        Mock::given(method("GET"))
            .and(path("/v1/global-metrics/quotes/latest"))
            .respond_with(ResponseTemplate::new(200).set_body_string(response_body))
            .mount(&mock_server)
            .await;

        let request = GetGlobalMetricsQuotesLatestRequest::default();
        let result = client.get_global_metrics_quotes_latest(request).await;
        assert!(result.is_ok());
        let response = result.unwrap();
        assert_eq!(response.data.btc_dominance, 50.5);
    }

    #[tokio::test]
    async fn test_get_fear_and_greed_latest() {
        let mock_server = MockServer::start().await;
        let client = Client::new("test-key").with_base_url(mock_server.uri());

        let response_body = r#"{
            "status": {
                "timestamp": "2024-01-01T00:00:00.000Z",
                "error_code": 0,
                "error_message": null,
                "elapsed": 10,
                "credit_count": 1,
                "notice": null
            },
            "data": {
                "value": 75,
                "value_classification": "Greed",
                "timestamp": "1704067200",
                "time_until_update": "3600"
            }
        }"#;

        Mock::given(method("GET"))
            .and(path("/v3/fear-and-greed/latest"))
            .respond_with(ResponseTemplate::new(200).set_body_string(response_body))
            .mount(&mock_server)
            .await;

        let request = GetFearAndGreedLatestRequest::default();
        let result = client.get_fear_and_greed_latest(request).await;
        assert!(result.is_ok());
        let response = result.unwrap();
        assert_eq!(response.data.value, 75.0);
        assert_eq!(response.data.value_classification, "Greed");
    }

    #[tokio::test]
    async fn test_get_key_info() {
        let mock_server = MockServer::start().await;
        let client = Client::new("test-key").with_base_url(mock_server.uri());

        let response_body = r#"{
            "status": {
                "timestamp": "2024-01-01T00:00:00.000Z",
                "error_code": 0,
                "error_message": null,
                "elapsed": 10,
                "credit_count": 1,
                "notice": null
            },
            "data": {
                "plan": {
                    "credit_limit_daily": 333,
                    "credit_limit_daily_reset": "2024-01-02T00:00:00.000Z",
                    "credit_limit_monthly": 10000,
                    "credit_limit_monthly_reset": "2024-02-01T00:00:00.000Z",
                    "rate_limit_minute": 30
                },
                "usage": {
                    "current_day": {
                        "credits_used": 50,
                        "credits_left": 283
                    },
                    "current_month": {
                        "credits_used": 1500,
                        "credits_left": 8500
                    }
                }
            }
        }"#;

        Mock::given(method("GET"))
            .and(path("/v1/key/info"))
            .respond_with(ResponseTemplate::new(200).set_body_string(response_body))
            .mount(&mock_server)
            .await;

        let result = client.get_key_info().await;
        assert!(result.is_ok());
        let response = result.unwrap();
        assert_eq!(response.data.plan.credit_limit_daily, Some(333));
        assert_eq!(response.data.plan.credit_limit_monthly, Some(10000));
        let current_day = response.data.usage.current_day.as_ref().unwrap();
        assert_eq!(current_day.credits_used, Some(50));
        let current_month = response.data.usage.current_month.as_ref().unwrap();
        assert_eq!(current_month.credits_left, Some(8500));
    }

    #[tokio::test]
    async fn test_api_error_handling() {
        let mock_server = MockServer::start().await;
        let client = Client::new("invalid-key").with_base_url(mock_server.uri());

        let response_body = r#"{
            "status": {
                "timestamp": "2024-01-01T00:00:00.000Z",
                "error_code": 1001,
                "error_message": "This API Key is invalid.",
                "elapsed": 0,
                "credit_count": 0,
                "notice": null
            },
            "data": []
        }"#;

        Mock::given(method("GET"))
            .and(path("/v1/cryptocurrency/listings/latest"))
            .respond_with(ResponseTemplate::new(200).set_body_string(response_body))
            .mount(&mock_server)
            .await;

        let result = client
            .get_listings_latest(GetListingsLatestRequest::default())
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        match err {
            CmcError::Api { code, message } => {
                assert_eq!(code, 1001);
                assert_eq!(message, "This API Key is invalid.");
            }
            _ => panic!("Expected CmcError::Api, got {:?}", err),
        }
    }
}