okx-client 0.1.2

Async Rust client for the OKX exchange API v5
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
440
441
442
443
444
mod response;

pub mod account;
pub mod affiliate;
pub mod algo;
pub mod block_trading;
pub mod broker;
pub mod convert;
pub mod copy_trading;
pub mod finance;
pub mod funding;
pub mod grid_trading;
pub mod market;
pub mod public;
pub mod signal_bot;
pub mod spread_trading;
pub mod subaccount;
pub mod system;
pub mod trade;
pub mod trading_data;

use reqwest::header::{HeaderMap, HeaderValue};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::policies::ExponentialBackoff;
use reqwest_retry::RetryTransientMiddleware;
use reqwest_tracing::TracingMiddleware;
use secrecy::ExposeSecret;
use serde::de::DeserializeOwned;
use serde::Serialize;
use tracing::instrument;

use crate::auth;
use crate::config::{ClientConfig, TradingMode};
use crate::constants;
use crate::error::{OkxError, OkxResult};

use self::response::OkxResponse;

/// HTTP REST client for the OKX API v5.
///
/// Provides methods for supported OKX REST endpoints, organized by domain.
/// Complete historical endpoint parity is not claimed.
/// Methods are defined in domain-specific files (e.g., `trade.rs`, `account.rs`).
pub struct RestClient {
    retryable_http: ClientWithMiddleware,
    non_retryable_http: ClientWithMiddleware,
    config: ClientConfig,
}

impl RestClient {
    /// Create a new `RestClient` with the given configuration.
    pub fn new(config: ClientConfig) -> OkxResult<Self> {
        let mut default_headers = HeaderMap::new();
        default_headers.insert("Content-Type", HeaderValue::from_static("application/json"));
        default_headers.insert("Accept", HeaderValue::from_static("application/json"));

        if config.trading_mode == TradingMode::Demo {
            default_headers.insert(
                constants::HEADER_SIMULATED_TRADING,
                HeaderValue::from_static("1"),
            );
        }

        let client = reqwest::Client::builder()
            .default_headers(default_headers)
            .timeout(config.request_timeout)
            .pool_max_idle_per_host(10)
            .build()
            .map_err(OkxError::Http)?;

        let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);

        let non_retryable_http = ClientBuilder::new(client.clone())
            .with(TracingMiddleware::default())
            .build();

        let retryable_http = ClientBuilder::new(client)
            .with(TracingMiddleware::default())
            .with(RetryTransientMiddleware::new_with_policy(retry_policy))
            .build();

