digdigdig3 0.1.25

Unified async Rust API for 44 exchange connectors — crypto, stocks, forex. 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
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
//! # Precision utilities
//!
//! Safe f64 → string conversion for exchange order prices and quantities.
//! Uses [`rust_decimal`] internally to avoid IEEE-754 drift that accumulates
//! when operating directly on `f64` values.
//!
//! ## Why not `f64` arithmetic?
//!
//! `0.1 + 0.2 == 0.30000000000000004` in IEEE-754. When you divide a price by
//! its `tick_size` with plain `f64` you may land on `99.9999…` instead of
//! `100.0`, causing the floor to snap down one tick — an invalid price that
//! the exchange will reject.
//!
//! The string-path (`price.to_string()` → `Decimal::from_str`) uses Ryu's
//! shortest-round-trip representation, which is always the "human intended"
//! value, making it the same approach as CCXT.
//!
//! ## Functions
//!
//! | Function | Rounding | Use case |
//! |---|---|---|
//! | [`safe_price`] | nearest (round) | limit/stop price fields |
//! | [`safe_qty`] | floor (truncate) | order quantity fields |
//! | [`format_price`] | nearest, trailing zeros | exchanges requiring exact decimal places |
//! | [`format_qty`] | floor, trailing zeros | exchanges requiring exact decimal places |

use rust_decimal::Decimal;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::RwLock;

/// Converts an f64 price to a string rounded to the nearest `tick_size`.
///
/// Uses string-path conversion (Ryu shortest round-trip) to avoid sub-tick
/// IEEE-754 drift. Rounding is *nearest* (same behaviour as CCXT) so that
/// `100.055` with tick `0.01` yields `"100.06"`.
///
/// Returns [`Decimal::normalize`]d output — no trailing zeros
/// (`"50000"` not `"50000.00"`). Use [`format_price`] if your exchange
/// requires a fixed number of decimal places.
///
/// # Examples
///
/// ```
/// use digdigdig3::safe_price;
/// assert_eq!(safe_price(100.05, "0.01"), "100.05");
/// assert_eq!(safe_price(100.054, "0.01"), "100.05");
/// assert_eq!(safe_price(100.055, "0.01"), "100.06");
/// // Fixes 0.1 + 0.2 drift
/// assert_eq!(safe_price(0.1_f64 + 0.2_f64, "0.01"), "0.3");
/// ```
pub fn safe_price(price: f64, tick_size: &str) -> String {
    let d = Decimal::from_str(&price.to_string()).unwrap_or_default();
    let tick = Decimal::from_str(tick_size).unwrap_or(Decimal::ONE);
    if tick.is_zero() {
        return d.normalize().to_string();
    }
    let steps = (d / tick).round();
    let rounded = steps * tick;
    rounded.normalize().to_string()
}

/// Converts an f64 quantity to a string truncated (floored) to `step_size`.
///
/// Quantity is always rounded **down** — never claim more than available.
/// This matches CCXT `TRUNCATE` mode and prevents over-spending errors.
///
/// Returns [`Decimal::normalize`]d output — no trailing zeros.
/// Use [`format_qty`] if your exchange requires a fixed decimal count.
///
/// # Examples
///
/// ```
/// use digdigdig3::safe_qty;
/// assert_eq!(safe_qty(1.999, "0.01"), "1.99");    // floor, NOT round
/// assert_eq!(safe_qty(0.12345, "0.001"), "0.123");
/// assert_eq!(safe_qty(0.999, "0.01"), "0.99");    // never rounds up
/// ```
pub fn safe_qty(qty: f64, step_size: &str) -> String {
    let d = Decimal::from_str(&qty.to_string()).unwrap_or_default();
    let step = Decimal::from_str(step_size).unwrap_or(Decimal::ONE);
    if step.is_zero() {
        return d.normalize().to_string();
    }
    let steps = (d / step).floor();
    let rounded = steps * step;
    rounded.normalize().to_string()
}

/// Formats a price with *exactly* the number of decimal places implied by
/// `tick_size`, padding with trailing zeros if needed.
///
/// Some exchanges (e.g. Binance futures) reject `"100.5"` when the tick is
/// `0.01` and require `"100.50"` instead. Use this variant in those cases.
///
/// Rounding is *nearest* (same as [`safe_price`]).
///
/// # Examples
///
/// ```
/// use digdigdig3::format_price;
/// assert_eq!(format_price(100.5,  "0.01"), "100.50");
/// assert_eq!(format_price(100.0,  "0.01"), "100.00");
/// assert_eq!(format_price(67543.2,"0.01"), "67543.20");
/// ```
pub fn format_price(price: f64, tick_size: &str) -> String {
    let d = Decimal::from_str(&price.to_string()).unwrap_or_default();
    let tick = Decimal::from_str(tick_size).unwrap_or(Decimal::ONE);
    if tick.is_zero() {
        return d.normalize().to_string();
    }
    let decimals = decimal_places(tick_size);
    let steps = (d / tick).round();
    let rounded = steps * tick;
    format!("{:.prec$}", rounded, prec = decimals)
}

