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
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
#![allow(clippy::too_many_arguments)]
use std::collections::HashMap;

use chrono::{NaiveDate, NaiveDateTime};
use reqwest::Error;
use serde::de::DeserializeOwned;

use crate::params::{
    CompaniesCoinId, DerivativeExchangeOrder, DerivativesIncludeTickers, MarketsOrder, OhlcDays,
    PriceChangePercentage, TickersOrder,
};

use crate::response::{
    asset_platforms::AssetPlatform,
    coins::{
        Category, CategoryId, CoinsItem, CoinsListItem, CoinsMarketItem, Contract, History,
        MarketChart,
    },
    common::{StatusUpdates, Tickers},
    companies::CompaniesPublicTreasury,
    derivatives::{Derivative, DerivativeExchangeId},
    events::Events,
    events::{EventCountries, EventTypes},
    exchange_rates::ExchangeRates,
    exchanges::VolumeChartData,
    exchanges::{Exchange, ExchangeId},
    finance::{FinancePlatform, FinanceProduct},
    global::{Global, GlobalDefi},
    indexes::Index,
    indexes::{IndexId, MarketIndex},
    ping::SimplePing,
    simple::{Price, SupportedVsCurrencies},
    trending::Trending,
};

/// CoinGecko client
pub struct CoinGeckoClient {
    host: &'static str,
}

/// Creates a new CoinGeckoClient with host https://api.coingecko.com/api/v3
///
/// # Examples
///
/// ```rust
/// use coingecko_rs::CoinGeckoClient;
/// let client = CoinGeckoClient::default();
/// ```
impl Default for CoinGeckoClient {
    fn default() -> Self {
        CoinGeckoClient::new("https://api.coingecko.com/api/v3")
    }
}

impl CoinGeckoClient {
    /// Creates a new CoinGeckoClient client with a custom host url
    ///
    /// # Examples
    ///
    /// ```rust
    /// use coingecko_rs::CoinGeckoClient;
    /// let client = CoinGeckoClient::new("https://some.url");
    /// ```
    pub fn new(host: &'static str) -> Self {
        CoinGeckoClient { host }
    }

    async fn get<R: DeserializeOwned>(&self, endpoint: &str) -> Result<R, Error> {
        reqwest::get(format!("{host}/{ep}", host = self.host, ep = endpoint))
            .await?
            .json()
            .await
    }

    /// Check API server status
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.ping().await;
    /// }
    /// ```
    pub async fn ping(&self) -> Result<SimplePing, Error> {
        self.get("/ping").await
    }

    /// Get the current price of any cryptocurrencies in any other supported currencies that you need
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.price(&["bitcoin", "ethereum"], &["usd"], true, true, true, true).await;
    /// }
    /// ```
    pub async fn price<Id: AsRef<str>, Curr: AsRef<str>>(
        &self,
        ids: &[Id],
        vs_currencies: &[Curr],
        include_market_cap: bool,
        include_24hr_vol: bool,
        include_24hr_change: bool,
        include_last_updated_at: bool,
    ) -> Result<HashMap<String, Price>, Error> {
        let ids = ids.iter().map(AsRef::as_ref).collect::<Vec<_>>();
        let vs_currencies = vs_currencies.iter().map(AsRef::as_ref).collect::<Vec<_>>();
        let req = format!("/simple/price?ids={}&vs_currencies={}&include_market_cap={}&include_24hr_vol={}&include_24hr_change={}&include_last_updated_at={}", ids.join("%2C"), vs_currencies.join("%2C"), include_market_cap, include_24hr_vol, include_24hr_change, include_last_updated_at);
        self.get(&req).await
    }