        Ok(Self {
            retryable_http,
            non_retryable_http,
            config,
        })
    }

    /// Create a `RestClient` with default configuration (unauthenticated, global, live).
    pub fn default_client() -> OkxResult<Self> {
        Self::new(ClientConfig::default())
    }

    /// Returns the base URL for REST requests.
    fn base_url(&self) -> &str {
        if let Some(ref url) = self.config.base_url_override {
            return url;
        }
        self.config.region.rest_base_url()
    }

    /// Returns a reference to the client configuration.
    pub fn config(&self) -> &ClientConfig {
        &self.config
    }

    /// Generate an ISO 8601 timestamp for REST signing.
    fn timestamp() -> OkxResult<String> {
        // Use system time to build an ISO 8601 timestamp.
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_err(|_| OkxError::Config("system time is before Unix epoch".into()))?;
        let secs = now.as_secs();
        let millis = now.subsec_millis();

        // Convert to datetime components without `chrono`.
        let days = secs / 86400;
        let time_secs = secs % 86400;
        let hours = time_secs / 3600;
        let minutes = (time_secs % 3600) / 60;
        let seconds = time_secs % 60;

        // Calculate `year`, `month`, and `day` from days since epoch.
        let (year, month, day) = days_to_date(days);

        Ok(format!(
            "{year:04}-{month:02}-{day:02}T{hours:02}:{minutes:02}:{seconds:02}.{millis:03}Z"
        ))
    }

    /// Build auth headers for signed requests.
    fn auth_headers(
        &self,
        timestamp: &str,
        method: &str,
        endpoint: &str,
        body: &str,
    ) -> OkxResult<HeaderMap> {
        let creds = self
            .config
            .credentials
            .as_ref()
            .ok_or_else(|| OkxError::Auth("Credentials required for private endpoint".into()))?;

        let signature = auth::sign_rest(timestamp, method, endpoint, body, &creds.api_secret)?;

        let mut headers = HeaderMap::new();
        headers.insert(
            constants::HEADER_ACCESS_KEY,
            HeaderValue::from_str(&creds.api_key)
                .map_err(|e| OkxError::Auth(format!("Invalid API key header: {e}")))?,
        );
        headers.insert(
            constants::HEADER_ACCESS_SIGN,
            HeaderValue::from_str(&signature)
                .map_err(|e| OkxError::Auth(format!("Invalid signature header: {e}")))?,
        );
        headers.insert(
            constants::HEADER_ACCESS_TIMESTAMP,
            HeaderValue::from_str(timestamp)
                .map_err(|e| OkxError::Auth(format!("Invalid timestamp header: {e}")))?,
        );
        headers.insert(
            constants::HEADER_ACCESS_PASSPHRASE,
            HeaderValue::from_str(creds.passphrase.expose_secret())
                .map_err(|e| OkxError::Auth(format!("Invalid passphrase header: {e}")))?,
        );

        Ok(headers)
    }

    /// Serialize query parameters to a query string (e.g., `?key=val&key2=val2`).
    fn serialize_query_string<P: Serialize>(params: &P) -> OkxResult<String> {
        let value = serde_json::to_value(params)?;
        if let serde_json::Value::Object(map) = value {
            let pairs: Vec<String> = map
                .into_iter()
                .filter(|(_, v)| !v.is_null())
                .map(|(k, v)| {
                    let val = match v {
                        serde_json::Value::String(s) => s,
                        other => other.to_string(),
                    };
                    format!("{}={}", k, urlencoding::encode(&val))
                })
                .collect();
            if pairs.is_empty() {
                Ok(String::new())
            } else {
                Ok(format!("?{}", pairs.join("&")))
            }
        } else {
            Ok(String::new())
        }
    }


    /// Public GET request.
    #[instrument(skip(self, params), fields(endpoint))]
    pub(crate) async fn get<T, P>(&self, endpoint: &str, params: Option<&P>) -> OkxResult<Vec<T>>
    where
        T: DeserializeOwned,
        P: Serialize,
    {
        let url = format!("{}{}", self.base_url(), endpoint);
        let mut request = self.retryable_http.get(&url);

        if let Some(p) = params {
            let qs = Self::serialize_query_string(p)?;
            if !qs.is_empty() {
                request = self.retryable_http.get(format!("{url}{qs}"));
            }
        }

        let response = request.send().await?;
        let body = response.text().await.map_err(OkxError::Http)?;
        let parsed: OkxResponse<Vec<T>> = serde_json::from_str(&body)?;
        parsed.into_result()
    }

    /// Public POST request.
    #[instrument(skip(self, params), fields(endpoint))]
    #[allow(dead_code)]
    pub(crate) async fn post<T, P>(&self, endpoint: &str, params: &P) -> OkxResult<Vec<T>>
    where
        T: DeserializeOwned,
        P: Serialize,
    {
        let url = format!("{}{}", self.base_url(), endpoint);
        let body = serde_json::to_string(params)?;

        let response = self
            .non_retryable_http
            .post(&url)
            .header("Content-Type", "application/json")
            .body(body)
            .send()
            .await?;

        let resp_body = response.text().await.map_err(OkxError::Http)?;
        let parsed: OkxResponse<Vec<T>> = serde_json::from_str(&resp_body)?;
        parsed.into_result()
    }


    /// Signed GET request (for private endpoints).
    #[instrument(skip(self, params), fields(endpoint))]
    pub(crate) async fn get_signed<T, P>(
        &self,
        endpoint: &str,
        params: Option<&P>,
    ) -> OkxResult<Vec<T>>
    where
        T: DeserializeOwned,
        P: Serialize,
    {
        let timestamp = Self::timestamp()?;
        let qs = if let Some(p) = params {
            Self::serialize_query_string(p)?
        } else {
            String::new()
        };

        let auth_headers = self.auth_headers(&timestamp, "GET", endpoint, &qs)?;
        let url = format!("{}{}{}", self.base_url(), endpoint, qs);

        let response = self
            .retryable_http
            .get(&url)
            .headers(auth_headers)
            .send()
            .await?;

        let body = response.text().await.map_err(OkxError::Http)?;
        let parsed: OkxResponse<Vec<T>> = serde_json::from_str(&body)?;
        parsed.into_result()
    }

    /// Signed POST request (for private endpoints).
    /// Auto-injects the program ID tag into the request body.
    #[instrument(skip(self, params), fields(endpoint))]
    pub(crate) async fn post_signed<T, P>(
        &self,
        endpoint: &str,
        params: &P,
    ) -> OkxResult<Vec<T>>
    where
        T: DeserializeOwned,
        P: Serialize,
    {
        let timestamp = Self::timestamp()?;
        let body = inject_program_tag(&serde_json::to_value(params)?)?;

        let auth_headers = self.auth_headers(&timestamp, "POST", endpoint, &body)?;
        let url = format!("{}{}", self.base_url(), endpoint);

        let response = self
            .non_retryable_http
            .post(&url)
            .headers(auth_headers)
            .header("Content-Type", "application/json")
            .body(body)
            .send()
            .await?;

        let resp_body = response.text().await.map_err(OkxError::Http)?;
        let parsed: OkxResponse<Vec<T>> = serde_json::from_str(&resp_body)?;
        parsed.into_result()
    }
}