/// Formats a quantity with *exactly* the number of decimal places implied by
/// `step_size`, padding with trailing zeros if needed, using floor rounding.
///
/// # Examples
///
/// ```
/// use digdigdig3::format_qty;
/// assert_eq!(format_qty(1.5,  "0.001"),   "1.500");
/// assert_eq!(format_qty(0.1,  "0.00001"), "0.10000");
/// ```
pub fn format_qty(qty: f64, step_size: &str) -> String {
    let d = Decimal::from_str(&qty.to_string()).unwrap_or_default();
    let step = Decimal::from_str(step_size).unwrap_or(Decimal::ONE);
    if step.is_zero() {
        return d.normalize().to_string();
    }
    let decimals = decimal_places(step_size);
    let steps = (d / step).floor();
    let rounded = steps * step;
    format!("{:.prec$}", rounded, prec = decimals)
}

/// Returns the number of decimal places encoded in a tick/step string.
///
/// `"0.001"` → 3, `"1"` → 0, `"0.5"` → 1.
fn decimal_places(s: &str) -> usize {
    s.find('.').map(|dot| s.len() - dot - 1).unwrap_or(0)
}

/// Convert integer precision (number of decimal places) to tick/step string.
///
/// `2` → `"0.01"`, `4` → `"0.0001"`, `0` → `"1"`, `8` → `"0.00000001"`.
fn precision_to_tick(digits: u8) -> String {
    if digits == 0 {
        return "1".to_string();
    }
    let mut s = "0.".to_string();
    for _ in 0..(digits - 1) {
        s.push('0');
    }
    s.push('1');
    s
}

// ═══════════════════════════════════════════════════════════════════════════════
// PRECISION CACHE
// ═══════════════════════════════════════════════════════════════════════════════

/// Per-symbol precision info for safe price/qty formatting.
#[derive(Clone, Debug)]
pub struct PrecisionInfo {
    /// Tick size as string, e.g. `"0.01"`.
    pub tick_size: String,
    /// Step size as string, e.g. `"0.001"`.
    pub step_size: String,
}

/// Thread-safe cache of per-symbol precision info.
///
/// Populated from [`ExchangeInfo`] after a connector calls `get_exchange_info()`.
/// Used internally by connectors in `place_order()` / `amend_order()` to convert
/// raw `f64` prices and quantities into safe, exchange-valid strings.
///
/// # Usage
///
/// ```ignore
/// // In connector constructor or lazy init:
/// let info = self.get_exchange_info().await?;
/// self.precision.load_from_symbols(&info.symbols);
///
/// // In place_order:
/// let price_str = self.precision.price(&symbol, price);
/// let qty_str   = self.precision.qty(&symbol, quantity);
/// ```
pub struct PrecisionCache {
    cache: RwLock<HashMap<String, PrecisionInfo>>,
}

impl PrecisionCache {
    /// Create an empty cache.
    pub fn new() -> Self {
        Self {
            cache: RwLock::new(HashMap::new()),
        }
    }

    /// Load precision info from a slice of [`SymbolInfo`].
    ///
    /// For each symbol:
    /// - Uses `tick_size` / `step_size` if present and > 0
    /// - Falls back to `price_precision` / `quantity_precision` integer digits
    ///
    /// Call this after `get_exchange_info()` succeeds.
    pub fn load_from_symbols(&self, symbols: &[crate::core::types::SymbolInfo]) {
        let mut cache = self.cache.write().unwrap();
        for s in symbols {
            let tick = match s.tick_size {
                Some(t) if t > 0.0 => t.to_string(),
                _ => precision_to_tick(s.price_precision),
            };
            let step = match s.step_size {
                Some(t) if t > 0.0 => t.to_string(),
                _ => precision_to_tick(s.quantity_precision),
            };
            cache.insert(s.symbol.clone(), PrecisionInfo {
                tick_size: tick,
                step_size: step,
            });
        }
    }