    /// Get current price of tokens (using contract addresses) for a given platform in any other currency that you need
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///     let uniswap_contract = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";
    ///
    ///     client.token_price(
    ///         "ethereum",
    ///         &[uniswap_contract],
    ///         &["usd"],
    ///         true,
    ///         true,
    ///         true,
    ///         true,
    ///     ).await;
    /// }
    /// ```
    pub async fn token_price<Addr: AsRef<str>, Curr: AsRef<str>>(
        &self,
        id: &str,
        contract_addresses: &[Addr],
        vs_currencies: &[Curr],
        include_market_cap: bool,
        include_24hr_vol: bool,
        include_24hr_change: bool,
        include_last_updated_at: bool,
    ) -> Result<HashMap<String, Price>, Error> {
        let contract_addresses = contract_addresses
            .iter()
            .map(AsRef::as_ref)
            .collect::<Vec<_>>();
        let vs_currencies = vs_currencies.iter().map(AsRef::as_ref).collect::<Vec<_>>();
        let req = format!("/simple/token_price/{}?contract_addresses={}&vs_currencies={}&include_market_cap={}&include_24hr_vol={}&include_24hr_change={}&include_last_updated_at={}", id, contract_addresses.join("%2C"), vs_currencies.join("%2C"), include_market_cap, include_24hr_vol, include_24hr_change, include_last_updated_at);
        self.get(&req).await
    }

    /// Get list of supported_vs_currencies
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.supported_vs_currencies().await;
    /// }
    /// ```
    pub async fn supported_vs_currencies(&self) -> Result<SupportedVsCurrencies, Error> {
        self.get("/simple/supported_vs_currencies").await
    }

    /// List all supported coins id, name and symbol (no pagination required)
    ///
    /// Use this to obtain all the coins’ id in order to make API calls
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.coins_list(true).await;
    /// }
    /// ```
    pub async fn coins_list(&self, include_platform: bool) -> Result<Vec<CoinsListItem>, Error> {
        let req = format!("/coins/list?include_platform={}", include_platform);
        self.get(&req).await
    }

    /// List all supported coins price, market cap, volume, and market related data
    ///
    /// Use this to obtain all the coins market data (price, market cap, volume)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::{
    ///         params::{MarketsOrder, PriceChangePercentage},
    ///         CoinGeckoClient,
    ///     };
    ///     let client = CoinGeckoClient::default();
    ///     
    ///     client.coins_markets(
    ///         "usd",
    ///         &["bitcoin"],
    ///         None,
    ///         MarketsOrder::GeckoDesc,
    ///         1,
    ///         0,
    ///         true,
    ///         &[
    ///             PriceChangePercentage::OneHour,
    ///             PriceChangePercentage::TwentyFourHours,
    ///             PriceChangePercentage::SevenDays,
    ///             PriceChangePercentage::FourteenDays,
    ///             PriceChangePercentage::ThirtyDays,
    ///             PriceChangePercentage::OneYear,
    ///         ],
    ///     ).await;
    /// }
    /// ```
    pub async fn coins_markets<Id: AsRef<str>>(
        &self,
        vs_currency: &str,
        ids: &[Id],
        category: Option<&str>,
        order: MarketsOrder,
        per_page: i64,
        page: i64,
        sparkline: bool,
        price_change_percentage: &[PriceChangePercentage],
    ) -> Result<Vec<CoinsMarketItem>, Error> {
        let ids = ids.iter().map(AsRef::as_ref).collect::<Vec<_>>();

        let category = match category {
            Some(c) => format!("&category={}", c),
            _ => String::from(""),
        };

        let order = match order {
            MarketsOrder::MarketCapDesc => "market_cap_desc",
            MarketsOrder::MarketCapAsc => "market_cap_asc",
            MarketsOrder::GeckoDesc => "gecko_desc",
            MarketsOrder::GeckoAsc => "gecko_asc",
            MarketsOrder::VolumeDesc => "volume_desc",
            MarketsOrder::VolumeAsc => "volume_asc",
            MarketsOrder::IdDesc => "id_desc",
            MarketsOrder::IdAsc => "id_asc",
        };

        let price_change_percentage = price_change_percentage.iter().fold(
            Vec::with_capacity(price_change_percentage.len()),
            |mut acc, x| {
                let current = match *x {
                    PriceChangePercentage::OneHour => "1h",
                    PriceChangePercentage::TwentyFourHours => "24h",
                    PriceChangePercentage::SevenDays => "7d",
                    PriceChangePercentage::FourteenDays => "14d",
                    PriceChangePercentage::ThirtyDays => "30d",
                    PriceChangePercentage::TwoHundredDays => "200d",
                    PriceChangePercentage::OneYear => "1y",
                };

                acc.push(current);
                acc
            },
        );

        let req = format!("/coins/markets?vs_currency={}&ids={}{}&order={}&per_page={}&page={}&sparkline={}&price_change_percentage={}", vs_currency, ids.join("%2C"), category, order, per_page, page, sparkline, price_change_percentage.join("%2C"));
        self.get(&req).await
    }

