templar_common/vault/
errors.rs

1use super::*;
2
3/// Vault operation errors.
4#[near(serializers = [json])]
5pub enum Error {
6    /// Index drift or stale op_id.
7    IndexDrifted(ExpectedIdx, ActualIdx),
8    /// Callback resolved different market.
9    MarketDrifted {
10        expected: MarketId,
11        actual: MarketId,
12    },
13    /// Unknown market.
14    MissingMarket(MarketId),
15    /// Not in withdrawing state.
16    NotWithdrawing,
17    /// Not in allocating state.
18    NotAllocating,
19    /// Not in refreshing state.
20    NotRefreshing,
21    /// Not in payout state.
22    NotPayout,
23    /// Market transfer failed.
24    MarketTransferFailed,
25    /// Supply position not found.
26    MissingSupplyPosition,
27    /// Position read failed.
28    PositionReadFailed,
29    /// Balance read failed.
30    BalanceReadFailed,
31    /// Insufficient liquidity across markets.
32    InsufficientLiquidity,
33    /// Zero amount provided.
34    ZeroAmount,
35}
36
37impl std::fmt::Debug for Error {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        std::fmt::Display::fmt(self, f)
40    }
41}
42
43impl std::fmt::Display for Error {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            Error::IndexDrifted(expected, actual) => {
47                write!(f, "vault index drifted: expected {expected}, got {actual}")
48            }
49            Error::MarketDrifted { expected, actual } => {
50                write!(f, "vault market drifted: expected {expected}, got {actual}")
51            }
52            Error::MissingMarket(market) => write!(f, "missing market: {market}"),
53            Error::NotWithdrawing => f.write_str("vault is not withdrawing"),
54            Error::NotAllocating => f.write_str("vault is not allocating"),
55            Error::NotRefreshing => f.write_str("vault is not refreshing"),
56            Error::NotPayout => f.write_str("vault is not in payout"),
57            Error::MarketTransferFailed => f.write_str("market transfer failed"),
58            Error::MissingSupplyPosition => f.write_str("missing supply position"),
59            Error::PositionReadFailed => f.write_str("position read failed"),
60            Error::BalanceReadFailed => f.write_str("balance read failed"),
61            Error::InsufficientLiquidity => f.write_str("insufficient liquidity"),
62            Error::ZeroAmount => f.write_str("zero amount"),
63        }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::Error;
70    use crate::vault::MarketId;
71
72    #[test]
73    fn display_uses_human_readable_message() {
74        let error = Error::MarketDrifted {
75            expected: MarketId(1),
76            actual: MarketId(2),
77        };
78
79        assert_eq!(error.to_string(), "vault market drifted: expected 1, got 2");
80    }
81}