balancer_maths_rust/common/
errors.rs

1//! Custom error types for the Balancer maths library
2
3use std::fmt;
4
5/// Errors that can occur during pool operations
6#[derive(Debug, Clone, PartialEq)]
7pub enum PoolError {
8    /// Invalid amount provided (zero or negative)
9    InvalidAmount,
10
11    /// Insufficient liquidity for the operation
12    InsufficientLiquidity,
13
14    /// Mathematical overflow occurred
15    MathOverflow,
16
17    /// Invalid pool type specified
18    InvalidPoolType,
19
20    /// Invalid token index
21    InvalidTokenIndex,
22
23    /// Invalid swap parameters
24    InvalidSwapParameters,
25
26    /// Invalid liquidity parameters
27    InvalidLiquidityParameters,
28
29    /// Pool not found
30    PoolNotFound,
31
32    /// Hook error
33    HookError(String),
34
35    /// Custom error message
36    Custom(String),
37
38    /// Zero invariant error
39    ZeroInvariant,
40
41    /// Maximum input ratio exceeded
42    MaxInRatioExceeded,
43
44    /// Maximum output ratio exceeded
45    MaxOutRatioExceeded,
46
47    /// Invalid input parameters
48    InvalidInput(String),
49
50    // Python SystemError equivalents
51    /// Input token not found on pool
52    InputTokenNotFound,
53
54    /// Output token not found on pool
55    OutputTokenNotFound,
56
57    /// Trade amount too small
58    TradeAmountTooSmall,
59
60    /// Before swap hook failed
61    BeforeSwapHookFailed,
62
63    /// After swap hook failed
64    AfterSwapHookFailed,
65
66    /// Before add liquidity hook failed
67    BeforeAddLiquidityHookFailed,
68
69    /// After add liquidity hook failed
70    AfterAddLiquidityHookFailed,
71
72    /// Before remove liquidity hook failed
73    BeforeRemoveLiquidityHookFailed,
74
75    /// After remove liquidity hook failed
76    AfterRemoveLiquidityHookFailed,
77
78    /// Unsupported pool type
79    UnsupportedPoolType(String),
80
81    /// Unsupported hook type
82    UnsupportedHookType(String),
83
84    /// No state for hook
85    NoStateForHook(String),
86
87    /// Stable invariant didn't converge
88    StableInvariantDidntConverge,
89}
90
91impl fmt::Display for PoolError {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        match self {
94            PoolError::InvalidAmount => write!(f, "Invalid amount provided"),
95            PoolError::InsufficientLiquidity => write!(f, "Insufficient liquidity"),
96            PoolError::MathOverflow => write!(f, "Mathematical overflow occurred"),
97            PoolError::InvalidPoolType => write!(f, "Invalid pool type"),
98            PoolError::InvalidTokenIndex => write!(f, "Invalid token index"),
99            PoolError::InvalidSwapParameters => write!(f, "Invalid swap parameters"),
100            PoolError::InvalidLiquidityParameters => write!(f, "Invalid liquidity parameters"),
101            PoolError::PoolNotFound => write!(f, "Pool not found"),
102            PoolError::HookError(msg) => write!(f, "Hook error: {}", msg),
103            PoolError::Custom(msg) => write!(f, "Custom error: {}", msg),
104            PoolError::ZeroInvariant => write!(f, "Zero invariant"),
105            PoolError::MaxInRatioExceeded => write!(f, "Maximum input ratio exceeded"),
106            PoolError::MaxOutRatioExceeded => write!(f, "Maximum output ratio exceeded"),
107            PoolError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
108
109            // Python SystemError equivalents
110            PoolError::InputTokenNotFound => write!(f, "Input token not found on pool"),
111            PoolError::OutputTokenNotFound => write!(f, "Output token not found on pool"),
112            PoolError::TradeAmountTooSmall => write!(f, "TradeAmountTooSmall"),
113            PoolError::BeforeSwapHookFailed => write!(f, "BeforeSwapHookFailed"),
114            PoolError::AfterSwapHookFailed => write!(f, "AfterSwapHookFailed"),
115            PoolError::BeforeAddLiquidityHookFailed => write!(f, "BeforeAddLiquidityHookFailed"),
116            PoolError::AfterAddLiquidityHookFailed => write!(f, "AfterAddLiquidityHookFailed"),
117            PoolError::BeforeRemoveLiquidityHookFailed => {
118                write!(f, "BeforeRemoveLiquidityHookFailed")
119            }
120            PoolError::AfterRemoveLiquidityHookFailed => {
121                write!(f, "AfterRemoveLiquidityHookFailed")
122            }
123            PoolError::UnsupportedPoolType(pool_type) => {
124                write!(f, "Unsupported Pool Type: {}", pool_type)
125            }
126            PoolError::UnsupportedHookType(hook_type) => {
127                write!(f, "Unsupported Hook Type: {}", hook_type)
128            }
129            PoolError::NoStateForHook(hook_name) => write!(f, "No state for Hook: {}", hook_name),
130            PoolError::StableInvariantDidntConverge => {
131                write!(f, "Stable invariant didn't converge")
132            }
133        }
134    }
135}
136
137impl std::error::Error for PoolError {}
138
139impl From<String> for PoolError {
140    fn from(msg: String) -> Self {
141        PoolError::Custom(msg)
142    }
143}
144
145impl From<&str> for PoolError {
146    fn from(msg: &str) -> Self {
147        PoolError::Custom(msg.to_string())
148    }
149}