    /// Get safe price string for a symbol (rounded to nearest tick).
    ///
    /// Falls back to raw `f64::to_string()` if symbol is not in cache.
    pub fn price(&self, symbol: &str, price: f64) -> String {
        if let Some(info) = self.cache.read().unwrap().get(symbol) {
            safe_price(price, &info.tick_size)
        } else {
            price.to_string()
        }
    }

    /// Get safe quantity string for a symbol (floored to step_size).
    ///
    /// Falls back to raw `f64::to_string()` if symbol is not in cache.
    pub fn qty(&self, symbol: &str, qty: f64) -> String {
        if let Some(info) = self.cache.read().unwrap().get(symbol) {
            safe_qty(qty, &info.step_size)
        } else {
            qty.to_string()
        }
    }

    /// Get formatted price with trailing zeros (for exchanges requiring exact decimal places).
    pub fn formatted_price(&self, symbol: &str, price: f64) -> String {
        if let Some(info) = self.cache.read().unwrap().get(symbol) {
            format_price(price, &info.tick_size)
        } else {
            price.to_string()
        }
    }

    /// Get formatted quantity with trailing zeros.
    pub fn formatted_qty(&self, symbol: &str, qty: f64) -> String {
        if let Some(info) = self.cache.read().unwrap().get(symbol) {
            format_qty(qty, &info.step_size)
        } else {
            qty.to_string()
        }
    }

    /// Check if precision info is loaded for a symbol.
    pub fn has_symbol(&self, symbol: &str) -> bool {
        self.cache.read().unwrap().contains_key(symbol)
    }

    /// Number of symbols in cache.
    pub fn len(&self) -> usize {
        self.cache.read().unwrap().len()
    }

    /// Whether the cache is empty (no symbols loaded).
    pub fn is_empty(&self) -> bool {
        self.cache.read().unwrap().is_empty()
    }
}