    /// Get current data (name, price, market, ... including exchange tickers) for a coin
    ///
    /// **IMPORTANT**:
    /// Ticker object is limited to 100 items, to get more tickers, use coin_tickers
    /// Ticker is_stale is true when ticker that has not been updated/unchanged from the exchange for a while.
    /// Ticker is_anomaly is true if ticker’s price is outliered by our system.
    /// You are responsible for managing how you want to display these information (e.g. footnote, different background, change opacity, hide)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///     
    ///     client.coin("bitcoin", true, true, true, true, true, true).await;
    /// }
    /// ```
    pub async fn coin(
        &self,
        id: &str,
        localization: bool,
        tickers: bool,
        market_data: bool,
        community_data: bool,
        developer_data: bool,
        sparkline: bool,
    ) -> Result<CoinsItem, Error> {
        let req = format!("/coins/{}?localization={}&tickers={}&market_data={}&community_data={}&developer_data={}&sparkline={}", id, localization, tickers, market_data, community_data, developer_data, sparkline);
        self.get(&req).await
    }

    /// Get coin tickers (paginated to 100 items)
    ///
    /// **IMPORTANT**:
    /// Ticker is_stale is true when ticker that has not been updated/unchanged from the exchange for a while.
    /// Ticker is_anomaly is true if ticker’s price is outliered by our system.
    /// You are responsible for managing how you want to display these information (e.g. footnote, different background, change opacity, hide)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::{params::TickersOrder, CoinGeckoClient};
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.coin_tickers::<&str>("bitcoin", None, true, 1, TickersOrder::VolumeDesc, true).await;
    /// }
    /// ```
    pub async fn coin_tickers<Ex: AsRef<str>>(
        &self,
        id: &str,
        exchange_ids: Option<&[Ex]>,
        include_exchange_logo: bool,
        page: i64,
        order: TickersOrder,
        depth: bool,
    ) -> Result<Tickers, Error> {
        let order = match order {
            TickersOrder::TrustScoreAsc => "trust_score_asc",
            TickersOrder::TrustScoreDesc => "trust_score_desc",
            TickersOrder::VolumeDesc => "volume_desc",
        };

        let req = match exchange_ids {
            Some(e_ids) => {
                let e_ids = e_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>();
                format!("/coins/{}/tickers?exchange_ids={}&include_exchange_logo={}&page={}&order={}&depth={}", id, e_ids.join("%2C"), include_exchange_logo, &page, order, depth)
            }
            None => format!(
                "/coins/{}/tickers?include_exchange_logo={}&page={}&order={}&depth={}",
                id, include_exchange_logo, &page, order, depth
            ),
        };

        self.get(&req).await
    }

