Skip to main content

rust_okx/api/convert/
api.rs

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