rust-okx 0.6.4

Async Rust client for the OKX v5 REST API
Documentation
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
use crate::model::{NumberString, deserialize_vec_or_empty_string};
use serde::Deserialize;

/// Option summary row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct OptionSummary {
    /// Value returned by OKX in the `instType` field.
    #[serde(default)]
    pub inst_type: String,
    /// Value returned by OKX in the `instId` field.
    #[serde(default)]
    pub inst_id: String,
    /// Value returned by OKX in the `uly` field.
    #[serde(default)]
    pub uly: String,
    /// Value returned by OKX in the `instFamily` field.
    #[serde(default)]
    pub inst_family: String,
    /// Value returned by OKX in the `delta` field.
    #[serde(default)]
    pub delta: NumberString,
    /// Value returned by OKX in the `gamma` field.
    #[serde(default)]
    pub gamma: NumberString,
    /// Value returned by OKX in the `vega` field.
    #[serde(default)]
    pub vega: NumberString,
    /// Value returned by OKX in the `theta` field.
    #[serde(default)]
    pub theta: NumberString,
    /// Value returned by OKX in the `deltaBs` field.
    #[serde(default)]
    pub delta_bs: NumberString,
    /// Value returned by OKX in the `gammaBs` field.
    #[serde(default)]
    pub gamma_bs: NumberString,
    /// Value returned by OKX in the `vegaBs` field.
    #[serde(default)]
    pub vega_bs: NumberString,
    /// Value returned by OKX in the `thetaBs` field.
    #[serde(default)]
    pub theta_bs: NumberString,
    /// Value returned by OKX in the `lever` field.
    #[serde(default)]
    pub lever: NumberString,
    /// Value returned by OKX in the `markVol` field.
    #[serde(default)]
    pub mark_vol: NumberString,
    /// Value returned by OKX in the `realVol` field.
    #[serde(default)]
    pub real_vol: NumberString,
    /// Value returned by OKX in the `volLv` field.
    #[serde(default)]
    pub vol_lv: NumberString,
    /// Value returned by OKX in the `bidVol` field.
    #[serde(default)]
    pub bid_vol: NumberString,
    /// Value returned by OKX in the `askVol` field.
    #[serde(default)]
    pub ask_vol: NumberString,
    /// Value returned by OKX in the `fwdPx` field.
    #[serde(default)]
    pub fwd_px: NumberString,
    /// Value returned by OKX in the `ts` field.
    #[serde(default)]
    pub ts: NumberString,
}

/// Estimated delivery or exercise price row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct EstimatedPrice {
    /// Value returned by OKX in the `instType` field.
    #[serde(default)]
    pub inst_type: String,
    /// Value returned by OKX in the `instId` field.
    #[serde(default)]
    pub inst_id: String,
    /// Value returned by OKX in the `settlePx` field.
    #[serde(default)]
    pub settle_px: NumberString,
    /// Value returned by OKX in the `ts` field.
    #[serde(default)]
    pub ts: NumberString,
}

/// Discount tier returned for interest-free quota calculations (legacy format).
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DiscountLevel {
    /// Value returned by OKX in the `discountRate` field.
    #[serde(default)]
    pub discount_rate: NumberString,
    /// Value returned by OKX in the `minAmt` field.
    #[serde(default)]
    pub min_amt: NumberString,
    /// Value returned by OKX in the `maxAmt` field.
    #[serde(default)]
    pub max_amt: NumberString,
}

/// Discount tier returned in the `details` array.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DiscountDetail {
    /// Value returned by OKX in the `discountRate` field.
    #[serde(default)]
    pub discount_rate: NumberString,
    /// Value returned by OKX in the `liqPenaltyRate` field.
    #[serde(default)]
    pub liq_penalty_rate: NumberString,
    /// Value returned by OKX in the `minAmt` field.
    #[serde(default)]
    pub min_amt: NumberString,
    /// Value returned by OKX in the `maxAmt` field.
    #[serde(default)]
    pub max_amt: NumberString,
    /// Value returned by OKX in the `tier` field.
    #[serde(default)]
    pub tier: String,
    /// Value returned by OKX in the `disCcyEq` field.
    #[serde(default)]
    pub dis_ccy_eq: NumberString,
}

