cexy 0.1.0-dev.16

Official Rust SDK for the CEXY.io exchange API (REST + WebSocket)
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
//! The REST client and its options.

use std::fmt;
use std::sync::Arc;
use std::time::Duration;

use url::Url;

use crate::auth::{ApiKeyAuthenticator, Authenticator, REDACTED};
use crate::clock::{self, Clock};
use crate::error::{Error, Result};
use crate::limiter::{PlacementHold, RateLimitState, RateLimiter};
use crate::models_gen::{ExchangeConfig, ServerTime};
use crate::operations_gen::OperationId;
use crate::services::{
    Account, Assets, Exports, Fees, Futures, Markets, Networks, Pools, Trading, Wallet,
};
use crate::signing::HmacAuthenticator;
use crate::transport::{
    Call, Random, Resolved, RetryHook, RetryInfo, Transport, decode_data, origin,
};

/// A retry callback: see [`ClientOptions::on_retry`].
pub type OnRetry = Arc<dyn Fn(&RetryInfo<'_>) + Send + Sync>;

/// The production API.
pub const DEFAULT_BASE_URL: &str = "https://api.cexy.io";

/// Default client-side limit without credentials (the server allows about 120 a minute per IP).
pub const DEFAULT_RPM_ANONYMOUS: u32 = 100;

/// Default client-side limit with an API key (the server allows about 600 a minute per key).
pub const DEFAULT_RPM_WITH_KEY: u32 = 300;

/// How a [`Client`] sends `api_key` and `api_secret`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuthScheme {
    /// `X-API-Key` and `X-API-Secret` headers. The API is switching this off
    /// (`SIGNATURE_REQUIRED`); kept only for servers that still accept it.
    Headers,
    /// The default: every private request is signed ([`HmacAuthenticator`]) and the secret
    /// never leaves the process. A key issued before signing existed fails with
    /// `KEY_NOT_SIGNABLE` (create a new key); there is no fallback.
    #[default]
    Hmac,
}

/// Configures a [`Client`]. The default gives an anonymous client for public data.
#[derive(Clone, Default)]
pub struct ClientOptions {
    /// API key id (`ak_…`). Give it together with `api_secret`, or not at all.
    pub api_key: Option<String>,
    /// API key secret. Never logged, never put in a URL.
    pub api_secret: Option<String>,
    /// How `api_key`/`api_secret` are used. Default [`AuthScheme::Hmac`] (request signing);
    /// [`AuthScheme::Headers`] sends the secret header, which the API is switching off.
    pub auth: AuthScheme,
    /// A custom credentials scheme. Mutually exclusive with `api_key`/`api_secret`.
    pub authenticator: Option<Arc<dyn Authenticator>>,
    /// Default [`DEFAULT_BASE_URL`]. Must be `https://` (see `allow_insecure`).
    pub base_url: Option<String>,
    /// Allow plain `http://` (and `ws://` for WebSocket), but ONLY for a loopback host
    /// (localhost, 127.0.0.1, ::1), for example a local test server.
    pub allow_insecure: bool,
    /// Per-attempt timeout. Default 10 s.
    pub timeout: Option<Duration>,
    /// Retries after the first attempt for retryable failures. Default 3; `Some(0)` disables.
    pub max_retries: Option<u32>,
    /// Client-side rate limit in requests per minute. Default [`DEFAULT_RPM_ANONYMOUS`] without
    /// credentials and [`DEFAULT_RPM_WITH_KEY`] with them. It adapts downwards to the server's
    /// `X-RateLimit-*` headers.
    pub requests_per_minute: Option<u32>,
    /// Disables the client-side rate limiter (and the placement hold that follows a 429 for
    /// order actions).
    pub disable_rate_limit: bool,
    /// A client-side budget for placements, in order actions a second (a batch of N counts N),
    /// off by default. A market maker's key has 50 a second on the server: set `Some(50.0)` to
    /// wait locally instead of being refused. Cancels never count against it.
    pub order_actions_per_second: Option<f64>,
    /// Appended to the User-Agent: `"my-bot/1.2"` gives `"cexy-rust/<VERSION> my-bot/1.2"`.
    pub user_agent_suffix: Option<String>,
    /// Called before each retry (for logging or metrics). Never receives credentials.
    pub on_retry: Option<OnRetry>,
}

impl ClientOptions {
    /// Options for an API key pair.
    pub fn with_api_key(api_key: impl Into<String>, api_secret: impl Into<String>) -> Self {
        ClientOptions {
            api_key: Some(api_key.into()),
            api_secret: Some(api_secret.into()),
            ..Default::default()
        }
    }
}

impl fmt::Debug for ClientOptions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let key = self.api_key.as_ref().map(|_| REDACTED);
        f.debug_struct("ClientOptions")
            .field("base_url", &self.base_url)
            .field("api_key", &key)
            .field("api_secret", &self.api_secret.as_ref().map(|_| REDACTED))
            .field("auth", &self.auth)
            .field(
                "authenticator",
                &self.authenticator.as_ref().map(|a| a.kind().to_string()),
            )
            .field("allow_insecure", &self.allow_insecure)
            .finish_non_exhaustive()
    }
}