impl Default for PrecisionCache {
    fn default() -> Self {
        Self::new()
    }
}

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

    // === safe_price tests ===

    #[test]
    fn test_safe_price_basic() {
        assert_eq!(safe_price(100.05, "0.01"), "100.05");
        assert_eq!(safe_price(50000.0, "0.01"), "50000");
        assert_eq!(safe_price(1.23456, "0.001"), "1.235");
    }

    #[test]
    fn test_safe_price_sub_tick_drift() {
        // 100.05 is stored as 100.04999... in f64
        // With floor() this would give 100.04 — WRONG
        // With round() via string path — correct
        assert_eq!(safe_price(100.05, "0.01"), "100.05");
        assert_eq!(safe_price(0.1 + 0.2, "0.01"), "0.3");
    }

    #[test]
    fn test_safe_price_round_nearest() {
        assert_eq!(safe_price(100.054, "0.01"), "100.05");
        assert_eq!(safe_price(100.055, "0.01"), "100.06"); // round up
        assert_eq!(safe_price(100.056, "0.01"), "100.06");
    }

    #[test]
    fn test_safe_price_btc() {
        assert_eq!(safe_price(67543.25, "0.01"), "67543.25");
        assert_eq!(safe_price(67543.251, "0.01"), "67543.25");
        assert_eq!(safe_price(67543.255, "0.01"), "67543.26");
    }

    #[test]
    fn test_safe_price_large_tick() {
        // Some futures have tick_size = 0.5 or 1.0
        assert_eq!(safe_price(100.3, "0.5"), "100.5");
        assert_eq!(safe_price(100.2, "0.5"), "100");
        assert_eq!(safe_price(100.0, "1"), "100");
        assert_eq!(safe_price(100.6, "1"), "101");
    }

    // === safe_qty tests ===

    #[test]
    fn test_safe_qty_basic() {
        assert_eq!(safe_qty(1.999, "0.01"), "1.99"); // floor, NOT round
        assert_eq!(safe_qty(0.12345, "0.001"), "0.123");
    }

    #[test]
    fn test_safe_qty_never_exceed() {
        // Critical: quantity must NEVER be rounded UP
        assert_eq!(safe_qty(0.999, "0.01"), "0.99");
        assert_eq!(safe_qty(1.0, "0.01"), "1");
        assert_eq!(safe_qty(0.12399, "0.001"), "0.123");
    }

    #[test]
    fn test_safe_qty_btc() {
        assert_eq!(safe_qty(0.00012345, "0.00001"), "0.00012");
        assert_eq!(safe_qty(1.5, "0.001"), "1.5");
    }

    // === format_price tests (with trailing zeros) ===

    #[test]
    fn test_format_price_trailing_zeros() {
        assert_eq!(format_price(100.5, "0.01"), "100.50");
        assert_eq!(format_price(100.0, "0.01"), "100.00");
        assert_eq!(format_price(67543.2, "0.01"), "67543.20");
    }

    #[test]
    fn test_format_qty_trailing_zeros() {
        assert_eq!(format_qty(1.5, "0.001"), "1.500");
        assert_eq!(format_qty(0.1, "0.00001"), "0.10000");
    }

    // === edge cases ===

    #[test]
    fn test_zero_price() {
        assert_eq!(safe_price(0.0, "0.01"), "0");
        assert_eq!(safe_qty(0.0, "0.001"), "0");
    }

    #[test]
    fn test_very_small_values() {
        assert_eq!(safe_qty(0.00000001, "0.00000001"), "0.00000001");
        assert_eq!(safe_price(0.00000001, "0.00000001"), "0.00000001");
    }

    // === precision_to_tick ===

    #[test]
    fn test_precision_to_tick() {
        assert_eq!(precision_to_tick(0), "1");
        assert_eq!(precision_to_tick(1), "0.1");
        assert_eq!(precision_to_tick(2), "0.01");
        assert_eq!(precision_to_tick(4), "0.0001");
        assert_eq!(precision_to_tick(8), "0.00000001");
    }

    // === PrecisionCache ===

    #[test]
    fn test_cache_fallback_before_load() {
        let cache = PrecisionCache::new();
        // Before loading — raw f64 toString fallback
        assert_eq!(cache.price("BTCUSDT", 67543.251), "67543.251");
        assert_eq!(cache.qty("BTCUSDT", 0.123456), "0.123456");
        assert!(cache.is_empty());
    }

    #[test]
    fn test_cache_with_tick_step() {
        let cache = PrecisionCache::new();
        let symbols = vec![
            crate::core::types::SymbolInfo {
                symbol: "BTCUSDT".to_string(),
                base_asset: "BTC".to_string(),
                quote_asset: "USDT".to_string(),
                status: "TRADING".to_string(),
                price_precision: 2,
                quantity_precision: 5,
                tick_size: Some(0.01),
                step_size: Some(0.00001),
                min_quantity: None,
                max_quantity: None,
                min_notional: None,
                account_type: Default::default(),
            },
        ];
        cache.load_from_symbols(&symbols);

        assert_eq!(cache.len(), 1);
        assert!(cache.has_symbol("BTCUSDT"));

        // Price — rounds to nearest tick
        assert_eq!(cache.price("BTCUSDT", 67543.251), "67543.25");
        assert_eq!(cache.price("BTCUSDT", 67543.255), "67543.26");

        // Qty — floors to step
        assert_eq!(cache.qty("BTCUSDT", 0.123456), "0.12345");
        assert_eq!(cache.qty("BTCUSDT", 0.123459), "0.12345"); // floor, NOT round

        // Unknown symbol — fallback
        assert_eq!(cache.price("UNKNOWN", 100.123), "100.123");
    }

    #[test]
    fn test_cache_digits_fallback() {
        let cache = PrecisionCache::new();
        // No tick_size/step_size — falls back to precision digits
        let symbols = vec![
            crate::core::types::SymbolInfo {
                symbol: "ETHUSDT".to_string(),
                base_asset: "ETH".to_string(),
                quote_asset: "USDT".to_string(),
                status: "TRADING".to_string(),
                price_precision: 2,
                quantity_precision: 3,
                tick_size: None,
                step_size: None,
                min_quantity: None,
                max_quantity: None,
                min_notional: None,
                account_type: Default::default(),
            },
        ];
        cache.load_from_symbols(&symbols);

        // precision 2 → tick "0.01", precision 3 → step "0.001"
        assert_eq!(cache.price("ETHUSDT", 3456.789), "3456.79");
        assert_eq!(cache.qty("ETHUSDT", 1.2345), "1.234");
    }

    #[test]
    fn test_cache_formatted_trailing_zeros() {
        let cache = PrecisionCache::new();
        let symbols = vec![
            crate::core::types::SymbolInfo {
                symbol: "BTCUSDT".to_string(),
                base_asset: "BTC".to_string(),
                quote_asset: "USDT".to_string(),
                status: "TRADING".to_string(),
                price_precision: 2,
                quantity_precision: 3,
                tick_size: Some(0.01),
                step_size: Some(0.001),
                min_quantity: None,
                max_quantity: None,
                min_notional: None,
                account_type: Default::default(),
            },
        ];
        cache.load_from_symbols(&symbols);

        assert_eq!(cache.formatted_price("BTCUSDT", 100.5), "100.50");
        assert_eq!(cache.formatted_qty("BTCUSDT", 1.5), "1.500");
    }
}