Skip to main content

tempo_precompiles/stablecoin_dex/
error.rs

1/// Errors that can occur when working with orders.
2#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
3pub enum OrderError {
4    /// Flip tick constraint violated for a bid flip order.
5    /// Pre-T5: `flip_tick` must be `> tick`.
6    /// T5+ (TIP-1030): `flip_tick` must be `>= tick`.
7    #[error("invalid flip tick for bid order: flip_tick ({flip_tick}) must be >= tick ({tick})")]
8    InvalidBidFlipTick {
9        /// The order's tick
10        tick: i16,
11        /// The invalid flip_tick value
12        flip_tick: i16,
13    },
14
15    /// Flip tick constraint violated for an ask flip order.
16    /// Pre-T5: `flip_tick` must be `< tick`.
17    /// T5+ (TIP-1030): `flip_tick` must be `<= tick`.
18    #[error("invalid flip tick for ask order: flip_tick ({flip_tick}) must be <= tick ({tick})")]
19    InvalidAskFlipTick {
20        /// The order's tick
21        tick: i16,
22        /// The invalid flip_tick value
23        flip_tick: i16,
24    },
25
26    /// Attempted to fill more than the remaining amount
27    #[error("cannot fill {requested} when only {available} is available")]
28    FillAmountExceedsRemaining {
29        /// Amount requested to fill
30        requested: u128,
31        /// Amount available to fill
32        available: u128,
33    },
34
35    /// Tick value is out of valid bounds
36    #[error("tick {tick} is out of bounds (min: {min}, max: {max})")]
37    InvalidTick {
38        /// The invalid tick value
39        tick: i16,
40        /// Minimum valid tick
41        min: i16,
42        /// Maximum valid tick
43        max: i16,
44    },
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_invalid_flip_tick_bid() {
53        let err = OrderError::InvalidBidFlipTick {
54            tick: 5,
55            flip_tick: 3,
56        };
57        let msg = err.to_string();
58        assert!(msg.contains("bid"));
59        assert!(msg.contains(">= tick"));
60        assert!(msg.contains("5"));
61        assert!(msg.contains("3"));
62    }
63
64    #[test]
65    fn test_invalid_flip_tick_ask() {
66        let err = OrderError::InvalidAskFlipTick {
67            tick: 5,
68            flip_tick: 7,
69        };
70        let msg = err.to_string();
71        assert!(msg.contains("ask"));
72        assert!(msg.contains("<= tick"));
73        assert!(msg.contains("5"));
74        assert!(msg.contains("7"));
75    }
76
77    #[test]
78    fn test_fill_amount_exceeds_remaining() {
79        let err = OrderError::FillAmountExceedsRemaining {
80            requested: 1000,
81            available: 600,
82        };
83        let msg = err.to_string();
84        assert!(msg.contains("1000"));
85        assert!(msg.contains("600"));
86    }
87
88    #[test]
89    fn test_invalid_tick() {
90        let err = OrderError::InvalidTick {
91            tick: 3000,
92            min: -2000,
93            max: 2000,
94        };
95        let msg = err.to_string();
96        assert_eq!(msg, "tick 3000 is out of bounds (min: -2000, max: 2000)");
97    }
98}