/// Per-call overrides: see [`Client::with_options`].
#[derive(Debug, Clone, Default)]
pub struct CallOptions {
    /// Per-attempt timeout.
    pub timeout: Option<Duration>,
    /// Retries after the first attempt (`Some(0)` disables).
    pub max_retries: Option<u32>,
    /// The Idempotency-Key of a mutation (generated when absent). The server honours it on pool
    /// join and exit; set it yourself to make a retry across process restarts safe there.
    /// It is sent only there: orders and cancels do not honour it (their safety comes from
    /// `client_order_id`), so the SDK sends none.
    pub idempotency_key: Option<String>,
}

/// The CEXY.io REST client. Cloning is cheap and clones share the connection pool and the rate
/// limiter. Use one client per API key.
#[derive(Clone)]
pub struct Client {
    pub(crate) t: Arc<Transport>,
    pub(crate) opts: CallOptions,
}

impl Client {
    /// Checks the options and returns a client. It sends nothing.
    pub fn new(options: ClientOptions) -> Result<Client> {
        Client::build(options, clock::system(), Arc::new(rand::random::<f64>))
    }

    pub(crate) fn build(o: ClientOptions, clock: Arc<dyn Clock>, random: Random) -> Result<Client> {
        let auth: Option<Arc<dyn Authenticator>> =
            match (&o.api_key, &o.api_secret, &o.authenticator) {
                (Some(_), None, _) | (None, Some(_), _) => {
                    return Err(Error::config(
                        "api_key and api_secret must be given together (got only one of them)",
                    ));
                }
                (Some(_), Some(_), Some(_)) => {
                    return Err(Error::config(
                        "pass either api_key/api_secret or authenticator, not both",
                    ));
                }
                (Some(k), Some(s), None) => match o.auth {
                    AuthScheme::Hmac => {
                        Some(Arc::new(HmacAuthenticator::new(k.clone(), s.clone())?))
                    }
                    AuthScheme::Headers => {
                        Some(Arc::new(ApiKeyAuthenticator::new(k.clone(), s.clone())?))
                    }
                },
                (None, None, a) => a.clone(),
            };

        let base = o
            .base_url
            .as_deref()
            .unwrap_or(DEFAULT_BASE_URL)
            .trim_end_matches('/')
            .to_string();
        let u = Url::parse(&base)
            .map_err(|_| Error::config(format!("base_url is not a valid URL: {base:?}")))?;
        if u.host_str().is_none_or(str::is_empty) {
            return Err(Error::config(format!(
                "base_url is not a valid URL: {base:?}"
            )));
        }
        if !u.username().is_empty()
            || u.password().is_some()
            || u.query().is_some()
            || u.fragment().is_some()
        {
            return Err(Error::config(
                "base_url must not contain credentials, a query or a fragment",
            ));
        }
        check_secure_url(&u, "https", "http", o.allow_insecure, "base_url")?;

        let timeout = o.timeout.unwrap_or(Duration::from_secs(10));
        if timeout.is_zero() {
            return Err(Error::config("timeout must be > 0"));
        }
        let limiter = if o.disable_rate_limit {
            None
        } else {
            let rpm = o.requests_per_minute.unwrap_or(if auth.is_some() {
                DEFAULT_RPM_WITH_KEY
            } else {
                DEFAULT_RPM_ANONYMOUS
            });
            if rpm == 0 {
                return Err(Error::config("requests_per_minute must be > 0"));
            }
            Some(RateLimiter::new(rpm, clock.clone()))
        };
        let order_limiter = match o.order_actions_per_second {
            None => None,
            Some(n) if n.is_finite() && n > 0.0 => Some(RateLimiter::per_second(n, clock.clone())),
            Some(_) => {
                return Err(Error::config(
                    "order_actions_per_second must be a positive number",
                ));
            }
        };
        let placement_hold = limiter.as_ref().map(|_| PlacementHold::new(clock.clone()));
        let mut user_agent = crate::USER_AGENT.to_string();
        if let Some(s) = o
            .user_agent_suffix
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
        {
            if s.contains(['\r', '\n']) {
                return Err(Error::config(
                    "user_agent_suffix must not contain line breaks",
                ));
            }
            user_agent.push(' ');
            user_agent.push_str(s);
        }
        let http = no_redirect_client()?;
        let on_retry: Option<RetryHook> = o.on_retry.clone();
        let t = Transport {
            base_url: base,
            origin: origin(&u),
            http,
            auth,
            limiter,
            placement_hold,
            order_limiter,
            user_agent,
            timeout,
            max_retries: o.max_retries.unwrap_or(3),
            clock,
            random,
            on_retry,
            allow_insecure: o.allow_insecure,
        };
        Ok(Client {
            t: Arc::new(t),
            opts: CallOptions::default(),
        })
    }

    /// A client that applies `options` to every call made through it (it shares everything
    /// else with `self`): `client.with_options(CallOptions { timeout: Some(..), ..Default::default() })`.
    pub fn with_options(&self, options: CallOptions) -> Client {
        Client {
            t: self.t.clone(),
            opts: options,
        }
    }

