Skip to main content

rust_okx/api/convert/
api.rs

1use crate::api::convert::requests::{ConvertCurrencyPairRequest, ConvertCurrencyRequest};
2use crate::client::OkxClient;
3use crate::error::Error;
4use crate::transport::Transport;
5
6use super::requests::{ConvertQuoteRequest, ConvertTradeRequest};
7use super::responses::{ConvertCurrencyPair, ConvertQuote};
8
9const CURRENCIES: &str = "/api/v5/asset/convert/currencies";
10const CURRENCY_PAIR: &str = "/api/v5/asset/convert/currency-pair";
11const ESTIMATE_QUOTE: &str = "/api/v5/asset/convert/estimate-quote";
12const TRADE: &str = "/api/v5/asset/convert/trade";
13const HISTORY: &str = "/api/v5/asset/convert/history";
14
15/// Accessor for OKX Convert endpoints.
16///
17/// Obtain one via [`OkxClient::convert`](crate::OkxClient::convert). All
18/// methods require credentials.
19pub struct Convert<'a, T> {
20    client: &'a OkxClient<T>,
21}
22
23impl<'a, T: Transport> Convert<'a, T> {
24    pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
25        Self { client }
26    }
27
28    /// Retrieve currencies supported by Convert.
29    ///
30    /// `GET /api/v5/asset/convert/currencies`. Authenticated.
31    ///
32    /// # Errors
33    ///
34    /// Returns [`Error::Configuration`] without credentials, [`Error::Api`] on a
35    /// non-zero OKX code, or transport/decode errors.
36    pub async fn get_currencies(&self) -> Result<Vec<super::responses::ConvertCurrency>, Error> {
37        self.client
38            .get(CURRENCIES, &ConvertCurrencyRequest::new(), true)
39            .await
40    }
41
42    /// Retrieve a Convert currency pair.
43    ///
44    /// `GET /api/v5/asset/convert/currency-pair`. Authenticated.
45    ///
46    /// # Errors
47    ///
48    /// See [`get_currencies`](Self::get_currencies).
49    pub async fn get_currency_pair(
50        &self,
51        from_ccy: Option<&str>,
52        to_ccy: Option<&str>,
53    ) -> Result<Vec<ConvertCurrencyPair>, Error> {
54        let mut query = ConvertCurrencyPairRequest::new();
55        if let Some(value) = from_ccy {
56            query = query.param("fromCcy", value);
57        }
58        if let Some(value) = to_ccy {
59            query = query.param("toCcy", value);
60        }
61        self.client.get(CURRENCY_PAIR, &query, true).await
62    }
63
64    /// Estimate a Convert quote.
65    ///
66    /// `POST /api/v5/asset/convert/estimate-quote`. Authenticated.
67    ///
68    /// # Errors
69    ///
70    /// See [`get_currencies`](Self::get_currencies).
71    pub async fn estimate_quote(
72        &self,
73        request: &ConvertQuoteRequest,
74    ) -> Result<Vec<ConvertQuote>, Error> {
75        self.client.post(ESTIMATE_QUOTE, request, true).await
76    }
77
78    /// Execute a Convert trade.
79    ///
80    /// `POST /api/v5/asset/convert/trade`. Authenticated.
81    ///
82    /// # Errors
83    ///
84    /// See [`get_currencies`](Self::get_currencies).
85    pub async fn convert_trade(
86        &self,
87        request: &ConvertTradeRequest,
88    ) -> Result<Vec<super::responses::ConvertTradeResult>, Error> {
89        self.client.post(TRADE, request, true).await
90    }
91
92    /// Retrieve Convert history.
93    ///
94    /// `GET /api/v5/asset/convert/history`. Authenticated.
95    ///
96    /// # Errors
97    ///
98    /// See [`get_currencies`](Self::get_currencies).
99    pub async fn get_convert_history(
100        &self,
101        request: &super::requests::ConvertHistoryRequest,
102    ) -> Result<Vec<super::responses::ConvertHistory>, Error> {
103        self.client.get(HISTORY, request, true).await
104    }
105}