Skip to main content

a2a_swap_sdk/
error.rs

1//! SDK error type.
2
3use solana_sdk::pubkey::Pubkey;
4
5/// All errors returned by the A2A-Swap SDK.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    // ── RPC / network ────────────────────────────────────────────────────────
9    /// A Solana JSON-RPC call failed.
10    #[error("RPC error: {0}")]
11    Rpc(#[from] solana_client::client_error::ClientError),
12
13    // ── Pool discovery ───────────────────────────────────────────────────────
14    /// No pool exists for the given mint pair in either PDA ordering.
15    #[error("Pool not found for mints {0} / {1}")]
16    PoolNotFound(Pubkey, Pubkey),
17
18    /// The pool exists but both vaults are empty (lp_supply == 0 and reserves == 0).
19    #[error("Pool has no liquidity — seed it with provide_liquidity first")]
20    NoLiquidity,
21
22    // ── Provide liquidity ────────────────────────────────────────────────────
23    /// Pool is empty and no `amount_b` was provided to set the initial price.
24    #[error("amount_b is required when the pool is empty (first deposit sets the price)")]
25    AmountBRequired,
26
27    /// The proportional `amount_b` computed from live reserves rounded to zero.
28    #[error("Computed amount_b = 0 — deposit amount_a is too small relative to reserves; \
29             pass amount_b explicitly")]
30    AmountBZero,
31
32    // ── Swap slippage ────────────────────────────────────────────────────────
33    /// The real output would fall below the caller's minimum.
34    #[error("Slippage guard triggered: estimated_out={estimated}, min_amount_out={min}")]
35    SlippageExceeded { estimated: u64, min: u64 },
36
37    // ── Arithmetic ───────────────────────────────────────────────────────────
38    #[error("Integer overflow in fee / swap math")]
39    MathOverflow,
40
41    // ── Account parsing ──────────────────────────────────────────────────────
42    /// Raw account bytes could not be deserialized.
43    #[error("Account parse error at offset {offset}: {reason}")]
44    ParseError { offset: usize, reason: String },
45
46    // ── Validation ───────────────────────────────────────────────────────────
47    #[error("Invalid argument: {0}")]
48    InvalidArgument(String),
49}
50
51/// Convenience alias so every module can write `Result<T>`.
52pub type Result<T> = std::result::Result<T, Error>;