    pub(crate) fn resolved(&self) -> Resolved {
        Resolved {
            timeout: self.opts.timeout.unwrap_or(self.t.timeout),
            max_retries: self.opts.max_retries.unwrap_or(self.t.max_retries),
            idempotency_key: self.opts.idempotency_key.clone(),
        }
    }

    /// Whether private endpoints are available.
    pub fn has_credentials(&self) -> bool {
        self.t.auth.is_some()
    }

    /// The API base URL.
    pub fn base_url(&self) -> &str {
        &self.t.base_url
    }

    /// The User-Agent the client sends.
    pub fn user_agent(&self) -> &str {
        &self.t.user_agent
    }

    /// The client-side rate limiter state, or `None` when it is disabled.
    pub fn rate_limit(&self) -> Option<RateLimitState> {
        self.t.limiter.as_ref().map(RateLimiter::state)
    }

    /// The server clock. Compare it with yours to detect skew.
    pub async fn time(&self) -> Result<ServerTime> {
        self.get(Call::new(OperationId::ServerTime)).await
    }

    /// The public exchange configuration (maintenance state, page sizes, ...).
    pub async fn config(&self) -> Result<ExchangeConfig> {
        self.get(Call::new(OperationId::ExchangeConfig)).await
    }

    /// Markets, tickers, order books, public trades and candles.
    pub fn markets(&self) -> Markets<'_> {
        Markets { c: self }
    }
    /// The asset catalogue.
    pub fn assets(&self) -> Assets<'_> {
        Assets { c: self }
    }
    /// Blockchain networks.
    pub fn networks(&self) -> Networks<'_> {
        Networks { c: self }
    }
    /// Fee schedules.
    pub fn fees(&self) -> Fees<'_> {
        Fees { c: self }
    }
    /// Liquidity pools. Join and exit need an API key with the trade scope.
    pub fn pools(&self) -> Pools<'_> {
        Pools { c: self }
    }
    /// Balances, ledger, notifications, sub-accounts and API keys.
    pub fn account(&self) -> Account<'_> {
        Account { c: self }
    }
    /// CSV exports.
    pub fn exports(&self) -> Exports<'_> {
        Exports { c: self }
    }
    /// Wallet reads. API keys can never withdraw or transfer; there are no such methods.
    pub fn wallet(&self) -> Wallet<'_> {
        Wallet { c: self }
    }
    /// Orders and your trades. Placing and cancelling need the trade scope.
    pub fn trading(&self) -> Trading<'_> {
        Trading { c: self }
    }

    /// Futures market data (public) and the account's own futures data (read scope). Read only.
    pub fn futures(&self) -> Futures<'_> {
        Futures {
            c: self,
            max_busy_retries: crate::services::DEFAULT_MAX_BUSY_RETRIES,
        }
    }

    pub(crate) async fn get<T: serde::de::DeserializeOwned>(&self, c: Call) -> Result<T> {
        let op = c.op;
        let raw = self.t.request(c, &self.resolved()).await?;
        decode_data(op, &raw)
    }
}

impl fmt::Debug for Client {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let auth = match &self.t.auth {
            Some(a) => format!("{} {REDACTED}", a.kind()),
            None => "none".to_string(),
        };
        write!(f, "cexy::Client({}, auth={auth})", self.t.base_url)
    }
}

/// A reqwest client that never follows redirects. Following one could send X-API-Key and
/// X-API-Secret to another host, even over plain http, and a 307/308 would re-send an order. The
/// 3xx response is returned instead and becomes an `UNEXPECTED_REDIRECT` error.
fn no_redirect_client() -> Result<reqwest::Client> {
    let mut tls = (*crate::tls::client_config()).clone();
    tls.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
    reqwest::Client::builder()
        .redirect(reqwest::redirect::Policy::none())
        .tls_backend_preconfigured(tls)
        .build()
        .map_err(|e| Error::config(format!("cannot build the HTTP client: {e}")))
}

/// Whether `host` is a loopback name or address, the only hosts where plain-text transport may
/// be allowed.
pub fn is_local_host(host: &str) -> bool {
    let h = host
        .trim_start_matches('[')
        .trim_end_matches(']')
        .to_lowercase();
    h == "localhost" || h == "127.0.0.1" || h == "::1"
}

/// Requires scheme `secure`; `insecure` is accepted only with `allow_insecure` and a loopback
/// host. Credentials must never travel in clear text.
pub(crate) fn check_secure_url(
    u: &Url,
    secure: &str,
    insecure: &str,
    allow_insecure: bool,
    what: &str,
) -> Result<()> {
    let scheme = u.scheme().to_lowercase();
    if scheme == secure {
        return Ok(());
    }
    if scheme == insecure {
        if !allow_insecure {
            return Err(Error::config(format!(
                "{what} must use {secure}:// (set allow_insecure only for a local test server)"
            )));
        }
        if !is_local_host(u.host_str().unwrap_or("")) {
            return Err(Error::config(format!(
                "{what}: {insecure}:// is only allowed for localhost, 127.0.0.1 or ::1"
            )));
        }
        return Ok(());
    }
    Err(Error::config(format!("{what} must use {secure}://")))
}