revolutx 0.4.0

Unofficial Rust SDK for the Revolut X Crypto Exchange 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Shared domain primitives used across the SDK.
//!
//! These types give the public API trading-domain meaning instead of bare
//! strings and floats. Monetary and quantity values are represented with
//! [`rust_decimal::Decimal`] (re-exported at the crate root as
//! [`crate::Decimal`]) so prices, sizes, balances, and fees are never subject
//! to binary floating-point rounding.

use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;

use rust_decimal::Decimal;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;
use uuid::Uuid;

use crate::error::{Error, Result};

/// A trading pair or asset symbol, e.g. `BTC-USD`, `BTC/USD`, or `BTC`.
///
/// The exchange uses the dash form (`BTC-USD`) in request paths and order
/// placement, and the slash form (`BTC/USD`) in some responses (and the
/// [`CurrencyPairs`](crate::model::configuration::CurrencyPairs) map key).
/// [`Symbol`] is a validated, non-empty wrapper;
/// [`from_pair`](Self::from_pair) builds one from a pair's base and quote, and
/// [`dashed`](Self::dashed) / [`slashed`](Self::slashed) convert between the two
/// forms so callers never hand-roll the separator swap.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
#[serde(transparent)]
pub struct Symbol(String);

impl Symbol {
    /// Creates a symbol, rejecting empty or whitespace-only input.
    pub fn new(value: impl Into<String>) -> Result<Self> {
        let value = value.into();
        if value.trim().is_empty() {
            return Err(Error::invalid_request("symbol must not be empty"));
        }
        Ok(Self(value))
    }

    /// Builds a symbol from a pair's `base` and `quote` assets in the dash form the
    /// exchange uses in request paths — `Symbol::from_pair("BTC", "USD")` is
    /// `BTC-USD`. The convenient way to turn a
    /// [`CurrencyPair`](crate::model::configuration::CurrencyPair) into the
    /// path/placement symbol.
    pub fn from_pair(base: &str, quote: &str) -> Self {
        Self(format!("{base}-{quote}"))
    }

    /// Returns the symbol as a string slice (in whatever form it was created with).
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// This symbol in the **dash** form (`BTC-USD`) the exchange uses in request
    /// paths and order placement. Borrows when already dashed; converts a slash
    /// otherwise.
    pub fn dashed(&self) -> Cow<'_, str> {
        if self.0.contains('/') {
            Cow::Owned(self.0.replace('/', "-"))
        } else {
            Cow::Borrowed(&self.0)
        }
    }

    /// This symbol in the **slash** form (`BTC/USD`) the exchange uses in some
    /// responses and that reads naturally for humans. Borrows when already
    /// slashed; converts a dash otherwise.
    pub fn slashed(&self) -> Cow<'_, str> {
        if self.0.contains('-') {
            Cow::Owned(self.0.replace('-', "/"))
        } else {
            Cow::Borrowed(&self.0)
        }
    }
}

impl fmt::Display for Symbol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // `pad` (not `write_str`) so width/fill/alignment like `{symbol:<12}` work.
        f.pad(&self.0)
    }
}

impl FromStr for Symbol {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self> {
        Self::new(s)
    }
}

impl TryFrom<&str> for Symbol {
    type Error = Error;
    fn try_from(value: &str) -> Result<Self> {
        Self::new(value)
    }
}

impl TryFrom<String> for Symbol {
    type Error = Error;
    fn try_from(value: String) -> Result<Self> {
        Self::new(value)
    }
}

impl<'de> Deserialize<'de> for Symbol {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
        let raw = String::deserialize(deserializer)?;
        Self::new(raw).map_err(de::Error::custom)
    }
}

/// The side of an order or trade.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Side {
    /// Buy (acquire the base asset).
    Buy,
    /// Sell (dispose of the base asset).
    Sell,
}

impl Side {
    /// The on-the-wire token for this side (`buy` or `sell`).
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Buy => "buy",
            Self::Sell => "sell",
        }
    }
}

impl fmt::Display for Side {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// An order identifier assigned by the exchange (the `venue_order_id`).
///
/// The API documents these as UUIDs, but [`OrderId`] is a forgiving string
/// wrapper so the SDK keeps working if the server ever returns another format.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct OrderId(String);

impl OrderId {
    /// Wraps a raw order identifier.
    pub fn new(value: impl Into<String>) -> Self {
        Self(value.into())
    }

    /// Returns the identifier as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for OrderId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl FromStr for OrderId {
    type Err = std::convert::Infallible;
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        Ok(Self(s.to_owned()))
    }
}

impl From<&str> for OrderId {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

impl From<String> for OrderId {
    fn from(value: String) -> Self {
        Self(value)
    }
}

/// A client-generated order identifier used for idempotency.
///
/// The exchange requires this to be a UUID, so [`ClientOrderId`] wraps
/// [`uuid::Uuid`] and serializes as its hyphenated string form. A fresh random
/// (v4) id can be created with [`ClientOrderId::random`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClientOrderId(Uuid);

impl ClientOrderId {
    /// Generates a new random (v4) client order id.
    pub fn random() -> Self {
        Self(Uuid::new_v4())
    }

