Skip to main content

strike_sdk/
error.rs

1//! Error types for the Strike SDK.
2
3use std::fmt;
4
5/// Errors returned by the Strike SDK.
6#[derive(Debug)]
7pub enum StrikeError {
8    /// RPC transport error.
9    Rpc(alloy::transports::TransportError),
10    /// Contract call reverted with a reason.
11    Contract(String),
12    /// Nonce mismatch (nonce-manager feature).
13    NonceMismatch { expected: u64, got: u64 },
14    /// Market is not in an active state.
15    MarketNotActive(u64),
16    /// Insufficient USDT balance for the operation.
17    InsufficientBalance,
18    /// Configuration error.
19    Config(String),
20    /// No wallet configured (tried to send a transaction in read-only mode).
21    NoWallet,
22    /// WebSocket connection error.
23    WebSocket(String),
24    /// Indexer API error.
25    Indexer(String),
26    /// Generic error wrapper.
27    Other(eyre::Report),
28}
29
30impl fmt::Display for StrikeError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Rpc(e) => write!(f, "RPC error: {e}"),
34            Self::Contract(reason) => write!(f, "contract reverted: {reason}"),
35            Self::NonceMismatch { expected, got } => {
36                write!(f, "nonce mismatch: expected {expected}, got {got}")
37            }
38            Self::MarketNotActive(id) => write!(f, "market {id} is not active"),
39            Self::InsufficientBalance => write!(f, "insufficient USDT balance"),
40            Self::Config(msg) => write!(f, "config error: {msg}"),
41            Self::NoWallet => write!(f, "no wallet configured — cannot send transactions"),
42            Self::WebSocket(msg) => write!(f, "WebSocket error: {msg}"),
43            Self::Indexer(msg) => write!(f, "indexer error: {msg}"),
44            Self::Other(e) => write!(f, "{e}"),
45        }
46    }
47}
48
49impl std::error::Error for StrikeError {}
50
51impl From<alloy::transports::TransportError> for StrikeError {
52    fn from(e: alloy::transports::TransportError) -> Self {
53        Self::Rpc(e)
54    }
55}
56
57impl From<eyre::Report> for StrikeError {
58    fn from(e: eyre::Report) -> Self {
59        Self::Other(e)
60    }
61}
62
63impl From<reqwest::Error> for StrikeError {
64    fn from(e: reqwest::Error) -> Self {
65        Self::Indexer(e.to_string())
66    }
67}
68
69/// Result type alias for Strike SDK operations.
70pub type Result<T> = std::result::Result<T, StrikeError>;