Skip to main content

rust_okx/api/convert/
responses.rs

1use std::fmt;
2
3use serde::{Deserialize, Deserializer};
4
5use crate::model::{NumberString, OrderSide};
6
7/// A currency supported by OKX Convert.
8#[derive(Debug, Clone, Deserialize)]
9#[serde(rename_all = "camelCase")]
10#[non_exhaustive]
11pub struct ConvertCurrency {
12    /// Currency code, e.g. `BTC`.
13    pub ccy: String,
14
15    /// Deprecated minimum amount to convert.
16    ///
17    /// OKX currently returns an empty string for this field.
18    #[serde(default)]
19    pub min: NumberString,
20
21    /// Deprecated maximum amount to convert.
22    ///
23    /// OKX currently returns an empty string for this field.
24    #[serde(default)]
25    pub max: NumberString,
26}
27
28/// Limits and metadata for a currency pair supported by OKX Convert.
29#[derive(Debug, Clone, Deserialize)]
30#[serde(rename_all = "camelCase")]
31#[non_exhaustive]
32pub struct ConvertCurrencyPair {
33    /// Currency pair, e.g. `BTC-USDT`.
34    pub inst_id: String,
35
36    /// Base currency, e.g. `BTC` in `BTC-USDT`.
37    pub base_ccy: String,
38
39    /// Maximum amount of base currency that can be converted.
40    pub base_ccy_max: NumberString,
41
42    /// Minimum amount of base currency that can be converted.
43    pub base_ccy_min: NumberString,
44
45    /// Quote currency, e.g. `USDT` in `BTC-USDT`.
46    pub quote_ccy: String,
47
48    /// Maximum amount of quote currency that can be converted.
49    pub quote_ccy_max: NumberString,
50
51    /// Minimum amount of quote currency that can be converted.
52    pub quote_ccy_min: NumberString,
53}
54
55/// A quote returned by the Convert estimate-quote endpoint.
56#[derive(Debug, Clone, Deserialize)]
57#[serde(rename_all = "camelCase")]
58#[non_exhaustive]
59pub struct ConvertQuote {
60    /// Quotation generation time as a Unix timestamp in milliseconds.
61    pub quote_time: NumberString,
62
63    /// Quote validity period in milliseconds.
64    pub ttl_ms: NumberString,
65
66    /// Client-supplied quote request ID, when supplied.
67    #[serde(default)]
68    pub cl_q_req_id: String,
69
70    /// Server-assigned quote ID.
71    pub quote_id: String,
72
73    /// Base currency, e.g. `BTC` in `BTC-USDT`.
74    pub base_ccy: String,
75
76    /// Quote currency, e.g. `USDT` in `BTC-USDT`.
77    pub quote_ccy: String,
78
79    /// Trade side based on the base currency.
80    pub side: OrderSide,
81
82    /// Original RFQ amount requested by the client.
83    pub orig_rfq_sz: NumberString,
84
85    /// Actual RFQ amount accepted for the quote.
86    pub rfq_sz: NumberString,
87
88    /// Currency in which the RFQ amount is denominated.
89    pub rfq_sz_ccy: String,
90
91    /// Conversion price denominated in the quote currency.
92    pub cnvt_px: NumberString,
93
94    /// Quoted amount of the base currency.
95    pub base_sz: NumberString,
96
97    /// Quoted amount of the quote currency.
98    pub quote_sz: NumberString,
99}
100
101/// State of a Convert trade.
102#[derive(Debug, Clone, PartialEq, Eq, Hash)]
103#[non_exhaustive]
104pub enum ConvertTradeState {
105    /// The conversion completed successfully.
106    FullyFilled,
107
108    /// The conversion was rejected.
109    Rejected,
110
111    /// A state not modeled by this crate version.
112    Unknown(String),
113}
114
115impl ConvertTradeState {
116    /// Return the OKX wire representation of this state.
117    pub fn as_str(&self) -> &str {
118        match self {
119            Self::FullyFilled => "fullyFilled",
120            Self::Rejected => "rejected",
121            Self::Unknown(value) => value,
122        }
123    }
124}
125
126impl From<&str> for ConvertTradeState {
127    fn from(value: &str) -> Self {
128        match value {
129            "fullyFilled" => Self::FullyFilled,
130            "rejected" => Self::Rejected,
131            other => Self::Unknown(other.to_owned()),
132        }
133    }
134}
135
136impl fmt::Display for ConvertTradeState {
137    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
138        formatter.write_str(self.as_str())
139    }
140}
141
142impl<'de> Deserialize<'de> for ConvertTradeState {
143    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
144    where
145        D: Deserializer<'de>,
146    {
147        let value = String::deserialize(deserializer)?;
148        Ok(Self::from(value.as_str()))
149    }
150}
151
152/// Result returned after executing a Convert trade.
153#[derive(Debug, Clone, Deserialize)]
154#[serde(rename_all = "camelCase")]
155#[non_exhaustive]
156pub struct ConvertTradeResult {
157    /// Server-assigned trade ID.
158    pub trade_id: String,
159
160    /// Quote ID used to execute the conversion.
161    pub quote_id: String,
162
163    /// Client-supplied trade request ID, when supplied.
164    #[serde(default)]
165    pub cl_t_req_id: String,
166
167    /// Trade execution state.
168    pub state: ConvertTradeState,
169
170    /// Currency pair, e.g. `BTC-USDT`.
171    pub inst_id: String,
172
173    /// Base currency, e.g. `BTC` in `BTC-USDT`.
174    pub base_ccy: String,
175
176    /// Quote currency, e.g. `USDT` in `BTC-USDT`.
177    pub quote_ccy: String,
178
179    /// Trade side based on the base currency.
180    pub side: OrderSide,
181
182    /// Filled price denominated in the quote currency.
183    pub fill_px: NumberString,
184
185    /// Filled amount of the base currency.
186    pub fill_base_sz: NumberString,
187
188    /// Filled amount of the quote currency.
189    pub fill_quote_sz: NumberString,
190
191    /// Convert trade time as a Unix timestamp in milliseconds.
192    pub ts: NumberString,
193}
194
195/// A historical Convert trade.
196#[derive(Debug, Clone, Deserialize)]
197#[serde(rename_all = "camelCase")]
198#[non_exhaustive]
199pub struct ConvertHistory {
200    /// Server-assigned trade ID.
201    pub trade_id: String,
202
203    /// Client-supplied trade request ID, when supplied.
204    #[serde(default)]
205    pub cl_t_req_id: String,
206
207    /// Trade execution state.
208    pub state: ConvertTradeState,
209
210    /// Currency pair, e.g. `BTC-USDT`.
211    pub inst_id: String,
212
213    /// Base currency, e.g. `BTC` in `BTC-USDT`.
214    pub base_ccy: String,
215
216    /// Quote currency, e.g. `USDT` in `BTC-USDT`.
217    pub quote_ccy: String,
218
219    /// Trade side based on the base currency.
220    pub side: OrderSide,
221
222    /// Filled price denominated in the quote currency.
223    pub fill_px: NumberString,
224
225    /// Filled amount of the base currency.
226    pub fill_base_sz: NumberString,
227
228    /// Filled amount of the quote currency.
229    pub fill_quote_sz: NumberString,
230
231    /// Convert trade time as a Unix timestamp in milliseconds.
232    pub ts: NumberString,
233}