/// Discount-rate and interest-free quota row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DiscountRateInterestFreeQuota {
    /// Value returned by OKX in the `ccy` field.
    #[serde(default)]
    pub ccy: String,
    /// Value returned by OKX in the `amt` field.
    #[serde(default)]
    pub amt: NumberString,
    /// Value returned by OKX in the `discountLv` field (discount level ID).
    #[serde(default)]
    pub discount_lv: String,
    /// Value returned by OKX in the `details` field.
    #[serde(default)]
    pub details: Vec<DiscountDetail>,
    /// Value returned by OKX in the `collateralRestrict` field.
    #[serde(default)]
    pub collateral_restrict: bool,
    /// Value returned by OKX in the `minDiscountRate` field.
    #[serde(default)]
    pub min_discount_rate: NumberString,
}

/// Per-currency base rate and quota.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LoanQuotaBasic {
    /// Value returned by OKX in the `ccy` field.
    #[serde(default)]
    pub ccy: String,
    /// Value returned by OKX in the `rate` field.
    #[serde(default)]
    pub rate: NumberString,
    /// Value returned by OKX in the `quota` field.
    #[serde(default)]
    pub quota: NumberString,
}

/// User-level quota coefficient.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LoanQuotaLevel {
    /// Value returned by OKX in the `level` field.
    #[serde(default)]
    pub level: String,
    /// Value returned by OKX in the `loanQuotaCoef` field.
    #[serde(default)]
    pub loan_quota_coef: NumberString,
    /// Value returned by OKX in the `irDiscount` field.
    #[serde(default)]
    pub ir_discount: NumberString,
}

/// Currency with a customized interest rate.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LoanQuotaConfiguredCurrency {
    /// Value returned by OKX in the `ccy` field.
    #[serde(default)]
    pub ccy: String,
    /// Value returned by OKX in the `rate` field.
    #[serde(default)]
    pub rate: NumberString,
}

/// Customized absolute loan quota.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct LoanQuotaConfig {
    /// Value returned by OKX in the `ccy` field.
    #[serde(default)]
    pub ccy: String,
    /// Value returned by OKX in the `stgyType` field.
    #[serde(default)]
    pub stgy_type: String,
    /// Value returned by OKX in the `quota` field.
    #[serde(default)]
    pub quota: NumberString,
    /// Value returned by OKX in the `level` field.
    #[serde(default)]
    pub level: String,
}

/// Interest-rate and loan-quota response row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InterestRateLoanQuota {
    /// Value returned by OKX in the `basic` field.
    #[serde(default, deserialize_with = "deserialize_vec_or_empty_string")]
    pub basic: Vec<LoanQuotaBasic>,
    /// Value returned by OKX in the `vip` field.
    #[serde(default, deserialize_with = "deserialize_vec_or_empty_string")]
    pub vip: Vec<LoanQuotaLevel>,
    /// Value returned by OKX in the `regular` field.
    #[serde(default, deserialize_with = "deserialize_vec_or_empty_string")]
    pub regular: Vec<LoanQuotaLevel>,
    /// Value returned by OKX in the `configCcyList` field.
    #[serde(default, deserialize_with = "deserialize_vec_or_empty_string")]
    pub config_ccy_list: Vec<LoanQuotaConfiguredCurrency>,
    /// Value returned by OKX in the `config` field.
    #[serde(default, deserialize_with = "deserialize_vec_or_empty_string")]
    pub config: Vec<LoanQuotaConfig>,
}

/// VIP interest-rate loan quota row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct VipInterestRateLoanQuota {
    /// Value returned by OKX in the `ccy` field.
    #[serde(default)]
    pub ccy: String,
    /// Value returned by OKX in the `level` field.
    #[serde(default)]
    pub level: String,
    /// Value returned by OKX in the `rate` field.
    #[serde(default)]
    pub rate: NumberString,
    /// Value returned by OKX in the `quota` field.
    #[serde(default)]
    pub quota: NumberString,
}

/// A single price range and tick-size definition.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TickBandDetail {
    /// Value returned by OKX in the `minPx` field.
    #[serde(default)]
    pub min_px: NumberString,
    /// Value returned by OKX in the `maxPx` field.
    #[serde(default)]
    pub max_px: NumberString,
    /// Value returned by OKX in the `tickSz` field.
    #[serde(default)]
    pub tick_sz: NumberString,
}

/// Instrument tick-band row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InstrumentTickBand {
    /// Value returned by OKX in the `instType` field.
    #[serde(default)]
    pub inst_type: String,
    /// Value returned by OKX in the `instFamily` field.
    #[serde(default)]
    pub inst_family: String,
    /// Value returned by OKX in the `tickBand` field.
    #[serde(default, deserialize_with = "deserialize_vec_or_empty_string")]
    pub tick_band: Vec<TickBandDetail>,
    /// Value returned by OKX in the `ts` field.
    #[serde(default)]
    pub ts: NumberString,
}