/// Inject the OKX program ID tag into a JSON value.
/// If the value is an object, adds `"tag": PROGRAM_ID"`.
/// If the value is an array, injects into each element.
fn inject_program_tag(value: &serde_json::Value) -> OkxResult<String> {
    let mut val = value.clone();
    match &mut val {
        serde_json::Value::Object(map) => {
            if !map.contains_key("tag") {
                map.insert(
                    "tag".to_string(),
                    serde_json::Value::String(constants::PROGRAM_ID.to_string()),
                );
            }
        }
        serde_json::Value::Array(arr) => {
            for item in arr.iter_mut() {
                if let serde_json::Value::Object(map) = item {
                    if !map.contains_key("tag") {
                        map.insert(
                            "tag".to_string(),
                            serde_json::Value::String(constants::PROGRAM_ID.to_string()),
                        );
                    }
                }
            }
        }
        _ => {}
    }
    Ok(serde_json::to_string(&val)?)
}

/// Convert days since Unix epoch to (year, month, day).
fn days_to_date(total_days: u64) -> (u64, u64, u64) {
    // Based on http://howardhinnant.github.io/date_algorithms.html.
    let z = total_days + 719468;
    let era = z / 146097;
    let doe = z - era * 146097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };
    (y, m, d)
}

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

    #[test]
    fn test_timestamp_format() {
        let ts = RestClient::timestamp().unwrap();
        // Expected format: `2024-01-15T12:30:45.123Z`.
        assert!(ts.ends_with('Z'));
        assert_eq!(ts.len(), 24);
        assert_eq!(&ts[4..5], "-");
        assert_eq!(&ts[7..8], "-");
        assert_eq!(&ts[10..11], "T");
        assert_eq!(&ts[13..14], ":");
        assert_eq!(&ts[16..17], ":");
        assert_eq!(&ts[19..20], ".");
    }

    #[test]
    fn test_days_to_date_epoch() {
        let (y, m, d) = days_to_date(0);
        assert_eq!((y, m, d), (1970, 1, 1));
    }

    #[test]
    fn test_days_to_date_known() {
        // `2024-01-15` is day `19737`.
        let (y, m, d) = days_to_date(19737);
        assert_eq!((y, m, d), (2024, 1, 15));
    }

    #[test]
    fn test_serialize_query_string() {
        #[derive(Serialize)]
        #[serde(rename_all = "camelCase")]
        struct Params {
            inst_id: String,
            #[serde(skip_serializing_if = "Option::is_none")]
            ccy: Option<String>,
        }

        let params = Params {
            inst_id: "BTC-USDT".to_string(),
            ccy: None,
        };
        let qs = RestClient::serialize_query_string(&params).unwrap();
        assert_eq!(qs, "?instId=BTC-USDT");
    }

    #[test]
    fn test_inject_program_tag_object() {
        let val = serde_json::json!({"instId": "BTC-USDT", "sz": "1"});
        let result = inject_program_tag(&val).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(
            parsed["tag"],
            serde_json::Value::String(constants::PROGRAM_ID.to_string())
        );
    }

    #[test]
    fn test_inject_program_tag_array() {
        let val = serde_json::json!([{"instId": "BTC-USDT"}, {"instId": "ETH-USDT"}]);
        let result = inject_program_tag(&val).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(
            parsed[0]["tag"],
            serde_json::Value::String(constants::PROGRAM_ID.to_string())
        );
        assert_eq!(
            parsed[1]["tag"],
            serde_json::Value::String(constants::PROGRAM_ID.to_string())
        );
    }

    #[test]
    fn test_inject_program_tag_preserves_existing() {
        let val = serde_json::json!({"instId": "BTC-USDT", "tag": "custom"});
        let result = inject_program_tag(&val).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(
            parsed["tag"],
            serde_json::Value::String("custom".to_string())
        );
    }
}