Skip to main content

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    TokenAmountOutIsGreaterThanBalance,
91}
92
93impl fmt::Display for PoolError {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        match self {
96            PoolError::InvalidAmount => write!(f, "Invalid amount provided"),
97            PoolError::InsufficientLiquidity => write!(f, "Insufficient liquidity"),
98            PoolError::MathOverflow => write!(f, "Mathematical overflow occurred"),
99            PoolError::InvalidPoolType => write!(f, "Invalid pool type"),
100            PoolError::InvalidTokenIndex => write!(f, "Invalid token index"),
101            PoolError::InvalidSwapParameters => write!(f, "Invalid swap parameters"),
102            PoolError::InvalidLiquidityParameters => write!(f, "Invalid liquidity parameters"),
103            PoolError::PoolNotFound => write!(f, "Pool not found"),
104            PoolError::HookError(msg) => write!(f, "Hook error: {}", msg),
105            PoolError::Custom(msg) => write!(f, "Custom error: {}", msg),
106            PoolError::ZeroInvariant => write!(f, "Zero invariant"),
107            PoolError::MaxInRatioExceeded => write!(f, "Maximum input ratio exceeded"),
108            PoolError::MaxOutRatioExceeded => write!(f, "Maximum output ratio exceeded"),
109            PoolError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
110
111            // Python SystemError equivalents
112            PoolError::InputTokenNotFound => write!(f, "Input token not found on pool"),
113            PoolError::OutputTokenNotFound => write!(f, "Output token not found on pool"),
114            PoolError::TradeAmountTooSmall => write!(f, "TradeAmountTooSmall"),
115            PoolError::BeforeSwapHookFailed => write!(f, "BeforeSwapHookFailed"),
116            PoolError::AfterSwapHookFailed => write!(f, "AfterSwapHookFailed"),
117            PoolError::BeforeAddLiquidityHookFailed => write!(f, "BeforeAddLiquidityHookFailed"),
118            PoolError::AfterAddLiquidityHookFailed => write!(f, "AfterAddLiquidityHookFailed"),
119            PoolError::BeforeRemoveLiquidityHookFailed => {
120                write!(f, "BeforeRemoveLiquidityHookFailed")
121            }
122            PoolError::AfterRemoveLiquidityHookFailed => {
123                write!(f, "AfterRemoveLiquidityHookFailed")
124            }
125            PoolError::UnsupportedPoolType(pool_type) => {
126                write!(f, "Unsupported Pool Type: {}", pool_type)
127            }
128            PoolError::UnsupportedHookType(hook_type) => {
129                write!(f, "Unsupported Hook Type: {}", hook_type)
130            }
131            PoolError::NoStateForHook(hook_name) => write!(f, "No state for Hook: {}", hook_name),
132            PoolError::StableInvariantDidntConverge => {
133                write!(f, "Stable invariant didn't converge")
134            }
135            PoolError::TokenAmountOutIsGreaterThanBalance => {
136                write!(f, "Token amount out is greater than balance")
137            }
138        }
139    }
140}
141
142impl std::error::Error for PoolError {}
143
144impl From<String> for PoolError {
145    fn from(msg: String) -> Self {
146        PoolError::Custom(msg)
147    }
148}
149
150impl From<&str> for PoolError {
151    fn from(msg: &str) -> Self {
152        PoolError::Custom(msg.to_string())
153    }
154}