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 create a flipped order from a non-flip order
27    #[error("cannot create flipped order from a non-flip order")]
28    NotAFlipOrder,
29
30    /// Attempted to create a flipped order from an order that is not fully filled
31    #[error("order must be fully filled to flip, but {remaining} amount remains")]
32    OrderNotFullyFilled {
33        /// Remaining amount that needs to be filled
34        remaining: u128,
35    },
36
37    /// Attempted to fill more than the remaining amount
38    #[error("cannot fill {requested} when only {available} is available")]
39    FillAmountExceedsRemaining {
40        /// Amount requested to fill
41        requested: u128,
42        /// Amount available to fill
43        available: u128,
44    },
45
46    /// Tick value is out of valid bounds
47    #[error("tick {tick} is out of bounds (min: {min}, max: {max})")]
48    InvalidTick {
49        /// The invalid tick value
50        tick: i16,
51        /// Minimum valid tick
52        min: i16,
53        /// Maximum valid tick
54        max: i16,
55    },
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_invalid_flip_tick_bid() {
64        let err = OrderError::InvalidBidFlipTick {
65            tick: 5,
66            flip_tick: 3,
67        };
68        let msg = err.to_string();
69        assert!(msg.contains("bid"));
70        assert!(msg.contains(">= tick"));
71        assert!(msg.contains("5"));
72        assert!(msg.contains("3"));
73    }
74
75    #[test]
76    fn test_invalid_flip_tick_ask() {
77        let err = OrderError::InvalidAskFlipTick {
78            tick: 5,
79            flip_tick: 7,
80        };
81        let msg = err.to_string();
82        assert!(msg.contains("ask"));
83        assert!(msg.contains("<= tick"));
84        assert!(msg.contains("5"));
85        assert!(msg.contains("7"));
86    }
87
88    #[test]
89    fn test_not_a_flip_order() {
90        let err = OrderError::NotAFlipOrder;
91        assert_eq!(
92            err.to_string(),
93            "cannot create flipped order from a non-flip order"
94        );
95    }
96
97    #[test]
98    fn test_order_not_fully_filled() {
99        let err = OrderError::OrderNotFullyFilled { remaining: 500 };
100        let msg = err.to_string();
101        assert!(msg.contains("500"));
102        assert!(msg.contains("must be fully filled"));
103    }
104
105    #[test]
106    fn test_fill_amount_exceeds_remaining() {
107        let err = OrderError::FillAmountExceedsRemaining {
108            requested: 1000,
109            available: 600,
110        };
111        let msg = err.to_string();
112        assert!(msg.contains("1000"));
113        assert!(msg.contains("600"));
114    }
115
116    #[test]
117    fn test_invalid_tick() {
118        let err = OrderError::InvalidTick {
119            tick: 3000,
120            min: -2000,
121            max: 2000,
122        };
123        let msg = err.to_string();
124        assert_eq!(msg, "tick 3000 is out of bounds (min: -2000, max: 2000)");
125    }
126}