    /// Get historical data (name, price, market, stats) at a given date for a coin
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use chrono::NaiveDate;
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.coin_history("bitcoin", NaiveDate::from_ymd(2017, 12, 30), true).await;
    /// }
    /// ```
    pub async fn coin_history(
        &self,
        id: &str,
        date: NaiveDate,
        localization: bool,
    ) -> Result<History, Error> {
        let formatted_date = date.format("%d-%m-%Y").to_string();

        let req = format!(
            "/coins/{}/history?date={}&localization={}",
            id, formatted_date, localization
        );
        self.get(&req).await
    }

    /// Get historical market data include price, market cap, and 24h volume (granularity auto)
    ///
    /// **Minutely data will be used for duration within 1 day, Hourly data will be used for duration between 1 day and 90 days, Daily data will be used for duration above 90 days.**
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.coin_market_chart("bitcoin", "usd", 1, true).await;
    /// }
    /// ```
    pub async fn coin_market_chart(
        &self,
        id: &str,
        vs_currency: &str,
        days: i64,
        use_daily_interval: bool,
    ) -> Result<MarketChart, Error> {
        let req = match use_daily_interval {
            true => format!(
                "/coins/{}/market_chart?vs_currency={}&days={}",
                id, vs_currency, days
            ),
            false => format!(
                "/coins/{}/market_chart?vs_currency={}&days={}&interval=daily",
                id, vs_currency, days
            ),
        };

        self.get(&req).await
    }

    /// Get historical market data include price, market cap, and 24h volume within a range of timestamp (granularity auto)
    ///
    /// - **Data granularity is automatic (cannot be adjusted)**
    /// - **1 day from query time = 5 minute interval data**
    /// - **1 - 90 days from query time = hourly data**
    /// - **above 90 days from query time = daily data (00:00 UTC)**
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use chrono::NaiveDate;
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     let from = NaiveDate::from_ymd(2014, 2, 16).and_hms(19, 0, 32);
    ///     let to = NaiveDate::from_ymd(2015, 1, 30).and_hms(0, 20, 32);
    ///
    ///     client.coin_market_chart_range("bitcoin", "usd", from, to).await;
    /// }
    /// ```
    pub async fn coin_market_chart_range(
        &self,
        id: &str,
        vs_currency: &str,
        from: NaiveDateTime,
        to: NaiveDateTime,
    ) -> Result<MarketChart, Error> {
        let from_unix_timestamp = from.timestamp();
        let to_unix_timestamp = to.timestamp();

        let req = format!(
            "/coins/{}/market_chart/range?vs_currency={}&from={}&to={}",
            id, vs_currency, from_unix_timestamp, to_unix_timestamp
        );
        self.get(&req).await
    }

    /// Get status updates for a given coin
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///     client.coin_status_updates("qtum", 10, 1).await;
    /// }
    /// ```
    pub async fn coin_status_updates(
        &self,
        id: &str,
        per_page: i64,
        page: i64,
    ) -> Result<StatusUpdates, Error> {
        let req = format!(
            "/coins/{}/status_updates?per_page={}&page={}",
            id, per_page, page,
        );

        self.get(&req).await
    }

    /// Get coin's OHLC
    ///
    /// Candle’s body:
    /// 1 - 2 days: 30 minutes
    /// 3 - 30 days: 4 hours
    /// 31 and before: 4 days
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::{params::OhlcDays, CoinGeckoClient};
    ///     let client = CoinGeckoClient::default();
    ///     client.coin_ohlc("bitcoin", "usd", OhlcDays::OneDay).await;
    /// }
    /// ```
    pub async fn coin_ohlc(
        &self,
        id: &str,
        vs_currency: &str,
        days: OhlcDays,
    ) -> Result<Vec<Vec<f64>>, Error> {
        let days = match days {
            OhlcDays::OneDay => 1,
            OhlcDays::SevenDays => 7,
            OhlcDays::FourteenDays => 14,
            OhlcDays::ThirtyDays => 30,
            OhlcDays::NinetyDays => 90,
            OhlcDays::OneHundredEightyDays => 180,
            OhlcDays::ThreeHundredSixtyFiveDays => 365,
        };

        let req = format!(
            "/coins/{}/ohlc?vs_currency={}&days={}",
            id, vs_currency, days
        );
        self.get(&req).await
    }