    /// Wraps an existing UUID.
    pub const fn from_uuid(id: Uuid) -> Self {
        Self(id)
    }

    /// Returns the underlying UUID.
    pub const fn as_uuid(&self) -> Uuid {
        self.0
    }
}

impl fmt::Display for ClientOrderId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<Uuid> for ClientOrderId {
    fn from(value: Uuid) -> Self {
        Self(value)
    }
}

impl Default for ClientOrderId {
    /// Produces a fresh random (v4) client order id, suitable as a unique
    /// idempotency key.
    fn default() -> Self {
        Self::random()
    }
}

impl FromStr for ClientOrderId {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self> {
        Uuid::parse_str(s)
            .map(Self)
            .map_err(|e| Error::invalid_request(format!("invalid client order id: {e}")))
    }
}

impl Serialize for ClientOrderId {
    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        serializer.collect_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for ClientOrderId {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
        let raw = String::deserialize(deserializer)?;
        raw.parse().map_err(de::Error::custom)
    }
}

/// A strictly positive limit price, used when constructing orders.
///
/// Serializes as a decimal string to match the exchange wire format. Response
/// models expose raw [`Decimal`] values instead, since those are observations
/// from the exchange rather than client-authored inputs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Price(Decimal);

/// A strictly positive order size or amount, used when constructing orders.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Quantity(Decimal);

macro_rules! positive_decimal {
    ($name:ident, $label:literal) => {
        impl $name {
            #[doc = concat!("Creates a ", $label, ", rejecting zero or negative values.")]
            pub fn new(value: impl Into<Decimal>) -> Result<Self> {
                let value = value.into();
                if value <= Decimal::ZERO {
                    return Err(Error::invalid_request(concat!($label, " must be positive")));
                }
                Ok(Self(value))
            }

            #[doc = "Returns the underlying decimal value."]
            pub const fn value(&self) -> Decimal {
                self.0
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "{}", self.0)
            }
        }

        impl TryFrom<Decimal> for $name {
            type Error = Error;
            fn try_from(value: Decimal) -> Result<Self> {
                Self::new(value)
            }
        }

        impl FromStr for $name {
            type Err = Error;
            fn from_str(s: &str) -> Result<Self> {
                let value = Decimal::from_str(s).map_err(|e| {
                    Error::invalid_request(format!(concat!("invalid ", $label, ": {}"), e))
                })?;
                Self::new(value)
            }
        }

        impl From<$name> for Decimal {
            fn from(value: $name) -> Decimal {
                value.0
            }
        }

        impl Serialize for $name {
            fn serialize<S: Serializer>(
                &self,
                serializer: S,
            ) -> std::result::Result<S::Ok, S::Error> {
                serializer.collect_str(&self.0)
            }
        }

        impl<'de> Deserialize<'de> for $name {
            fn deserialize<D: Deserializer<'de>>(
                deserializer: D,
            ) -> std::result::Result<Self, D::Error> {
                let raw = String::deserialize(deserializer)?;
                raw.parse().map_err(de::Error::custom)
            }
        }
    };
}

positive_decimal!(Price, "price");
positive_decimal!(Quantity, "quantity");

/// A point in time reported by the exchange.
///
/// The Revolut X API returns timestamps in two shapes: Unix epoch milliseconds
/// (integers) on authenticated endpoints, and ISO-8601 / RFC 3339 strings on
/// the public endpoints. [`Timestamp`] deserializes from either form into a
/// single [`time::OffsetDateTime`], so callers get one consistent instant type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Timestamp(OffsetDateTime);

impl Timestamp {
    /// Builds a timestamp from Unix epoch milliseconds.
    pub fn from_unix_millis(millis: i64) -> Self {
        let nanos = i128::from(millis) * 1_000_000;
        Self(OffsetDateTime::from_unix_timestamp_nanos(nanos).unwrap_or(OffsetDateTime::UNIX_EPOCH))
    }

    /// Returns the instant as Unix epoch milliseconds.
    // Epoch milliseconds for any representable `OffsetDateTime` (max year 9999)
    // fit in `i64`; the i128 -> i64 narrowing cannot lose information here, and a
    // `const fn` cannot use `i64::try_from`.
    #[allow(clippy::cast_possible_truncation)]
    pub const fn unix_millis(&self) -> i64 {
        (self.0.unix_timestamp_nanos() / 1_000_000) as i64
    }

    /// Returns the underlying [`time::OffsetDateTime`] (always in UTC).
    pub const fn datetime(&self) -> OffsetDateTime {
        self.0
    }
}

impl From<OffsetDateTime> for Timestamp {
    fn from(value: OffsetDateTime) -> Self {
        Self(value)
    }
}

impl fmt::Display for Timestamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0.format(&Rfc3339) {
            Ok(s) => f.write_str(&s),
            Err(_) => write!(f, "{}", self.unix_millis()),
        }
    }
}

