datamaxi/dex.rs
1use crate::api::{Client, Config, Datamaxi, Result};
2pub use crate::models::{CandleOptions, PoolsOptions, TradeOptions};
3use crate::models::{CandleResponse, PoolsResponse, TradeResponse};
4use std::collections::HashMap;
5
6/// Provides methods for retrieving DEX candle data and related information.
7#[derive(Clone)]
8pub struct Dex {
9 pub client: Client,
10}
11
12impl Dex {
13 /// Retrieves candle data for a specified chain, exchange, and pool. Additional parameters can be
14 /// provided to filter and sort the results. The response will contain an array of candle data
15 /// objects, each representing a single candle with open, high, low, close, and volume values.
16 pub fn candle<C, E, P>(
17 &self,
18 chain: C,
19 exchange: E,
20 pool: P,
21 options: CandleOptions,
22 ) -> Result<CandleResponse>
23 where
24 C: Into<String>,
25 E: Into<String>,
26 P: Into<String>,
27 {
28 let mut parameters = HashMap::new();
29
30 // required
31 parameters.insert("chain".to_string(), chain.into());
32 parameters.insert("exchange".to_string(), exchange.into());
33 parameters.insert("pool".to_string(), pool.into());
34
35 // optional
36 parameters.extend(
37 [
38 options
39 .market
40 .map(|market| ("market".to_string(), market.to_string())),
41 options
42 .interval
43 .map(|interval| ("interval".to_string(), interval.to_string())),
44 options
45 .page
46 .map(|page| ("page".to_string(), page.to_string())),
47 options
48 .limit
49 .map(|limit| ("limit".to_string(), limit.to_string())),
50 options
51 .from
52 .map(|from| ("from".to_string(), from.to_string())),
53 options.to.map(|to| ("to".to_string(), to.to_string())),
54 options
55 .sort
56 .map(|sort| ("sort".to_string(), sort.to_string())),
57 ]
58 .into_iter()
59 .flatten(),
60 );
61
62 self.client.get("/dex/candle", Some(parameters))
63 }
64
65 /// Retrieves trade data for a specified chain, exchange, and pool. Additional parameters can be
66 /// provided to filter and sort the results. The response will contain an array of trade data
67 /// objects, each representing a single trade with price, amount, and timestamp values.
68 pub fn trade<C, E, P>(
69 &self,
70 chain: C,
71 exchange: E,
72 pool: P,
73 options: TradeOptions,
74 ) -> Result<TradeResponse>
75 where
76 C: Into<String>,
77 E: Into<String>,
78 P: Into<String>,
79 {
80 let mut parameters = HashMap::new();
81
82 // required
83 parameters.insert("chain".to_string(), chain.into());
84 parameters.insert("exchange".to_string(), exchange.into());
85 parameters.insert("pool".to_string(), pool.into());
86
87 // optional
88 parameters.extend(
89 [
90 options
91 .page
92 .map(|page| ("page".to_string(), page.to_string())),
93 options
94 .limit
95 .map(|limit| ("limit".to_string(), limit.to_string())),
96 options
97 .from
98 .map(|from| ("from".to_string(), from.to_string())),
99 options.to.map(|to| ("to".to_string(), to.to_string())),
100 options
101 .sort
102 .map(|sort| ("sort".to_string(), sort.to_string())),
103 ]
104 .into_iter()
105 .flatten(),
106 );
107
108 self.client.get("/dex/trade", Some(parameters))
109 }
110
111 /// Retrieves information about available pools, including details about the chain, exchange,
112 /// base and quote symbols, and pool address. Optional parameters can be provided to filter the
113 /// results by chain and exchange.
114 pub fn pools(&self, options: PoolsOptions) -> Result<Vec<PoolsResponse>> {
115 let mut parameters = HashMap::new();
116
117 // optional
118 parameters.extend(
119 [
120 options
121 .exchange
122 .map(|exchange| ("exchange".to_string(), exchange.to_string())),
123 options
124 .chain
125 .map(|chain| ("chain".to_string(), chain.to_string())),
126 ]
127 .into_iter()
128 .flatten(),
129 );
130
131 self.client.get("/dex/pools", Some(parameters))
132 }
133
134 /// Retrieves a list of available chains for candle data.
135 pub fn chains(&self) -> Result<Vec<String>> {
136 self.client.get("/dex/chains", None)
137 }
138
139 /// Retrieves a list of available exchanges for candle data.
140 pub fn exchanges(&self) -> Result<Vec<String>> {
141 self.client.get("/dex/exchanges", None)
142 }
143
144 /// Retrieves a list of available intervals for candle data.
145 pub fn intervals(&self) -> Result<Vec<String>> {
146 self.client.get("/dex/intervals", None)
147 }
148}
149
150/// Implements the `Datamaxi` trait for `Dex`, providing methods
151/// to create new instances of `Dex` with or without a custom base URL.
152impl Datamaxi for Dex {
153 /// Creates a new `Dex` instance with the default base URL.
154 ///
155 /// # Parameters
156 /// - `api_key`: A `String` representing the API key used to authenticate requests.
157 ///
158 /// # Returns
159 /// A new `Dex` instance configured with the default base URL and the provided `api_key`.
160 ///
161 /// # Example
162 /// ```rust
163 /// use crate::datamaxi::api::Datamaxi;
164 /// let dex = datamaxi::dex::Dex::new("my_api_key".to_string());
165 /// ```
166 fn new(api_key: String) -> Dex {
167 let config = Config {
168 base_url: None, // Default base URL will be used
169 api_key, // Provided API key
170 };
171 Dex {
172 client: Client::new(config), // Create a new client with the provided config
173 }
174 }
175
176 /// Creates a new `Dex` instance with a custom base URL.
177 ///
178 /// # Parameters
179 /// - `api_key`: A `String` representing the API key used to authenticate requests.
180 /// - `base_url`: A `String` representing the custom base URL for API requests.
181 ///
182 /// # Returns
183 /// A new `Dex` instance configured with the provided `base_url` and `api_key`.
184 ///
185 /// # Example
186 /// ```rust
187 /// use crate::datamaxi::api::Datamaxi;
188 /// let dex = datamaxi::dex::Dex::new_with_base_url("my_api_key".to_string(), "https://custom-api.example.com".to_string());
189 /// ```
190 fn new_with_base_url(api_key: String, base_url: String) -> Dex {
191 let config = Config {
192 base_url: Some(base_url), // Use the provided custom base URL
193 api_key, // Provided API key
194 };
195 Dex {
196 client: Client::new(config), // Create a new client with the provided config
197 }
198 }
199}