    /// Get coin info from contract address
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///     let uniswap_contract = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";
    ///
    ///     client.contract("ethereum", &uniswap_contract).await;
    /// }
    /// ```
    pub async fn contract(&self, id: &str, contract_address: &str) -> Result<Contract, Error> {
        let req = format!("/coins/{}/contract/{}", id, contract_address);
        self.get(&req).await
    }

    /// Get historical market data include price, market cap, and 24h volume (granularity auto)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///     let uniswap_contract = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";
    ///
    ///     client.contract_market_chart("ethereum", &uniswap_contract, "usd", 1).await;
    /// }
    /// ```
    pub async fn contract_market_chart(
        &self,
        id: &str,
        contract_address: &str,
        vs_currency: &str,
        days: i64,
    ) -> Result<MarketChart, Error> {
        let req = format!(
            "/coins/{}/contract/{}/market_chart/?vs_currency={}&days={}",
            id, contract_address, vs_currency, days
        );
        self.get(&req).await
    }

    /// Get historical market data include price, market cap, and 24h volume within a range of timestamp (granularity auto)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use chrono::NaiveDate;
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///     let uniswap_contract = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";
    ///
    ///     let from = NaiveDate::from_ymd(2014, 2, 16).and_hms(19, 0, 32);
    ///     let to = NaiveDate::from_ymd(2015, 1, 30).and_hms(0, 20, 32);
    ///
    ///     client.contract_market_chart_range("ethereum", &uniswap_contract, "usd", from, to).await;
    /// }
    /// ```
    pub async fn contract_market_chart_range(
        &self,
        id: &str,
        contract_address: &str,
        vs_currency: &str,
        from: NaiveDateTime,
        to: NaiveDateTime,
    ) -> Result<MarketChart, Error> {
        let from_unix_timestamp = from.timestamp();
        let to_unix_timestamp = to.timestamp();

        let req = format!(
            "/coins/{}/contract/{}/market_chart/range?vs_currency={}&from={}&to={}",
            id, contract_address, vs_currency, from_unix_timestamp, to_unix_timestamp
        );
        self.get(&req).await
    }

    /// List all asset platforms (Blockchain networks)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.asset_platforms().await;
    /// }
    /// ```
    pub async fn asset_platforms(&self) -> Result<Vec<AssetPlatform>, Error> {
        self.get("/asset_platforms").await
    }

    /// List all categories
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.categories_list().await;
    /// }
    /// ```
    pub async fn categories_list(&self) -> Result<Vec<CategoryId>, Error> {
        self.get("/coins/categories/list").await
    }

    /// List all categories with market data
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.categories().await;
    /// }
    /// ```
    pub async fn categories(&self) -> Result<Vec<Category>, Error> {
        self.get("/coins/categories").await
    }

    /// List all exchanges
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.exchanges(10, 1).await;
    /// }
    /// ```
    pub async fn exchanges(&self, per_page: i64, page: i64) -> Result<Vec<Exchange>, Error> {
        let req = format!("/exchanges?per_page={}&page={}", per_page, page);
        self.get(&req).await
    }

    /// List all supported markets id and name (no pagination required)
    ///
    /// Use this to obtain all the markets’ id in order to make API calls
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.exchanges_list().await;
    /// }
    /// ```
    pub async fn exchanges_list(&self) -> Result<Vec<ExchangeId>, Error> {
        self.get("/exchanges/list").await
    }