impl Serialize for Timestamp {
    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        let formatted = self.0.format(&Rfc3339).map_err(serde::ser::Error::custom)?;
        serializer.serialize_str(&formatted)
    }
}

impl<'de> Deserialize<'de> for Timestamp {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
        struct TimestampVisitor;

        impl Visitor<'_> for TimestampVisitor {
            type Value = Timestamp;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("an epoch-millisecond integer or an RFC 3339 timestamp string")
            }

            fn visit_i64<E: de::Error>(self, v: i64) -> std::result::Result<Timestamp, E> {
                Ok(Timestamp::from_unix_millis(v))
            }

            fn visit_u64<E: de::Error>(self, v: u64) -> std::result::Result<Timestamp, E> {
                Ok(Timestamp::from_unix_millis(
                    i64::try_from(v).unwrap_or(i64::MAX),
                ))
            }

            fn visit_str<E: de::Error>(self, v: &str) -> std::result::Result<Timestamp, E> {
                OffsetDateTime::parse(v, &Rfc3339)
                    .map(Timestamp)
                    .map_err(|e| de::Error::custom(format!("invalid RFC 3339 timestamp: {e}")))
            }
        }

        deserializer.deserialize_any(TimestampVisitor)
    }
}

/// Serde adapter for integer values the API encodes as JSON strings (e.g. the
/// order-book `no` count). Serializes back to a string for round-tripping.
pub(crate) mod string_u64 {
    use serde::{Deserialize, Deserializer, Serializer};

    // The `&u64` signature is mandated by serde's `serialize_with` contract.
    #[allow(clippy::trivially_copy_pass_by_ref)]
    pub fn serialize<S: Serializer>(value: &u64, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_str(value)
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u64, D::Error> {
        let raw = String::deserialize(deserializer)?;
        raw.trim().parse().map_err(serde::de::Error::custom)
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn symbol_rejects_empty() {
        assert!(Symbol::new("").is_err());
        assert!(Symbol::new("   ").is_err());
        assert_eq!(Symbol::new("BTC-USD").unwrap().as_str(), "BTC-USD");
    }

    #[test]
    fn symbol_normalizes_between_dash_and_slash_forms() {
        assert_eq!(Symbol::from_pair("BTC", "USD").as_str(), "BTC-USD");

        let dashed = Symbol::new("BTC-USD").unwrap();
        assert_eq!(dashed.dashed(), "BTC-USD"); // already dashed
        assert_eq!(dashed.slashed(), "BTC/USD"); // converted

        let slashed = Symbol::new("BTC/USD").unwrap();
        assert_eq!(slashed.dashed(), "BTC-USD"); // converted
        assert_eq!(slashed.slashed(), "BTC/USD"); // already slashed

        // Display honors padding/alignment (used by the CLI tables).
        assert_eq!(format!("{dashed:<8}|"), "BTC-USD |");
    }

    #[test]
    fn price_and_quantity_reject_non_positive() {
        assert!(Price::from_str("0").is_err());
        assert!(Price::from_str("-1").is_err());
        assert!(Quantity::from_str("0").is_err());
        assert_eq!(Price::from_str("50000.50").unwrap().to_string(), "50000.50");
    }

    #[test]
    fn decimal_values_round_trip_without_float() {
        // Trailing zeros and scale are preserved exactly (no float conversion).
        let q = Quantity::from_str("1000.00000000").unwrap();
        assert_eq!(q.to_string(), "1000.00000000");
        let json = serde_json::to_string(&q).unwrap();
        assert_eq!(json, "\"1000.00000000\"");
    }

    #[test]
    fn client_order_id_round_trips_as_string() {
        let id: ClientOrderId = "3fa85f64-5717-4562-b3fc-2c963f66afa6".parse().unwrap();
        assert_eq!(
            serde_json::to_string(&id).unwrap(),
            "\"3fa85f64-5717-4562-b3fc-2c963f66afa6\""
        );
        assert!("not-a-uuid".parse::<ClientOrderId>().is_err());
    }

    #[test]
    fn timestamp_accepts_millis_and_rfc3339() {
        let from_millis: Timestamp = serde_json::from_str("3318215482991").unwrap();
        assert_eq!(from_millis.unix_millis(), 3_318_215_482_991);

        let from_iso: Timestamp = serde_json::from_str("\"2025-08-08T21:40:35.133962Z\"").unwrap();
        assert_eq!(from_iso.datetime().year(), 2025);

        // Round-trip an epoch-millis value back out as RFC 3339.
        assert!(
            serde_json::to_string(&from_millis)
                .unwrap()
                .contains("2075")
        );
    }

    #[test]
    fn string_u64_parses_quoted_integer() {
        #[derive(serde::Deserialize)]
        struct Wrap {
            #[serde(with = "string_u64")]
            n: u64,
        }
        let w: Wrap = serde_json::from_str(r#"{"n":"3"}"#).unwrap();
        assert_eq!(w.n, 3);
    }
}