/// Public option-trade row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct PublicOptionTrade {
    /// Value returned by OKX in the `instId` field.
    #[serde(default)]
    pub inst_id: String,
    /// Value returned by OKX in the `tradeId` field.
    #[serde(default)]
    pub trade_id: String,
    /// Value returned by OKX in the `px` field.
    #[serde(default)]
    pub px: NumberString,
    /// Value returned by OKX in the `sz` field.
    #[serde(default)]
    pub sz: NumberString,
    /// Value returned by OKX in the `side` field.
    #[serde(default)]
    pub side: String,
    /// Value returned by OKX in the `fillVol` field.
    #[serde(default)]
    pub fill_vol: NumberString,
    /// Value returned by OKX in the `fwdPx` field.
    #[serde(default)]
    pub fwd_px: NumberString,
    /// Value returned by OKX in the `idxPx` field.
    #[serde(default)]
    pub idx_px: NumberString,
    /// Value returned by OKX in the `markPx` field.
    #[serde(default)]
    pub mark_px: NumberString,
    /// Value returned by OKX in the `ts` field.
    #[serde(default)]
    pub ts: NumberString,
}

/// Market-data history row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MarketDataHistory {
    /// Value returned by OKX in the `module` field.
    #[serde(default)]
    pub module: String,
    /// Value returned by OKX in the `instType` field.
    #[serde(default)]
    pub inst_type: String,
    /// Value returned by OKX in the `instId` field.
    #[serde(default)]
    pub inst_id: String,
    /// Value returned by OKX in the `dateAggrType` field.
    #[serde(default)]
    pub date_aggr_type: String,
    /// Value returned by OKX in the `value` field.
    #[serde(default)]
    pub value: NumberString,
    /// Value returned by OKX in the `ts` field.
    #[serde(default)]
    pub ts: NumberString,
}

/// MM Program instrument-type classification row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MmInstrumentType {
    /// Value returned by OKX in the `instId` field.
    #[serde(default)]
    pub inst_id: String,
    /// Value returned by OKX in the `instType` field.
    #[serde(default)]
    pub inst_type: String,
    /// Value returned by OKX in the `pairType` field (MM Program classification type).
    #[serde(default)]
    pub pair_type: String,
}

/// Macro-economic calendar row.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct EconomicCalendar {
    /// Value returned by OKX in the `calendarId` field.
    #[serde(default)]
    pub calendar_id: String,
    /// Value returned by OKX in the `date` field (Unix milliseconds).
    #[serde(default)]
    pub date: NumberString,
    /// Value returned by OKX in the `region` field.
    #[serde(default)]
    pub region: String,
    /// Value returned by OKX in the `category` field.
    #[serde(default)]
    pub category: String,
    /// Value returned by OKX in the `event` field.
    #[serde(default)]
    pub event: String,
    /// Value returned by OKX in the `refDate` field (Unix milliseconds).
    #[serde(default)]
    pub ref_date: NumberString,
    /// Value returned by OKX in the `actual` field.
    #[serde(default)]
    pub actual: String,
    /// Value returned by OKX in the `previous` field.
    #[serde(default)]
    pub previous: String,
    /// Value returned by OKX in the `forecast` field.
    #[serde(default)]
    pub forecast: String,
    /// Value returned by OKX in the `dateSpan` field.
    #[serde(default)]
    pub date_span: String,
    /// Value returned by OKX in the `importance` field.
    #[serde(default)]
    pub importance: String,
    /// Value returned by OKX in the `uTime` field (Unix milliseconds).
    #[serde(default)]
    pub u_time: NumberString,
    /// Value returned by OKX in the `prevInitial` field.
    #[serde(default)]
    pub prev_initial: String,
    /// Value returned by OKX in the `ccy` field.
    #[serde(default)]
    pub ccy: String,
    /// Value returned by OKX in the `unit` field.
    #[serde(default)]
    pub unit: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn option_summary_ignores_future_fields() {
        let row: OptionSummary = serde_json::from_str(
            r#"{"instType":"OPTION","instId":"BTC-USD-1-C","delta":"0.5","futureField":"ok"}"#,
        )
        .unwrap();
        assert_eq!(row.inst_type, "OPTION");
        assert_eq!(row.inst_id, "BTC-USD-1-C");
    }
}