    /// Get exchange volume in BTC and top 100 tickers only
    ///
    /// **IMPORTANT**:
    /// Ticker object is limited to 100 items, to get more tickers, use exchange_tickers
    /// Ticker is_stale is true when ticker that has not been updated/unchanged from the exchange for a while.
    /// Ticker is_anomaly is true if ticker’s price is outliered by our system.
    /// You are responsible for managing how you want to display these information (e.g. footnote, different background, change opacity, hide)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.exchange("binance").await;
    /// }
    /// ```
    pub async fn exchange(&self, id: &str) -> Result<Exchange, Error> {
        let req = format!("/exchanges/{}", id);
        self.get(&req).await
    }

    /// Get exchange tickers (paginated)
    ///
    /// **IMPORTANT**:
    /// Ticker is_stale is true when ticker that has not been updated/unchanged from the exchange for a while.
    /// Ticker is_anomaly is true if ticker’s price is outliered by our system.
    /// You are responsible for managing how you want to display these information (e.g. footnote, different background, change opacity, hide)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::{params::TickersOrder, CoinGeckoClient};
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.exchange_tickers("binance", Some(&["btc"]), true, 1, TickersOrder::TrustScoreAsc, true).await;
    /// }
    /// ```
    pub async fn exchange_tickers<CoinId: AsRef<str>>(
        &self,
        id: &str,
        coin_ids: Option<&[CoinId]>,
        include_exchange_logo: bool,
        page: i64,
        order: TickersOrder,
        depth: bool,
    ) -> Result<Tickers, Error> {
        let order = match order {
            TickersOrder::TrustScoreAsc => "trust_score_asc",
            TickersOrder::TrustScoreDesc => "trust_score_desc",
            TickersOrder::VolumeDesc => "volume_desc",
        };

        let req = match coin_ids {
            Some(c_ids) => {
                let c_ids = c_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>();
                format!("/exchanges/{}/tickers?coin_ids={}&include_exchange_logo={}&page={}&order={}&depth={}", id, c_ids.join("%2C"), include_exchange_logo, &page, order, depth)
            }
            None => format!(
                "/exchanges/{}/tickers?include_exchange_logo={}&page={}&order={}&depth={}",
                id, include_exchange_logo, &page, order, depth
            ),
        };

        self.get(&req).await
    }

    /// Get status updates for a given exchange
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.exchange_status_updates("binance", 10, 1).await;
    /// }
    /// ```
    pub async fn exchange_status_updates(
        &self,
        id: &str,
        per_page: i64,
        page: i64,
    ) -> Result<StatusUpdates, Error> {
        let req = format!(
            "/exchanges/{}/status_updates?per_page={}&page={}",
            id, per_page, page,
        );

        self.get(&req).await
    }

    /// Get volume_chart data for a given exchange
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.exchange_volume_chart("binance", 1).await;
    /// }
    /// ```
    pub async fn exchange_volume_chart(
        &self,
        id: &str,
        days: i64,
    ) -> Result<Vec<VolumeChartData>, Error> {
        let req = format!("/exchanges/{}/volume_chart?days={}", id, days);
        self.get(&req).await
    }

    /// List all finance platforms
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.finance_platforms(10, 1).await;
    /// }
    /// ```
    pub async fn finance_platforms(
        &self,
        per_page: i64,
        page: i64,
    ) -> Result<Vec<FinancePlatform>, Error> {
        let req = format!("/finance_platforms?per_page={}&page={}", per_page, page,);

        self.get(&req).await
    }

    /// List all finance products
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.finance_products(10, 1).await;
    /// }
    /// ```
    pub async fn finance_products(
        &self,
        per_page: i64,
        page: i64,
    ) -> Result<Vec<FinanceProduct>, Error> {
        let req = format!("/finance_products?per_page={}&page={}", per_page, page,);

        self.get(&req).await
    }

    /// List all market indexes
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.indexes(10, 1).await;
    /// }
    /// ```
    pub async fn indexes(&self, per_page: i64, page: i64) -> Result<Vec<Index>, Error> {
        let req = format!("/indexes?per_page={}&page={}", per_page, page,);

        self.get(&req).await
    }

    /// Get market index by market id and index id
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.indexes_market_id("binance_futures", "BTC").await;
    /// }
    /// ```
    pub async fn indexes_market_id(&self, market_id: &str, id: &str) -> Result<MarketIndex, Error> {
        let req = format!("/indexes/{}/{}", market_id, id);
        self.get(&req).await
    }

    /// List market indexes id and name
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.indexes_list().await;
    /// }
    /// ```
    pub async fn indexes_list(&self) -> Result<Vec<IndexId>, Error> {
        self.get("/indexes/list").await
    }

    /// List all derivative tickers
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::{params::DerivativesIncludeTickers, CoinGeckoClient};
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.derivatives(Some(DerivativesIncludeTickers::All)).await;
    /// }
    /// ```
    pub async fn derivatives(
        &self,
        include_tickers: Option<DerivativesIncludeTickers>,
    ) -> Result<Vec<Derivative>, Error> {
        let include_tickers = match include_tickers {
            Some(ic_enum) => match ic_enum {
                DerivativesIncludeTickers::All => "all",
                DerivativesIncludeTickers::Unexpired => "unexpired",
            },
            None => "unexpired",
        };

        let req = format!("/derivatives?include_tickers={}", include_tickers);
        self.get(&req).await
    }

    /// List all derivative exchanges
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::{params::DerivativeExchangeOrder, CoinGeckoClient};
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.derivative_exchanges(DerivativeExchangeOrder::NameAsc, 10, 1).await;
    /// }
    /// ```
    pub async fn derivative_exchanges(
        &self,
        order: DerivativeExchangeOrder,
        per_page: i64,
        page: i64,
    ) -> Result<Vec<Derivative>, Error> {
        let order = match order {
            DerivativeExchangeOrder::NameAsc => "name_asc",
            DerivativeExchangeOrder::NameDesc => "name_desc",
            DerivativeExchangeOrder::OpenInterestBtcAsc => "open_interest_btc_asc",
            DerivativeExchangeOrder::OpenInterestBtcDesc => "open_interest_btc_desc",
            DerivativeExchangeOrder::TradeVolume24hBtcAsc => "trade_volume_24h_btc_asc",
            DerivativeExchangeOrder::TradeVolume24hBtcDesc => "trade_volume_24h_btc_desc",
        };

        let req = format!(
            "/derivatives/exchanges?order={}&per_page={}&page={}",
            order, per_page, page
        );
        self.get(&req).await
    }

    /// Show derivative exchange data
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::{params::DerivativesIncludeTickers, CoinGeckoClient};
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.derivatives_exchange("bitmex", Some(DerivativesIncludeTickers::All)).await;
    /// }
    /// ```
    pub async fn derivatives_exchange(
        &self,
        id: &str,
        include_tickers: Option<DerivativesIncludeTickers>,
    ) -> Result<Vec<Derivative>, Error> {
        let include_tickers = match include_tickers {
            Some(ic_enum) => match ic_enum {
                DerivativesIncludeTickers::All => "all",
                DerivativesIncludeTickers::Unexpired => "unexpired",
            },
            None => "unexpired",
        };

        let req = format!(
            "/derivatives/exchanges/{}?include_tickers={}",
            id, include_tickers
        );
        self.get(&req).await
    }

    /// List all derivative exchanges name and identifier
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.derivative_exchanges_list().await;
    /// }
    /// ```
    pub async fn derivative_exchanges_list(&self) -> Result<Vec<DerivativeExchangeId>, Error> {
        self.get("/derivatives/exchanges/list").await
    }

    /// List all status_updates with data (description, category, created_at, user, user_title and pin)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.status_updates(Some("general"), Some("coin"), 10, 1).await;
    /// }
    /// ```
    pub async fn status_updates(
        &self,
        category: Option<&str>,
        project_type: Option<&str>,
        per_page: i64,
        page: i64,
    ) -> Result<StatusUpdates, Error> {
        let mut params: Vec<String> = Vec::with_capacity(4);

        if let Some(c) = category {
            params.push(format!("category={}", c));
        }

        if let Some(t) = project_type {
            params.push(format!("project_type={}", t));
        }

        params.push(per_page.to_string());
        params.push(page.to_string());

        let req = format!("/status_updates?{}", params.join("&"));

        self.get(&req).await
    }

    /// Get events, paginated by 100
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use chrono::NaiveDate;
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     let from = NaiveDate::from_ymd(2021, 10, 7);
    ///     let to = NaiveDate::from_ymd(2022, 10, 7);
    ///
    ///     client.events(Some("HK"), Some("Event"), 1, true, from, to).await;
    /// }
    /// ```
    pub async fn events(
        &self,
        country_code: Option<&str>,
        event_type: Option<&str>,
        page: i64,
        upcoming_events_only: bool,
        from_date: NaiveDate,
        to_date: NaiveDate,
    ) -> Result<Events, Error> {
        let mut params: Vec<String> = Vec::with_capacity(2);

        if let Some(c) = country_code {
            params.push(format!("country_code={}", c));
        }

        if let Some(t) = event_type {
            params.push(format!("type={}", t));
        }

        let from_date = from_date.format("%Y-%m-%d").to_string();
        let to_date = to_date.format("%Y-%m-%d").to_string();

        let req = format!(
            "/events?{}&page={}&upcoming_events_only={}&from_date={}&to_date={}",
            params.join("&"),
            page,
            upcoming_events_only,
            from_date,
            to_date,
        );

        self.get(&req).await
    }

    /// Get list of event countries
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.event_countries().await;
    /// }
    /// ```
    pub async fn event_countries(&self) -> Result<EventCountries, Error> {
        self.get("/events/types").await
    }

    /// Get list of event types
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.event_types().await;
    /// }
    /// ```
    pub async fn event_types(&self) -> Result<EventTypes, Error> {
        self.get("/events/types").await
    }

    /// Get BTC-to-Currency exchange rates
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.exchange_rates().await;
    /// }
    /// ```
    pub async fn exchange_rates(&self) -> Result<ExchangeRates, Error> {
        self.get("/exchange_rates").await
    }

    /// Top-7 trending coins on CoinGecko as searched by users in the last 24 hours (Ordered by most popular first)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.trending().await;
    /// }
    /// ```
    pub async fn trending(&self) -> Result<Trending, Error> {
        self.get("/search/trending").await
    }

    /// Get cryptocurrency global data
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.global().await;
    /// }
    /// ```
    pub async fn global(&self) -> Result<Global, Error> {
        self.get("/global").await
    }

    /// Get Top 100 Cryptocurrency Global Eecentralized Finance(defi) data
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::CoinGeckoClient;
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.global_defi().await;
    /// }
    /// ```
    pub async fn global_defi(&self) -> Result<GlobalDefi, Error> {
        self.get("/global/decentralized_finance_defi").await
    }

    /// Get public companies bitcoin or ethereum holdings (Ordered by total holdings descending)
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[tokio::main]
    /// async fn main() {
    ///     use coingecko_rs::{params::CompaniesCoinId, CoinGeckoClient};
    ///     let client = CoinGeckoClient::default();
    ///
    ///     client.companies(CompaniesCoinId::Bitcoin).await;
    /// }
    /// ```
    pub async fn companies(
        &self,
        coin_id: CompaniesCoinId,
    ) -> Result<CompaniesPublicTreasury, Error> {
        let req = match coin_id {
            CompaniesCoinId::Bitcoin => "/companies/public_treasury/bitcoin".to_string(),
            CompaniesCoinId::Ethereum => "/companies/public_treasury/ethereum".to_string(),
        };

        self.get(&req).await
    }
}